changeset 3793:2914188e710d

Remove duplication of settings between in gub, gpkg and gub-tester. Also add option to settings to print layout. Duplications in test-lily dir and makefiles remain. Next step for local home buildin: platform setting defaults to working dir for gub and gpkg, or `local'.
author Jan Nieuwenhuizen <janneke@gnu.org>
date Mon, 10 Sep 2007 16:44:32 +0200
parents 6df11c823795
children f053248a0a48
files bin/gpkg bin/gub bin/gub-tester gub/context.py gub/gubb.py gub/misc.py gub/oslog.py gub/settings.py
diffstat 8 files changed, 130 insertions(+), 97 deletions(-) [+]
line wrap: on
line diff
--- a/bin/gpkg	Mon Sep 10 12:55:02 2007 +0200
+++ b/bin/gpkg	Mon Sep 10 16:44:32 2007 +0200
@@ -133,30 +133,30 @@
     p.add_option ('-p', '--platform',
                   default=None,
                   dest='platform',
-                  metavar="PLATFORM",
-                  help="platform to use")
+                  metavar='PLATFORM',
+                  help='platform to use')
 
-    p.add_option ('', '--name',
-                  help="print package name only",
-                  action="store_true",
-                  dest="print_only_name")
+    p.add_option ('-n', '--name',
+                  help='print package name only',
+                  action='store_true',
+                  dest='print_only_name')
     p.add_option ('-r','--root',
-                  help="set platform root",
-                  metavar="DIR",
-                  dest="root",
-                  action="store")
+                  help='set platform root',
+                  metavar='DIR',
+                  dest='root',
+                  action='store')
     p.add_option ('-x', '--no-deps',
-                  help="ignore dependencies",
-                  action="store",
-                  dest="no_deps")
+                  help='ignore dependencies',
+                  action='store',
+                  dest='no_deps')
     p.add_option ('','--dbdir',
-                  action="store",
-                  dest="dbdir",
-                  help="set db directory")
+                  action='store',
+                  dest='dbdir',
+                  help='set db directory')
     p.add_option ('-v', '--verbose',
                   action='store_true',
-                  dest="be_verbose",
-                  help="be verbose")
+                  dest='verbose',
+                  help='be verbose')
     return p
 
 def parse_options ():
@@ -165,49 +165,41 @@
 
     options.command = ''
     options.arguments = []
-
     if len (arguments) > 0:
         options.command = re.sub ('-', '_', arguments.pop (0))
+    options.arguments = arguments
 
-    options.arguments = arguments
-    if not options.root:
-        if not options.platform:
-            sys.stderr.write ('need platform or root setting, use -p option')
-            sys.stderr.write ('\n\n')
-            p.print_help ()
-            sys.exit (2)
-        # FIXME: what if user changes ~/.gubrc?  should use gubb.Settings!
-        options.root = ('target/%s/root' % options.platform)
-        GUB_LOCAL_PREFIX = os.environ.get ('GUB_LOCAL_PREFIX')
-        if GUB_LOCAL_PREFIX and options.platform == 'local':
-            options.root = GUB_LOCAL_PREFIX
+    if not options.command:
+        print p.print_help ()
+        sys.exit (2)
     return options
 
 def main ():
     options = parse_options ()
+    from gub.settings import Settings
+    settings = Settings (options)
+    options.root = settings.system_root
+    options.platform = settings.platform
+    osi = oslog.Os_commands ('/dev/null', oslog.level['error'])
+    osi.stage ('root: ' + settings.system_root + '\n')
+    osi.info ('platform: ' + settings.platform + '\n')
     target_manager = gup.DependencyManager (options.root,
-                                            oslog.Os_commands ("/dev/null",
-                                                               oslog.level['info']),
+                                            osi,
                                             dbdir=options.dbdir)
 
-    branch_dict = {}
-    for b in options.branches:
-        (package, branch_name) = b.split ('=')
-        branch_dict[package] = branch_name
     if options.command == 'install':
-        platform = options.platform
-        # FIXME: what if user changes ~/.gubrc?  should use gubb.Settings!
-        target_manager.read_package_headers ('packages/%(platform)s/' % locals (), branch_dict)
-        target_manager.read_package_headers ('packages/%(platform)s/cross/' % locals (), branch_dict)
+        target_manager.read_package_headers (settings.packages,
+                                             settings.branches)
+        target_manager.read_package_headers (settings.cross_packages,
+                                             settings.branches)
 
-    if options.command:
-        commands = Command (target_manager, options)
-        if options.command in Command.__dict__:
-            Command.__dict__[options.command] (commands)
-        else:
-            sys.stderr.write ('no such command: ' + options.command)
-            sys.stderr.write ('\n')
-            sys.exit (2)
+    commands = Command (target_manager, options)
+    if options.command in Command.__dict__:
+        Command.__dict__[options.command] (commands)
+    else:
+        sys.stderr.write ('no such command: ' + options.command)
+        sys.stderr.write ('\n')
+        sys.exit (2)
 
 if __name__ == '__main__':
     main ()
--- a/bin/gub	Mon Sep 10 12:55:02 2007 +0200
+++ b/bin/gub	Mon Sep 10 16:44:32 2007 +0200
@@ -208,8 +208,8 @@
     if status:
         if settings.options.verbose < settings.os_interface.level['output']:
             print >>sys.stderr,\
-                (''.join (open (settings.os_interface.log_file_name)
-                          .readlines ()[-50:]))
+                ('\n\nTail of ' + settings.os_interface.log_file_name + ':\n'
+                 + '\n'.join (settings.os_interface.read_tail ()))
             log_file = settings.os_interface.log_file_name
         settings.os_interface.error (misc.exception_string (e))
     else:
@@ -230,11 +230,6 @@
     cli_parser = get_cli_parser ()
     (options, files) = cli_parser.parse_args ()
 
-    if not options.platform:
-        print 'error: no platform specified'
-        cli_parser.print_help ()
-        sys.exit (2)
-
     settings = gub.settings.Settings (options)
     urg_add_more_settings_for_gub (settings, options)
 
@@ -242,6 +237,14 @@
         inspect (settings, files)
         sys.exit (0)
 
+    settings.os_interface.stage ('root: ' + settings.system_root + '\n')
+    settings.os_interface.info ('platform: ' + settings.platform + '\n')
+
+    if not files:
+        settings.os_interface.error ('error: nothing to do\n')
+        cli_parser.print_help ()
+        sys.exit (2)
+        
     sys.exit (exceptional_build (settings, files))
 
 if __name__ == '__main__':
--- a/bin/gub-tester	Mon Sep 10 12:55:02 2007 +0200
+++ b/bin/gub-tester	Mon Sep 10 16:44:32 2007 +0200
@@ -19,27 +19,9 @@
 import time
 import dbhash
 
-
 from gub import repository
 from gub import oslog
 
-
-################################################################
-# utils.
-
-def system (c):
-    print c
-    if os.system (c):
-        raise Exception ('barf')
-    
-    
-def read_tail (file, amount=10240):
-    f = open (file)
-    f.seek (0, 2)
-    length = f.tell()
-    f.seek (- min (length, amount), 1)
-    return f.read ()
-
 def canonicalize_string (target):
     canonicalize = re.sub ('[ \t\n]', '_', target)
     canonicalize = re.sub ('[^a-zA-Z0-9-]+', '_', canonicalize)
@@ -47,9 +29,6 @@
 
 main_log = None
 
-################################################################
-#
-
 def result_message (parts, subject='') :
     """Concatenate PARTS to a Message object."""
     
@@ -252,11 +231,11 @@
     result = 'unknown'
     attachments = []
 
-    body = read_tail (log, 10240).split ('\n')
+    body = test_log.read_tail ()
     if stat:
         result = 'FAIL'
         attachments = ['error for\n\n\t%s\n\n\n%s' % (target,
-                                               '\n'.join (body[-0:]))]
+                                                      '\n'.join (body))]
         if options.append_diff:
             attachments += [repo.get_diff_from_tag (base_tag)]
     else:
@@ -380,6 +359,10 @@
     return failures
 
 def test_self (options, args):
+    def system (c):
+        print c
+        if os.system (c):
+            raise Exception ('barf')
     self_test_dir = 'test-gub-test.darcs'
     system ('rm -rf %s ' %  self_test_dir)
     system ('mkdir %s ' %  self_test_dir)
--- a/gub/context.py	Mon Sep 10 12:55:02 2007 +0200
+++ b/gub/context.py	Mon Sep 10 16:44:32 2007 +0200
@@ -64,11 +64,11 @@
 
     def __setattr__(self, k, v):
         if (type(v) == type('')
-            and k <> '_substitution_dict' and self._substitution_dict):
+            and k != '_substitution_dict' and self._substitution_dict):
             print 'was already set in'
-            print ''.join(traceback.format_list (self._substitution_assignment_traceback))
+            print ''.join (traceback.format_list (self._substitution_assignment_traceback))
 
-            raise SetAttrTooLate((k, self))
+            raise SetAttrTooLate ((k, self))
 
         self.__dict__[k] = v
         
@@ -79,7 +79,7 @@
             d = d.copy ()
             
         ms = inspect.getmembers (self)
-        vars = dict((k, v) for (k, v) in ms if type (v) == type (''))
+        vars = dict ((k, v) for (k, v) in ms if type (v) == type (''))
 
         member_substs = {}
         for (name, method) in ms:
@@ -125,7 +125,7 @@
                 # if this happens derived classes cannot override settings
                 # from the baseclass.
                 print ' Cannot Context.expand() in __init__()'
-                raise ExpandInInit()
+                raise ExpandInInit ()
             
         d = self._substitution_dict
         if env:
--- a/gub/gubb.py	Mon Sep 10 12:55:02 2007 +0200
+++ b/gub/gubb.py	Mon Sep 10 16:44:32 2007 +0200
@@ -852,7 +852,8 @@
             name, version_tuple, format = misc.split_ball (ball)
         elif name.find ('/') >= 0:
             name = os.path.basename (name)
-        print 'NO SPEC for', name
+        settings.os_interface.error ('NO SPEC for package: %(name)s\n'
+                                     % locals ())
         from new import classobj
         # Direct url build feature
         #   * gub http://ftp.gnu.org/pub/gnu/tar/tar-1.18.tar.gz
--- a/gub/misc.py	Mon Sep 10 12:55:02 2007 +0200
+++ b/gub/misc.py	Mon Sep 10 16:44:32 2007 +0200
@@ -250,6 +250,24 @@
     s = re.sub ('_%\(version\)s', '-%(version)s', s)
     return s
 
+def read_tail (file, size=10240, lines=50, marker=None):
+    '''
+Efficiently read tail of a file, return list of full lines.
+
+Typical used for reading tail of a log file.  Read a maximum of
+SIZE, return a maximum line count of LINES, truncate everything
+before MARKER.
+'''
+    f = open (file)
+    f.seek (0, 2)
+    length = f.tell()
+    f.seek (- min (length, size), 1)
+    s = f.read ()
+    if marker:
+        p = s.find (marker)
+        s = s[p:]
+    return s.split ('\n')[-lines:]
+
 class MethodOverrider:
     """Override a object method with a function defined outside the
 class hierarchy.
--- a/gub/oslog.py	Mon Sep 10 12:55:02 2007 +0200
+++ b/gub/oslog.py	Mon Sep 10 16:44:32 2007 +0200
@@ -31,7 +31,8 @@
         self.dry_run = dry_run
         self.log_file_name = log_file_name
         self.log_file = open (self.log_file_name, 'a')
-        self.log_file.write ('\n\n * Starting build: %s\n' %  now ())
+        self.start_marker = ' * Starting build: %s\n' %  now ()
+        self.log_file.write ('\n\n' + self.start_marker)
         self.fakeroot_cmd = False
 
         # ARRRGH no python doc on Feisty?
@@ -41,6 +42,10 @@
             misc.bind_method (__log, self)
             self.i = self.__log
 
+    def read_tail (self, size=10240, lines=100):
+        return misc.read_tail (self.log_file_name, size, lines,
+                               self.start_marker)
+        
     def fakeroot (self, s):
         self.fakeroot_cmd = s
         
--- a/gub/settings.py	Mon Sep 10 12:55:02 2007 +0200
+++ b/gub/settings.py	Mon Sep 10 16:44:32 2007 +0200
@@ -30,31 +30,60 @@
 }
 
 distros = ('cygwin')
-            
+
+class UnknownPlatform (Exception):
+    pass
+
+def get_platform_from_dir (settings, dir):
+    m = re.match ('.*?([^/]+)(' + settings.root_dir + ')?/*$', dir)
+    if m:
+        return m.group (1)
+    return None
+
 class Settings (context.Context):
     def __init__ (self, options):
         context.Context.__init__ (self)
-        self.platform = options.platform
+
+        # TODO: local-prefix, target-prefix, cross-prefix?
+        self.prefix_dir = '/usr'
+        self.root_dir = '/root'
+        self.cross_dir = '/cross'
 
+        if not options.platform:
+            if options.__dict__.has_key ('root') and options.root:
+                options.platform = get_platform_from_dir (self, options.root)
+                if not options.platform:
+                    self.error ('invalid root: %(root)s, no platform found'
+                                % options)
+                    raise UnknownPlatform (options.root)
+            else:
+                guess = get_platform_from_dir (self, os.getcwd ())
+                if guess in platforms.keys ():
+                    options.platforms = guess
+            
+        if not options.platform:
+            options.platform = 'local'
+
+        self.platform = options.platform
         if self.platform not in platforms.keys ():
-            raise 'unknown platform', self.platform
+            raise UnknownPlatform (self.platform)
 
         GUB_LOCAL_PREFIX = os.environ.get ('GUB_LOCAL_PREFIX')
         
         # config dirs
 
-        # TODO: local-prefix, target-prefix, cross-prefix?
-        self.prefix_dir = '/usr'
-        self.root_dir = '/root'
-        self.cross_dir = '/cross'
         if self.platform == 'local' and GUB_LOCAL_PREFIX:
             self.prefix_dir = ''
 
         # gubdir is top of `installed' gub repository
-        self.gubdir = os.getcwd ()
+        ## self.gubdir = os.getcwd ()
+        self.gubdir = os.path.dirname (os.path.dirname (__file__))
+        if not self.gubdir:
+            self.gubdir = os.getcwd ()
 
         # workdir is top of writable build stuff
-        self.workdir = os.getcwd ()
+        ##self.workdir = os.getcwd ()
+        self.workdir = self.gubdir
         
         # gubdir based: fixed repository layout
         self.patchdir = self.gubdir + '/patches'
@@ -236,10 +265,12 @@
     return p
 
 def as_variables (settings):
+    lst = []
     for k in settings.__dict__.keys ():
         v = settings.__dict__[k]
         if type (v) == type (str ()):
-            print '%(k)s=%(v)s' % locals ()
+            lst.append ('%(k)s=%(v)s' % locals ())
+    return lst
 
 def main ():
     cli_parser = get_cli_parser ()
@@ -248,7 +279,7 @@
         raise 'barf'
         sys.exit (2)
     settings = Settings (options)
-    print as_variables (settings)
+    print '\n'.join (as_variables (settings))
 
 if __name__ == '__main__':
     main ()