setup.cfg cleanup tool

This commit is contained in:
Tino Schreiber 2023-09-26 09:47:35 +00:00
parent 8b41b7aa73
commit 420e377b3a

View File

@ -5,6 +5,7 @@ import json
import pathlib import pathlib
import os import os
import argparse import argparse
import re
from github import Auth from github import Auth
from github import Github from github import Github
@ -12,7 +13,6 @@ from github import Github
def get_git_token(args): def get_git_token(args):
auth = '' auth = ''
print(args.git_token)
if args.git_token: if args.git_token:
auth = args.git_token auth = args.git_token
elif os.environ('GIT_TOKEN'): elif os.environ('GIT_TOKEN'):
@ -24,9 +24,10 @@ def get_git_token(args):
def fix_repos(args): def fix_repos(args):
# get Auth information # get Auth information
auth = get_git_token(args) token = get_git_token(args)
auth = Auth.Token(token)
workdir = pathlib.Path(args.work_dir) workdir = pathlib.Path(args.work_dir)
branch = "main" branch = args.branch
if args.target_environment == 'public': if args.target_environment == 'public':
g = Github(auth=auth) g = Github(auth=auth)
@ -38,15 +39,47 @@ def fix_repos(args):
repos.append(repo.full_name) repos.append(repo.full_name)
for r in repos: for r in repos:
r = 'opentelekomcloud-docs/gaussdb-nosql'
repo = g.get_repo(r) repo = g.get_repo(r)
print(repo.clone_url) path = workdir.joinpath(repo.name)
git.Repo.clone_from(repo.clone_url, to_path=workdir + repo.name, branch=branch) 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 = r"(.*)-(.*)\s*="
new_file_content = re.sub(pattern, r'\1_\2=', file_content)
break break
def main(): def main():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Update conf.py file in repositories." description="Fix setup.cfg"
)
parser.add_argument(
"--branch",
default="main",
help="Branch name to be pulled from"
)
parser.add_argument(
"--commit-description",
default=(
"Update tox.ini && conf.py file\n\n"
"Performed-by: gitea/infra/hc-tools/"
"/test.py"
),
help="Commit description for the commit",
)
parser.add_argument(
"--file-name",
default="setup.cfg",
help="Relative path to file without starting /.\n"
"default: setup.cfg"
) )
parser.add_argument( parser.add_argument(
"--git-token", "--git-token",
@ -67,15 +100,7 @@ def main():
required=True, required=True,
help="Working directory to use for repository checkout.", help="Working directory to use for repository checkout.",
) )
parser.add_argument(
"--commit-description",
default=(
"Update tox.ini && conf.py file\n\n"
"Performed-by: gitea/infra/hc-tools/"
"/test.py"
),
help="Commit description for the commit",
)
args = parser.parse_args() args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)