From 2135cdbc1a674448081749c4de341b583e73e9f6 Mon Sep 17 00:00:00 2001 From: Anders Ingemann Date: Wed, 26 Jun 2013 20:14:37 +0200 Subject: [PATCH] PEP8 fixes --- base/log.py | 14 +++++++------- base/main.py | 1 + base/manifest.py | 2 ++ base/phase.py | 2 +- base/task.py | 1 - base/tasklist.py | 18 +++++++++--------- common/exceptions.py | 4 ++++ common/phases.py | 22 +++++++++++----------- common/tasks.py | 1 + providers/ec2/tasks/connection.py | 2 +- providers/ec2/tasks/ebs.py | 6 ++++++ providers/ec2/tasks/host.py | 1 + providers/ec2/tasks/packages.py | 8 ++++---- 13 files changed, 48 insertions(+), 34 deletions(-) diff --git a/base/log.py b/base/log.py index 4341f8a..0f5e4c2 100644 --- a/base/log.py +++ b/base/log.py @@ -2,7 +2,6 @@ import logging def get_logfile_path(manifest_path): - import sys import os.path from datetime import datetime @@ -12,6 +11,7 @@ def get_logfile_path(manifest_path): filename = "{timestamp}_{name}.log".format(timestamp=timestamp, name=manifest_name) return os.path.normpath(os.path.join(os.path.dirname(__file__), '../logs', filename)) + def setup_logger(logfile=None, debug=False): root = logging.getLogger() root.setLevel(logging.NOTSET) @@ -32,14 +32,14 @@ def setup_logger(logfile=None, debug=False): class ConsoleFormatter(logging.Formatter): - level_colors = { - logging.ERROR: 'red', - logging.WARNING: 'magenta', - logging.INFO: 'blue', - } + level_colors = {logging.ERROR: 'red', + logging.WARNING: 'magenta', + logging.INFO: 'blue', + } + def format(self, record): if(record.levelno in self.level_colors): - from termcolor import colored, cprint + from termcolor import colored record.msg = colored(record.msg, self.level_colors[record.levelno]) return super(ConsoleFormatter, self).format(record) diff --git a/base/main.py b/base/main.py index 99a71d2..f2e4f74 100644 --- a/base/main.py +++ b/base/main.py @@ -7,6 +7,7 @@ def main(): log.setup_logger(logfile=logfile, debug=args.debug) run(args) + def get_args(): from argparse import ArgumentParser parser = ArgumentParser(description='Bootstrap Debian for the cloud.') diff --git a/base/manifest.py b/base/manifest.py index 6eb61f1..836bd00 100644 --- a/base/manifest.py +++ b/base/manifest.py @@ -13,12 +13,14 @@ def load_manifest(path): manifest.load_plugins() return (provider, manifest) + def load_json(path): import json from minify_json import json_minify with open(path) as stream: return json.loads(json_minify(stream.read(), False)) + class Manifest(object): def __init__(self, path): self.path = path diff --git a/base/phase.py b/base/phase.py index 75a2dff..9dab1b3 100644 --- a/base/phase.py +++ b/base/phase.py @@ -13,6 +13,6 @@ class Phase(object): def __str__(self): return '{name}'.format(name=self.__class__.__name__) - + def __repr__(self): return self.__str__() diff --git a/base/task.py b/base/task.py index bd6c345..84d5174 100644 --- a/base/task.py +++ b/base/task.py @@ -2,7 +2,6 @@ from common.exceptions import TaskListError class Task(object): - phase = None before = [] after = [] diff --git a/base/tasklist.py b/base/tasklist.py index 192160a..f1cbf8f 100644 --- a/base/tasklist.py +++ b/base/tasklist.py @@ -34,7 +34,7 @@ class TaskList(object): log.info('Running {task}'.format(task=task)) task.run(bootstrap_info) tasks_completed.append(task) - except Exception, e: + except Exception as e: log.exception(e) log.error('Rolling back') for task in reversed(tasks_completed): @@ -58,7 +58,6 @@ class TaskList(object): succeeding_phases = order[order.index(task.phase)+1:] graph[task].extend([succ for succ in tasks if succ.phase in succeeding_phases]) - components = self.strongly_connected_components(graph) cycles_found = 0 for component in components: @@ -79,12 +78,13 @@ class TaskList(object): # Find the strongly connected components in a graph using Tarjan's algorithm. # graph should be a dictionary mapping node names to lists of successor nodes. - result = [ ] - stack = [ ] - low = { } + result = [] + stack = [] + low = {} def visit(node): - if node in low: return + if node in low: + return num = len(low) low[node] = num @@ -109,16 +109,16 @@ class TaskList(object): def topological_sort(self, graph): # Source: http://www.logarithmic.net/pfh-files/blog/01208083168/sort.py - count = { } + count = {} for node in graph: count[node] = 0 for node in graph: for successor in graph[node]: count[successor] += 1 - ready = [ node for node in graph if count[node] == 0 ] + ready = [node for node in graph if count[node] == 0] - result = [ ] + result = [] while ready: node = ready.pop(-1) result.append(node) diff --git a/common/exceptions.py b/common/exceptions.py index 5e325f5..a38dcdb 100644 --- a/common/exceptions.py +++ b/common/exceptions.py @@ -5,17 +5,21 @@ class ManifestError(Exception): self.message = message self.manifest = manifest self.json_path = json_path + def __str__(self): if self.json_path is not None: path = '.'.join(self.json_path) return "{2}\n\tFile: {0}\n\tJSON path: {1}".format(self.manifest.path, path, self.message) return "{0}: {1}".format(self.manifest.path, self.message) + class TaskListError(Exception): def __init__(self, message): self.message = message + def __str__(self): return "Error in tasklist: {0}".format(self.message) + class TaskException(Exception): pass diff --git a/common/phases.py b/common/phases.py index f274c11..6c4ab82 100644 --- a/common/phases.py +++ b/common/phases.py @@ -11,14 +11,14 @@ unmount_volume = Phase('Unmounting the bootstrap volume') register_image = Phase('Uploading/Registering with the provider') cleanup = Phase('Removing temporary files') -order = [preparation - ,volume_creation - ,volume_preparation - ,volume_mounting - ,install_os - ,modify_system - ,clean_system - ,unmount_volume - ,register_image - ,cleanup - ] +order = [preparation, + volume_creation, + volume_preparation, + volume_mounting, + install_os, + modify_system, + clean_system, + unmount_volume, + register_image, + cleanup, + ] diff --git a/common/tasks.py b/common/tasks.py index c919c08..c527e72 100644 --- a/common/tasks.py +++ b/common/tasks.py @@ -6,6 +6,7 @@ class TriggerRollback(Task): phase = phases.cleanup description = 'Triggering a rollback by throwing an exception' + def run(self, info): from common.exceptions import TaskException raise TaskException('Trigger rollback') diff --git a/providers/ec2/tasks/connection.py b/providers/ec2/tasks/connection.py index 818160e..336a467 100644 --- a/providers/ec2/tasks/connection.py +++ b/providers/ec2/tasks/connection.py @@ -33,7 +33,7 @@ class Connect(Task): description = 'Connecting to EC2' phase = phases.preparation after = [GetCredentials, host.GetInfo] - + def run(self, info): super(Connect, self).run(info) from boto.ec2 import connect_to_region diff --git a/providers/ec2/tasks/ebs.py b/providers/ec2/tasks/ebs.py index d3f149f..90beeb6 100644 --- a/providers/ec2/tasks/ebs.py +++ b/providers/ec2/tasks/ebs.py @@ -10,6 +10,7 @@ class CreateVolume(Task): after = [Connect] description = 'Creating an EBS volume for bootstrapping' + def run(self, info): volume_size = int(info.manifest.volume['size']/1024) @@ -19,15 +20,18 @@ class CreateVolume(Task): info.volume.update() rollback_description = 'Deleting the EBS volume' + def rollback(self, info): info.volume.delete() del info.volume + class AttachVolume(Task): phase = phases.volume_creation after = [CreateVolume] description = 'Attaching the EBS volume' + def run(self, info): def char_range(c1, c2): """Generates the characters from `c1` to `c2`, inclusive.""" @@ -51,11 +55,13 @@ class AttachVolume(Task): info.volume.update() rollback_description = 'Detaching the EBS volume' + def rollback(self, info): info.volume.detach() while info.volume.attachment_state() is not None: time.sleep(2) info.volume.update() + class VolumeError(TaskException): pass diff --git a/providers/ec2/tasks/host.py b/providers/ec2/tasks/host.py index 11f1a31..937856c 100644 --- a/providers/ec2/tasks/host.py +++ b/providers/ec2/tasks/host.py @@ -2,6 +2,7 @@ from base import Task from common import phases import packages + class CheckPackages(Task): description = 'Checking installed host packages' phase = phases.preparation diff --git a/providers/ec2/tasks/packages.py b/providers/ec2/tasks/packages.py index 3e449f3..d8b2c71 100644 --- a/providers/ec2/tasks/packages.py +++ b/providers/ec2/tasks/packages.py @@ -38,18 +38,18 @@ class ImagePackages(Task): if manifest.virtualization == 'pvm': include.add('grub-pc') - + exclude = set(['isc-dhcp-client', 'isc-dhcp-common', ]) - + # In squeeze, we need a special kernel flavor for xen kernels = {'squeeze': {'amd64': 'linux-image-xen-amd64', 'i386': 'linux-image-xen-686', }, 'wheezy': {'amd64': 'linux-image-amd64', 'i386': 'linux-image-686', }, } include.add(kernels.get(manifest.system['release']).get(manifest.system['architecture'])) - + include = include.union(manifest.system['packages']) - + info.img_packages = include, exclude