117 lines
3.2 KiB
Python
117 lines
3.2 KiB
Python
|
|
import logging
|
|
import git
|
|
import json
|
|
import pathlib
|
|
import os
|
|
import argparse
|
|
import re
|
|
|
|
from github import Auth
|
|
from github import Github
|
|
|
|
|
|
def get_git_token(args):
|
|
auth = ''
|
|
if args.git_token:
|
|
auth = args.git_token
|
|
elif os.environ('GIT_TOKEN'):
|
|
auth = os.environ('GIT_TOKEN')
|
|
else:
|
|
raise Exception('No valid Auth Token ')
|
|
return auth
|
|
|
|
|
|
def fix_repos(args):
|
|
# get Auth information
|
|
token = get_git_token(args)
|
|
auth = Auth.Token(token)
|
|
workdir = pathlib.Path(args.work_dir)
|
|
branch = args.branch
|
|
|
|
if args.target_environment == 'public':
|
|
g = Github(auth=auth)
|
|
org = g.get_organization("opentelekomcloud-docs")
|
|
|
|
repos = []
|
|
|
|
for repo in org.get_repos():
|
|
repos.append(repo.full_name)
|
|
|
|
for r in repos:
|
|
# r = 'opentelekomcloud-docs/gaussdb-nosql'
|
|
repo = g.get_repo(r)
|
|
path = workdir.joinpath(repo.name)
|
|
if path.exists() and path.is_dir():
|
|
local_repo = git.Repo(path)
|
|
origin = local_repo.remotes.origin
|
|
origin.pull(branch)
|
|
else:
|
|
git.Repo.clone_from(repo.clone_url, to_path=path, branch=branch)
|
|
file_absolute_path = path / args.file_name
|
|
if file_absolute_path.exists():
|
|
with file_absolute_path.open('r') as file:
|
|
file_content = file.read()
|
|
# Pattern to match strings: description-file = README.rst
|
|
# and substitute with: description_file = README.rst
|
|
pattern = r"(.*)-(.*)\s*="
|
|
new_file_content = re.sub(pattern, r'\1_\2=', file_content)
|
|
break
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(
|
|
description="Fix setup.cfg",
|
|
formatter_class=argparse.RawTextHelpFormatter
|
|
)
|
|
parser.add_argument(
|
|
"--branch",
|
|
default="main",
|
|
help="Branch name to be pulled from"
|
|
)
|
|
parser.add_argument(
|
|
"--commit-description",
|
|
default=(
|
|
"Update setup.cfg file\n\n"
|
|
"Performed-by: gitea/infra/hc-tools/"
|
|
"/test.py"
|
|
),
|
|
help="Commit description",
|
|
)
|
|
parser.add_argument(
|
|
"--file-name",
|
|
default="setup.cfg",
|
|
help="Relative path to file without starting /.\n"
|
|
"default: setup.cfg"
|
|
)
|
|
parser.add_argument(
|
|
"--git-token",
|
|
help="Git token for git authentication or use env variable\n"
|
|
"\'GIT_TOKEN\'"
|
|
)
|
|
parser.add_argument(
|
|
"--target-environment",
|
|
required=True,
|
|
choices=['public', 'internal', 'public-swiss', 'internal-swiss'],
|
|
help="Environment to be used as source\n"
|
|
"public: Github Org: opentelekomcloud-docs/\n"
|
|
"internal: Gitea Org: docs/\n"
|
|
"internal-swiss: Gitea Org: docs-swiss/\n"
|
|
"public-swiss: GitHub Org: opentelekomcloud-docs-swiss/\n"
|
|
)
|
|
parser.add_argument(
|
|
"--work-dir",
|
|
required=True,
|
|
help="Working directory to use for repository checkout.",
|
|
)
|
|
|
|
|
|
args = parser.parse_args()
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
fix_repos(args)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|