adding ability to download a distro from url and show patches from that distro 81/50581/7
authorJamo Luhrsen <jluhrsen@redhat.com>
Tue, 17 Jan 2017 22:44:20 +0000 (14:44 -0800)
committerJamo Luhrsen <jluhrsen@redhat.com>
Thu, 19 Jan 2017 19:26:12 +0000 (11:26 -0800)
Change-Id: I2bb12f6b989af23ed82b48859fbb114ee124847c
Signed-off-by: Jamo Luhrsen <jluhrsen@redhat.com>
tools/distchanges/changes.py
tools/distchanges/gerritquery.py

index d5a45e78ff638293a267a7ee7f1f2829fe66911b..cb64fa15e92232dcd89614abe7be96ec24f09098 100644 (file)
@@ -3,7 +3,9 @@ import argparse
 import gerritquery
 import os
 import re
+import shutil
 import sys
+import urllib3
 import zipfile
 
 """
@@ -46,6 +48,7 @@ class Changes:
     PROJECT_NAMES = NETVIRT_PROJECTS
     VERBOSE = 0
     DISTRO_PATH = "/tmp/distribution-karaf"
+    DISTRO_URL = None
     REMOTE_URL = gerritquery.GerritQuery.REMOTE_URL
     BRANCH = "master"
     LIMIT = 10
@@ -53,6 +56,7 @@ class Changes:
 
     gerritquery = None
     distro_path = DISTRO_PATH
+    distro_url = DISTRO_URL
     project_names = PROJECT_NAMES
     branch = BRANCH
     limit = LIMIT
@@ -97,6 +101,42 @@ class Changes:
         for project in project_names:
             self.projects[project] = {"commit": [], "includes": []}
 
+    def download_distro(self):
+        """
+        Download the distribution from self.distro_url and extract it to self.distro_path
+        """
+        if self.verbose >= 2:
+            print("attempting to download distribution from %s and extract to %s " %
+                  (self.distro_url, self.distro_path))
+
+        tmp_distro_zip = '/tmp/distro.zip'
+        tmp_unzipped_location = '/tmp/distro_unzipped'
+        downloader = urllib3.PoolManager(cert_reqs='CERT_NONE')
+
+        # disabling warnings to prevent scaring the user with InsecureRequestWarning
+        urllib3.disable_warnings()
+
+        downloaded_distro = downloader.request('GET', self.distro_url)
+        with open(tmp_distro_zip, 'wb') as f:
+            f.write(downloaded_distro.data)
+
+        downloaded_distro.release_conn()
+
+        # after the .zip is extracted we want to rename it to be the distro_path which may have
+        # been given by the user
+        distro_zip = zipfile.ZipFile(tmp_distro_zip, 'r')
+        distro_zip.extractall(tmp_unzipped_location)
+        unzipped_distro_folder = os.listdir(tmp_unzipped_location)
+
+        # if the distro_path already exists, we wont overwrite it and just continue hoping what's
+        # there is relevant (and maybe already put there by this tool earlier)
+        try:
+            os.rename(tmp_unzipped_location + "/" + unzipped_distro_folder[0], self.distro_path)
+        except OSError as e:
+            print(e)
+            print("Unable to move extracted files from %s to %s. Using whatever bits are already there" %
+                  (tmp_unzipped_location, self.distro_path))
+
     def get_includes(self, project, changeid=None, msg=None):
         """
         Get the gerrits that would be included before the change merge time.
@@ -234,6 +274,10 @@ class Changes:
 
         self.init()
         self.print_options()
+
+        if self.distro_url is not None:
+            self.download_distro()
+
         for project in self.projects:
             changeid = self.find_distro_changeid(project)
             if changeid:
@@ -247,6 +291,8 @@ class Changes:
                             help="git branch for patch under test")
         parser.add_argument("-d", "--distro-path", dest="distro_path", default=self.DISTRO_PATH,
                             help="path to the expanded distribution, i.e. " + self.DISTRO_PATH)
+        parser.add_argument("-u", "--distro-url", dest="distro_url", default=self.DISTRO_URL,
+                            help="optional url to download a distribution " + str(self.DISTRO_URL))
         parser.add_argument("-l", "--limit", dest="limit", type=int, default=self.LIMIT,
                             help="number of gerrits to return")
         parser.add_argument("-p", "--projects", dest="projects", default=self.PROJECT_NAMES,
@@ -257,7 +303,6 @@ class Changes:
                             help="git remote url to use for gerrit")
         parser.add_argument("-v", "--verbose", dest="verbose", action="count", default=self.VERBOSE,
                             help="Output more information about what's going on")
-
         parser.add_argument("--license", dest="license", action="store_true",
                             help="Print the license and exit")
         parser.add_argument("-V", "--version", action="version",
@@ -272,6 +317,7 @@ class Changes:
 
         self.branch = options.branch
         self.distro_path = options.distro_path
+        self.distro_url = options.distro_url
         self.limit = options.limit
         self.project_names = options.projects
         self.qlimit = options.qlimit
index 9891d5f20c3c2a1fdbbcf65f0bfbcf22efccb1c4..7ebecc51294feffc8ad8254f5fc3d0bc6463fcdf 100644 (file)
@@ -73,7 +73,8 @@ class GerritQuery:
     @staticmethod
     def print_safe_encoding(string):
         if sys.stdout.encoding is None:
-            print(string)
+            # just print(string) could still throw a UnicodeEncodeError sometimes so casting string to unicode
+            print(unicode(string))
         else:
             print(string.encode(sys.stdout.encoding, 'replace'))