Commit f58bbc57 authored by Nikos Skalkotos's avatar Nikos Skalkotos
Browse files

Correct some pep8 errors in the python helpers

parent ed49b246
#!/usr/bin/env python
# Copyright (C) 2011 GRNET S.A.
# Copyright (C) 2011 GRNET S.A.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -32,24 +32,25 @@ import json
from StringIO import StringIO
from optparse import OptionParser
def parse_arguments(input_args):
usage = "Usage: %prog [options] <output_file>"
parser = OptionParser(usage=usage)
parser.add_option("-i", "--input",
action="store",type='string', dest="input_file",
help="get input from FILE instead of stdin",
metavar="FILE")
action="store", type='string', dest="input_file",
help="get input from FILE instead of stdin",
metavar="FILE")
opts, args = parser.parse_args(input_args)
if len(args) != 1:
parser.error('output file is missing')
output_file = args[0]
if opts.input_file is not None:
if not os.path.isfile(opts.input_file):
parser.error('input file does not exist')
return (opts.input_file, output_file)
......@@ -64,7 +65,7 @@ def main():
os.environ['SNF_IMAGE_PROPERTY_' + str(key).upper()] = value
p = subprocess.Popen(['bash', '-c', 'set'], stdout=subprocess.PIPE)
output = StringIO(p.communicate()[0]);
output = StringIO(p.communicate()[0])
for line in iter(output):
if line.startswith('SNF_IMAGE_PROPERTY_'):
outfh.write('export ' + line)
......
......@@ -42,7 +42,9 @@ class MBR(object):
) = struct.unpack(self.format, raw_part)
def pack(self):
return struct.pack(self.format,
return struct.pack
(
self.format,
self.status,
self.start,
self.type,
......@@ -96,11 +98,11 @@ class MBR(object):
def __init__(self, block):
raw_part = {}
self.code_area, \
raw_part[0], \
raw_part[1], \
raw_part[2], \
raw_part[3], \
self.signature = struct.unpack(self.format, block)
raw_part[0], \
raw_part[1], \
raw_part[2], \
raw_part[3], \
self.signature = struct.unpack(self.format, block)
self.part = {}
for i in range(4):
......@@ -113,7 +115,8 @@ class MBR(object):
def pack(self):
"""Packs an MBR to a binary string."""
return struct.pack(self.format,
return struct.pack(
self.format,
self.code_area,
self.part[0].pack(),
self.part[1].pack(),
......@@ -150,23 +153,6 @@ class GPTPartitionTable(object):
def __init__(self, block):
self.signature, \
self.revision, \
self.hdr_size, \
self.header_crc32, \
self.current_lba, \
self.backup_lba, \
self.first_usable_lba, \
self.last_usable_lba, \
self.uuid, \
self.part_entry_start, \
self.part_count, \
self.part_entry_size, \
self.part_crc32 = struct.unpack(self.format, block)
def pack(self):
"""Packs a GPT Header to a binary string."""
return struct.pack(self.format,
self.signature, \
self.revision, \
self.hdr_size, \
self.header_crc32, \
......@@ -178,6 +164,24 @@ class GPTPartitionTable(object):
self.part_entry_start, \
self.part_count, \
self.part_entry_size, \
self.part_crc32 = struct.unpack(self.format, block)
def pack(self):
"""Packs a GPT Header to a binary string."""
return struct.pack(
self.format,
self.signature,
self.revision,
self.hdr_size,
self.header_crc32,
self.current_lba,
self.backup_lba,
self.first_usable_lba,
self.last_usable_lba,
self.uuid,
self.part_entry_start,
self.part_count,
self.part_entry_size,
self.part_crc32
)
......@@ -200,7 +204,7 @@ class GPTPartitionTable(object):
# Partition entries (LBA 2...34)
d.seek(self.primary.part_entry_start * BLOCKSIZE)
entries_size = self.primary.part_count * \
self.primary.part_entry_size
self.primary.part_entry_size
self.part_entries = d.read(entries_size)
# Secondary GPT Header (LBA -1)
......@@ -228,7 +232,7 @@ class GPTPartitionTable(object):
self.primary.backup_lba = lba_count - 1 # LBA-1
self.primary.last_usable_lba = lba_count - 34 # LBA-34
self.primary.header_crc32 = \
binascii.crc32(self.primary.pack()) & 0xffffffff
binascii.crc32(self.primary.pack()) & 0xffffffff
# Fix Secondary header
self.secondary.header_crc32 = 0
......@@ -236,7 +240,7 @@ class GPTPartitionTable(object):
self.secondary.last_usable_lba = lba_count - 34 # LBA-34
self.secondary.part_entry_start = lba_count - 33 # LBA-33
self.secondary.header_crc32 = \
binascii.crc32(self.secondary.pack()) & 0xffffffff
binascii.crc32(self.secondary.pack()) & 0xffffffff
# Copy the new partition table back to the device
with open(self.disk, "wb") as d:
......
......@@ -43,12 +43,12 @@ def parse_arguments(input_args):
usage = "Usage: %prog [options] <target>"
parser = OptionParser(usage=usage)
parser.add_option("-i", "--input",
action="store", type='string', dest="input_file",
help="get input from FILE instead of stdin",
metavar="FILE")
parser.add_option("-d", "--decode",
action="store_true", dest="decode", default=False,
help="decode files under target and create manifest")
action="store", type='string', dest="input_file",
help="get input from FILE instead of stdin",
metavar="FILE")
parser.add_option("-d", "--decode", action="store_true", dest="decode",
default=False,
help="decode files under target and create manifest")
opts, args = parser.parse_args(input_args)
......
#!/usr/bin/env python
# Copyright (C) 2011 GRNET S.A.
# Copyright (C) 2011 GRNET S.A.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -26,7 +26,7 @@ To do this, it generates a random salt internally.
"""
import sys
import crypt
import crypt
from string import ascii_letters, digits
from random import choice
......@@ -45,6 +45,7 @@ HASH_ID_FROM_METHOD = {
'sha512': '6'
}
def random_salt(length=8):
pool = ascii_letters + digits + "/" + "."
return ''.join(choice(pool) for i in range(length))
......@@ -53,27 +54,25 @@ def random_salt(length=8):
def parse_arguments(input_args):
usage = "usage: %prog [-h] [-m encrypt-method] <password>"
parser = OptionParser(usage=usage)
parser.add_option("-m", "--encrypt-method",
dest="encrypt_method",
type='choice',
default="sha512",
choices=HASH_ID_FROM_METHOD.keys(),
help="encrypt password with ENCRYPT_METHOD [%default] \
(supported: " + ", ".join(HASH_ID_FROM_METHOD.keys()) +")",
parser.add_option(
"-m", "--encrypt-method", dest="encrypt_method", type='choice',
default="sha512", choices=HASH_ID_FROM_METHOD.keys(),
help="encrypt password with ENCRYPT_METHOD [%default] (supported: " +
", ".join(HASH_ID_FROM_METHOD.keys()) + ")"
)
(opts, args) = parser.parse_args(input_args)
if len(args) != 1:
parser.error('password is missing')
parser.error('password is missing')
return (args[0], opts.encrypt_method)
def main():
(password, method) = parse_arguments(sys.argv[1:])
(passwd, method) = parse_arguments(sys.argv[1:])
salt = random_salt()
hash = crypt.crypt(password, "$"+HASH_ID_FROM_METHOD[method]+"$"+salt)
hash = crypt.crypt(passwd, "$" + HASH_ID_FROM_METHOD[method] + "$" + salt)
sys.stdout.write("%s\n" % (hash))
return 0
......
......@@ -32,7 +32,8 @@ import prctl
import signal
import socket
MSG_TYPE="image-copy-progress"
MSG_TYPE = "image-copy-progress"
def parse_arguments(args):
from optparse import OptionParser
......@@ -50,12 +51,12 @@ def parse_arguments(args):
parser.add_option("-r", "--read-bytes",
action="store", type="int", dest="read_bytes",
metavar="BYTES_TO_READ",
help="The expected number of bytes to be read, " \
help="The expected number of bytes to be read, "
"used to compute input progress",
default=None)
parser.add_option("-o", "--output_fd", dest="output", default=None,
metavar="FILE", type="int",
help="Write output notifications to this file descriptor")
parser.add_option(
"-o", "--output_fd", dest="output", default=None, metavar="FILE",
type="int", help="Write output notifications to this file descriptor")
(opts, args) = parser.parse_args(args)
......@@ -88,7 +89,7 @@ def report_wait_status(pid, status):
sys.stderr.write("Child PID = %d stopped by signal, signal = %d\n" %
(pid, os.WSTOPSIG(status)))
else:
sys.stderr.write("Internal error: Unhandled case, " \
sys.stderr.write("Internal error: Unhandled case, "
"PID = %d, status = %d\n" % (pid, status))
sys.exit(1)
sys.stderr.flush()
......@@ -132,8 +133,7 @@ def main():
if wpid == pid:
report_wait_status(pid, status)
if (os.WIFEXITED(status) or os.WIFSIGNALED(status)):
if not (os.WIFEXITED(status) and
os.WEXITSTATUS(status) == 0):
if not (os.WIFEXITED(status) and os.WEXITSTATUS(status) == 0):
return 1
else:
message['position'] = message['total']
......
......@@ -49,7 +49,8 @@ def parse_options(input_args):
default=None, metavar="IFNAME",
help="listen on interface IFNAME for monitoring data")
parser.add_option("-f", "--filter", type="string", dest="filter",
parser.add_option(
"-f", "--filter", type="string", dest="filter",
help="add FILTER to incomint traffice when working on an interface",
default=None, metavar="FILTER")
......@@ -61,7 +62,7 @@ def parse_options(input_args):
options.fd = args[0]
if options.filter is not None and options.ifname is None:
parser.error('You need to define an interface since filters are' \
parser.error('You need to define an interface since filters are'
'defined')
return options
......@@ -127,7 +128,8 @@ class HelperMonitor(object):
if self.lines_left > STDERR_MAXLINES:
error("Too many lines in the STDERR output")
elif self.lines_left < 0:
error("Second field of STDERR: %d is invalid" % self.lines_left)
error("Second field of STDERR: %d is invalid" %
self.lines_left)
if self.lines_left > 0:
self.stderr = m.group(2) + "\n"
......@@ -137,13 +139,13 @@ class HelperMonitor(object):
self.send("STDERR", self.stderr)
self.stderr = ""
elif self.line.startswith("TASK_START:") \
or self.line.startswith("TASK_END:") \
or self.line.startswith("WARNING:") \
or self.line.startswith("ERROR:"):
or self.line.startswith("TASK_END:") \
or self.line.startswith("WARNING:") \
or self.line.startswith("ERROR:"):
(msg_type, _, value) = self.line.partition(':')
if self.line.startswith("WARNING:") or \
self.line.startswith("ERROR:"):
self.line.startswith("ERROR:"):
value = [value]
self.send(msg_type, value)
else:
......@@ -181,7 +183,7 @@ if __name__ == "__main__":
if options.ifname is not None:
try:
sniff(filter=options.filter, iface=options.ifname,
prn=lambda x: monitor.process(x.payload.getfieldval("load")))
prn=lambda x: monitor.process(x.payload.getfieldval("load")))
except socket.error as e:
# Network is down
if e.errno == 100:
......
......@@ -48,7 +48,7 @@ if __name__ == "__main__":
msg_type = sys.argv[1]
if msg_type not in PROTOCOL.keys():
if msg_type not in PROTOCOL.keys():
sys.stderr.write("Unknown message type: %s\n" % msg_type)
sys.exit(1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment