diff --git a/autodist-git b/autodist-git index e61dd36..dbb6f81 100755 --- a/autodist-git +++ b/autodist-git @@ -1,16 +1,20 @@ #!/usr/bin/python3 +""" +autodist git integration +""" import argparse import glob import os import re -import rpm import subprocess +import sys import tempfile from shutil import copyfile -from pyrpm.spec import Spec, replace_macros -from gitea import * -from git import Repo from functools import cmp_to_key +from gitea import Gitea, NotFoundException, Organization, Repository +import rpm +from pyrpm.spec import Spec, replace_macros +from git import Repo from configobj import ConfigObj @@ -18,9 +22,6 @@ cfg = ConfigObj(infile='/etc/autodist/config-git') gitea = Gitea(cfg["GITEA_URL"], cfg["GITEA_TOKEN"]) org = Organization.request(gitea, cfg["GITEA_ORGANIZATION"]) -# argparse options -options = None - def comparePkgInfo(item1, item2): return rpm.labelCompare( (str(item1['e']), item1['v'], item1['r']), @@ -69,9 +70,7 @@ def findOrCreateRepo(pkg_name, pkg_description=None, create=True): gitea_repo = giteaGetRepository(repo_name) return gitea_repo -def commitReleaseFromDir(pkg_info, gitea_repo, repo, temp_dir): - global options - +def commitReleaseFromDir(options, pkg_info, gitea_repo, repo, temp_dir): src_dir = pkg_info["src"] if src_dir.endswith(".src.rpm"): @@ -80,7 +79,7 @@ def commitReleaseFromDir(pkg_info, gitea_repo, repo, temp_dir): src_temp_dir = tempfile.TemporaryDirectory() src_dir = src_temp_dir.name subprocess.run(["autospec", "-x", pkg_info["src"], f'--destdir={src_dir}'], - stdout=subprocess.PIPE) + stdout=subprocess.PIPE, check=False) # Delete binary source archives for pattern in [ '*.zip','*.tar.bz2','*.tar.xz','*.tar.gz','*.tgz','*.txz','*.iso', @@ -145,7 +144,7 @@ def commitReleaseFromDir(pkg_info, gitea_repo, repo, temp_dir): return # Create/update README.md - with open(f'{temp_dir.name}/README.md', "w") as readme_file: + with open(file=f'{temp_dir.name}/README.md', mode="w", encoding="utf8") as readme_file: readme_file.write(f"# {pkg_info['name']}\n\n{spec_description}") repo.index.add(['README.md']) @@ -180,9 +179,7 @@ def commitReleaseFromDir(pkg_info, gitea_repo, repo, temp_dir): repo.create_tag(new_tag, message=f'Release {new_tag}') #origin.push(new_tag) -def findAndCommitPackageReleases(pkgname, pkgvr): - global options - +def findAndCommitPackageReleases(options, pkgname, pkgvr): print(f'Processing package {pkgname} on {cfg["GITEA_URL"]}...') pkgs_info = [] @@ -190,10 +187,10 @@ def findAndCommitPackageReleases(pkgname, pkgvr): # Find from archive dir pkgnamere = pkgname.replace('+','\+') dirs = [f for f in os.listdir(f'{cfg["ARCHIVE_DIR"]}/{pkgname[0:1]}') if re.match(f'{pkgnamere}-[^-]*-[^-]*$', f)] - for dir in dirs: - pkg_dir = f'{cfg["ARCHIVE_DIR"]}/{pkgname[0:1]}/{dir}' + for _dir in dirs: + pkg_dir = f'{cfg["ARCHIVE_DIR"]}/{pkgname[0:1]}/{_dir}' spec = Spec.from_file(f'{pkg_dir}/{pkgname}.spec') - parts = re.split(f'{pkgnamere}-([^-]*)-([^-]*)$', dir) + parts = re.split(f'{pkgnamere}-([^-]*)-([^-]*)$', _dir) epoch = 0 if spec.epoch is None else int(spec.epoch) version = parts[1] @@ -203,12 +200,12 @@ def findAndCommitPackageReleases(pkgname, pkgvr): # Find from OLD_DIR dirs = [f for f in os.listdir(f'{cfg["OLD_DIR"]}') if re.match(f'{pkgnamere}_[0-9]*.[0-9]*$', f)] - for dir in dirs: - srpms_list = glob.glob(f'{cfg["OLD_DIR"]}/{dir}/{pkgname}-*.src.rpm') + for _dir in dirs: + srpms_list = glob.glob(f'{cfg["OLD_DIR"]}/{_dir}/{pkgname}-*.src.rpm') for srpm in srpms_list: parts = re.split('.*-([^-]*)-([^-]*).src.rpm$', srpm) epoch = subprocess.run(['rpm', '-q', '--queryformat=%{epoch}', '-p', srpm], - stdout=subprocess.PIPE).stdout.decode('utf-8') + stdout=subprocess.PIPE, check=False).stdout.decode('utf-8') # result is "(none)" if no Epoch is set if len(epoch) > 2: epoch = "0" @@ -223,7 +220,7 @@ def findAndCommitPackageReleases(pkgname, pkgvr): src_path = f'{cfg["SRPMS_DIR"]}/{srpm}' parts = re.split('.*-([^-]*)-([^-]*).src.rpm$', srpm) epoch = subprocess.run(['rpm', '-q', '--queryformat=%{epoch}', '-p', src_path], - stdout=subprocess.PIPE).stdout.decode('utf-8') + stdout=subprocess.PIPE, check=False).stdout.decode('utf-8') # result is "(none)" if no Epoch is set if len(epoch) > 2: epoch = "0" @@ -266,7 +263,7 @@ def findAndCommitPackageReleases(pkgname, pkgvr): #exit(1) #pkg_dir = f'{cfg["ARCHIVE_DIR"]}/{pkgname[0:1]}/{pkgname}-{vr}' - commitReleaseFromDir(pkg_info, gitea_repo, repo, temp_dir) + commitReleaseFromDir(options, pkg_info, gitea_repo, repo, temp_dir) new_commits = True if new_commits: @@ -280,7 +277,8 @@ def findAndCommitPackageReleases(pkgname, pkgvr): def main(): - global options + # argparse options + options = None parser = argparse.ArgumentParser(prog='autodist-git', description='RPM repository sync and management with git service.', @@ -303,7 +301,7 @@ def main(): try: options = parser.parse_args() except: - exit(1) + sys.exit(1) if options.mode == 'syncpkg': if options.delete: @@ -315,7 +313,7 @@ def main(): if gitea_repo is not None: print(f'Deleting repository for {options.pkgname}...') gitea_repo.delete() - findAndCommitPackageReleases(options.pkgname, options.pkgver) + findAndCommitPackageReleases(options, options.pkgname, options.pkgver) elif options.mode == 'syncrepo': dir_list = sorted(filter(os.path.isfile, glob.glob(f'{cfg["SRPMS_DIR"]}/*.src.rpm'))) @@ -343,7 +341,7 @@ def main(): found_newer = True else: for repo_tag in repo_tags: - parts = re.split(f'([^-]*)-([^-]*)$', repo_tag["name"]) + parts = re.split('([^-]*)-([^-]*)$', repo_tag["name"]) tag_item = { 'e':0, 'v': parts[1], 'r': parts[2]} compare = comparePkgInfo(pkg_item, tag_item) if compare == 0: @@ -355,7 +353,7 @@ def main(): if not found_equal: if repo_tags is not None: print(f'{pkg_name} ({pkg_vr}): needs update') - findAndCommitPackageReleases(pkg_name, None) + findAndCommitPackageReleases(options, pkg_name, None) if found_newer: if options.verbose: print(f'{pkg_name} ({pkg_vr}): found_equal={found_equal} found_newer={found_newer} found_older={found_older}')