diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2018-06-20 14:41:08 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2018-06-20 14:41:08 +0200 |
commit | c97618e1f707e34cf8d20827c9bb2b79f2f987fc (patch) | |
tree | 11ad617cc4c46dff65f874eb90ef6cfaf170b92a | |
parent | ad170a5611b1d2a67a9cc9cf2c89b5fea4b91fe5 (diff) | |
download | dlp_check_version_PyPI-c97618e1f707e34cf8d20827c9bb2b79f2f987fc.tar.gz |
More cleanup.
-rwxr-xr-x | dlpcvp.py | 66 |
1 files changed, 41 insertions, 25 deletions
@@ -10,7 +10,7 @@ import sqlite3 import tempfile import urllib.request from urllib.request import Request, urlopen -from typing import Optional, Tuple +from typing import Iterable, Tuple import xml.etree.ElementTree as ET import rpm @@ -35,8 +35,14 @@ password_mgr.add_password(None, OBS_base, user, passw) handler = urllib.request.HTTPBasicAuthHandler(password_mgr) opener = urllib.request.build_opener(handler) -def get_version_from_pypi(name:str, etag: Optional[str] = None) -> Tuple[str, str, str]: + +def get_version_from_pypi(name:str, etag: str = None) -> Tuple[str, str, str]: + """ + For the given name of module return the latest version available on PyPI. + """ + log.debug('name = %s', name) req = Request(url=PyPI_base.format(name)) + log.debug('req URL = %s', req.full_url) if etag is not None: req.add_header('ETag', etag) @@ -49,8 +55,12 @@ def get_version_from_pypi(name:str, etag: Optional[str] = None) -> Tuple[str, st else: IOError(resp.info()) -def list_packages(project:str, etag: Optional[str] = None) -> Tuple[str, ...]: - req = Request(url=OBS_base+'/source/{}'.format(project)) + +def suse_packages(project:str, etag: str = None) -> Iterable[str]: + """ + Iterator returning names of all packages in the given project + """ + req = Request(url=OBS_base+f'/source/{project}') if etag is not None: req.add_header('ETag', etag) @@ -59,18 +69,21 @@ def list_packages(project:str, etag: Optional[str] = None) -> Tuple[str, ...]: if resp.getcode() == 200: raw_xml_data = ET.parse(resp) root = raw_xml_data.getroot() - out = [] for elem in root.iter('entry'): - out.append(elem.get('name')) + yield elem.get('name') - return tuple(out) -def package_version(project:str, pkg:str, etag: Optional[str] = None) -> Tuple[str, ...]: - # GET https://api.opensuse.org/source/devel:languages:python:singlespec-staging/python-venusian?rev=latest - req_spc_name = Request(url=OBS_base+'/source/{}/{}'.format(project, pkg)) +def package_version(project:str, pkg:str, + etag_fn: str = None, etag_spcf: str = None) -> Tuple[str, ...]: + """ + Return the version of the given package in the given project. - if etag is not None: - req_spc_name.add_header('ETag', etag) + Downloads SPEC file from OBS and parses it. + """ + req_spc_name = Request(url=OBS_base+f'/source/{project}/{pkg}') + + if etag_fn is not None: + req_spc_name.add_header('ETag', etag_fn) with opener.open(req_spc_name) as resp: if resp.getcode() == 200: @@ -82,15 +95,17 @@ def package_version(project:str, pkg:str, etag: Optional[str] = None) -> Tuple[s if name.endswith('.spec'): spec_files.append(name) - log.debug('spec_files = %s', spec_files) if spec_files: spc_fname = sorted(spec_files, key=len) - log.debug('spc_fname = %s', spc_fname) spc_fname = spc_fname[0] else: return None - req_spec = Request(url=OBS_base+'/source/{}/{}/{}'.format(project, pkg, spc_fname)) + req_spec = Request(url=OBS_base+f'/source/{project}/{pkg}/{spc_fname}') + + if etag_spcf is not None: + req_spc_name.add_header('ETag', etag_spcf) + with opener.open(req_spec) as resp: spec_file_str = resp.read() spec_file_name = '' @@ -102,8 +117,7 @@ def package_version(project:str, pkg:str, etag: Optional[str] = None) -> Tuple[s try: spc = rpm.spec(spec_file_name) except Exception as ex: - print('Exception: {}\n'.format(ex)) - print("Cannot parse {}".format(pkg)) + log.exception("Cannot parse {}".format(pkg)) else: try: return spc.packages[0].header['Version'].decode() @@ -111,11 +125,13 @@ def package_version(project:str, pkg:str, etag: Optional[str] = None) -> Tuple[s pass -cutchars = len('python-') -project = 'devel:languages:python:singlespec-staging' -for pkg in list_packages(project): - pkg = pkg.strip() - if pkg: - assert pkg.startswith('python-') - pypi_name = pkg[cutchars:] - print("{} {}".format(pkg, package_version(project, pkg))) +if __name__=='__main__': + cutchars = len('python-') + project = 'devel:languages:python:singlespec-staging' + for pkg in suse_packages(project): + if pkg: + assert pkg.startswith('python-') + pypi_name = pkg[cutchars:] + pypi_ver = get_version_from_pypi(pypi_name) + suse_ver = package_version(project, pkg) + print(f"{pkg} {suse_ver} {pypi_ver}") |