summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkennyballou <kballou@onyx.boisestate.edu>2013-02-14 13:39:35 -0700
committerkennyballou <kballou@onyx.boisestate.edu>2013-02-14 13:39:35 -0700
commitfe375100833c625c3076a010ef572cf30eb18c7e (patch)
tree9d6af93daa53350c07edfd6d0c1ebf0090fc0fb1
parent86249dfc82e273fb947b653303d03eacafd9d16b (diff)
downloadxnt-fe375100833c625c3076a010ef572cf30eb18c7e.tar.gz
xnt-fe375100833c625c3076a010ef572cf30eb18c7e.tar.xz
Refactor build module loading
Add field to commands to flag the build module to be loaded. Refactor commands to use dependency injection for the build module (this should allow for some testing of the commands).
-rw-r--r--xnt/basecommand.py1
-rw-r--r--xnt/commands/help.py1
-rw-r--r--xnt/commands/listtargets.py12
-rw-r--r--xnt/commands/target.py12
-rw-r--r--xnt/commands/version.py1
-rw-r--r--xnt/runner.py28
6 files changed, 43 insertions, 12 deletions
diff --git a/xnt/basecommand.py b/xnt/basecommand.py
index 0cd772c..83bdf0a 100644
--- a/xnt/basecommand.py
+++ b/xnt/basecommand.py
@@ -23,6 +23,7 @@ class Command(object):
name = None
usage = None
hidden = False
+ need_build = False
def __init__(self):
pass
diff --git a/xnt/commands/help.py b/xnt/commands/help.py
index 88fd01a..746ad78 100644
--- a/xnt/commands/help.py
+++ b/xnt/commands/help.py
@@ -23,6 +23,7 @@ class HelpCommand(Command):
name = 'help'
usage = """"""
summary = 'Print Usage Summary'
+ needs_build = False
def run(self, arguments=[]):
from xnt.commands import get_summaries
diff --git a/xnt/commands/listtargets.py b/xnt/commands/listtargets.py
index f86ec2a..4ecc645 100644
--- a/xnt/commands/listtargets.py
+++ b/xnt/commands/listtargets.py
@@ -18,7 +18,6 @@
from xnt.basecommand import Command
from xnt.status_codes import SUCCESS, ERROR
-from xnt.xenant import loadBuild
import logging
logger = logging.getLogger(__name__)
@@ -27,15 +26,18 @@ class ListTargetsCommand(Command):
name = 'list-targets'
usage = """"""
summary = "Prints targets in build file"
+ needs_build = True
+
+ def __init__(self, build):
+ self.build = build
def run(self, arguments=[]):
- build = loadBuild()
- logger.debug("build is null? %s", build == None)
+ logger.debug("build is null? %s", self.build == None)
try:
- for f in dir(build):
+ for f in dir(self.build):
logger.debug("Attribute %s:", f)
try:
- fa = getattr(build, f)
+ fa = getattr(self.build, f)
if fa.decorator == "target":
print(f + ":")
if fa.__doc__:
diff --git a/xnt/commands/target.py b/xnt/commands/target.py
index b1f3d2b..ad43dd0 100644
--- a/xnt/commands/target.py
+++ b/xnt/commands/target.py
@@ -18,7 +18,6 @@
from xnt.basecommand import Command
from xnt.status_codes import SUCCESS, ERROR, UNKNOWN_ERROR
-from xnt.xenant import loadBuild
import logging
logger = logging.getLogger("xnt")
@@ -27,6 +26,10 @@ class TargetCommand(Command):
name = '<target>'
usage = """"""
summary = "Invokes target(s) in build.py"
+ needs_build = True
+
+ def __init__(self, build):
+ self.build = build
def run(self, targets=[], props=[]):
if targets:
@@ -39,7 +42,6 @@ class TargetCommand(Command):
return self.callTarget("default", props)
def callTarget(self, targetName, props):
- build = loadBuild()
def processParams(params, buildProperties={}):
properties = buildProperties if buildProperties is not None else {}
for p in params:
@@ -48,15 +50,15 @@ class TargetCommand(Command):
return properties
def __getProperties():
try:
- return getattr(build, "properties")
+ return getattr(self.build, "properties")
except AttributeError:
return None
try:
if len(props) > 0:
- setattr(build,
+ setattr(self.build,
"properties",
processParams(props, __getProperties()))
- target = getattr(build, targetName)
+ target = getattr(self.build, targetName)
ec = target()
return ec if ec else 0
except AttributeError:
diff --git a/xnt/commands/version.py b/xnt/commands/version.py
index 34853c4..350a739 100644
--- a/xnt/commands/version.py
+++ b/xnt/commands/version.py
@@ -23,6 +23,7 @@ class VersionCommand(Command):
name = 'version'
usage = """"""
summary = "Print Version of Xnt"
+ needs_build = False
def run(arguments=[]):
from xnt import __version__
diff --git a/xnt/runner.py b/xnt/runner.py
index 3d78f7a..b0cf15e 100644
--- a/xnt/runner.py
+++ b/xnt/runner.py
@@ -46,10 +46,13 @@ def main():
for cmd in cmds:
if cmd in commands:
cmd_found = True
- command = commands[cmd]()
+ if commands[cmd].needs_build:
+ command = commands[cmd](loadBuild())
+ else:
+ command = commands[cmd]()
ec = command.run()
if cmd_found == False:
- command = TargetCommand()
+ command = TargetCommand(loadBuild())
ec = command.run(targets=cmds, props=params)
elapsed_time = time.time() - start_time
logger.info("Execution time: %.3f", elapsed_time)
@@ -61,6 +64,27 @@ def main():
if ec != 0:
sys.exit(ec)
+def loadBuild(path=""):
+ if not path:
+ path = os.getcwd()
+ else:
+ path = os.path.abspath(path)
+ sys.path.append(path)
+ cwd = os.getcwd()
+ os.chdir(path)
+ if not os.path.exists("build.py"):
+ logger.error("There was no build file")
+ sys.exit(1)
+ try:
+ return __import__("build", fromlist=[])
+ except ImportError:
+ logger.error("HOW?!")
+ return None
+ finally:
+ sys.path.remove(path)
+ del sys.modules["build"]
+ os.chdir(cwd)
+
if __name__ == "__main__":
ec = main()
if ec: