summaryrefslogtreecommitdiff
path: root/xnt/xenant.py
diff options
context:
space:
mode:
authorkennyballou <kballou@onyx.boisestate.edu>2013-03-09 18:33:32 -0700
committerkennyballou <kballou@onyx.boisestate.edu>2013-03-09 18:33:32 -0700
commit532f5d98c15f091a270e68450112e91ad085f6bf (patch)
tree140620a5a5e1e7284976521294c53bb354bbd2c7 /xnt/xenant.py
parentaeb0672db3f2a4ddb4812344398831180a723d0c (diff)
downloadxnt-532f5d98c15f091a270e68450112e91ad085f6bf.tar.gz
xnt-532f5d98c15f091a270e68450112e91ad085f6bf.tar.xz
Refactor/ Move Program flow to use `argparse`
Diffstat (limited to 'xnt/xenant.py')
-rw-r--r--xnt/xenant.py126
1 files changed, 99 insertions, 27 deletions
diff --git a/xnt/xenant.py b/xnt/xenant.py
index e67ef7c..9e5a3df 100644
--- a/xnt/xenant.py
+++ b/xnt/xenant.py
@@ -21,9 +21,9 @@ import os
import sys
import time
import logging
-from xnt.cmdoptions import OPTIONS
-from xnt.commands import COMMANDS
-from xnt.commands.target import TargetCommand
+import argparse
+from xnt import __version__
+from xnt.status_codes import SUCCESS, ERROR, UNKNOWN_ERROR
logging.basicConfig(format="%(asctime)s:%(levelname)s:%(message)s")
LOGGER = logging.Logger(name=__name__)
@@ -32,30 +32,15 @@ LOGGER.addHandler(logging.StreamHandler())
def main():
"""Xnt Entry Point"""
start_time = time.time()
- params = list(p for p in sys.argv[1:] if p.startswith('-D'))
- flags = list(o for o in sys.argv[1:]
- if o.startswith('-') and o not in params)
- cmds = list(c for c in sys.argv[1:]
- if c not in flags and c not in params)
- #Loop flags and apply them
- for flag in flags:
- if flag in OPTIONS:
- OPTIONS[flag]()
- else:
- LOGGER.debug("%s is not a vaild option", flag)
- #run things
- cmd_found = False
- for cmd in cmds:
- if cmd in COMMANDS:
- cmd_found = True
- if COMMANDS[cmd].needs_build:
- command = COMMANDS[cmd](load_build())
- else:
- command = COMMANDS[cmd]()
- error_code = command.run()
- if cmd_found == False:
- command = TargetCommand(load_build())
- error_code = command.run(targets=cmds, props=params)
+ args = parse_args(sys.argv[1:])
+ if args["verbose"]:
+ LOGGER.setLevel(logging.DEBUG)
+ if args["list-targets"]:
+ error_code = list_targets(load_build())
+ else:
+ error_code = invoke_build(load_build(),
+ args["targets"],
+ args["properties"])
elapsed_time = time.time() - start_time
LOGGER.info("Execution time: %.3f", elapsed_time)
if error_code != 0:
@@ -91,5 +76,92 @@ def load_build(path=""):
del sys.modules["build"]
os.chdir(cwd)
+def invoke_build(build, targets=None, props=None):
+ """Invoke Build with `targets` passing `props`"""
+ def call_target(target_name, props):
+ """Call target on build module"""
+ def process_params(params, existing_props=None):
+ """Parse and separate properties and append to build module"""
+ properties = existing_props if existing_props is not None else {}
+ for param in params:
+ name, value = param.split("=")
+ properties[name] = value
+ return properties
+ def __get_properties():
+ """Return the properties dictionary of the build module"""
+ try:
+ return getattr(build, "PROPERTIES")
+ except AttributeError:
+ LOGGER.warning("Build file specifies no properties")
+ return None
+ try:
+ if props and len(props) > 0:
+ setattr(build,
+ "PROPERTIES",
+ process_params(props, __get_properties()))
+ target = getattr(build, target_name)
+ error_code = target()
+ return error_code if error_code else 0
+ except AttributeError:
+ LOGGER.error("There was no target: %s", target_name)
+ return ERROR
+ except Exception as ex:
+ LOGGER.critical(ex)
+ return UNKNOWN_ERROR
+ if targets and len(targets) > 0:
+ for target in targets:
+ error_code = call_target(target, props)
+ if error_code:
+ return error_code
+ return SUCCESS
+ else:
+ return call_target("default", props)
+
+def list_targets(build):
+ """List targets (and doctstrings) of the provided build module"""
+ try:
+ for attr in dir(build):
+ try:
+ func = getattr(build, attr)
+ if func.decorator == "target":
+ print(attr + ":")
+ if func.__doc__:
+ print(func.__doc__)
+ print("")
+ except AttributeError:
+ pass
+ except AttributeError as ex:
+ LOGGER.error(ex)
+ return ERROR
+ return SUCCESS
+
+def parse_args(args_in):
+ """Parse and group arguments"""
+ parser = argparse.ArgumentParser(prog="Xnt")
+ parser.add_argument("-v", "--verbose",
+ help="Enable verbose output",
+ action="store_true",
+ dest="verbose")
+ parser.add_argument(
+ "--version",
+ action="version",
+ version=__version__,
+ help="Print Xnt Version and quit")
+ parser.add_argument("-l", "--list-targets",
+ action="store_true",
+ dest="list-targets",
+ help="Print build targets")
+ # Properties Group
+ params_group = parser.add_argument_group("Properties")
+ params_group.add_argument(
+ "-D", dest="properties", action="append",
+ help="Property values to be passed to the build module")
+ target_group = parser.add_argument_group("Targets")
+
+ # Targets Group
+ target_group.add_argument("targets", nargs=argparse.REMAINDER,
+ help="Name(s) of targets to invoke")
+ return vars(parser.parse_args(args_in))
+
if __name__ == "__main__":
main()