diff options
author | 2014-08-12 00:09:34 -0400 | |
---|---|---|
committer | 2014-08-15 17:42:40 -0400 | |
commit | 3ea8512b470742610c3e450cbab20fc9e5609bb2 (patch) | |
tree | d28a985cab44a3c48dcdb4f6f9e08ee39173c5a8 | |
parent | Centralizes common archive overlay code (diff) | |
download | layman-3ea8512b470742610c3e450cbab20fc9e5609bb2.tar.gz layman-3ea8512b470742610c3e450cbab20fc9e5609bb2.tar.bz2 layman-3ea8512b470742610c3e450cbab20fc9e5609bb2.zip |
Migrates run_command to utily.py
-rw-r--r-- | layman/overlays/bzr.py | 9 | ||||
-rw-r--r-- | layman/overlays/cvs.py | 13 | ||||
-rw-r--r-- | layman/overlays/darcs.py | 7 | ||||
-rw-r--r-- | layman/overlays/g_common.py | 8 | ||||
-rw-r--r-- | layman/overlays/g_sorcery.py | 8 | ||||
-rw-r--r-- | layman/overlays/git.py | 16 | ||||
-rw-r--r-- | layman/overlays/mercurial.py | 9 | ||||
-rw-r--r-- | layman/overlays/rsync.py | 4 | ||||
-rw-r--r-- | layman/overlays/source.py | 95 | ||||
-rw-r--r-- | layman/overlays/svn.py | 16 | ||||
-rw-r--r-- | layman/overlays/tar.py | 3 | ||||
-rw-r--r-- | layman/utils.py | 98 |
12 files changed, 152 insertions, 134 deletions
diff --git a/layman/overlays/bzr.py b/layman/overlays/bzr.py index ea3e787..614f816 100644 --- a/layman/overlays/bzr.py +++ b/layman/overlays/bzr.py @@ -28,7 +28,7 @@ __version__ = "$Id: bzr.py 236 2006-09-05 20:39:37Z wrobel $" # #------------------------------------------------------------------------------- -from layman.utils import path +from layman.utils import path, run_command from layman.overlays.source import OverlaySource, require_supported #=============================================================================== @@ -77,7 +77,7 @@ class BzrOverlay(OverlaySource): else: args = ['branch', src, target] return self.postsync( - self.run_command(self.command(), args, cmd=self.type), + run_command(self.config, self.command(), args, cmd=self.type), cwd=target) def update(self, base, src): @@ -98,7 +98,7 @@ class BzrOverlay(OverlaySource): if self.config['quiet']: args.append('--quiet') return self.postsync( - self.run_command(self.command(), args, cmd=self.type), + run_command(self.config, self.command(), args, cmd=self.type), cwd=target) def sync(self, base): @@ -116,7 +116,8 @@ class BzrOverlay(OverlaySource): else: args = ['pull', '--overwrite', self.src] return self.postsync( - self.run_command(self.command(), args, cwd=target, cmd=self.type), + run_command(self.config, self.command(), args, cwd=target, + cmd=self.type), cwd=target) def supported(self): diff --git a/layman/overlays/cvs.py b/layman/overlays/cvs.py index 70638e3..62ad156 100644 --- a/layman/overlays/cvs.py +++ b/layman/overlays/cvs.py @@ -29,7 +29,7 @@ __version__ = "$Id$" import xml.etree.ElementTree as ET # Python 2.5 import re -from layman.utils import path +from layman.utils import path, run_command from layman.overlays.source import OverlaySource, require_supported #=============================================================================== @@ -71,7 +71,7 @@ class CvsOverlay(OverlaySource): args.append(self.branch) return self.postsync( - self.run_command(self.command(), args, cwd=base, + run_command(self.config, self.command(), args, cwd=base, env=dict(CVSROOT=self.src), cmd=self.type), cwd=target) @@ -90,7 +90,8 @@ class CvsOverlay(OverlaySource): # First echo the new repository to CVS/Root args = [src, '>', '/CVS/Root'] - result = self.run_command('echo', args, cmd='echo', cwd=target) + result = run_command(self.config, 'echo', args, cmd='echo', + cwd=target) if result == 0: location = src.split(':')[3] @@ -106,7 +107,8 @@ class CvsOverlay(OverlaySource): # sed -i 's/<old_location>/<new_location>/ <target>/CVS/Repository args = ['-i', expression, '/CVS/Repository'] - return self.run_command('sed', args, cmd='sed', cwd=target) + return run_command(self.config, 'sed', args, cmd='sed', + cwd=target) return result @@ -129,7 +131,8 @@ class CvsOverlay(OverlaySource): if len(cfg_opts): args.append(cfg_opts) return self.postsync( - self.run_command(self.command(), args, cwd=target, cmd=self.type), + run_command(self.config, self.command(), args, cwd=target, + cmd=self.type), cwd=target) def supported(self): diff --git a/layman/overlays/darcs.py b/layman/overlays/darcs.py index 53966c6..9e951da 100644 --- a/layman/overlays/darcs.py +++ b/layman/overlays/darcs.py @@ -27,7 +27,7 @@ __version__ = "$Id: darcs.py 236 2006-09-05 20:39:37Z wrobel $" # #------------------------------------------------------------------------------- -from layman.utils import path +from layman.utils import path, run_command from layman.overlays.source import OverlaySource, require_supported #=============================================================================== @@ -71,7 +71,7 @@ class DarcsOverlay(OverlaySource): src, target] return self.postsync( - self.run_command(self.command(), args, cmd=self.type), + run_command(self.config, self.command(), args, cmd=self.type), cwd=target) def sync(self, base): @@ -89,7 +89,8 @@ class DarcsOverlay(OverlaySource): else: args = ['pull', '--all', self.src] return self.postsync( - self.run_command(self.command(), args, cwd=target, cmd=self.type), + run_command(self.config, self.command(), args, cwd=target, + cmd=self.type), cwd=target) def supported(self): diff --git a/layman/overlays/g_common.py b/layman/overlays/g_common.py index f3eb58d..9c5f6b7 100644 --- a/layman/overlays/g_common.py +++ b/layman/overlays/g_common.py @@ -25,7 +25,7 @@ from __future__ import unicode_literals #------------------------------------------------------------------------------- import os -from layman.utils import path +from layman.utils import path, run_command from layman.overlays.source import OverlaySource, require_supported #=============================================================================== @@ -69,12 +69,14 @@ class GCommonOverlay(OverlaySource): target = path([base, self.parent.name]) args = [target, 'sync', self.driver, self.remote_uri] - returncode = self.run_command(self.command(), args, cwd=target) + returncode = run_command(self.config, self.command(), args, + cwd=target) if returncode: return returncode args = [target, 'generate-tree'] return self.postsync( - self.run_command(self.command(), args, cwd=target, cmd=self.type), + run_command(self.config, self.command(), args, cwd=target, + cmd=self.type), cwd=target) def supported(self): diff --git a/layman/overlays/g_sorcery.py b/layman/overlays/g_sorcery.py index 19bfe18..cc84aeb 100644 --- a/layman/overlays/g_sorcery.py +++ b/layman/overlays/g_sorcery.py @@ -26,7 +26,7 @@ from __future__ import unicode_literals #------------------------------------------------------------------------------- import os -from layman.utils import path +from layman.utils import path, run_command from layman.overlays.source import OverlaySource, require_supported #=============================================================================== @@ -70,12 +70,14 @@ class GSorceryOverlay(OverlaySource): target = path([base, self.parent.name]) args = [self.backend, '-o', target, '-r', self.repository, 'sync'] - returncode = self.run_command(self.command(), args, cwd=target) + returncode = run_command(self.config, self.command(), args, + cwd=target) if returncode: return returncode args = [self.backend, '-o', target, 'generate-tree'] return self.postsync( - self.run_command(self.command(), args, cwd=target, cmd=self.type), + run_command(self.config, self.command(), args, cwd=target, + cmd=self.type), cwd=target) def supported(self): diff --git a/layman/overlays/git.py b/layman/overlays/git.py index f3ada66..f647139 100644 --- a/layman/overlays/git.py +++ b/layman/overlays/git.py @@ -28,7 +28,7 @@ __version__ = "$Id: git.py 146 2006-05-27 09:52:36Z wrobel $" import xml.etree.ElementTree as ET -from layman.utils import path +from layman.utils import path, run_command from layman.overlays.source import OverlaySource, require_supported #=============================================================================== @@ -86,7 +86,8 @@ class GitOverlay(OverlaySource): # adding cwd=base due to a new git bug in selinux due to # not having user_home_dir_t and portage_fetch_t permissions # but changing dir works around it. - success = self.run_command(self.command(), args, cmd=self.type, cwd=base) + success = run_command(self.config, self.command(), args,cmd=self.type, + cwd=base) self.output.debug("cloned git repo...success=%s" % str(success), 8) success = self.set_user(target) return self.postsync(success, cwd=target) @@ -97,13 +98,14 @@ class GitOverlay(OverlaySource): email = '"%s"' % self.config['git_email'] args = ['config', 'user.name', user] self.output.debug("set git user info...args=%s" % ' '.join(args), 8) - failure = self.run_command(self.command(), args, cmd=self.type, cwd=target) + failure = run_command(self.config, self.command(), args, cmd=self.type, cwd=target) if failure: self.output.debug("set git user info...failure setting name") return failure args = ['config', 'user.email', email] self.output.debug("set git user info...args=%s" % ' '.join(args), 8) - return self.run_command(self.command(), args, cmd=self.type, cwd=target) + return run_command(self.config, self.command(), args, cmd=self.type, + cwd=target) def update(self, base, src): ''' @@ -118,7 +120,8 @@ class GitOverlay(OverlaySource): # git remote set-url <name> <newurl> <oldurl> args = ['remote', 'set-url', 'origin', self._fix_git_source(src), self._fix_git_source(self.src)] - return self.run_command(self.command(), args, cmd=self.type, cwd=target) + return run_command(self.config, self.command(), args, cmd=self.type, + cwd=target) def sync(self, base): '''Sync overlay.''' @@ -137,7 +140,8 @@ class GitOverlay(OverlaySource): args.append(cfg_opts) return self.postsync( - self.run_command(self.command(), args, cwd=target, cmd=self.type), + run_command(self.config, self.command(), args, cwd=target, + cmd=self.type), cwd=target) def supported(self): diff --git a/layman/overlays/mercurial.py b/layman/overlays/mercurial.py index a1b7846..fa32555 100644 --- a/layman/overlays/mercurial.py +++ b/layman/overlays/mercurial.py @@ -30,7 +30,7 @@ __version__ = "$Id: mercurial.py 236 2006-09-05 20:39:37Z wrobel $" import re import xml.etree.ElementTree as ET -from layman.utils import path +from layman.utils import path, run_command from layman.overlays.source import OverlaySource, require_supported #=============================================================================== @@ -85,7 +85,7 @@ class MercurialOverlay(OverlaySource): args.append(self.branch) return self.postsync( - self.run_command(self.command(), args, cmd=self.type), + run_command(self.config, self.command(), args, cmd=self.type), cwd=target) def update(self, base, src): @@ -110,7 +110,7 @@ class MercurialOverlay(OverlaySource): args = ['-i', expression, hgrc] # Run sed. - return self.run_command('sed', args, cmd='sed', cwd=target) + return run_command(self.config, 'sed', args, cmd='sed', cwd=target) def sync(self, base): '''Sync overlay.''' @@ -128,7 +128,8 @@ class MercurialOverlay(OverlaySource): args = ['pull', '-u', self.src] return self.postsync( - self.run_command(self.command(), args, cwd=target, cmd=self.type), + run_command(self.config, self.command(), args, cwd=target, + cmd=self.type), cwd=target) def supported(self): diff --git a/layman/overlays/rsync.py b/layman/overlays/rsync.py index 407a09e..f540b76 100644 --- a/layman/overlays/rsync.py +++ b/layman/overlays/rsync.py @@ -26,7 +26,7 @@ __version__ = "$Id: rsync.py 236 2006-09-05 20:39:37Z wrobel $" # #------------------------------------------------------------------------------- -from layman.utils import path +from layman.utils import path, run_command from layman.overlays.source import OverlaySource, require_supported #=============================================================================== @@ -80,7 +80,7 @@ class RsyncOverlay(OverlaySource): args.append(target) return self.postsync( - self.run_command(self.command(), args, cmd=self.type), + run_command(self.config, self.command(), args, cmd=self.type), cwd=target) def supported(self): diff --git a/layman/overlays/source.py b/layman/overlays/source.py index 7c9674b..baabad3 100644 --- a/layman/overlays/source.py +++ b/layman/overlays/source.py @@ -20,7 +20,7 @@ import copy import sys import shutil import subprocess -from layman.utils import path +from layman.utils import path, resolve_command, run_command supported_cache = {} @@ -33,27 +33,10 @@ def _supported(key, check_supported=None): supported_cache[key] = check_supported() return supported_cache[key] -def _resolve_command(command, _output): - if os.path.isabs(command): - if not os.path.exists(command): - _output('Program "%s" not found' % command) - return ('File', None) - return ('File', command) - else: - kind = 'Command' - env_path = os.environ['PATH'] - for d in env_path.split(os.pathsep): - f = os.path.join(d, command) - if os.path.exists(f): - return ('Command', f) - _output('Cound not resolve command ' +\ - '"%s" based on PATH "%s"' % (command, env_path)) - return ('Command', None) - def require_supported(binaries, _output): for command, mtype, package in binaries: - kind, path = _resolve_command(command, _output) + kind, path = resolve_command(command, _output) if not path: if _output: _output(kind + ' ' + command + ' seems to be missing!' @@ -134,73 +117,6 @@ class OverlaySource(object): def command(self): return self.config['%s_command' % self.__class__.type_key] - def run_command(self, command, args, **kwargs): - self.output.debug("OverlaySource.run_command(): " + command, 6) - file_to_run = _resolve_command(command, self.output.error)[1] - args = [file_to_run] + args - assert('pwd' not in kwargs) # Bug detector - - self.output.debug("OverlaySource.run_command(): cleared 'assert'", 7) - cwd = kwargs.get('cwd', None) - env = None - env_updates = None - if 'env' in kwargs: - # Build actual env from surrounding plus updates - env_updates = kwargs['env'] - env = copy.copy(os.environ) - env.update(env_updates) - - command_repr = ' '.join(args) - if env_updates is not None: - command_repr = '%s %s' % (' '.join('%s=%s' % (k, v) for (k, v) - in sorted(env_updates.items())), command_repr) - if cwd is not None: - command_repr = '( cd %s && %s )' % (cwd, command_repr) - - cmd = kwargs.get('cmd', '') - self.output.info('Running %s... # %s' % (cmd, command_repr), 2) - - if self.config['quiet']: - - input_source = subprocess.PIPE - output_target = open('/dev/null', 'w') - else: - # Re-use parent file descriptors - input_source = None - output_target = None - - proc = subprocess.Popen(args, - stdin=input_source, - stdout=output_target, - stderr=self.config['stderr'], - cwd=cwd, - env=env) - - if self.config['quiet']: - # Make child non-interactive - proc.stdin.close() - - try: - result = proc.wait() - except KeyboardInterrupt: - self.output.info('Interrupted manually', 2) - self.output.warn("Checking for cleanup actions to perform", 4) - self.cleanup() - result = 1 - except Exception as err: - self.output.error( - 'Unknown exception running command: %s' % command_repr) - self.output.error('Original error was: %s' % str(err)) - result = 1 - - if self.config['quiet']: - output_target.close() - - if result: - self.output.info('Failure result returned from %s' % cmd , 2) - - return result - def postsync(self, failed_sync, **kwargs): """Runs any repo specific postsync operations """ @@ -215,14 +131,9 @@ class OverlaySource(object): kwargs.get('cwd', '')).split() command = _opt[0] args = _opt[1:] - return self.run_command(command, args, + return run_command(self.config, command, args, cmd='%s_postsync' % self.__class__.type_key) return failed_sync def to_xml_hook(self, repo_elem): pass - - def cleanup(self): - '''cleanup a failed/interrupted process - overridden in subclass if it is needed.''' - pass diff --git a/layman/overlays/svn.py b/layman/overlays/svn.py index c97195b..d5e598e 100644 --- a/layman/overlays/svn.py +++ b/layman/overlays/svn.py @@ -30,9 +30,8 @@ from subprocess import PIPE, Popen # #------------------------------------------------------------------------------ -from layman.utils import path -from layman.overlays.source import (OverlaySource, require_supported, - _resolve_command) +from layman.utils import path, resolve_command, run_command +from layman.overlays.source import (OverlaySource, require_supported) #============================================================================== # @@ -87,7 +86,7 @@ class SvnOverlay(OverlaySource): args.append(self.target) return self.postsync( - self.run_command(self.command(), args, cmd=self.type), + run_command(self.config, self.command(), args, cmd=self.type), cwd=self.target) def update(self, base, src): @@ -105,7 +104,7 @@ class SvnOverlay(OverlaySource): args = ['switch', '--relocate', self._fix_svn_source(self.src), self._fix_svn_source(src)] return self.postsync( - self.run_command(self.command(), args, cmd=self.type), + run_command(self.config, self.command(), args, cmd=self.type), cwd=target) @@ -139,7 +138,7 @@ class SvnOverlay(OverlaySource): args.append(self.target) return self.postsync( - self.run_command(self.command(), args, cmd=self.type), + run_command(self.config, self.command(), args, cmd=self.type), cwd=self.target) def supported(self): @@ -156,7 +155,8 @@ class SvnOverlay(OverlaySource): self.output.warn("SVN: preparing to run cleanup()", 2) args = ["cleanup"] args.append(self.target) - cleanup = self.run_command(self.command(), args, cmd="svn cleanup") + cleanup = run_command(self.config, self.command(), args, + cmd="svn cleanup") return def check_upgrade(self, target): @@ -164,7 +164,7 @@ class SvnOverlay(OverlaySource): than checking if it does need an upgrade if it is actually needed. ''' - file_to_run = _resolve_command(self.command(), self.output.error)[1] + file_to_run = resolve_command(self.command(), self.output.error)[1] args = " ".join([file_to_run, " upgrade", target]) pipe = Popen(args, shell=True, stdout=PIPE, stderr=PIPE) if pipe: diff --git a/layman/overlays/tar.py b/layman/overlays/tar.py index 7d7bf89..bcba32c 100644 --- a/layman/overlays/tar.py +++ b/layman/overlays/tar.py @@ -31,6 +31,7 @@ import sys from layman.constants import FILE_EXTENSIONS from layman.overlays.archive import ArchiveOverlay from layman.overlays.source import require_supported +from layman.utils import run_command #=============================================================================== # @@ -78,7 +79,7 @@ class TarOverlay(ArchiveOverlay): ''' # tar -v -x -f SOURCE -C TARGET args = ['-v', '-x', '-f', pkg, '-C', dest_dir] - result = self.run_command(self.command(), args, cmd=self.type) + result = run_command(self.config, self.command(), args, cmd=self.type) return result diff --git a/layman/utils.py b/layman/utils.py index 33d2c30..b769302 100644 --- a/layman/utils.py +++ b/layman/utils.py @@ -31,10 +31,14 @@ __version__ = '$Id: utils.py 236 2006-09-05 20:39:37Z wrobel $' # #------------------------------------------------------------------------------- -import types, re, os -import sys -import locale import codecs +import copy +import locale +import os +import re +import subprocess +import sys +import types from layman.output import Message @@ -184,6 +188,7 @@ def path(path_elements): return pathname + def reload_config(config): ''' Rereads the layman config. @@ -196,6 +201,93 @@ def reload_config(config): config.update_defaults({'config': defaults['config']}) config.read_config(defaults) + +def resolve_command(command, output): + if os.path.isabs(command): + if not os.path.exists(command): + output('Program "%s" not found' % command) + return ('File', None) + return ('File', command) + else: + kind = 'Command' + env_path = os.environ['PATH'] + for d in env_path.split(os.pathsep): + f = os.path.join(d, command) + if os.path.exists(f): + return ('Command', f) + output('Cound not resolve command ' +\ + '"%s" based on PATH "%s"' % (command, env_path)) + return ('Command', None) + + +def run_command(config, command, args, **kwargs): + output = config['output'] + output.debug("Utils.run_command(): " + command, 6) + + file_to_run = resolve_command(command, output.error)[1] + args = [file_to_run] + args + assert('pwd' not in kwargs) # Bug detector + + output.debug("OverlaySource.run_command(): cleared 'assert'", 7) + cwd = kwargs.get('cwd', None) + env = None + env_updates = None + if 'env' in kwargs: + # Build actual env from surrounding plus updates + env_updates = kwargs['env'] + env = copy.copy(os.environ) + env.update(env_updates) + + command_repr = ' '.join(args) + if env_updates is not None: + command_repr = '%s %s' % (' '.join('%s=%s' % (k, v) for (k, v) + in sorted(env_updates.items())), command_repr) + if cwd is not None: + command_repr = '( cd %s && %s )' % (cwd, command_repr) + + cmd = kwargs.get('cmd', '') + output.info('Running %s... # %s' % (cmd, command_repr), 2) + + if config['quiet']: + + input_source = subprocess.PIPE + output_target = open('/dev/null', 'w') + else: + # Re-use parent file descriptors + input_source = None + output_target = None + + proc = subprocess.Popen(args, + stdin=input_source, + stdout=output_target, + stderr=config['stderr'], + cwd=cwd, + env=env) + + if config['quiet']: + # Make child non-interactive + proc.stdin.close() + + try: + result = proc.wait() + except KeyboardInterrupt: + output.info('Interrupted manually', 2) + result = 1 + except Exception as err: + output.error( + 'Unknown exception running command: %s' % command_repr) + output.error('Original error was: %s' % str(err)) + result = 1 + + if config['quiet']: + output_target.close() + + if result: + output.info('Failure result returned from %s' % cmd , 2) + + return result + + def verify_overlay_src(current_src, remote_srcs): ''' Verifies that the src-url of the overlay in |