[Reproducible-commits] [misc] 01/01: unreproducible-installed: Split code into functions and use CLI instead of hardcoded constants.

Valentin Lorentz progval-guest at moszumanska.debian.org
Sat Aug 15 10:05:56 UTC 2015


This is an automated email from the git hooks/post-receive script.

progval-guest pushed a commit to branch master
in repository misc.

commit 9ea16b99bb54d8264afadb178a41d1192da71932
Author: Valentin Lorentz <progval at progval.net>
Date:   Sat Aug 15 12:07:12 2015 +0200

    unreproducible-installed: Split code into functions and use CLI instead of hardcoded constants.
---
 unreproducible-installed | 136 +++++++++++++++++++++++++++++++++--------------
 1 file changed, 95 insertions(+), 41 deletions(-)

diff --git a/unreproducible-installed b/unreproducible-installed
index 86f2bf6..8a834f5 100755
--- a/unreproducible-installed
+++ b/unreproducible-installed
@@ -13,6 +13,7 @@ import sys
 import json
 import time
 import os.path
+import argparse
 import subprocess
 
 if sys.version_info[0] >= 3: # Python 3
@@ -20,48 +21,101 @@ if sys.version_info[0] >= 3: # Python 3
 else: # Python 2
     from urllib2 import urlopen
 
-URL = 'https://reproducible.debian.net/reproducible.json'
-PACKAGE_NOTES = os.path.join(os.path.dirname(__file__), '../notes/packages.yml')
 IGNORE_REPORTED = True
-SAVE_TO = '/tmp/reproducible.json'
-FILE_NAMES = ('./reproducible.json', SAVE_TO)
-MAX_AGE = 60*60
-
-if IGNORE_REPORTED:
-    import yaml
-
-for filename in FILE_NAMES:
-    if os.path.isfile(filename) and \
-            os.path.getmtime(filename) + MAX_AGE > time.time():
-        # If this file is recent enough, read it
-        with open(filename) as fd:
-            data = json.load(fd)
-        break
-else: # If no recent file has been found, download the data
-    response = urlopen(URL).read()
+DEFAULT_SAVE_TO = '/tmp/reproducible.json'
+DEFAULT_FILE_NAMES = ('./reproducible.json', DEFAULT_SAVE_TO)
+
+parser = argparse.ArgumentParser(
+    description='Lists unreproducible packages installed on this system.')
+rj_group = parser.add_argument_group('reproducible.json')
+rj_group.add_argument('--max-age', type=int, default=60*60,
+    help='Maximum age of the reproducible.json file. If the file is older, '
+         'it will be redownloaded. Setting this to 0 disables this check.')
+rj_group.add_argument('--download-url', type=str,
+    default='https://reproducible.debian.net/reproducible.json',
+    help='URL where to download reproducible.json from.')
+rj_group.add_argument('--file-name', type=str, default=None,
+    help='Where to find the reproducible.json file.')
+rj_group.add_argument('--save-to', type=str, default=None,
+    help='Where to store the reproducible.json if it was downloaded. '
+         'Defaults to --file-name if given, %s otherwise.')
+npy_group = parser.add_argument_group('nodes/packages.yml')
+npy_group.add_argument('--ignore-with-bugs', action='store_true',
+    help='Do not show packages with bugs reported by the RB team.')
+npy_group.add_argument('--package-notes-file', type=str,
+    default=os.path.join(os.path.dirname(__file__), '../notes/packages.yml'),
+    help='File containing packages notes, used with --ignore-with-bugs.')
+
+def find_data(args):
+    """Search for the reproducible.json file and load it."""
+    if args.file_name:
+        filenames = (args.file_name,)
+    else:
+        filenames = DEFAULT_FILE_NAMES
+    for filename in filenames:
+        if os.path.isfile(filename) and \
+                (args.max_age == 0 or
+                os.path.getmtime(filename) + args.max_age > time.time()):
+            # If this file is recent enough, read it
+            with open(filename) as fd:
+                return json.load(fd)
+    return None
+
+
+def download_data(args):
+    """Download the reproducible.json file, save it, and return its content."""
+    response = urlopen(args.download_url).read()
     if sys.version_info[0] >= 3:
         response = response.decode('utf-8')
-    if os.path.isfile(SAVE_TO):
-        os.unlink(SAVE_TO)
-    with open(SAVE_TO, 'a') as fd:
+
+    # Take the first that is not None.
+    save_to = args.save_to or args.file_name or DEFAULT_SAVE_TO
+
+    if os.path.isfile(save_to):
+        os.unlink(save_to)
+    with open(save_to, 'a') as fd:
         fd.write(response)
-    data = json.loads(response)
-
-unreproducible = {x['package']
-                  for x in data
-                  if x['status'] != 'reproducible'
-                  and x['suite'] == 'unstable'} # we only care about unstable
-
-if IGNORE_REPORTED:
-    with open(PACKAGE_NOTES) as fd:
-        notes = yaml.load(fd)
-    unreproducible = {x for x in unreproducible if 'bugs' in notes.get(x, {})}
-
-dpkg_output = subprocess.check_output(['dpkg', '-l'], universal_newlines=True)
-dpkg_list = dpkg_output.split('\n')
-installed = {x.split('  ')[1].split(':')[0] # keep the second field, strip arch
-             for x in dpkg_list
-             if x.startswith('i')} # ignore headers
-
-for package in sorted(installed & unreproducible):
-    print(package)
+    return json.loads(response)
+
+def get_unreproducible(args):
+    data = find_data(args)
+    if not data:
+        data = download_data(args)
+
+    unreproducible = {x['package']
+                      for x in data
+                      if x['status'] != 'reproducible'
+                      and x['suite'] == 'unstable'} # we only care about unstable
+    return unreproducible
+
+def get_installed_packages(args):
+    dpkg_output = subprocess.check_output(['dpkg', '-l'], universal_newlines=True)
+    dpkg_list = dpkg_output.split('\n')
+    installed = {x.split('  ')[1].split(':')[0] # keep the second field, strip arch
+                 for x in dpkg_list
+                 if x.startswith('i')} # ignore headers
+    return installed
+
+def main(args):
+    unreproducible = get_unreproducible(args)
+
+    if args.ignore_with_bugs:
+        import yaml
+        with open(args.package_notes_file) as fd:
+            notes = yaml.load(fd)
+        unreproducible = {x for x in unreproducible if 'bugs' in notes.get(x, {})}
+
+    installed = get_installed_packages(args)
+
+    for package in sorted(installed & unreproducible):
+        print(package)
+
+if __name__ == '__main__':
+    args = parser.parse_args()
+    if args.ignore_with_bugs:
+        try:
+            import yaml
+        except ImportError:
+            print('--ignore-with-bugs requires PyYaml to be installed.')
+            exit(1)
+    main(args)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/misc.git



More information about the Reproducible-commits mailing list