From 74d5761f260b5d887733e826b4ce6aed11f11487 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Tue, 1 Jan 2019 16:17:43 +0100 Subject: [PATCH] a little nicer --- logger.py | 2 +- plugins/privatebridge.py | 40 ++++++++++++++++++++-------------------- sliver_lxc.py | 4 ++-- tools.py | 7 ++++--- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/logger.py b/logger.py index 8216cfb..064e98e 100644 --- a/logger.py +++ b/logger.py @@ -110,7 +110,7 @@ class Buffer: def add(self, c): self.buffer += c - if c=='\n': + if c == '\n': self.flush() def flush(self): diff --git a/plugins/privatebridge.py b/plugins/privatebridge.py index 8e77524..9489f69 100644 --- a/plugins/privatebridge.py +++ b/plugins/privatebridge.py @@ -1,35 +1,31 @@ -#!/usr/bin/env python3 +""" +Private Bridge configurator. +""" -""" Private Bridge configurator. """ - -import http.client -import os import select -import shutil import subprocess import time import tools -from threading import Thread import logger -import tools priority = 9 class OvsException (Exception) : - def __init__ (self, message="no message"): - self.message=message - def __repr__ (self): return message + def __init__(self, message="no message"): + self.message = message + def __repr__(self): + return self.message def start(): logger.log('private bridge plugin starting up...') def log_call_read(command, timeout=logger.default_timeout_minutes*60, poll=1): - message=" ".join(command) + message = " ".join(command) logger.log("log_call: running command %s" % message) logger.verbose("log_call: timeout=%r s" % timeout) logger.verbose("log_call: poll=%r s" % poll) - trigger=time.time()+timeout + trigger = time.time()+timeout try: child = subprocess.Popen( command, bufsize=1, @@ -40,8 +36,9 @@ def log_call_read(command, timeout=logger.default_timeout_minutes*60, poll=1): stdout = "" while True: # see if anything can be read within the poll interval - (r, w, x)=select.select([child.stdout], [], [], poll) - if r: stdout = stdout + child.stdout.read(1) + (r, w, x) = select.select([child.stdout], [], [], poll) + if r: + stdout = stdout + child.stdout.read(1) # is process over ? returncode=child.poll() # yes @@ -55,12 +52,14 @@ def log_call_read(command, timeout=logger.default_timeout_minutes*60, poll=1): return (returncode, stdout) # child has failed else: - log("log_call:end command (%s) returned with code %d" %(message, returncode)) + log("log_call:end command (%s) returned with code %d" + %(message, returncode)) return (returncode, stdout) # no : still within timeout ? if time.time() >= trigger: child.terminate() - logger.log("log_call:end terminating command (%s) - exceeded timeout %d s"%(message, timeout)) + logger.log("log_call:end terminating command (%s) - exceeded timeout %d s" + %(message, timeout)) return (-2, None) break except Exception as e: @@ -109,8 +108,9 @@ def ovs_addport(name, portname, type, remoteip, key): if key: args = args + ["options:key=" + str(key)] - (returncode, stdout) = ovs_vsctl(args) - if (returncode != 0): raise OvsException("add-port") + returncode, stdout = ovs_vsctl(args) + if (returncode != 0): + raise OvsException("add-port") def ovs_delport(name, portname): (returncode, stdout) = ovs_vsctl(["del-port", name, portname]) @@ -182,7 +182,7 @@ def GetSlivers(data, conf = None, plc = None): sliver_name = sliver['name'] # build a dict of attributes, because it's more convenient - attributes={} + attributes = {} for attribute in sliver['attributes']: attributes[attribute['tagname']] = attribute['value'] diff --git a/sliver_lxc.py b/sliver_lxc.py index 19216d7..e54c453 100644 --- a/sliver_lxc.py +++ b/sliver_lxc.py @@ -80,8 +80,8 @@ class Sliver_LXC(Sliver_Libvirt, Initscript): .format(self.name) command = plain.split() logger.log_call(command, timeout=3) - - + + @staticmethod def create(name, rec=None): ''' diff --git a/tools.py b/tools.py index 96df451..d8f216e 100644 --- a/tools.py +++ b/tools.py @@ -168,7 +168,8 @@ def write_temp_file(do_write, mode=None, uidgid=None): return temporary_filename -def replace_file_with_string(target, new_contents, chmod=None, remove_if_empty=False): +def replace_file_with_string(target, new_contents, + chmod=None, remove_if_empty=False): """ Replace a target file with a new contents checks for changes: does not do anything if previous state was already right @@ -179,8 +180,8 @@ writes in a tmp file, which is then renamed (from sliverauth originally) returns True if a change occurred, or the file is deleted """ try: - with open(target) as f: - current = f.read() + with open(target) as feed: + current = feed.read() except: current = "" if current == new_contents: -- 2.43.0