aboutsummaryrefslogtreecommitdiffstats
path: root/dlpcvp.py
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2018-06-20 20:00:54 +0200
committerMatěj Cepl <mcepl@cepl.eu>2018-06-20 20:00:54 +0200
commitd9d9224f40ebc0e6a73e1fa828ebaa44891e443f (patch)
tree228d124069668b1cce83174a6b76581b6aed4494 /dlpcvp.py
parentce0d32c6b7aacc3e4f551b8cf4081ebc204cab11 (diff)
downloaddlp_check_version_PyPI-d9d9224f40ebc0e6a73e1fa828ebaa44891e443f.tar.gz
Cleanup and another attempt to silence rpm library.
Diffstat (limited to 'dlpcvp.py')
-rwxr-xr-xdlpcvp.py74
1 files changed, 47 insertions, 27 deletions
diff --git a/dlpcvp.py b/dlpcvp.py
index f63869d..cedcad6 100755
--- a/dlpcvp.py
+++ b/dlpcvp.py
@@ -12,7 +12,7 @@ import tempfile
import urllib.request
from urllib.error import HTTPError
from urllib.request import Request, urlopen
-from typing import Iterable, Tuple
+from typing import Iterable, Optional, Tuple
import xml.etree.ElementTree as ET
import rpm
@@ -42,9 +42,7 @@ 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)
@@ -54,8 +52,11 @@ def get_version_from_pypi(name: str, etag: str = None) -> Tuple[str, str, str]:
data = json.load(resp)
info_dict = data['info']
return info_dict['name'], info_dict['version'], resp.info()['ETag']
- except HTTPError:
- log.exception(f'Cannot find {name} on PyPI')
+ except HTTPError as ex:
+ if ex.getcode() == 404:
+ log.error(f'Cannot find {name} on PyPI')
+ else:
+ raise
def suse_packages(proj:str) -> Iterable[str]:
@@ -73,12 +74,16 @@ def suse_packages(proj:str) -> Iterable[str]:
root = raw_xml_data.getroot()
for elem in root.iter('entry'):
yield elem.get('name')
- except HTTPError:
- log.exception(f'Cannot find packages for {proj}!')
+ except HTTPError as ex:
+ if ex.getcode() == 404:
+ log.error(f'Cannot find packages for {proj}!')
+ else:
+ raise
def package_version(proj:str, pkg_name:str,
- etag_fn: str = None, etag_spcf: str = None) -> Tuple[str, str, str]:
+ etag_fn: str = None, etag_spcf: str = None) \
+ -> Optional[Tuple[str, str, str]]:
"""
Return the version of the given package in the given proj.
@@ -99,13 +104,20 @@ def package_version(proj:str, pkg_name:str,
name = elem.get('name')
if name.endswith('.spec'):
spec_files.append(name)
- except HTTPError:
- log.exception(f'Cannot accquire version of {pkg_name} in {proj}')
+ except HTTPError as ex:
+ if ex.getcode() == 404:
+ log.error(f'Cannot accquire version of {pkg_name} in {proj}')
+ else:
+ raise
if not spec_files:
IOError(f'Cannot find SPEC file for {pkg_name}')
- spc_fname = sorted(spec_files, key=len)[0]
+ try:
+ spc_fname = sorted(spec_files, key=len)[0]
+ except IndexError:
+ log.exception(f'Wrong value of spec_files: {spec_files}')
+ return
req_spec = Request(url=OBS_base + f'/source/{proj}/{pkg_name}/{spc_fname}')
@@ -125,22 +137,29 @@ def package_version(proj:str, pkg_name:str,
# rpm library generates awfull lot of nonsensical goo on
# stderr
- old_stderr = sys.stderr
- sys.stderr = os.devnull
- try:
- spc = rpm.spec(spec_file_name)
- except Exception as ex:
- log.exception("Cannot parse {}".format(pkg_name))
- else:
+ with open(os.devnull, 'wb') as nullf:
+ old_stderr = sys.stderr
+ old_stdout = sys.stdout
+ sys.stderr = nullf
+ sys.stdout = nullf
try:
- return spc.packages[0].header['Version'].decode(), \
- etag_fn, etag_spcf
- except IndexError:
- pass
- finally:
- sys.stderr = old_stderr
- except HTTPError:
- log.exception(f'Cannot parse SPEC file {spc_fname} for {pkg_name}')
+ spc = rpm.spec(spec_file_name)
+ except Exception as ex:
+ log.error("Cannot parse {}".format(pkg_name))
+ else:
+ try:
+ return spc.packages[0].header['Version'].decode(), \
+ etag_fn, etag_spcf
+ except IndexError:
+ pass
+ finally:
+ sys.stderr = old_stderr
+ sys.stdout = old_stdout
+ except HTTPError as ex:
+ if ex.getcode() == 404:
+ log.error(f'Cannot parse SPEC file {spc_fname} for {pkg_name}')
+ else:
+ raise
if __name__=='__main__':
@@ -152,4 +171,5 @@ if __name__=='__main__':
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}")
+ if suse_ver is not None:
+ print(f"{pkg} {suse_ver} {pypi_ver}")