From 5e928a3a38e20fa386d6ade2154676a5bc601b95 Mon Sep 17 00:00:00 2001 From: kennyballou Date: Thu, 7 Mar 2013 13:15:06 -0700 Subject: Rename zip task to create_zip Fix pep warning about clobbering built in function --- xnt/tasks.py | 2 +- xnt/tests/taskcompressiontests.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'xnt') diff --git a/xnt/tasks.py b/xnt/tasks.py index 22df254..0b2a042 100644 --- a/xnt/tasks.py +++ b/xnt/tasks.py @@ -80,7 +80,7 @@ def rm(*fileset): except: raise -def zip(dir,zipfilename): +def create_zip(dir,zipfilename): logger.info("Zipping %s as %s", dir, zipfilename) assert os.path.isdir(dir) and zipfilename with contextlib.closing(zipfile.ZipFile( diff --git a/xnt/tests/taskcompressiontests.py b/xnt/tests/taskcompressiontests.py index 90ba081..9fe7758 100644 --- a/xnt/tests/taskcompressiontests.py +++ b/xnt/tests/taskcompressiontests.py @@ -35,7 +35,7 @@ class TaskCompressionTests(unittest.TestCase): shutil.rmtree("temp") def test_zip(self): - xnt.tasks.zip("temp/testfolder1", "temp/myzip.zip") + xnt.tasks.create_zip("temp/testfolder1", "temp/myzip.zip") self.assertTrue(os.path.exists("temp/myzip.zip")) if __name__ == "__main__": -- cgit v1.2.1 From 0539fded91116c2d40ddf08ae888b02fd3a0a482 Mon Sep 17 00:00:00 2001 From: kennyballou Date: Thu, 7 Mar 2013 18:42:42 -0700 Subject: pylint ALL THE THINGS! --- xnt/__init__.py | 20 ++++-- xnt/basecommand.py | 10 +-- xnt/build/__init__.py | 1 + xnt/build/cc.py | 61 ++++++++-------- xnt/build/make.py | 40 ++++++----- xnt/cmdoptions.py | 11 ++- xnt/commands/__init__.py | 6 +- xnt/commands/help.py | 7 +- xnt/commands/listtargets.py | 31 +++++---- xnt/commands/target.py | 46 +++++++----- xnt/commands/version.py | 8 ++- xnt/status_codes.py | 4 ++ xnt/tasks.py | 131 ++++++++++++++++++++--------------- xnt/tests/__init__.py | 20 ++++++ xnt/tests/compilercollectiontests.py | 66 +++++++++++------- xnt/tests/taskcompressiontests.py | 24 +++---- xnt/tests/taskcopytests.py | 25 +++---- xnt/tests/taskmisctests.py | 42 +++++------ xnt/tests/taskmkdirtests.py | 24 +++---- xnt/tests/taskmovetests.py | 24 +++---- xnt/tests/taskremovetests.py | 30 ++++---- xnt/vcs/__init__.py | 12 ++++ xnt/vcs/cvs.py | 9 ++- xnt/vcs/git.py | 12 ++-- xnt/vcs/hg.py | 14 ++-- xnt/version.py | 5 +- xnt/xenant.py | 54 ++++++++------- 27 files changed, 425 insertions(+), 312 deletions(-) (limited to 'xnt') diff --git a/xnt/__init__.py b/xnt/__init__.py index 8e77489..baf4317 100644 --- a/xnt/__init__.py +++ b/xnt/__init__.py @@ -1,4 +1,9 @@ #!/usr/bin/env python +"""Main xnt module + +Contains definition for version (referenced from version module), license, +target decorator, and imports task methods from tasks module +""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -36,12 +41,17 @@ __license__ = """ along with this program. If not, see . """ -from xnt.tasks import * +from xnt.tasks import cp, mv, mkdir, rm, zip, log, call, setup -def target(fn): +def target(target_fn): + """Decorator function for marking a method in + build file as a "target" method, or a method meant + to be invoked from Xnt + """ def wrap(): - print(fn.__name__ + ":") - return fn() + """Inner wrapper function for decorator""" + print(target_fn.__name__ + ":") + return target_fn() wrap.decorator = "target" - wrap.__doc__ = fn.__doc__ + wrap.__doc__ = target_fn.__doc__ return wrap diff --git a/xnt/basecommand.py b/xnt/basecommand.py index 83bdf0a..bbbdac9 100644 --- a/xnt/basecommand.py +++ b/xnt/basecommand.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Basecommand class for xnt commands""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -16,10 +17,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import os -import sys - -class Command(object): +class Command(object): #pylint: disable-msg=R0903 + """Base Command Class Definition""" name = None usage = None hidden = False @@ -28,5 +27,6 @@ class Command(object): def __init__(self): pass - def run(arguments=[]): + def run(self, arguments=None): + """Invoke the Command""" pass diff --git a/xnt/build/__init__.py b/xnt/build/__init__.py index 64e5706..105d02f 100644 --- a/xnt/build/__init__.py +++ b/xnt/build/__init__.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Build Module""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou diff --git a/xnt/build/cc.py b/xnt/build/cc.py index e4592ca..d1a78ac 100644 --- a/xnt/build/cc.py +++ b/xnt/build/cc.py @@ -1,4 +1,8 @@ #!/usr/bin/env python +"""Common Compilers + +Definition of commonly used compilers +""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -16,58 +20,61 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -""" -Common Compilers -""" - import os import logging -import sys from xnt.tasks import call -logger = logging.getLogger(__name__) +LOGGER = logging.getLogger(__name__) -def gcc(src, flags=[]): +def gcc(src, flags=None): """gcc compiler, non-named output file""" return _gcc(src, flags) -def gpp(src, flags=[]): +def gpp(src, flags=None): """g++ compiler, non-named output file""" return _gcc(src, flags, "g++") -def gcc_o(src, o, flags=[]): +def gcc_o(src, output, flags=None): """gcc compiler, with output file""" - return _gcc_o(src, o, flags) + return _gcc_o(src, output, flags) -def gpp_o(src, o, flags=[]): +def gpp_o(src, output, flags=None): """g++ compiler, with output file""" - return _gcc_o(src, o, flags, "g++") + return _gcc_o(src, output, flags, "g++") -def javac(src, flags=[]): +def javac(src, flags=None): """Javac: compile Java programs""" - logger.info("Compiling %s", src) - cmd = __generateCommand(src, flags, "javac") + LOGGER.info("Compiling %s", src) + cmd = __generate_command(src, flags, "javac") return __compile(cmd) -def _gcc(src, flags=[], compiler="gcc"): - logger.info("Compiling %s", src) - return __compile(__generateCommand(src, flags, compiler)) +def _gcc(src, flags=None, compiler="gcc"): + """Compile using gcc""" + LOGGER.info("Compiling %s", src) + return __compile(__generate_command(src, flags, compiler)) -def _gcc_o(src, o, flags=[], compiler="gcc"): - logger.info("Compiling %s to %s", src, o) - cmd = __generateCommand(src, flags, compiler) +def _gcc_o(src, output, flags=None, compiler="gcc"): + """Compile with gcc specifying output file name""" + LOGGER.info("Compiling %s to %s", src, output) + cmd = __generate_command(src, flags, compiler) cmd.append("-o") - cmd.append(o) + cmd.append(output) return __compile(cmd) -def __generateCommand(src, flags=[], compiler="gcc"): +def __generate_command(src, flags=None, compiler="gcc"): + """Generate cmd list for call""" cmd = [compiler, src] - for f in flags: - cmd.append(f) + if flags: + for flag in flags: + cmd.append(flag) return cmd def __compile(cmd): + """Run Compile, using `xnt.tasks.call`""" return call(cmd) -def __is_newer(a, b): - return os.path.getmtime(a) > os.path.getmtime(b) +def __is_newer(file_a, file_b): + """Compare mmtimes of files + Return True if `file_a` is modified later than `file_b` + """ + return os.path.getmtime(file_a) > os.path.getmtime(file_b) diff --git a/xnt/build/make.py b/xnt/build/make.py index 80fcf1b..80bda63 100644 --- a/xnt/build/make.py +++ b/xnt/build/make.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Wrapping methods around build tools""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -18,32 +19,35 @@ import os import subprocess -import logging - -def ant(path="", target="",flags=[]): - cmd = __addFlags(["ant", target], flags) +def ant(path="", target="", flags=None): + """Wrapper around Apache Ant""" + cmd = __add_flags(["ant", target], flags) return __run_in(path, lambda: subprocess.call(cmd)) -def make(path="", target="",flags=[]): - cmd = __addFlags(["make", target], flags) +def make(path="", target="", flags=None): + """Wrapper around GNU Make""" + cmd = __add_flags(["make", target], flags) return __run_in(path, lambda: subprocess.call(cmd)) -def nant(path="", target="",flags=[]): - cmd = __addFlags(["nant", target], flags) +def nant(path="", target="", flags=None): + """Wrapper around .NET Ant""" + cmd = __add_flags(["nant", target], flags) return __run_in(path, lambda: subprocess.call(cmd)) -def __addFlags(cmd, flags): - c = list(cmd) - for f in flags: - c.append(f) - return c +def __add_flags(cmd, flags): + """Add flags to command and return new list""" + command = list(cmd) + for flag in flags: + command.append(flag) + return command -def __run_in(path, f): - oldPath = os.path.abspath(os.getcwd()) +def __run_in(path, function): + """Execute function while in a different running directory""" + cwd = os.path.abspath(os.getcwd()) if path and os.path.exists(path): os.chdir(os.path.abspath(path)) - result = f() - if oldPath: - os.chdir(oldPath) + result = function() + if cwd: + os.chdir(cwd) return result diff --git a/xnt/cmdoptions.py b/xnt/cmdoptions.py index 9f3c944..529f060 100644 --- a/xnt/cmdoptions.py +++ b/xnt/cmdoptions.py @@ -1,4 +1,8 @@ #!/usr/bin/env python +"""Xenant Options Defintion + +All available options for Xenant (and there actions) +""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -18,9 +22,10 @@ import logging -def flipVerboseFlag(): +def __flip_verbose_flag(): + """Turn on logging for xnt (and submodules)""" logging.getLogger("xnt").setLevel(logging.INFO) -options = { - "-v": flipVerboseFlag, +OPTIONS = { + "-v": __flip_verbose_flag, } diff --git a/xnt/commands/__init__.py b/xnt/commands/__init__.py index 8d51cec..583558b 100644 --- a/xnt/commands/__init__.py +++ b/xnt/commands/__init__.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Commands Module""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -21,7 +22,7 @@ from xnt.commands.listtargets import ListTargetsCommand from xnt.commands.version import VersionCommand from xnt.commands.target import TargetCommand -commands = { +COMMANDS = { HelpCommand.name: HelpCommand, ListTargetsCommand.name: ListTargetsCommand, VersionCommand.name: VersionCommand, @@ -29,9 +30,10 @@ commands = { } def get_summaries(ignore_hidden=True): + """Return a list of summaries about each command""" items = [] - for name, command_class in commands.items(): + for name, command_class in COMMANDS.items(): if ignore_hidden and command_class.hidden: continue items.append((name, command_class.summary)) diff --git a/xnt/commands/help.py b/xnt/commands/help.py index 35ec9bb..e72b13e 100644 --- a/xnt/commands/help.py +++ b/xnt/commands/help.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Xnt Help Command""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -19,13 +20,15 @@ from xnt.basecommand import Command from xnt.status_codes import SUCCESS -class HelpCommand(Command): +class HelpCommand(Command): #pylint: disable-msg=R0903 + """Help Command""" name = 'help' usage = """""" summary = 'Print Usage Summary' needs_build = False - def run(self, arguments=[]): + def run(self, arguments=None): + """Invoke Help""" from xnt.commands import get_summaries from xnt import __version__, __license__ commands = get_summaries() diff --git a/xnt/commands/listtargets.py b/xnt/commands/listtargets.py index 4ecc645..01f05de 100644 --- a/xnt/commands/listtargets.py +++ b/xnt/commands/listtargets.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""List Targets Xnt Command""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -20,32 +21,36 @@ from xnt.basecommand import Command from xnt.status_codes import SUCCESS, ERROR import logging -logger = logging.getLogger(__name__) +LOGGER = logging.getLogger(__name__) -class ListTargetsCommand(Command): +class ListTargetsCommand(Command): #pylint: disable-msg=R0903 + """List Targets Command""" name = 'list-targets' usage = """""" summary = "Prints targets in build file" needs_build = True def __init__(self, build): + """Initialization""" + Command.__init__(self) self.build = build - def run(self, arguments=[]): - logger.debug("build is null? %s", self.build == None) + def run(self, arguments=None): + """Invoke ListTargets""" + LOGGER.debug("build is null? %s", self.build == None) try: - for f in dir(self.build): - logger.debug("Attribute %s:", f) + for attr in dir(self.build): + LOGGER.debug("Attribute %s:", attr) try: - fa = getattr(self.build, f) - if fa.decorator == "target": - print(f + ":") - if fa.__doc__: - print(fa.__doc__) + func = getattr(self.build, attr) + if func.decorator == "target": + print(attr + ":") + if func.__doc__: + print(func.__doc__) print("\n") except AttributeError: pass - except Exception as ex: - logger.error(ex) + except AttributeError as ex: + LOGGER.error(ex) return ERROR return SUCCESS diff --git a/xnt/commands/target.py b/xnt/commands/target.py index ad43dd0..ca6f7fe 100644 --- a/xnt/commands/target.py +++ b/xnt/commands/target.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""(Generic) Target Xnt Command for invoking build targets""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -20,35 +21,42 @@ from xnt.basecommand import Command from xnt.status_codes import SUCCESS, ERROR, UNKNOWN_ERROR import logging -logger = logging.getLogger("xnt") +LOGGER = logging.getLogger("xnt") class TargetCommand(Command): + """Target Command""" name = '' usage = """""" summary = "Invokes target(s) in build.py" needs_build = True def __init__(self, build): + """Initialization""" + Command.__init__(self) self.build = build - def run(self, targets=[], props=[]): + def run(self, targets=None, props=None): #pylint: disable-msg=W0221 + """Invoke Target Command""" if targets: - for t in targets: - ec = self.callTarget(t, props) - if ec: - return ec + for target in targets: + error_code = self.call_target(target, props) + if error_code: + return error_code return SUCCESS else: - return self.callTarget("default", props) + return self.call_target("default", props) - def callTarget(self, targetName, props): - def processParams(params, buildProperties={}): - properties = buildProperties if buildProperties is not None else {} - for p in params: - name, value = p[2:].split("=") + def call_target(self, target_name, props): + """Invoke build target""" + def process_params(params, buildproperties=None): + """Parse the passed properties and append to build properties""" + properties = buildproperties if buildproperties is not None else {} + for param in params: + name, value = param[2:].split("=") properties[name] = value return properties - def __getProperties(): + def __get_properties(): + """Return the properties dictionary of the build module""" try: return getattr(self.build, "properties") except AttributeError: @@ -57,14 +65,14 @@ class TargetCommand(Command): if len(props) > 0: setattr(self.build, "properties", - processParams(props, __getProperties())) - target = getattr(self.build, targetName) - ec = target() - return ec if ec else 0 + process_params(props, __get_properties())) + target = getattr(self.build, target_name) + error_code = target() + return error_code if error_code else 0 except AttributeError: - logger.warning("There was no target: %s", targetName) + LOGGER.warning("There was no target: %s", target_name) return ERROR except Exception as ex: - logger.error(ex) + LOGGER.error(ex) return UNKNOWN_ERROR diff --git a/xnt/commands/version.py b/xnt/commands/version.py index 350a739..11a1879 100644 --- a/xnt/commands/version.py +++ b/xnt/commands/version.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Version Xnt Command""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -19,14 +20,15 @@ from xnt.basecommand import Command from xnt.status_codes import SUCCESS -class VersionCommand(Command): +class VersionCommand(Command): #pylint: disable-msg=R0903 + """Version Command""" name = 'version' usage = """""" summary = "Print Version of Xnt" needs_build = False - def run(arguments=[]): + def run(self, arguments=None): + """Invoke Version""" from xnt import __version__ print(__version__) - return SUCCESS diff --git a/xnt/status_codes.py b/xnt/status_codes.py index 3549a14..2aef2a8 100644 --- a/xnt/status_codes.py +++ b/xnt/status_codes.py @@ -1,4 +1,8 @@ #!/usr/bin/env python +"""Status Code Definitions + +Thess are the codes returned by Xenant for success and failure conditions +""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou diff --git a/xnt/tasks.py b/xnt/tasks.py index 22df254..f646ce4 100644 --- a/xnt/tasks.py +++ b/xnt/tasks.py @@ -1,4 +1,9 @@ #!/usr/bin/env python +"""Common Tasks Module + +Defines a set of operations that are common enough but also are tedious to +define +""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -19,14 +24,13 @@ import os import sys import subprocess -import time import shutil import zipfile import contextlib import glob import logging -logger = logging.getLogger(__name__) +LOGGER = logging.getLogger(__name__) #File associated tasks def expandpath(path): @@ -35,103 +39,116 @@ def expandpath(path): """ return glob.iglob(path) -def cp(src="",dst="",files=[]): +def cp(src="", dst="", files=None): #pylint: disable-msg=C0103 + """Copy `src` to `dst` or copy `files` to `dst` + + Copy a file or folder to a different file/folder + If no `src` file is specified, will attempt to copy `files` to `dst` + """ assert dst and src or len(files) > 0 - logger.info("Copying %s to %s", src, dst) - def copy(s,d): - if os.path.isdir(s): - shutil.copytree(s,d) + LOGGER.info("Copying %s to %s", src, dst) + def copy(source, destination): + """Copy file or folder to destination. + + Depending on the type of source, call the appropriate method + """ + if os.path.isdir(source): + shutil.copytree(source, destination) else: - shutil.copy2(s,d) + shutil.copy2(source, destination) if src: copy(src, dst) else: - for f in files: - copy(f, dst) + for file_to_copy in files: + copy(file_to_copy, dst) -def mv(src,dst): - logger.info("Moving %s to %s", src, dst) - shutil.move(src,dst) +def mv(src, dst): #pylint: disable-msg=C0103 + """Move file or folder to destination""" + LOGGER.info("Moving %s to %s", src, dst) + shutil.move(src, dst) -def mkdir(dir,mode=0o777): - if os.path.exists(dir): +def mkdir(directory, mode=0o777): + """Make a directory with mode""" + if os.path.exists(directory): return - logger.info("Making directory %s with mode %o", dir, mode) + LOGGER.info("Making directory %s with mode %o", directory, mode) try: - os.mkdir(dir,mode) - except IOError as e: - log(e, logging.WARNING) + os.mkdir(directory, mode) + except IOError as io_error: + log(io_error, logging.WARNING) except: raise -def rm(*fileset): +def rm(*fileset): #pylint: disable-msg=C0103 + """Remove a set of files""" try: - for g in fileset: - for f in expandpath(g): - if not os.path.exists(f): + for glob_set in fileset: + for file_to_delete in expandpath(glob_set): + if not os.path.exists(file_to_delete): continue - logger.info("Removing %s", f) - if os.path.isdir(f): - shutil.rmtree(f) + LOGGER.info("Removing %s", file_to_delete) + if os.path.isdir(file_to_delete): + shutil.rmtree(file_to_delete) else: - os.remove(f) - except OSError as e: - log(e, logging.WARNING) + os.remove(file_to_delete) + except OSError as os_error: + log(os_error, logging.WARNING) except: raise -def zip(dir,zipfilename): - logger.info("Zipping %s as %s", dir, zipfilename) - assert os.path.isdir(dir) and zipfilename +def zip(directory, zipfilename): + """Compress (Zip) folder""" + LOGGER.info("Zipping %s as %s", directory, zipfilename) + assert os.path.isdir(directory) and zipfilename with contextlib.closing(zipfile.ZipFile( zipfilename, "w", - zipfile.ZIP_DEFLATED)) as z: - for root, dirs, files in os.walk(dir): - for fn in files: - absfn = os.path.join(root, fn) - zfn = absfn[len(dir)+len(os.sep):] - z.write(absfn, zfn) + zipfile.ZIP_DEFLATED)) as zip_file: + for paths in os.walk(directory): + for file_name in paths[2]: + absfn = os.path.join(paths[0], file_name) + zip_file_name = absfn[len(directory) + len(os.sep):] + zip_file.write(absfn, zip_file_name) #Misc Tasks def echo(msg, tofile): - with open(tofile, "w") as f: - f.write(msg) + """Write a string to file""" + with open(tofile, "w") as file_to_write: + file_to_write.write(msg) -def log(msg="",lvl=logging.INFO): - logger.log(lvl, msg) +def log(msg="", lvl=logging.INFO): + """Log message using tasks global logger""" + LOGGER.log(lvl, msg) -def xnt(target, path): - """ - Invoke xnt on another build file in a different directory - """ +def call_xnt(target, path): + """Invoke xnt on another build file in a different directory""" import xnt.xenant xnt.xenant.invokeBuild( xnt.xenant.__loadBuild(path), target) def call(command, stdout=None, stderr=None): - """ - Execute the given command, redirecting stdout and stderr + """ Execute the given command, redirecting stdout and stderr to optionally given files + param: command - list of command and arguments param: stdout - file to redirect standard output to, if given param: stderr - file to redirect standard error to, if given """ return subprocess.call(args=command, stdout=stdout, stderr=stderr) -def setup(commands, dir=""): - """ - Invoke the ``setup.py`` file in the current or specified directory +def setup(commands, directory=""): + """Invoke the ``setup.py`` file in the current or specified directory + param: commands - list of commands and options to run/ append param: dir - (optional) directory to run from """ cmd = [sys.executable, "setup.py",] - for c in commands: - cmd.append(c) + for command in commands: + cmd.append(command) cwd = os.getcwd() - if dir: - os.chdir(dir) - ec = call(cmd) + if directory: + os.chdir(directory) + error_code = call(cmd) os.chdir(cwd) - return ec + return error_code diff --git a/xnt/tests/__init__.py b/xnt/tests/__init__.py index 44aacd8..b2f6702 100644 --- a/xnt/tests/__init__.py +++ b/xnt/tests/__init__.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Tests Module""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -16,3 +17,22 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import os +import shutil + +def set_up(): + """Shared Setup Code""" + os.mkdir("temp") + os.mkdir("temp/testfolder1") + for i in range(1, 5): + with open("temp/testfile" + str(i), "w") as test_file: + test_file.write("this is a test file") + with open("temp/test.py", "w") as test: + test.write("#!/usr/bin/env python\n") + test.write("import sys\n") + test.write("sys.stdout.write(sys.argv[1])\n") + test.write("sys.stderr.write(sys.argv[2])\n") + +def tear_down(): + """Shared Teardown Code""" + shutil.rmtree("temp") diff --git a/xnt/tests/compilercollectiontests.py b/xnt/tests/compilercollectiontests.py index ba470b7..50b8343 100644 --- a/xnt/tests/compilercollectiontests.py +++ b/xnt/tests/compilercollectiontests.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Test Common Compiler Collection""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -16,7 +17,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import sys import os import shutil import xnt.build.cc as cc @@ -24,11 +24,13 @@ import unittest #http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python def which(program): + """Similar to Linux/Unix `which`: return path of executable""" def is_exe(fpath): + """Determine if arguement exists and is executable""" return os.path.isfile(fpath) and os.access(fpath, os.X_OK) - fpath, fname = os.path.split(program) - if fpath: + fpath = os.path.split(program) + if fpath[0]: if is_exe(program): return program else: @@ -40,12 +42,16 @@ def which(program): return None +#pylint: disable-msg=R0904 +#pylint: disable-msg=C0103 @unittest.skipUnless(which("gcc"), "gcc is not in your path") -class gccTests(unittest.TestCase): - def setUp(self): +class GccTests(unittest.TestCase): + """Test GCC""" + def setUp(self): #pylint: disable-msg=R0201 + """Test Case Setup""" os.mkdir("temp") - with open("temp/hello.c", "w") as f: - f.write(""" + with open("temp/hello.c", "w") as test_code: + test_code.write(""" #include int main() { printf("Hello, World!\\n"); @@ -53,26 +59,31 @@ class gccTests(unittest.TestCase): } """) - def tearDown(self): + def tearDown(self): #pylint: disable-msg=R0201 + """Test Case Teardown""" shutil.rmtree("temp") def test_gcc(self): - oldPath = os.getcwd() + """Test Default GCC""" + cwd = os.getcwd() os.chdir("temp") cc.gcc("hello.c") self.assertTrue(os.path.isfile("a.out")) - os.chdir(oldPath) + os.chdir(cwd) def test_gcc_o(self): + """Test GCC with output""" cc.gcc_o("temp/hello.c", "temp/hello") self.assertTrue(os.path.isfile("temp/hello")) @unittest.skipUnless(which("g++"), "g++ is not in your path") -class gppTests(unittest.TestCase): - def setUp(self): +class GppTests(unittest.TestCase): + """Test G++ (C++ GCC)""" + def setUp(self): #pylint: disable-msg=R0201 + """Test Case Setup""" os.mkdir("temp") - with open("temp/hello.cpp", "w") as f: - f.write(""" + with open("temp/hello.cpp", "w") as test_code: + test_code.write(""" #include int main() { std::cout << "Hello, World!" << std::endl; @@ -80,26 +91,31 @@ class gppTests(unittest.TestCase): } """) - def tearDown(self): + def tearDown(self): #pylint: disable-msg=R0201 + """Test Case Teardown""" shutil.rmtree("temp") def test_gpp(self): - oldPath = os.getcwd() + """Test Default G++""" + cwd = os.getcwd() os.chdir("temp") cc.gpp("hello.cpp") self.assertTrue("a.out") - os.chdir(oldPath) + os.chdir(cwd) def test_gpp_o(self): + """Test G++ with output""" cc.gpp_o("temp/hello.cpp", "temp/hello") self.assertTrue(os.path.isfile("temp/hello")) @unittest.skipUnless(which("javac"), "javac is not in your path") -class javacTests(unittest.TestCase): - def setUp(self): +class JavacTests(unittest.TestCase): + """Test Javac""" + def setUp(self): #pylint: disable-msg=R0201 + """Test Case Setup""" os.mkdir("temp") - with open("temp/hello.java", "w") as f: - f.write(""" + with open("temp/hello.java", "w") as test_code: + test_code.write(""" class hello { public static void main(String[] args) { System.out.println("Hello, World!"); @@ -107,15 +123,17 @@ class javacTests(unittest.TestCase): } """) - def tearDown(self): + def tearDown(self): #pylint: disable-msg=R0201 + """Test Case Teardown""" shutil.rmtree("temp") def test_javac(self): - oldPath = os.getcwd() + """Test Default Javac""" + cwd = os.getcwd() os.chdir("temp") cc.javac("hello.java") self.assertTrue(os.path.isfile("hello.class")) - os.chdir(oldPath) + os.chdir(cwd) if __name__ == "__main__": unittest.main() diff --git a/xnt/tests/taskcompressiontests.py b/xnt/tests/taskcompressiontests.py index 90ba081..625a2b5 100644 --- a/xnt/tests/taskcompressiontests.py +++ b/xnt/tests/taskcompressiontests.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Test `xnt.tasks.zip`""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -16,25 +17,24 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import sys import os -import shutil import xnt.tasks +import xnt.tests import unittest +#pylint: disable-msg=C0103 +class TaskCompressionTests(unittest.TestCase): #pylint: disable-msg=R0904 + """Test Cases for Compression""" + def setUp(self): #pylint: disable-msg=R0201 + """Test Case Setup""" + xnt.tests.set_up() -class TaskCompressionTests(unittest.TestCase): - def setUp(self): - os.mkdir("temp") - os.mkdir("temp/testfolder1") - for i in range(1, 5): - with open("temp/testfile" + str(i), "w") as f: - f.write("this is a test file") - - def tearDown(self): - shutil.rmtree("temp") + def tearDown(self): #pylint: disable-msg=R0201 + """Test Case Teardown""" + xnt.tests.tear_down() def test_zip(self): + """Test zip method""" xnt.tasks.zip("temp/testfolder1", "temp/myzip.zip") self.assertTrue(os.path.exists("temp/myzip.zip")) diff --git a/xnt/tests/taskcopytests.py b/xnt/tests/taskcopytests.py index bb9af9c..5c8949e 100644 --- a/xnt/tests/taskcopytests.py +++ b/xnt/tests/taskcopytests.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Test xnt.tasks.cp""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -16,25 +17,24 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import sys import os -import shutil import xnt.tasks +import xnt.tests import unittest +#pylint: disable-msg=C0103 +class TaskCopyTests(unittest.TestCase): #pylint: disable-msg=R0904 + """Test Case for Copy Tasks Method""" + def setUp(self): #pylint: disable-msg=R0201 + """Test Setup""" + xnt.tests.set_up() -class TaskCopyTests(unittest.TestCase): - def setUp(self): - os.mkdir("temp") - os.mkdir("temp/testfolder1") - for i in range(1, 5): - with open("temp/testfile" + str(i), "w") as f: - f.write("this is a test file") - - def tearDown(self): - shutil.rmtree("temp") + def tearDown(self): #pylint: disable-msg=R0201 + """Test Teardown""" + xnt.tests.tear_down() def test_cp(self): + """Test default use of cp""" xnt.tasks.cp("temp/testfolder1", "temp/testfolder2") self.assertTrue(os.path.exists("temp/testfolder2")) self.assertTrue(os.path.exists("temp/testfolder1")) @@ -45,6 +45,7 @@ class TaskCopyTests(unittest.TestCase): self.assertEqual("this is a test file", testfile.read()) def test_cp_filelist(self): + """Test filelist copy""" xnt.tasks.cp(dst="temp/testfolder2", files=["temp/testfile1", "temp/testfile2", diff --git a/xnt/tests/taskmisctests.py b/xnt/tests/taskmisctests.py index d37ca20..0fa5e43 100644 --- a/xnt/tests/taskmisctests.py +++ b/xnt/tests/taskmisctests.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Misc Tasks Tests""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -16,36 +17,31 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import sys import os -import shutil import xnt.tasks +import xnt.tests import unittest +#pylint: disable-msg=C0103 +class TaskMiscTests(unittest.TestCase): #pylint: disable-msg=R0904 + """Test Misc Tasks""" + def setUp(self): #pylint: disable-msg=R0201 + """Test Case Setup""" + xnt.tests.set_up() -class TaskMiscTests(unittest.TestCase): - def setUp(self): - os.mkdir("temp") - os.mkdir("temp/testfolder1") - for i in range(1, 5): - with open("temp/testfile" + str(i), "w") as f: - f.write("this is a test file") - with open("temp/test.py", "w") as test: - test.write("#!/usr/bin/env python\n") - test.write("import sys\n") - test.write("sys.stdout.write(sys.argv[1])\n") - test.write("sys.stderr.write(sys.argv[2])\n") - - def tearDown(self): - shutil.rmtree("temp") + def tearDown(self): #pylint: disable-msg=R0201 + """Test Case Teardown""" + xnt.tests.tear_down() def test_echo(self): + """Test Echo Task""" xnt.tasks.echo("this is my cool echo", "temp/mynewcoolfile") self.assertTrue(os.path.exists("temp/mynewcoolfile")) - with open("temp/mynewcoolfile", "r") as f: - self.assertEqual("this is my cool echo", f.read()) + with open("temp/mynewcoolfile", "r") as temp_file: + self.assertEqual("this is my cool echo", temp_file.read()) def test_call(self): + """Test Call, testing redirection""" out = open("temp/testout", "w") err = open("temp/testerr", "w") xnt.tasks.call(["python", @@ -57,10 +53,10 @@ class TaskMiscTests(unittest.TestCase): err.close() self.assertTrue(os.path.exists("temp/testout")) self.assertTrue(os.path.exists("temp/testerr")) - with open("temp/testout", "r") as o: - self.assertEqual("42", o.read()) - with open("temp/testerr", "r") as e: - self.assertEqual("hello", e.read()) + with open("temp/testout", "r") as std_out: + self.assertEqual("42", std_out.read()) + with open("temp/testerr", "r") as std_err: + self.assertEqual("hello", std_err.read()) if __name__ == "__main__": unittest.main() diff --git a/xnt/tests/taskmkdirtests.py b/xnt/tests/taskmkdirtests.py index 5010c9b..2b55b06 100644 --- a/xnt/tests/taskmkdirtests.py +++ b/xnt/tests/taskmkdirtests.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Test `xnt.tasks.mkdir`""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -16,25 +17,24 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import sys import os -import shutil import xnt.tasks +import xnt.tests import unittest +#pylint: disable-msg=C0103 +class TaskMkdirTests(unittest.TestCase): #pylint: disable-msg=R0904 + """Test Cases for Mkdir""" + def setUp(self): #pylint: disable-msg=R0201 + """Test Case Setup""" + xnt.tests.set_up() -class TaskMkdirTests(unittest.TestCase): - def setUp(self): - os.mkdir("temp") - os.mkdir("temp/testfolder1") - for i in range(1, 5): - with open("temp/testfile" + str(i), "w") as f: - f.write("this is a test file") - - def tearDown(self): - shutil.rmtree("temp") + def tearDown(self): #pylint: disable-msg=R0201 + """Test Case Teardown""" + xnt.tests.tear_down() def test_mkdir(self): + """Test mkdir method""" xnt.tasks.mkdir("temp/mynewtestfolder") self.assertTrue(os.path.exists("temp/mynewtestfolder")) self.assertTrue(os.path.exists("temp/testfolder1")) diff --git a/xnt/tests/taskmovetests.py b/xnt/tests/taskmovetests.py index e3d9a06..72670cb 100644 --- a/xnt/tests/taskmovetests.py +++ b/xnt/tests/taskmovetests.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Test `xnt.tasks.mv`""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -16,25 +17,24 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import sys import os -import shutil import xnt.tasks +import xnt.tests import unittest +#pylint: disable-msg=C0103 +class TaskMoveTests(unittest.TestCase): #pylint: disable-msg=R0904 + """Test cases for move""" + def setUp(self): #pylint: disable-msg=R0201 + """Test Case Setup""" + xnt.tests.set_up() -class TaskMoveTests(unittest.TestCase): - def setUp(self): - os.mkdir("temp") - os.mkdir("temp/testfolder1") - for i in range(1, 5): - with open("temp/testfile" + str(i), "w") as f: - f.write("this is a test file") - - def tearDown(self): - shutil.rmtree("temp") + def tearDown(self): #pylint: disable-msg=R0201 + """Test Case Teardown""" + xnt.tests.tear_down() def test_mv(self): + """Test Moving files and folders""" xnt.tasks.mv("temp/testfolder1", "temp/testfolder2") self.assertTrue(os.path.exists("temp/testfolder2")) self.assertFalse(os.path.exists("temp/testfolder1")) diff --git a/xnt/tests/taskremovetests.py b/xnt/tests/taskremovetests.py index ffeef6d..f94103c 100644 --- a/xnt/tests/taskremovetests.py +++ b/xnt/tests/taskremovetests.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Test `xnt.tasks.rm`""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -16,31 +17,24 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import sys import os -import shutil import xnt.tasks +import xnt.tests import unittest +#pylint: disable-msg=C0103 +class TaskRemoveTests(unittest.TestCase): #pylint: disable-msg=R0904 + """Test Case for Remove""" + def setUp(self): #pylint: disable-msg=R0201 + """Test case Setup""" + xnt.tests.set_up() -class TaskRemoveTests(unittest.TestCase): - def setUp(self): - os.mkdir("temp") - os.mkdir("temp/testfolder1") - for i in range(1, 5): - with open("temp/testfile" + str(i), "w") as f: - f.write("this is a test file") - - def tearDown(self): - shutil.rmtree("temp") - - def test_mkdir(self): - xnt.tasks.mkdir("temp/mynewtestfolder") - self.assertTrue(os.path.exists("temp/mynewtestfolder")) - self.assertTrue(os.path.exists("temp/testfolder1")) - xnt.tasks.mkdir("temp/testfolder1") + def tearDown(self): #pylint: disable-msg=R0201 + """Test case Teardown""" + xnt.tests.tear_down() def test_rm(self): + """Remove cases""" xnt.tasks.rm("temp/testfolder1") self.assertFalse(os.path.exists("temp/testfolder1")) xnt.tasks.rm("temp/testfile1") diff --git a/xnt/vcs/__init__.py b/xnt/vcs/__init__.py index 64e5706..2e1aa99 100644 --- a/xnt/vcs/__init__.py +++ b/xnt/vcs/__init__.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Version Controll Module""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -15,3 +16,14 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . + +def clone_options(cmd, url, branch=None, dest=None): + """Build up common options for clone commands""" + new_cmd = list(cmd) + if branch: + new_cmd.append("--branch") + new_cmd.append(branch) + cmd.append(url) + if dest: + new_cmd.append(dest) + return new_cmd diff --git a/xnt/vcs/cvs.py b/xnt/vcs/cvs.py index cc1d765..8b479f3 100644 --- a/xnt/vcs/cvs.py +++ b/xnt/vcs/cvs.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""CVS Version Control Wrapper""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -17,10 +18,10 @@ # along with this program. If not, see . import os -import sys import subprocess def cvsco(module, rev="", dest=""): + """Run CVS Checkout""" cmd = ["cvs", "co", "-P"] if rev: cmd.append("-r") @@ -32,7 +33,9 @@ def cvsco(module, rev="", dest=""): subprocess.call(cmd) def cvsupdate(path): - oldPath = os.path.abspath(os.getcwd()) + """Run CVS Update""" + cwd = os.path.abspath(os.getcwd()) os.chdir(path) cmd = ["cvs", "update"] - os.chdir(oldPath) + subprocess.call(cmd) + os.chdir(cwd) diff --git a/xnt/vcs/git.py b/xnt/vcs/git.py index 3d35a2d..74d3724 100644 --- a/xnt/vcs/git.py +++ b/xnt/vcs/git.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Git Version Control Module""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -17,20 +18,17 @@ # along with this program. If not, see . import os -import sys import xnt.tasks +import xnt.vcs def gitclone(url, dest=None, branch=None): + """Clone a repository""" command = ["git", "clone"] - if branch: - command.append("--branch") - command.append(branch) - command.append(url) - if dest: - command.append(dest) + command = xnt.vcs.clone_options(command, url, branch, dest) xnt.tasks.call(command) def gitpull(path, source="origin", branch="master"): + """Pull/Update a cloned repository""" cwd = os.getcwd() os.chdir(path) command = ["git", "pull", source, branch] diff --git a/xnt/vcs/hg.py b/xnt/vcs/hg.py index bc92ea8..b0c9f7e 100644 --- a/xnt/vcs/hg.py +++ b/xnt/vcs/hg.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Mercurial Version Control Module/Wrapper""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -17,23 +18,20 @@ # along with this program. If not, see . import os -import sys import xnt.tasks +import xnt.vcs -def hgclone(url, dest=None,rev=None,branch=None): +def hgclone(url, dest=None, rev=None, branch=None): + """Clone a Mercurial Repository""" command = ["hg", "clone"] if rev: command.append("--rev") command.append(rev) - if branch: - command.append("--branch") - command.append(branch) - command.append(url) - if dest: - command.append(dest) + command = xnt.vcs.clone_options(command, url, branch, dest) xnt.tasks.call(command) def hgfetch(path, source='default'): + """Pull and Update an already cloned Mercurial Repository""" command = ["hg", "pull", "-u", source] cwd = os.getcwd() os.chdir(path) diff --git a/xnt/version.py b/xnt/version.py index 5858fa7..51a4891 100644 --- a/xnt/version.py +++ b/xnt/version.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Version information definition""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -16,5 +17,5 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -__version_info__ = (0, 5, 1) -__version__ = '.'.join(map(str, __version_info__)) +__version_info__ = (0, 5, 1, "lint_refactor", "dev") +__version__ = '.'.join(list(str(i) for i in __version_info__ if True)) diff --git a/xnt/xenant.py b/xnt/xenant.py index b0cf15e..e67ef7c 100644 --- a/xnt/xenant.py +++ b/xnt/xenant.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Xnt Runner Script""" # Xnt -- A Wrapper Build Tool # Copyright (C) 2012 Kenny Ballou @@ -20,15 +21,16 @@ import os import sys import time import logging -from xnt.cmdoptions import options -from xnt.commands import commands +from xnt.cmdoptions import OPTIONS +from xnt.commands import COMMANDS from xnt.commands.target import TargetCommand logging.basicConfig(format="%(asctime)s:%(levelname)s:%(message)s") -logger = logging.Logger(name=__name__) -logger.addHandler(logging.StreamHandler()) +LOGGER = logging.Logger(name=__name__) +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:] @@ -37,34 +39,38 @@ def main(): 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]() + if flag in OPTIONS: + OPTIONS[flag]() else: - logger.debug("%s is not a vaild option", flag) + LOGGER.debug("%s is not a vaild option", flag) #run things cmd_found = False for cmd in cmds: - if cmd in commands: + if cmd in COMMANDS: cmd_found = True - if commands[cmd].needs_build: - command = commands[cmd](loadBuild()) + if COMMANDS[cmd].needs_build: + command = COMMANDS[cmd](load_build()) else: - command = commands[cmd]() - ec = command.run() + command = COMMANDS[cmd]() + error_code = command.run() if cmd_found == False: - command = TargetCommand(loadBuild()) - ec = command.run(targets=cmds, props=params) + command = TargetCommand(load_build()) + error_code = command.run(targets=cmds, props=params) elapsed_time = time.time() - start_time - logger.info("Execution time: %.3f", elapsed_time) - if ec != 0: - logger.info("Failure") + LOGGER.info("Execution time: %.3f", elapsed_time) + if error_code != 0: + LOGGER.info("Failure") from xnt.tasks import rm rm("build.pyc", "__pycache__") - if ec != 0: - sys.exit(ec) + if error_code != 0: + sys.exit(error_code) -def loadBuild(path=""): +def load_build(path=""): + """Load build file + + Load the build.py and return the resulting import + """ if not path: path = os.getcwd() else: @@ -73,12 +79,12 @@ def loadBuild(path=""): cwd = os.getcwd() os.chdir(path) if not os.path.exists("build.py"): - logger.error("There was no build file") + LOGGER.error("There was no build file") sys.exit(1) try: return __import__("build", fromlist=[]) except ImportError: - logger.error("HOW?!") + LOGGER.error("HOW?!") return None finally: sys.path.remove(path) @@ -86,6 +92,4 @@ def loadBuild(path=""): os.chdir(cwd) if __name__ == "__main__": - ec = main() - if ec: - sys.exit(ec) + main() -- cgit v1.2.1 From 76d0ba3ae1f76900d4f802a7e37ca527a832dfba Mon Sep 17 00:00:00 2001 From: kennyballou Date: Thu, 7 Mar 2013 18:51:07 -0700 Subject: Refactor xnt.tasks.xnt Rename function to xntcall -- Fixes clobbering issues Update parameters to align with the target runner parameters Update doctstring --- xnt/tasks.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'xnt') diff --git a/xnt/tasks.py b/xnt/tasks.py index 22df254..00e562a 100644 --- a/xnt/tasks.py +++ b/xnt/tasks.py @@ -101,14 +101,17 @@ def echo(msg, tofile): def log(msg="",lvl=logging.INFO): logger.log(lvl, msg) -def xnt(target, path): - """ - Invoke xnt on another build file in a different directory +def xntcall(path, targets=None, props=None): + """Invoke xnt on another build file in a different directory + + param: path - to the build file (including build file) + param: targets - list of targets to execute + param: props - dictionary of properties to pass to the build module """ import xnt.xenant - xnt.xenant.invokeBuild( - xnt.xenant.__loadBuild(path), - target) + from xnt.commands.target import TargetCommand + command = TargetCommand(xnt.xenant.loadBuild(path)) + return command.run(targets=targets, props=None) def call(command, stdout=None, stderr=None): """ -- cgit v1.2.1