diff --git a/bin/gub b/bin/gub index 4e95faf7d..477bdc5ce 100755 --- a/bin/gub +++ b/bin/gub @@ -48,6 +48,7 @@ from gub import logging from gub import misc from gub import repository from gub.syntax import printf +from gub import loggedos import gub.settings # otherwise naming conflict with settings local vars. @@ -322,6 +323,8 @@ def main (): (options, files) = cli_parser.parse_args () options.verbosity -= options.quiet options.build_source = options.build_source or options.platform == 'cygwin' + if options.dry_run: + loggedos.dry_run () logging.default_logger.threshold = options.verbosity logging.info ('files: %(files)s\n' % locals ()) diff --git a/gub/loggedos.py b/gub/loggedos.py index 9c5a6d981..700d39f6a 100644 --- a/gub/loggedos.py +++ b/gub/loggedos.py @@ -6,38 +6,6 @@ from gub import logging from gub import misc -def system (logger, cmd, env=os.environ, ignore_errors=False): - # UGH, FIXME: - # There is loggedos usage that defies any PATH settings - tools_bin_dir = os.path.join (os.getcwd (), 'target/tools/root/usr/bin') - if not tools_bin_dir in env.get ('PATH', ''): - env['PATH'] = tools_bin_dir + misc.append_path (env.get ('PATH', '')) - env['SHELLOPTS'] = 'physical' - logger.write_log ('COMMAND defies PATH: ' + cmd + '\n', 'command') - - logger.write_log ('invoking %(cmd)s\n' % locals (), 'command') - proc = subprocess.Popen (cmd, bufsize=0, shell=True, env=env, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - close_fds=True) - - line = proc.stdout.readline () - while line: - if sys.version.startswith ('2'): - logger.write_log (line, 'output') - else: - logger.write_log (line.decode (sys.stdout.encoding), 'output') - line = proc.stdout.readline () - proc.wait () - - if proc.returncode: - m = 'Command barfed: %(cmd)s\n' % locals () - logger.write_log (m, 'error') - if not ignore_errors: - raise misc.SystemFailed (m) - return proc.returncode - - ######## # logged aliases to misc.py def logged_function (logger, function, *args, **kwargs): @@ -50,28 +18,82 @@ def logged_function (logger, function, *args, **kwargs): % (function.__name__, repr (args), repr (kwargs)), 'debug')]) return function (*args, **kwargs) -currentmodule = sys.modules[__name__] #ugh -for name, func in list ({'read_file': misc.read_file, - 'file_sub': misc.file_sub, - 'download_url': misc.download_url, - 'dump_file': misc.dump_file, - 'shadow':misc.shadow, - 'chmod': os.chmod, - 'makedirs': os.makedirs, - 'copy2': shutil.copy2, - 'remove': os.remove, - 'link': os.link, - 'symlink': os.symlink, - 'rename': os.rename, - 'read_pipe': misc.read_pipe}.items ()): +class Operations(object): + mapping = {'read_file': misc.read_file, + 'file_sub': misc.file_sub, + 'download_url': misc.download_url, + 'dump_file': misc.dump_file, + 'shadow':misc.shadow, + 'chmod': os.chmod, + 'makedirs': os.makedirs, + 'copy2': shutil.copy2, + 'remove': os.remove, + 'link': os.link, + 'symlink': os.symlink, + 'rename': os.rename, + 'read_pipe': misc.read_pipe} + + def __getattr__(self, name): + return self.mapping[name] + +class DryOperations(Operations): + def download_url (self, original_url, dest_dir): + print 'download_url droog' + pass + +class Module(object): + def __init__ (self, wrapped): + self.wrapped = wrapped + self.impl = Operations() + + def __getattr__ (self, name): + def with_logging (func): + def func_with_logging (logger, *args, **kwargs): + val = logged_function (logger, func, *args, **kwargs) + return val + return func_with_logging + return with_logging(getattr(self.impl, name)) + print 'get attr', name + try: + return getattr(self.wrapped, name) + except AttributeError: + return 'default' + + def dry_run (self): + self.impl = DryOperations() + + def system (self, logger, cmd, env=os.environ, ignore_errors=False): + # UGH, FIXME: + # There is loggedos usage that defies any PATH settings + tools_bin_dir = os.path.join (os.getcwd (), 'target/tools/root/usr/bin') + if not tools_bin_dir in env.get ('PATH', ''): + env['PATH'] = tools_bin_dir + misc.append_path (env.get ('PATH', '')) + env['SHELLOPTS'] = 'physical' + logger.write_log ('COMMAND defies PATH: ' + cmd + '\n', 'command') + + logger.write_log ('invoking %(cmd)s\n' % locals (), 'command') + proc = subprocess.Popen (cmd, bufsize=0, shell=True, env=env, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + close_fds=True) + + line = proc.stdout.readline () + while line: + if sys.version.startswith ('2'): + logger.write_log (line, 'output') + else: + logger.write_log (line.decode (sys.stdout.encoding), 'output') + line = proc.stdout.readline () + proc.wait () - def with_logging (func): - def func_with_logging (logger, *args, **kwargs): - val = logged_function (logger, func, *args, **kwargs) - return val - return func_with_logging - currentmodule.__dict__[name] = with_logging (func) + if proc.returncode: + m = 'Command barfed: %(cmd)s\n' % locals () + logger.write_log (m, 'error') + if not ignore_errors: + raise misc.SystemFailed (m) + return proc.returncode +sys.modules[__name__] = Module(sys.modules[__name__]) def test (): import unittest @@ -83,12 +105,13 @@ class Test_loggedos (unittest.TestCase): def setUp (self): # Urg: global?? self.logger = logging.set_default_log ('downloads/test/test.log', 5) + self.loggedos = Module('loggedos') def testDumpFile (self): - dump_file (self.logger, 'boe', 'downloads/test/a') + self.loggedos.dump_file (self.logger, 'boe', 'downloads/test/a') self.assert_ (os.path.exists ('downloads/test/a')) def testSystem (self): self.assertRaises (Exception, - system, self.logger, 'cp %(src)s %(dest)s') + self.loggedos.system, self.logger, 'cp %(src)s %(dest)s') suite = unittest.makeSuite (Test_loggedos) unittest.TextTestRunner (verbosity=2).run (suite)