From f975faf63cc8bb9ea14f8bee5f1202b7e9a6aeba Mon Sep 17 00:00:00 2001 From: Zygmunt Krynicki Date: Fri, 27 Jul 2018 12:35:13 +0200 Subject: [PATCH 1/6] Ignore .pyc files in the tree Signed-off-by: Zygmunt Krynicki --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index dde5b09a..c8cd8094 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/_site/ \ No newline at end of file +/_site/ +*.pyc From 9538233f68245afe594983ed02b505f91b810094 Mon Sep 17 00:00:00 2001 From: Zygmunt Krynicki Date: Fri, 27 Jul 2018 13:00:50 +0200 Subject: [PATCH 2/6] Use: if foo: ... rather than if(foo): ... The extra parentheses are not required and are not idiomatic python Signed-off-by: Zygmunt Krynicki --- imagefactory | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/imagefactory b/imagefactory index a05f842b..346718ce 100755 --- a/imagefactory +++ b/imagefactory @@ -90,7 +90,7 @@ class Application(Singleton): logger.removeHandler(currhandler) self.log = logging.getLogger('%s.%s' % (__name__, self.__class__.__name__)) # Considerably increases the logging output... - if (self.app_config['debug']): + if self.app_config['debug']: logging.getLogger('').setLevel(logging.DEBUG) ### Use FaultHandler if present... # Mark the start of this run in our stderr/stdout debug redirect @@ -108,11 +108,11 @@ class Application(Singleton): except: logging.debug("Unable to find python module, faulthandler... multi-thread tracebacks will not be available. See http://pypi.python.org/pypi/faulthandler/ for more information.") pass - elif (self.app_config['verbose']): + elif self.app_config['verbose']: logging.getLogger('').setLevel(logging.INFO) def signal_handler(self, signum, stack): - if (signum == signal.SIGTERM): + if signum == signal.SIGTERM: logging.warn('caught signal SIGTERM, stopping...') # Run the abort() method in all running builders @@ -166,7 +166,7 @@ class Application(Singleton): image = None parameters_file = self.app_config.get('parameters') - if(parameters_file): + if parameters_file: try: parameters = json.loads(parameters_file.read().rstrip()) except: @@ -192,25 +192,25 @@ class Application(Singleton): logging.debug("Parameters are:") logging.debug("\n%s" % (pprint.pformat(parameters))) - if(command in ('base_image', 'target_image', 'provider_image')): + if command in ('base_image', 'target_image', 'provider_image'): template = None tdl_file = self.app_config.get('template') - if(tdl_file): + if tdl_file: template = tdl_file.read() - if(command == 'base_image'): + if command == 'base_image': builder = BuildDispatcher().builder_for_base_image(template=template, parameters=parameters) image = builder.base_image thread = builder.base_thread - elif(command == 'target_image'): + elif command == 'target_image': builder = BuildDispatcher().builder_for_target_image(target=self.app_config['target'], image_id=self.app_config.get('id'), template=template, parameters=parameters) image = builder.target_image thread = builder.target_thread - elif(command == 'provider_image'): + elif command == 'provider_image': # This is a convenience argument for the CLI - Without it users are forced to pass in an entire JSON file # just to select the snapshot style of build - This argument is always present in provider_image invocations # and defaults to false. Do not override a pre-existing snapshot value if it is present in parameters @@ -233,7 +233,7 @@ class Application(Singleton): for key in image.metadata(): returnval[key] = getattr(image, key, None) - elif(command == 'import_base_image'): + elif command == 'import_base_image': import_image_file = self.app_config.get('image_file') logging.info('Importing image %s' % (import_image_file)) importer = BaseImageImporter(import_image_file) @@ -241,7 +241,7 @@ class Application(Singleton): for key in image.metadata(): returnval[key] = getattr(image, key, None) - elif(command == 'images'): + elif command == 'images': fetch_spec = json.loads(self.app_config['fetch_spec']) fetched_images = PersistentImageManager.default_manager().images_from_query(fetch_spec) images = list() @@ -250,25 +250,25 @@ class Application(Singleton): for key in image.metadata(): item[key] = getattr(image, key, None) images.append(item) - if(len(images) > 1): + if len(images) > 1: returnval['images'] = images else: try: returnval = images[0] except IndexError: - if(self.app_config['debug']): + if self.app_config['debug']: print "No images matching fetch specification (%s) found." % (self.app_config['fetch_spec']) except Exception as e: print e sys.exit(1) - elif(command == 'delete'): + elif command == 'delete': try: image_id = self.app_config['id'] provider = self._get_provider(self.app_config['provider']) image = PersistentImageManager.default_manager().image_with_id(image_id) - if(not image): - print ('No image found with id: %s' % image_id) + if not image: + print('No image found with id: %s' % image_id) return builder = Builder() builder.delete_image(provider=provider, @@ -282,17 +282,17 @@ class Application(Singleton): except Exception as e: self.log.exception(e) print('Failed to delete image %s, see the log for exception details.' % image_id) - elif(command == 'plugins'): + elif command == 'plugins': plugin_id = self.app_config.get('id') returnval = PluginManager().plugins[plugin_id].copy() if plugin_id else PluginManager().plugins.copy() formatted_returnval = json.dumps(returnval, indent=2) - if(self.app_config['output'] == 'json'): - if(PYGMENT and not self.app_config['raw']): + if self.app_config['output'] == 'json': + if PYGMENT and not self.app_config['raw']: print highlight(formatted_returnval, JSONLexer(), TerminalFormatter()) else: - if(self.app_config['debug'] and not self.app_config['raw']): + if self.app_config['debug'] and not self.app_config['raw']: print('Python module "pygments" found. Install this module if you want syntax colorization.') print(formatted_returnval) @@ -300,7 +300,7 @@ class Application(Singleton): # Wait for the primary worker thread to complete if it exists thread.join() - if (image and (self.app_config['output'] == 'log')): + if image and self.app_config['output'] == 'log': if image.status == "FAILED": print print "Image build FAILED with error: %s" % (image.status_detail['error']) From 086f24b3bde3331ed99f373afce593a8791d91c6 Mon Sep 17 00:00:00 2001 From: Zygmunt Krynicki Date: Fri, 27 Jul 2018 13:01:02 +0200 Subject: [PATCH 3/6] Bump Copyright --- imagefactory | 1 + 1 file changed, 1 insertion(+) diff --git a/imagefactory b/imagefactory index 346718ce..495e58fe 100755 --- a/imagefactory +++ b/imagefactory @@ -2,6 +2,7 @@ # encoding: utf-8 # Copyright 2012 Red Hat, Inc. +# Copyright 2018 Canonical, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. From 4efdf1cb6c4b003d5938b0253c632dec4b4618e4 Mon Sep 17 00:00:00 2001 From: Zygmunt Krynicki Date: Fri, 27 Jul 2018 13:13:23 +0200 Subject: [PATCH 4/6] Sort imports according to standards The standard import order is stdlib, 3rd party, internal with newline between each section. Signed-off-by: Zygmunt Krynicki --- imagefactory | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/imagefactory b/imagefactory index 495e58fe..8cd48f19 100755 --- a/imagefactory +++ b/imagefactory @@ -16,20 +16,21 @@ # See the License for the specific language governing permissions and # limitations under the License. -import sys +import json import logging -import signal import os -import json import pprint +import signal +import sys from time import asctime, localtime -from imgfac.Singleton import Singleton + from imgfac.ApplicationConfiguration import ApplicationConfiguration +from imgfac.BaseImageImporter import BaseImageImporter from imgfac.BuildDispatcher import BuildDispatcher -from imgfac.PluginManager import PluginManager -from imgfac.PersistentImageManager import PersistentImageManager from imgfac.Builder import Builder -from imgfac.BaseImageImporter import BaseImageImporter +from imgfac.PersistentImageManager import PersistentImageManager +from imgfac.PluginManager import PluginManager +from imgfac.Singleton import Singleton try: from pygments import highlight From 5c27b9c219f6a78339e757bd3695883988721328 Mon Sep 17 00:00:00 2001 From: Zygmunt Krynicki Date: Fri, 27 Jul 2018 13:14:05 +0200 Subject: [PATCH 5/6] Remove unneeded, empty __init__ Signed-off-by: Zygmunt Krynicki --- imagefactory | 3 --- 1 file changed, 3 deletions(-) diff --git a/imagefactory b/imagefactory index 8cd48f19..a92cdec6 100755 --- a/imagefactory +++ b/imagefactory @@ -64,9 +64,6 @@ guestfs.GuestFS = GuestFS class Application(Singleton): - def __init__(self): - pass - def _singleton_init(self): super(Application, self)._singleton_init() logging.basicConfig(level=logging.WARNING, format='%(asctime)s %(levelname)s %(name)s thread(%(threadName)s) Message: %(message)s') From b8e44412379815387909263076d3961492c03312 Mon Sep 17 00:00:00 2001 From: Zygmunt Krynicki Date: Fri, 27 Jul 2018 13:18:22 +0200 Subject: [PATCH 6/6] Future-import print_function, fix print calls Apart from the future import where print is a function, not a statement, fix incorrect print formatting calls. Calls like print("%s" % (foo)) are subtly broken, depending on the value of foo, since (foo) is not a tuple but really syntactic no-op. The correct way to do that is to use print("%s" % (foo,)) Signed-off-by: Zygmunt Krynicki --- imagefactory | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/imagefactory b/imagefactory index a92cdec6..8d43559b 100755 --- a/imagefactory +++ b/imagefactory @@ -16,6 +16,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import print_function + import json import logging import os @@ -256,9 +258,9 @@ class Application(Singleton): returnval = images[0] except IndexError: if self.app_config['debug']: - print "No images matching fetch specification (%s) found." % (self.app_config['fetch_spec']) + print("No images matching fetch specification (%s) found." % (self.app_config['fetch_spec'],)) except Exception as e: - print e + print(e) sys.exit(1) elif command == 'delete': @@ -289,7 +291,7 @@ class Application(Singleton): if self.app_config['output'] == 'json': if PYGMENT and not self.app_config['raw']: - print highlight(formatted_returnval, JSONLexer(), TerminalFormatter()) + print(highlight(formatted_returnval, JSONLexer(), TerminalFormatter())) else: if self.app_config['debug'] and not self.app_config['raw']: print('Python module "pygments" found. Install this module if you want syntax colorization.') @@ -301,23 +303,23 @@ class Application(Singleton): if image and self.app_config['output'] == 'log': if image.status == "FAILED": - print - print "Image build FAILED with error: %s" % (image.status_detail['error']) + print() + print("Image build FAILED with error: %s" % (image.status_detail['error'],)) sys.exit(1) - print - print "============ Final Image Details ============" - print "UUID: %s" % (image.identifier) - print "Type: %s" % (command) - print "Image filename: " + image.data + print() + print("============ Final Image Details ============") + print("UUID: %s" % (image.identifier,)) + print("Type: %s" % (command,)) + print("Image filename: " + image.data) if command == "provider_image" and image.status == "COMPLETE": - print "Image ID on provider: %s" % (image.identifier_on_provider) + print("Image ID on provider: %s" % (image.identifier_on_provider,)) if image.status == "COMPLETE": - print "Image build completed SUCCESSFULLY!" + print("Image build completed SUCCESSFULLY!") sys.exit(0) else: - print "WARNING - Reached end of build with an unexpected status - this should not happen" - print "Status: %s" % (image.status) - print "Status Details: %s" % (image.status_detail) + print("WARNING - Reached end of build with an unexpected status - this should not happen") + print("Status: %s" % (image.status,)) + print("Status Details: %s" % (image.status_detail,)) sys.exit(1) if __name__ == "__main__":