diff options
author | kballou <kballou@devnulllabs.io> | 2014-08-01 00:59:30 -0600 |
---|---|---|
committer | kballou <kballou@devnulllabs.io> | 2014-08-01 01:05:04 -0600 |
commit | 3f26015e82a50b3e01244c171d631c633c54d3f9 (patch) | |
tree | 7625b664590a16100dcf6dd5f2c4b1f1035de747 /xnt | |
parent | 885c4f9eaa06ae550fd27af0d74c1767f940ada9 (diff) | |
download | xnt-3f26015e82a50b3e01244c171d631c633c54d3f9.tar.gz xnt-3f26015e82a50b3e01244c171d631c633c54d3f9.tar.xz |
call: add `path` parameter
Add `path` parameter to call function so that users can specify the working
directory of the subprocess to invoke. This does remove some code
duplication.
Diffstat (limited to 'xnt')
-rw-r--r-- | xnt/tasks/__init__.py | 7 | ||||
-rw-r--r-- | xnt/tasks/build/make.py | 20 | ||||
-rw-r--r-- | xnt/tasks/build/tex.py | 67 | ||||
-rw-r--r-- | xnt/tasks/core_tasks.py | 49 | ||||
-rw-r--r-- | xnt/tasks/vcs/cvs.py | 6 | ||||
-rw-r--r-- | xnt/tasks/vcs/git.py | 6 | ||||
-rw-r--r-- | xnt/tasks/vcs/hg.py | 6 | ||||
-rw-r--r-- | xnt/tests/taskmisctests.py | 2 |
8 files changed, 78 insertions, 85 deletions
diff --git a/xnt/tasks/__init__.py b/xnt/tasks/__init__.py index 081ea55..48104d6 100644 --- a/xnt/tasks/__init__.py +++ b/xnt/tasks/__init__.py @@ -117,14 +117,15 @@ def xntcall(buildfile, targets=None, props=None): return xnt.tasks.core_tasks.__apply__( xnt.tasks.core_tasks.__xntcall__(buildfile, targets, props)) -def call(command, stdout=None, stderr=None): +def call(command, stdout=None, stderr=None, path=None): '''Execute given command, redirectoring stdout and stderr :param command: command, in the form of a list, to execute :param stdout: file to write stdout - :param stderr: file to write stderr''' + :param stderr: file to write stderr + :param path: working directory for command''' return xnt.tasks.core_tasks.__apply__( - xnt.tasks.core_tasks.__call__(command, stdout, stderr)) + xnt.tasks.core_tasks.__call__(command, stdout, stderr, path)) def setup(commands, directory=None): '''Invoke ``setup.py`` file in current or given directory diff --git a/xnt/tasks/build/make.py b/xnt/tasks/build/make.py index 017e81f..48bbac3 100644 --- a/xnt/tasks/build/make.py +++ b/xnt/tasks/build/make.py @@ -17,7 +17,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -import os from xnt.tasks.core_tasks import __apply__ from xnt.tasks.core_tasks import __call__ from xnt.tasks.core_tasks import __which__ @@ -49,8 +48,7 @@ def __ant__(target, path=None, flags=None, pkeys=None, pvalues=None): for param in zip(kwargs['pkeys'], kwargs['pvalues']): cmd.append('-D%s=%s' % param) cmd.append(target) - # TODO: this will need to wait for an upstream refactor - return __run_in(path, lambda: __apply__(__call__(cmd))) + return __apply__(__call__(cmd, path=path)) args = {'target': target, 'flags': flags, 'pkeys': pkeys, @@ -85,8 +83,7 @@ def __make__(target, path=None, flags=None, pkeys=None, pvalues=None): for param in zip(kwargs['pkeys'], kwargs['pvalues']): cmd.append('%s=%s' % param) cmd.append(target) - # TODO: this will need to wait for an upstream refactor - return __run_in(path, lambda: __apply__(__call__(cmd))) + return __apply__(__call__(cmd, path=path)) args = {'target': target, 'flags': flags, 'pkeys': pkeys, @@ -121,21 +118,10 @@ def __nant__(target, path=None, flags=None, pkeys=None, pvalues=None): for param in zip(kwargs['pkeys'], kwargs['pvalues']): cmd.append('-D%s=%s' % param) cmd.append(target) - # TODO: this will need to wait for an upstream refactor - return __run_in(path, lambda: __apply__(__call__(cmd))) + return __apply__(__call__(cmd, path=path)) args = {'target': target, 'flags': flags, 'pkeys': pkeys, 'pvalues': pvalues, 'path': path,} return ((__execute__, args),) - -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 = function() - if cwd: - os.chdir(cwd) - return result diff --git a/xnt/tasks/build/tex.py b/xnt/tasks/build/tex.py index 159e2f2..629864e 100644 --- a/xnt/tasks/build/tex.py +++ b/xnt/tasks/build/tex.py @@ -41,27 +41,32 @@ def __pdflatex__(document, '''Perform pdflatex build''' devnull = None if VERBOSE else open(os.devnull, 'w') documentbase = os.path.splitext(document)[0] - cwd = os.getcwd() - os.chdir(kwargs['directory']) def pdf(draftmode=False): """Generate PDF""" from xnt.tasks.core_tasks import __call__, __apply__ cmd = ["pdflatex", document, "-halt-on-error",] if draftmode: cmd.append('-draftmode') - return __apply__(__call__(cmd, stdout=devnull)) + return __apply__(__call__( + cmd, + stdout=devnull, + path=kwargs['directory'])) def run_bibtex(): """Generate BibTex References""" from xnt.tasks.core_tasks import __call__, __apply__ - return __apply__(__call__(["bibtex", documentbase + ".aux"], - stdout=devnull)) + return __apply__(__call__( + ["bibtex", documentbase + ".aux"], + stdout=devnull, + path=kwargs['directory'])) def makeglossaries(): """Generate Glossary""" from xnt.tasks.core_tasks import __call__, __apply__ - return __apply__(__call__(["makeglossaries", documentbase], - stdout=devnull)) + return __apply__(__call__( + ["makeglossaries", documentbase], + stdout=devnull, + path=kwargs['directory'])) error_codes = [] error_codes.append(pdf(draftmode=True)) @@ -71,7 +76,6 @@ def __pdflatex__(document, error_codes.append(run_bibtex()) error_codes.append(pdf(draftmode=True)) error_codes.append(pdf(draftmode=False)) - os.chdir(cwd) if devnull: devnull.close() return sum(error_codes) @@ -90,29 +94,30 @@ def __clean__(directory=None, remove_pdf=False): """ def __execute__(**kwargs): '''Perform clean operation''' - cwd = os.getcwd() - os.chdir(kwargs['directory']) - from xnt.tasks.core_tasks import __apply__, __remove__ - __apply__(__remove__("*.out", - "*.log", - "*.aux", - "*.toc", - "*.tol", - "*.tof", - "*.tot", - "*.bbl", - "*.blg", - "*.nav", - "*.snm", - "*.mtc", - "*.mtc0", - "*.glo", - "*.ist", - "*.glg", - "*.gls")) - if kwargs['remove_pdf']: - __apply__(__remove__("*.pdf")) - os.chdir(cwd) + from xnt.tasks.core_tasks import __run_in__ + def closure(): + '''closure around removal''' + from xnt.tasks.core_tasks import __apply__, __remove__ + __apply__(__remove__("*.out", + "*.log", + "*.aux", + "*.toc", + "*.tol", + "*.tof", + "*.tot", + "*.bbl", + "*.blg", + "*.nav", + "*.snm", + "*.mtc", + "*.mtc0", + "*.glo", + "*.ist", + "*.glg", + "*.gls")) + if kwargs['remove_pdf']: + __apply__(__remove__("*.pdf")) + return __run_in__(closure, kwargs['directory']) args = {'directory': directory if directory else os.getcwd(), 'remove_pdf': remove_pdf,} return ((__execute__, args),) diff --git a/xnt/tasks/core_tasks.py b/xnt/tasks/core_tasks.py index 175d5eb..3be3873 100644 --- a/xnt/tasks/core_tasks.py +++ b/xnt/tasks/core_tasks.py @@ -278,14 +278,13 @@ def __xntcall__(buildfile, targets=None, props=None): build = __load_build__(kwargs['buildfile']) path = os.path.dirname(kwargs['buildfile']) - cwd = os.getcwd() - os.chdir(path) - error_code = invoke_build( - build, - targets=kwargs['targets'], - props=kwargs['props']) - os.chdir(cwd) - return error_code + def closure(): + '''closure around build invocation''' + return invoke_build( + build, + targets=kwargs['targets'], + props=kwargs['props']) + return __run_in__(closure, path) args = {'buildfile': buildfile, 'targets': targets, 'props': props, } return ((__execute__, args),) @@ -311,21 +310,38 @@ def __xnt_list_targets__(buildfile): args = {'buildfile': buildfile,} return ((__execute__, args),) -def __call__(command, stdout=None, stderr=None): +def __run_in__(func, path): + cwd = os.getcwd() + os.chdir(path) + result = func() + os.chdir(cwd) + return result + +def __call__(command, stdout=None, stderr=None, path=None): """ 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 + :param: path - directory to execute process :return: the error code of the subbed out call, `$?` """ def __execute__(**kwargs): '''Perform subprocess call''' - return subprocess.call( - args=kwargs['command'], - stdout=kwargs['stdout'], stderr=kwargs['stderr']) - args = {'command': command, 'stdout': stdout, 'stderr': stderr,} + def closure(): + '''closure around subprocess call''' + return subprocess.call( + args=kwargs['command'], + stdout=kwargs['stdout'], + stderr=kwargs['stderr']) + if not kwargs['path']: + path = os.getcwd() + return __run_in__(closure, path) + args = {'command': command, + 'stdout': stdout, + 'stderr': stderr, + 'path': path,} return ((__execute__, args),) def __setup__(command=None, commands=None, directory=None): @@ -341,12 +357,7 @@ def __setup__(command=None, commands=None, directory=None): cmd = [sys.executable, "setup.py",] for command in kwargs['commands']: cmd.append(command) - cwd = os.getcwd() - if kwargs['directory']: - os.chdir(kwargs['directory']) - error_code = __apply__(__call__(cmd)) - os.chdir(cwd) - return error_code + return __apply__(__call__(cmd, path=kwargs['directory'])) if not commands: commands = [] if command: diff --git a/xnt/tasks/vcs/cvs.py b/xnt/tasks/vcs/cvs.py index c71639a..5b8cae0 100644 --- a/xnt/tasks/vcs/cvs.py +++ b/xnt/tasks/vcs/cvs.py @@ -17,7 +17,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -import os from xnt.tasks.core_tasks import __apply__ from xnt.tasks.core_tasks import __call__ from xnt.tasks.core_tasks import __which__ @@ -52,9 +51,6 @@ def __cvsupdate__(path): def __execute__(**kwargs): '''Perform CVS Update''' assert __apply__(__which__("cvs")) - cwd = os.path.abspath(os.getcwd()) - os.chdir(kwargs['path']) cmd = ["cvs", "update"] - __apply__(__call__(cmd)) - os.chdir(cwd) + __apply__(__call__(cmd, path=kwargs['path'])) return ((__execute__, {'path': path,}),) diff --git a/xnt/tasks/vcs/git.py b/xnt/tasks/vcs/git.py index 25c36af..b6ef0e3 100644 --- a/xnt/tasks/vcs/git.py +++ b/xnt/tasks/vcs/git.py @@ -17,7 +17,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -import os import xnt.tasks.vcs from xnt.tasks.core_tasks import __apply__, __call__, __which__ @@ -48,11 +47,8 @@ def __gitpull__(path, remote=None, branch=None): def __execute__(**kwargs): '''Perform git pull''' assert __apply__(__which__("git")) - cwd = os.getcwd() - os.chdir(kwargs['path']) command = ["git", "pull", kwargs['remote'], kwargs['branch']] - __apply__(__call__(command)) - os.chdir(cwd) + __apply__(__call__(command, path=kwargs['path'])) args = { 'path': path, 'remote': remote if remote else 'origin', diff --git a/xnt/tasks/vcs/hg.py b/xnt/tasks/vcs/hg.py index 63e697e..0d92c09 100644 --- a/xnt/tasks/vcs/hg.py +++ b/xnt/tasks/vcs/hg.py @@ -17,7 +17,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -import os import xnt.tasks.vcs from xnt.tasks.core_tasks import __apply__, __call__, __which__ @@ -52,9 +51,6 @@ def __hgfetch__(path, source=None): '''Perform hg pull''' assert __apply__(__which__("hg")) command = ["hg", "pull", "-u", kwargs['source']] - cwd = os.getcwd() - os.chdir(kwargs['path']) - __apply__(__call__(command)) - os.chdir(cwd) + __apply__(__call__(command, path=kwargs['path'])) args = {'path': path, 'source': source if source else 'default',} return ((__execute__, args),) diff --git a/xnt/tests/taskmisctests.py b/xnt/tests/taskmisctests.py index d1eae53..901d69c 100644 --- a/xnt/tests/taskmisctests.py +++ b/xnt/tests/taskmisctests.py @@ -63,6 +63,8 @@ class TaskMiscTests(unittest.TestCase): self.assertIsNone(result[0][1]['stdout']) self.assertTrue('stderr' in result[0][1]) self.assertIsNone(result[0][1]['stderr']) + self.assertTrue('path' in result[0][1]) + self.assertIsNone(result[0][1]['path']) def test_setup_with_single_command(self): '''Test setup function with a single command''' |