From 15f2430e9e3e9ed7e196ec8fbd9ff1702ca6cb74 Mon Sep 17 00:00:00 2001 From: Nicholas Corgan Date: Mon, 4 Nov 2013 08:20:47 -0800 Subject: uhd_images_downloader fixes/improvements * Permissions checking leads to clearer errors when user doesn't have write permissions * Relative paths work --- host/utils/uhd_images_downloader.py.in | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'host') diff --git a/host/utils/uhd_images_downloader.py.in b/host/utils/uhd_images_downloader.py.in index e7fc9e8a5..87c148258 100644 --- a/host/utils/uhd_images_downloader.py.in +++ b/host/utils/uhd_images_downloader.py.in @@ -44,7 +44,11 @@ class temp_dir(): self.name = tempfile.mkdtemp() return self.name def __exit__(self, type, value, traceback): - os.removedirs(self.name) + try: + os.removedirs(self.name) + except: + #Utility should have already detected this, but this is for safety + raise Exception("Could not install images! Make sure you have write permissions.") if __name__ == "__main__": @@ -59,17 +63,26 @@ if __name__ == "__main__": images_zip_md5sum = "@UHD_IMAGES_MD5SUM@" filename = images_src.split("/")[-1] + #Use this directory with relative paths + current_directory = os.getcwd() + with temp_dir() as dirname: os.chdir(dirname) #Configuring image destination if options.install_location != "": - images_dir = options.install_location + images_dir = os.path.abspath(os.path.join(current_directory, options.install_location)) elif os.environ.get("UHD_IMAGES_DIR") != "" and os.environ.get("UHD_IMAGES_DIR") != None: images_dir = os.environ.get("UHD_IMAGES_DIR") else: images_dir = "@CMAKE_INSTALL_PREFIX@/share/uhd/images" + #Before doing anything, check for write permissions in parent directory + parent_directory = os.path.dirname(images_dir) + if not os.access(parent_directory, os.W_OK): + sys.stderr.write("You do not have write permissions at the install location!\n") + sys.exit(1) + opener = urllib2.build_opener() opener.add_headers = [('User-Agent', 'UHD Images Downloader')] u = opener.open(images_src) @@ -118,10 +131,10 @@ if __name__ == "__main__": shutil.rmtree(images_dir) except: sys.stderr.write("\nMake sure you have write permissions in the images directory.\n") - sys.exit(0) + sys.exit(1) #Copying downloaded images into images_dir - shutil.copytree("tempdir/%s/share/uhd/images" % filename[:-4],images_dir) + shutil.copytree(os.path.join('tempdir', os.path.splitext(filename)[0], 'share', 'uhd', 'images'), images_dir) #Removing tempdir and zip file shutil.rmtree("tempdir") -- cgit v1.2.3 From d02adc5693fa34f81278c0d5c9d94bfd3bee7943 Mon Sep 17 00:00:00 2001 From: Nicholas Corgan Date: Fri, 8 Nov 2013 09:18:31 -0800 Subject: uhd_images_downloader: more improvements/fixes * Better error handling * Improved default download location logic --- host/utils/uhd_images_downloader.py.in | 38 +++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 14 deletions(-) (limited to 'host') diff --git a/host/utils/uhd_images_downloader.py.in b/host/utils/uhd_images_downloader.py.in index 87c148258..91f32825f 100644 --- a/host/utils/uhd_images_downloader.py.in +++ b/host/utils/uhd_images_downloader.py.in @@ -45,16 +45,23 @@ class temp_dir(): return self.name def __exit__(self, type, value, traceback): try: - os.removedirs(self.name) - except: + shutil.rmtree(self.name) + except OSError: #Utility should have already detected this, but this is for safety raise Exception("Could not install images! Make sure you have write permissions.") if __name__ == "__main__": + print + if os.environ.get("UHD_IMAGES_DIR") != None and os.environ.get("UHD_IMAGES_DIR") != "": + default_images_dir = os.environ.get("UHD_IMAGES_DIR") + print "UHD_IMAGES_DIR environment variable is set. Default install location: %s" % default_images_dir + else: + default_images_dir = "@CMAKE_INSTALL_PREFIX@/share/uhd/images" + #Command line options parser = OptionParser() - parser.add_option("--install-location", type="string", default="", help="Set custom install location for images") + parser.add_option("--install-location", type="string", default=default_images_dir, help="Set custom install location for images") parser.add_option("--buffer-size", type="int", default=8192, help="Set download buffer size, [default=%default]",) (options, args) = parser.parse_args() @@ -69,18 +76,21 @@ if __name__ == "__main__": with temp_dir() as dirname: os.chdir(dirname) - #Configuring image destination - if options.install_location != "": - images_dir = os.path.abspath(os.path.join(current_directory, options.install_location)) - elif os.environ.get("UHD_IMAGES_DIR") != "" and os.environ.get("UHD_IMAGES_DIR") != None: - images_dir = os.environ.get("UHD_IMAGES_DIR") + if options.install_location == default_images_dir: + images_dir = default_images_dir + elif os.path.isabs(options.install_location): + #Custom absolute path given + images_dir = options.install_location else: - images_dir = "@CMAKE_INSTALL_PREFIX@/share/uhd/images" - + #Custom relative path given, so construct absolute path + images_dir = os.path.abspath(os.path.join(current_directory, options.install_location)) + #Before doing anything, check for write permissions in parent directory parent_directory = os.path.dirname(images_dir) - if not os.access(parent_directory, os.W_OK): - sys.stderr.write("You do not have write permissions at the install location!\n") + if os.access(parent_directory, os.W_OK): + print "Downloading images to: %s" % images_dir + else: + print "You do not have write permissions at the install location!" sys.exit(1) opener = urllib2.build_opener() @@ -129,8 +139,8 @@ if __name__ == "__main__": if os.path.exists(images_dir): try: shutil.rmtree(images_dir) - except: - sys.stderr.write("\nMake sure you have write permissions in the images directory.\n") + except OSError: + print "Make sure you have write permissions in the images directory." sys.exit(1) #Copying downloaded images into images_dir -- cgit v1.2.3 From 402a39feb2600c2f366b8218ebd4c6b8562b4af4 Mon Sep 17 00:00:00 2001 From: Nicholas Corgan Date: Wed, 13 Nov 2013 06:59:47 -0800 Subject: uhd_images_downloader: redundancy fixes, better error handling --- host/utils/uhd_images_downloader.py.in | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'host') diff --git a/host/utils/uhd_images_downloader.py.in b/host/utils/uhd_images_downloader.py.in index 91f32825f..93abffaf4 100644 --- a/host/utils/uhd_images_downloader.py.in +++ b/host/utils/uhd_images_downloader.py.in @@ -46,8 +46,9 @@ class temp_dir(): def __exit__(self, type, value, traceback): try: shutil.rmtree(self.name) - except OSError: + except OSError,e: #Utility should have already detected this, but this is for safety + print str(e) raise Exception("Could not install images! Make sure you have write permissions.") if __name__ == "__main__": @@ -76,9 +77,7 @@ if __name__ == "__main__": with temp_dir() as dirname: os.chdir(dirname) - if options.install_location == default_images_dir: - images_dir = default_images_dir - elif os.path.isabs(options.install_location): + if os.path.isabs(options.install_location): #Custom absolute path given images_dir = options.install_location else: @@ -127,29 +126,32 @@ if __name__ == "__main__": os.remove(filename) os.chdir("/".join(images_dir.split("/")[:-1])) else: + temp_path = "tempdir" + #Extracting contents of zip file - if os.path.exists("tempdir"): - shutil.rmtree("tempdir") - os.mkdir("tempdir") + if os.path.exists(temp_path): + shutil.rmtree(temp_path) + os.mkdir(temp_path) images_zip = zipfile.ZipFile(filename) - images_zip.extractall("tempdir") + images_zip.extractall(temp_path) #Removing images currently in images_dir if os.path.exists(images_dir): try: shutil.rmtree(images_dir) - except OSError: + except OSError,e: + print str(e) print "Make sure you have write permissions in the images directory." sys.exit(1) #Copying downloaded images into images_dir - shutil.copytree(os.path.join('tempdir', os.path.splitext(filename)[0], 'share', 'uhd', 'images'), images_dir) + shutil.copytree(os.path.join(temp_path, os.path.splitext(filename)[0], 'share', 'uhd', 'images'), images_dir) #Removing tempdir and zip file - shutil.rmtree("tempdir") + shutil.rmtree(temp_path) images_zip.close() os.remove(filename) os.chdir(images_dir) - print "\nImages successfully installed to: %s" % images_dir + print "\n\nImages successfully installed!" -- cgit v1.2.3 From 57e5360ea9d0dffbd3140065fcdcecc10dfcecff Mon Sep 17 00:00:00 2001 From: Nicholas Corgan Date: Tue, 19 Nov 2013 06:32:02 -0800 Subject: uhd_images_downloader: more descriptive error when MD5 check fails --- host/utils/uhd_images_downloader.py.in | 2 ++ 1 file changed, 2 insertions(+) (limited to 'host') diff --git a/host/utils/uhd_images_downloader.py.in b/host/utils/uhd_images_downloader.py.in index 93abffaf4..bb082190c 100644 --- a/host/utils/uhd_images_downloader.py.in +++ b/host/utils/uhd_images_downloader.py.in @@ -123,8 +123,10 @@ if __name__ == "__main__": if images_zip_md5sum != downloaded_zip_md5sum: print "\nMD5 checksum does not match!" print "Expected %s, got %s" % (images_zip_md5sum, downloaded_zip_md5sum) + print "Images did not install. If problem persists, please contact support@ettus.com." os.remove(filename) os.chdir("/".join(images_dir.split("/")[:-1])) + sys.exit(1) else: temp_path = "tempdir" -- cgit v1.2.3