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):
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)

View file

@ -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.')

View file

@ -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

View file

@ -13,6 +13,6 @@ class Phase(object):
def __str__(self):
return '{name}'.format(name=self.__class__.__name__)
def __repr__(self):
return self.__str__()

View file

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

View file

@ -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)

View file

@ -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

View file

@ -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,
]

View file

@ -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')

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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