diff-packages: put old rpm in the cache dir
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Wed, 10 Apr 2013 17:19:24 +0000 (20:19 +0300)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Wed, 10 Apr 2013 17:19:36 +0000 (20:19 +0300)
Also adds new setting 'cache-dir' in the config file

Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
tools/getpacs/diff-packages.py

index 77d66ed..26df74a 100755 (executable)
@@ -8,8 +8,9 @@ import os
 import re, base64
 import ConfigParser
 
-username = ""
-password = ""
+USERNAME = ""
+PASSWORD = ""
+CACHE_DIR = "cache"
 
 def read_config(config_file):
     config_file = os.path.expanduser(config_file)
@@ -20,8 +21,8 @@ def read_config(config_file):
 def http_get(url):
     print "Downloading %s" %url
     request = urllib2.Request(url)
-    if username and password:
-        base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
+    if USERNAME and PASSWORD:
+        base64string = base64.encodestring('%s:%s' % (USERNAME, PASSWORD)).replace('\n', '')
         request.add_header("Authorization", "Basic %s" % base64string)
     html_page = urllib2.urlopen(request)
     return html_page
@@ -35,11 +36,8 @@ def download(url, out):
     else:
         print "Already exists: %s" % out
 
-def get_package_list2(image_name, base_url, build_id):
-    cache_dir = "cache"
-    if not os.path.exists(cache_dir):
-        os.makedirs(cache_dir)
-    cache_file = "%s/%s-%s.packages" %(cache_dir, image_name, build_id )
+def get_package_list(image_name, base_url, build_id, cachedir):
+    cache_file = "%s/%s-%s.packages" %(cachedir, image_name, build_id )
     package_file = None
     if not os.path.exists(cache_file):
         image_packages = "%s/%s/images/%s/%s-%s.packages" %(base_url, build_id, image_name, image_name, build_id )
@@ -88,16 +86,23 @@ else:
     release_url = "%s/%s" %(BASE, SNAPSHOTS)
 
 if options.username:
-    username = options.username
+    USERNAME = options.username
 elif config.has_option('DEFAULT', 'username'):
-    username = config.get('DEFAULT', 'username')
+    USERNAME = config.get('DEFAULT', 'username')
 if options.password:
-    password = options.password
+    PASSWORD = options.password
 elif config.has_option('DEFAULT', 'password'):
-    password = config.get('DEFAULT', 'password')
-
-p1 = get_package_list2(options.image, release_url, options.old)
-p2 = get_package_list2(options.image, release_url, options.new)
+    PASSWORD = config.get('DEFAULT', 'password')
+# Initialize cache dir
+if config.has_option('DEFAULT', 'cache-dir'):
+    CACHE_DIR = config.get('DEFAULT', 'cache-dir')
+CACHE_DIR = os.path.abspath(os.path.expanduser(CACHE_DIR))
+packages_files_dir = os.path.join(CACHE_DIR, 'packages-files')
+if not os.path.exists(packages_files_dir):
+    os.makedirs(packages_files_dir)
+
+p1 = get_package_list(options.image, release_url, options.old, packages_files_dir)
+p2 = get_package_list(options.image, release_url, options.new, packages_files_dir)
 
 pkgs1 = {'%s|%s' % (pkg, attr['arch']) for pkg, attr in p1.iteritems()}
 pkgs2 = {'%s|%s' % (pkg, attr['arch']) for pkg, attr in p2.iteritems()}
@@ -107,8 +112,9 @@ pkgs1 = {'%s|%s' % (pkg, attr['version']) for pkg, attr in p1.iteritems()}
 pkgs2 = {'%s|%s' % (pkg, attr['version']) for pkg, attr in p2.iteritems()}
 changedpkgs = [pkg.split('|')[0] for pkg in pkgs2.difference(pkgs1) if pkg.split('|')[0] in p1]
 
-if not os.path.exists('old'):
-    os.makedirs('old')
+old_pkgs_dir = os.path.join(CACHE_DIR, 'rpms')
+if not os.path.exists(old_pkgs_dir):
+    os.makedirs(old_pkgs_dir)
 if not os.path.exists('new'):
     os.makedirs('new')
 if not os.path.exists('rpms'):
@@ -122,7 +128,7 @@ for p in newpkgs:
 for p in changedpkgs:
     rpm = "%s-%s.%s.rpm" % (p, p1[p]['version'], p1[p]['arch'])
     arch = p1[p]['arch']
-    download("%s/%s/repos/pc/x86_64/packages/%s/%s" % (release_url, options.old, arch, rpm), "old/%s" % rpm)
+    download("%s/%s/repos/pc/x86_64/packages/%s/%s" % (release_url, options.old, arch, rpm), os.path.join(old_pkgs_dir, rpm))
     rpm = "%s-%s.%s.rpm" % (p, p2[p]['version'], p2[p]['arch'])
     download("%s/%s/repos/pc/x86_64/packages/%s/%s" %(release_url, options.new, arch, rpm), "rpms/%s" % rpm)