Skip to content

Commit

Permalink
Make all of the tools have a main function.
Browse files Browse the repository at this point in the history
This just cleans up the global namespace better.

Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette committed Jun 20, 2018
1 parent a87a75e commit 47274f2
Show file tree
Hide file tree
Showing 3 changed files with 274 additions and 262 deletions.
126 changes: 65 additions & 61 deletions oz-customize
Original file line number Diff line number Diff line change
Expand Up @@ -45,75 +45,79 @@ def usage():
oz.GuestFactory.distrolist()
sys.exit(1)

try:
opts, args = getopt.gnu_getopt(sys.argv[1:], 'c:d:hm:', ['config', 'debug',
'mac-address',
'help'])
except getopt.GetoptError as err:
print(str(err))
usage()

loglevel = logging.ERROR
logformat = "%(message)s"
config_file = None
macaddress = None
for o, a in opts:
if o in ("-c", "--config"):
config_file = a
elif o in ("-d", "--debug"):
try:
d_int = int(a)
except ValueError:
def main():
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], 'c:d:hm:', ['config', 'debug',
'mac-address',
'help'])
except getopt.GetoptError as err:
print(str(err))
usage()

loglevel = logging.ERROR
logformat = "%(message)s"
config_file = None
macaddress = None
for o, a in opts:
if o in ("-c", "--config"):
config_file = a
elif o in ("-d", "--debug"):
try:
d_int = int(a)
except ValueError:
usage()
if d_int == 0:
loglevel = logging.ERROR
elif d_int == 1:
loglevel = logging.WARNING
elif d_int == 2:
loglevel = logging.INFO
elif d_int == 3:
loglevel = logging.DEBUG
elif d_int >= 4:
loglevel = logging.DEBUG
logformat = logging.BASIC_FORMAT
elif o in ("-h", "--help"):
usage()
if d_int == 0:
loglevel = logging.ERROR
elif d_int == 1:
loglevel = logging.WARNING
elif d_int == 2:
loglevel = logging.INFO
elif d_int == 3:
loglevel = logging.DEBUG
elif d_int >= 4:
loglevel = logging.DEBUG
logformat = logging.BASIC_FORMAT
elif o in ("-h", "--help"):
elif o in ("-m", "--mac-address"):
macaddress = a
else:
assert False, "unhandled option"

if len(args) != 2:
usage()
elif o in ("-m", "--mac-address"):
macaddress = a
else:
assert False, "unhandled option"

if len(args) != 2:
usage()
try:
config = oz.ozutil.parse_config(config_file)

try:
config = oz.ozutil.parse_config(config_file)
tdlfile = args[0]
libvirt_xml_file = args[1]

tdlfile = args[0]
libvirt_xml_file = args[1]
logging.basicConfig(level=loglevel, format=logformat)

logging.basicConfig(level=loglevel, format=logformat)
tdl = oz.TDL.TDL(open(tdlfile, 'r').read())

tdl = oz.TDL.TDL(open(tdlfile, 'r').read())
guest = oz.GuestFactory.guest_factory(tdl, config, None, macaddress=macaddress)

guest = oz.GuestFactory.guest_factory(tdl, config, None, macaddress=macaddress)
fp = open(libvirt_xml_file, 'r')

fp = open(libvirt_xml_file, 'r')
# Arbitrarily limit the size of the XML file that we will support to 5MB.
# this should be plenty for a normal libvirt XML, and this should prevent
# us from causing OOMs on bogus files
if os.fstat(fp.fileno())[stat.ST_SIZE] > (5 * 1024 * 1024):
raise Exception("libvirt XML file is too big!")

# Arbitrarily limit the size of the XML file that we will support to 5MB.
# this should be plenty for a normal libvirt XML, and this should prevent
# us from causing OOMs on bogus files
if os.fstat(fp.fileno())[stat.ST_SIZE] > (5 * 1024 * 1024):
raise Exception("libvirt XML file is too big!")
guest.customize(fp.read())
fp.close()
except Exception as exc:
if loglevel > logging.DEBUG:
print("")
print("ERROR: %s" % (str(exc)))
print("")
print("(use -d3 to get the full backtrace)")
print("")
else:
raise

guest.customize(fp.read())
fp.close()
except Exception as exc:
if loglevel > logging.DEBUG:
print("")
print("ERROR: %s" % (str(exc)))
print("")
print("(use -d3 to get the full backtrace)")
print("")
else:
raise
if __name__ == "__main__":
main()
152 changes: 78 additions & 74 deletions oz-generate-icicle
Original file line number Diff line number Diff line change
Expand Up @@ -45,79 +45,83 @@ def usage():
oz.GuestFactory.distrolist()
sys.exit(1)

try:
opts, args = getopt.gnu_getopt(sys.argv[1:], 'c:d:hi:', ['config', 'debug',
'help', 'icicle'])
except getopt.GetoptError as err:
print(str(err))
usage()

loglevel = logging.ERROR
logformat = "%(message)s"
config_file = None
icicle_file = None
for o, a in opts:
if o in ("-c", "--config"):
config_file = a
elif o in ("-d", "--debug"):
try:
d_int = int(a)
except ValueError:
def main():
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], 'c:d:hi:', ['config', 'debug',
'help', 'icicle'])
except getopt.GetoptError as err:
print(str(err))
usage()

loglevel = logging.ERROR
logformat = "%(message)s"
config_file = None
icicle_file = None
for o, a in opts:
if o in ("-c", "--config"):
config_file = a
elif o in ("-d", "--debug"):
try:
d_int = int(a)
except ValueError:
usage()
if d_int == 0:
loglevel = logging.ERROR
elif d_int == 1:
loglevel = logging.WARNING
elif d_int == 2:
loglevel = logging.INFO
elif d_int == 3:
loglevel = logging.DEBUG
elif d_int >= 4:
loglevel = logging.DEBUG
logformat = logging.BASIC_FORMAT
elif o in ("-h", "--help"):
usage()
if d_int == 0:
loglevel = logging.ERROR
elif d_int == 1:
loglevel = logging.WARNING
elif d_int == 2:
loglevel = logging.INFO
elif d_int == 3:
loglevel = logging.DEBUG
elif d_int >= 4:
loglevel = logging.DEBUG
logformat = logging.BASIC_FORMAT
elif o in ("-h", "--help"):
elif o in ("-i", "--icicle"):
icicle_file = a
else:
assert False, "unhandled option"

if len(args) != 2:
usage()
elif o in ("-i", "--icicle"):
icicle_file = a
else:
assert False, "unhandled option"

if len(args) != 2:
usage()

try:
config = oz.ozutil.parse_config(config_file)

tdlfile = args[0]
libvirt_xml_file = args[1]

logging.basicConfig(level=loglevel, format=logformat)

tdl = oz.TDL.TDL(open(tdlfile, 'r').read())

guest = oz.GuestFactory.guest_factory(tdl, config, None)

fp = open(libvirt_xml_file, 'r')

# Arbitrarily limit the size of the XML file that we will support to 5MB.
# this should be plenty for a normal libvirt XML, and this should prevent
# us from causing OOMs on bogus files
if os.fstat(fp.fileno())[stat.ST_SIZE] > (5 * 1024 * 1024):
raise Exception("libvirt XML file is too big!")

icicle_xml = guest.generate_icicle(fp.read())
fp.close()
if icicle_file is None:
print(icicle_xml)
else:
open(icicle_file, 'w').write(icicle_xml)
print("ICICLE XML was written to " + icicle_file)
except Exception as exc:
if loglevel > logging.DEBUG:
print("")
print("ERROR: %s" % (str(exc)))
print("")
print("(use -d3 to get the full backtrace)")
print("")
else:
raise

try:
config = oz.ozutil.parse_config(config_file)

tdlfile = args[0]
libvirt_xml_file = args[1]

logging.basicConfig(level=loglevel, format=logformat)

tdl = oz.TDL.TDL(open(tdlfile, 'r').read())

guest = oz.GuestFactory.guest_factory(tdl, config, None)

fp = open(libvirt_xml_file, 'r')

# Arbitrarily limit the size of the XML file that we will support to 5MB.
# this should be plenty for a normal libvirt XML, and this should prevent
# us from causing OOMs on bogus files
if os.fstat(fp.fileno())[stat.ST_SIZE] > (5 * 1024 * 1024):
raise Exception("libvirt XML file is too big!")

icicle_xml = guest.generate_icicle(fp.read())
fp.close()
if icicle_file is None:
print(icicle_xml)
else:
open(icicle_file, 'w').write(icicle_xml)
print("ICICLE XML was written to " + icicle_file)
except Exception as exc:
if loglevel > logging.DEBUG:
print("")
print("ERROR: %s" % (str(exc)))
print("")
print("(use -d3 to get the full backtrace)")
print("")
else:
raise

if __name__ == "__main__":
main()
Loading

0 comments on commit 47274f2

Please sign in to comment.