changeset 5878:769a4ef2e83e

added dry_run for download_url.
author Eddy Pronk <epronk@muftor.com>
date Tue, 05 Jan 2010 22:33:50 +1100
parents c6e9fa177a62
children c7402f3a77bc
files bin/gub gub/loggedos.py
diffstat 2 files changed, 80 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/bin/gub	Sat Jan 02 01:18:06 2010 -0800
+++ b/bin/gub	Tue Jan 05 22:33:50 2010 +1100
@@ -48,6 +48,7 @@
 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 @@
     (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 ())
--- a/gub/loggedos.py	Sat Jan 02 01:18:06 2010 -0800
+++ b/gub/loggedos.py	Tue Jan 05 22:33:50 2010 +1100
@@ -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 @@
           % (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 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)
+    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 ()
+
+        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 @@
         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)