PEP8 fixes

This commit is contained in:
Anders Ingemann 2013-06-26 20:14:37 +02:00
parent 96028f96e1
commit 2135cdbc1a
13 changed files with 48 additions and 34 deletions

View file

@ -2,7 +2,6 @@ import logging
def get_logfile_path(manifest_path): def get_logfile_path(manifest_path):
import sys
import os.path import os.path
from datetime import datetime from datetime import datetime
@ -12,6 +11,7 @@ def get_logfile_path(manifest_path):
filename = "{timestamp}_{name}.log".format(timestamp=timestamp, name=manifest_name) filename = "{timestamp}_{name}.log".format(timestamp=timestamp, name=manifest_name)
return os.path.normpath(os.path.join(os.path.dirname(__file__), '../logs', filename)) return os.path.normpath(os.path.join(os.path.dirname(__file__), '../logs', filename))
def setup_logger(logfile=None, debug=False): def setup_logger(logfile=None, debug=False):
root = logging.getLogger() root = logging.getLogger()
root.setLevel(logging.NOTSET) root.setLevel(logging.NOTSET)
@ -32,14 +32,14 @@ def setup_logger(logfile=None, debug=False):
class ConsoleFormatter(logging.Formatter): class ConsoleFormatter(logging.Formatter):
level_colors = { level_colors = {logging.ERROR: 'red',
logging.ERROR: 'red', logging.WARNING: 'magenta',
logging.WARNING: 'magenta', logging.INFO: 'blue',
logging.INFO: 'blue', }
}
def format(self, record): def format(self, record):
if(record.levelno in self.level_colors): 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]) record.msg = colored(record.msg, self.level_colors[record.levelno])
return super(ConsoleFormatter, self).format(record) return super(ConsoleFormatter, self).format(record)

View file

@ -7,6 +7,7 @@ def main():
log.setup_logger(logfile=logfile, debug=args.debug) log.setup_logger(logfile=logfile, debug=args.debug)
run(args) run(args)
def get_args(): def get_args():
from argparse import ArgumentParser from argparse import ArgumentParser
parser = ArgumentParser(description='Bootstrap Debian for the cloud.') parser = ArgumentParser(description='Bootstrap Debian for the cloud.')

View file

@ -13,12 +13,14 @@ def load_manifest(path):
manifest.load_plugins() manifest.load_plugins()
return (provider, manifest) return (provider, manifest)
def load_json(path): def load_json(path):
import json import json
from minify_json import json_minify from minify_json import json_minify
with open(path) as stream: with open(path) as stream:
return json.loads(json_minify(stream.read(), False)) return json.loads(json_minify(stream.read(), False))
class Manifest(object): class Manifest(object):
def __init__(self, path): def __init__(self, path):
self.path = path self.path = path

View file

@ -2,7 +2,6 @@ from common.exceptions import TaskListError
class Task(object): class Task(object):
phase = None phase = None
before = [] before = []
after = [] after = []

View file

@ -34,7 +34,7 @@ class TaskList(object):
log.info('Running {task}'.format(task=task)) log.info('Running {task}'.format(task=task))
task.run(bootstrap_info) task.run(bootstrap_info)
tasks_completed.append(task) tasks_completed.append(task)
except Exception, e: except Exception as e:
log.exception(e) log.exception(e)
log.error('Rolling back') log.error('Rolling back')
for task in reversed(tasks_completed): for task in reversed(tasks_completed):
@ -58,7 +58,6 @@ class TaskList(object):
succeeding_phases = order[order.index(task.phase)+1:] succeeding_phases = order[order.index(task.phase)+1:]
graph[task].extend([succ for succ in tasks if succ.phase in succeeding_phases]) graph[task].extend([succ for succ in tasks if succ.phase in succeeding_phases])
components = self.strongly_connected_components(graph) components = self.strongly_connected_components(graph)
cycles_found = 0 cycles_found = 0
for component in components: for component in components:
@ -79,12 +78,13 @@ class TaskList(object):
# Find the strongly connected components in a graph using Tarjan's algorithm. # 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. # graph should be a dictionary mapping node names to lists of successor nodes.
result = [ ] result = []
stack = [ ] stack = []
low = { } low = {}
def visit(node): def visit(node):
if node in low: return if node in low:
return
num = len(low) num = len(low)
low[node] = num low[node] = num
@ -109,16 +109,16 @@ class TaskList(object):
def topological_sort(self, graph): def topological_sort(self, graph):
# Source: http://www.logarithmic.net/pfh-files/blog/01208083168/sort.py # Source: http://www.logarithmic.net/pfh-files/blog/01208083168/sort.py
count = { } count = {}
for node in graph: for node in graph:
count[node] = 0 count[node] = 0
for node in graph: for node in graph:
for successor in graph[node]: for successor in graph[node]:
count[successor] += 1 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: while ready:
node = ready.pop(-1) node = ready.pop(-1)
result.append(node) result.append(node)

View file

@ -5,17 +5,21 @@ class ManifestError(Exception):
self.message = message self.message = message
self.manifest = manifest self.manifest = manifest
self.json_path = json_path self.json_path = json_path
def __str__(self): def __str__(self):
if self.json_path is not None: if self.json_path is not None:
path = '.'.join(self.json_path) path = '.'.join(self.json_path)
return "{2}\n\tFile: {0}\n\tJSON path: {1}".format(self.manifest.path, path, self.message) 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) return "{0}: {1}".format(self.manifest.path, self.message)
class TaskListError(Exception): class TaskListError(Exception):
def __init__(self, message): def __init__(self, message):
self.message = message self.message = message
def __str__(self): def __str__(self):
return "Error in tasklist: {0}".format(self.message) return "Error in tasklist: {0}".format(self.message)
class TaskException(Exception): class TaskException(Exception):
pass pass

View file

@ -11,14 +11,14 @@ unmount_volume = Phase('Unmounting the bootstrap volume')
register_image = Phase('Uploading/Registering with the provider') register_image = Phase('Uploading/Registering with the provider')
cleanup = Phase('Removing temporary files') cleanup = Phase('Removing temporary files')
order = [preparation order = [preparation,
,volume_creation volume_creation,
,volume_preparation volume_preparation,
,volume_mounting volume_mounting,
,install_os install_os,
,modify_system modify_system,
,clean_system clean_system,
,unmount_volume unmount_volume,
,register_image register_image,
,cleanup cleanup,
] ]

View file

@ -6,6 +6,7 @@ class TriggerRollback(Task):
phase = phases.cleanup phase = phases.cleanup
description = 'Triggering a rollback by throwing an exception' description = 'Triggering a rollback by throwing an exception'
def run(self, info): def run(self, info):
from common.exceptions import TaskException from common.exceptions import TaskException
raise TaskException('Trigger rollback') raise TaskException('Trigger rollback')

View file

@ -10,6 +10,7 @@ class CreateVolume(Task):
after = [Connect] after = [Connect]
description = 'Creating an EBS volume for bootstrapping' description = 'Creating an EBS volume for bootstrapping'
def run(self, info): def run(self, info):
volume_size = int(info.manifest.volume['size']/1024) volume_size = int(info.manifest.volume['size']/1024)
@ -19,15 +20,18 @@ class CreateVolume(Task):
info.volume.update() info.volume.update()
rollback_description = 'Deleting the EBS volume' rollback_description = 'Deleting the EBS volume'
def rollback(self, info): def rollback(self, info):
info.volume.delete() info.volume.delete()
del info.volume del info.volume
class AttachVolume(Task): class AttachVolume(Task):
phase = phases.volume_creation phase = phases.volume_creation
after = [CreateVolume] after = [CreateVolume]
description = 'Attaching the EBS volume' description = 'Attaching the EBS volume'
def run(self, info): def run(self, info):
def char_range(c1, c2): def char_range(c1, c2):
"""Generates the characters from `c1` to `c2`, inclusive.""" """Generates the characters from `c1` to `c2`, inclusive."""
@ -51,11 +55,13 @@ class AttachVolume(Task):
info.volume.update() info.volume.update()
rollback_description = 'Detaching the EBS volume' rollback_description = 'Detaching the EBS volume'
def rollback(self, info): def rollback(self, info):
info.volume.detach() info.volume.detach()
while info.volume.attachment_state() is not None: while info.volume.attachment_state() is not None:
time.sleep(2) time.sleep(2)
info.volume.update() info.volume.update()
class VolumeError(TaskException): class VolumeError(TaskException):
pass pass

View file

@ -2,6 +2,7 @@ from base import Task
from common import phases from common import phases
import packages import packages
class CheckPackages(Task): class CheckPackages(Task):
description = 'Checking installed host packages' description = 'Checking installed host packages'
phase = phases.preparation phase = phases.preparation