autodist-git: pylint suggested fixes

This commit is contained in:
Silvan Calarco 2024-11-07 18:44:46 +01:00
parent 1a5b85cf9c
commit ade2fbc122

View File

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