first draft for the release-changelog command - still has bugs, mostly checkpointing
[build.git] / module-tools.py
index 70feac3..45cddb9 100755 (executable)
@@ -8,27 +8,53 @@ import time
 from glob import glob
 from optparse import OptionParser
 
-def prompt (question,default=True):
-    if default:
-        question += " [y]/n ? "
-    else:
-        question += " y/[n] ? "
+# e.g. other_choices = [ ('d','iff') , ('g','uess') ] - lowercase 
+def prompt (question,default=True,other_choices=[],allow_outside=False):
+    if not isinstance (other_choices,list):
+        other_choices = [ other_choices ]
+    chars = [ c for (c,rest) in other_choices ]
+
+    choices = []
+    if 'y' not in chars:
+        if default is True: choices.append('[y]')
+        else : choices.append('y')
+    if 'n' not in chars:
+        if default is False: choices.append('[n]')
+        else : choices.append('n')
+
+    for (char,choice) in other_choices:
+        if default == char:
+            choices.append("["+char+"]"+choice)
+        else:
+            choices.append("<"+char+">"+choice)
     try:
-        answer=raw_input(question)
+        answer=raw_input(question + " " + "/".join(choices) + " ? ")
         if not answer:
             return default
-        elif answer[0] in [ 'y','Y']:
-            return True
-        elif answer[0] in [ 'n','N']:
-            return False
-        else:
-            return prompt(question,default)
-    except KeyboardInterrupt:
-        print "Aborted"
-        return False
+        answer=answer[0].lower()
+        if answer == 'y':
+            if 'y' in chars: return 'y'
+            else: return True
+        elif answer == 'n':
+            if 'n' in chars: return 'n'
+            else: return False
+        elif other_choices:
+            for (char,choice) in other_choices:
+                if answer == char:
+                    return char
+            if allow_outside:
+                return answer
+        return prompt(question,default,other_choices)
     except:
         raise
 
+def default_editor():
+    try:
+        editor = os.environ['EDITOR']
+    except:
+        editor = "emacs"
+    return editor
+
 class Command:
     def __init__ (self,command,options):
         self.command=command
@@ -36,18 +62,24 @@ class Command:
         self.tmp="/tmp/command-%d"%os.getpid()
 
     def run (self):
-        if self.options.verbose:
+        if self.options.dry_run:
+            print 'dry_run',self.command
+            return 0
+        if self.options.verbose and self.options.mode not in Main.silent_modes:
             print '+',self.command
             sys.stdout.flush()
         return os.system(self.command)
 
     def run_silent (self):
+        if self.options.dry_run:
+            print 'dry_run',self.command
+            return 0
         if self.options.verbose:
             print '+',self.command,' .. ',
             sys.stdout.flush()
         retcod=os.system(self.command + " &> " + self.tmp)
         if retcod != 0:
-            print "FAILED ! -- output quoted below "
+            print "FAILED ! -- out+err below (command was %s)"%self.command
             os.system("cat " + self.tmp)
             print "FAILED ! -- end of quoted output"
         elif self.options.verbose:
@@ -61,6 +93,9 @@ class Command:
 
     # returns stdout, like bash's $(mycommand)
     def output_of (self,with_stderr=False):
+        if self.options.dry_run:
+            print 'dry_run',self.command
+            return 'dry_run output'
         tmp="/tmp/status-%d"%os.getpid()
         if self.options.debug:
             print '+',self.command,' .. ',
@@ -94,6 +129,7 @@ class Svnpath:
         command="svn status %s"%self.path
         return len(Command(command,self.options).output_of(True)) != 0
 
+# support for tagged module is minimal, and is for the Build class only
 class Module:
 
     svn_magic_line="--This line, and those below, will be ignored--"
@@ -124,7 +160,10 @@ class Module:
 
 
     # for parsing module spec name:branch
-    matcher_branch_spec=re.compile("\A(?P<name>[\w-]+):(?P<branch>[\w\.]+)\Z")
+    matcher_branch_spec=re.compile("\A(?P<name>[\w-]+):(?P<branch>[\w\.-]+)\Z")
+    # special form for tagged module - for Build
+    matcher_tag_spec=re.compile("\A(?P<name>[\w-]+)@(?P<tagname>[\w\.-]+)\Z")
+    # parsing specfiles
     matcher_rpm_define=re.compile("%(define|global)\s+(\S+)\s+(\S*)\s*")
 
     def __init__ (self,module_spec,options):
@@ -134,26 +173,34 @@ class Module:
             self.name=attempt.group('name')
             self.branch=attempt.group('branch')
         else:
-            self.name=module_spec
-            self.branch=None
+            attempt=Module.matcher_tag_spec.match(module_spec)
+            if attempt:
+                self.name=attempt.group('name')
+                self.tagname=attempt.group('tagname')
+            else:
+                self.name=module_spec
 
         self.options=options
-        self.moddir="%s/%s"%(options.workdir,self.name)
+        self.module_dir="%s/%s"%(options.workdir,self.name)
 
     def friendly_name (self):
-        if not self.branch:
-            return self.name
-        else:
+        if hasattr(self,'branch'):
             return "%s:%s"%(self.name,self.branch)
+        elif hasattr(self,'tagname'):
+            return "%s@%s"%(self.name,self.tagname)
+        else:
+            return self.name
 
     def edge_dir (self):
-        if not self.branch:
-            return "%s/trunk"%(self.moddir)
+        if hasattr(self,'branch'):
+            return "%s/branches/%s"%(self.module_dir,self.branch)
+        elif hasattr(self,'tagname'):
+            return "%s/tags/%s"%(self.module_dir,self.tagname)
         else:
-            return "%s/branches/%s"%(self.moddir,self.branch)
+            return "%s/trunk"%(self.module_dir)
 
     def tags_dir (self):
-        return "%s/tags"%(self.moddir)
+        return "%s/tags"%(self.module_dir)
 
     def run (self,command):
         return Command(command,self.options).run()
@@ -161,16 +208,24 @@ class Module:
         return Command(command,self.options).run_fatal()
     def run_prompt (self,message,command):
         if not self.options.verbose:
-            question=message
+            while True:
+                choice=prompt(message,True,('s','how'))
+                if choice is True:
+                    self.run(command)
+                    return
+                elif choice is False:
+                    return
+                else:
+                    print 'About to run:',command
         else:
-            question="Want to run " + command
-        if prompt(question,True):
-            self.run(command)            
+            question=message+" - want to run " + command
+            if prompt(question,True):
+                self.run(command)            
 
     @staticmethod
     def init_homedir (options):
         topdir=options.workdir
-        if options.verbose:
+        if options.verbose and options.mode not in Main.silent_modes:
             print 'Checking for',topdir
         storage="%s/%s"%(topdir,Module.config_storage)
         # sanity check. Either the topdir exists AND we have a config/storage
@@ -208,25 +263,24 @@ that for other purposes than tagging"""%topdir
                 (key,value)=re.compile("^(.+)=(.+)$").match(line).groups()
                 Module.config[key]=value                
             f.close()
-        if options.verbose:
+        if options.verbose and options.mode not in Main.silent_modes:
             print '******** Using config'
             for (key,message,default) in Module.configKeys:
                 print '\t',key,'=',Module.config[key]
 
-    def init_moddir (self):
+    def init_module_dir (self):
         if self.options.verbose:
-            print 'Checking for',self.moddir
-        if not os.path.isdir (self.moddir):
-            self.run_fatal("svn up -N %s"%self.moddir)
-        if not os.path.isdir (self.moddir):
-            print 'Cannot find %s - check module name'%self.moddir
-            sys.exit(1)
+            print 'Checking for',self.module_dir
+        if not os.path.isdir (self.module_dir):
+            self.run_fatal("svn update -N %s"%self.module_dir)
+        if not os.path.isdir (self.module_dir):
+            raise Exception, 'Cannot find %s - check module name'%self.module_dir
 
     def init_subdir (self,fullpath):
         if self.options.verbose:
             print 'Checking for',fullpath
         if not os.path.isdir (fullpath):
-            self.run_fatal("svn up -N %s"%fullpath)
+            self.run_fatal("svn update -N %s"%fullpath)
 
     def revert_subdir (self,fullpath):
         if self.options.fast_checks:
@@ -247,8 +301,10 @@ that for other purposes than tagging"""%topdir
 
     def init_edge_dir (self):
         # if branch, edge_dir is two steps down
-        if self.branch:
-            self.init_subdir("%s/branches"%self.moddir)
+        if hasattr(self,'branch'):
+            self.init_subdir("%s/branches"%self.module_dir)
+        elif hasattr(self,'tagname'):
+            self.init_subdir("%s/tags"%self.module_dir)
         self.init_subdir(self.edge_dir())
 
     def revert_edge_dir (self):
@@ -265,8 +321,7 @@ that for other purposes than tagging"""%topdir
             try:
                 return glob("%s/*.spec"%self.edge_dir())[0]
             except:
-                print 'Cannot guess specfile for module %s'%self.name
-                sys.exit(1)
+                raise Exception, 'Cannot guess specfile for module %s'%self.name
 
     def all_specnames (self):
         return glob("%s/*.spec"%self.edge_dir())
@@ -314,8 +369,10 @@ that for other purposes than tagging"""%topdir
             print '2st pass parsing done, varnames=',varnames,'result=',result
         return result
 
-    def patch_spec_var (self, patch_dict):
+    def patch_spec_var (self, patch_dict,define_missing=False):
         for specfile in self.all_specnames():
+            # record the keys that were changed
+            changed = dict ( [ (x,False) for x in patch_dict.keys() ] )
             newspecfile=specfile+".new"
             if self.options.verbose:
                 print 'Patching',specfile,'for',patch_dict.keys()
@@ -327,9 +384,18 @@ that for other purposes than tagging"""%topdir
                 if attempt:
                     (define,var,value)=attempt.groups()
                     if var in patch_dict.keys():
+                        if self.options.debug:
+                            print 'rewriting %s as %s'%(var,patch_dict[var])
                         new.write('%%%s %s %s\n'%(define,var,patch_dict[var]))
+                        changed[var]=True
                         continue
                 new.write(line)
+            if define_missing:
+                for (key,was_changed) in changed.iteritems():
+                    if not was_changed:
+                        if self.options.debug:
+                            print 'rewriting missing %s as %s'%(key,patch_dict[key])
+                        new.write('\n%%define %s %s\n'%(key,patch_dict[key]))
             spec.close()
             new.close()
             os.rename(newspecfile,specfile)
@@ -337,11 +403,16 @@ that for other purposes than tagging"""%topdir
     def unignored_lines (self, logfile):
         result=[]
         exclude="Tagging module %s"%self.name
+        white_line_matcher = re.compile("\A\s*\Z")
         for logline in file(logfile).readlines():
             if logline.strip() == Module.svn_magic_line:
                 break
-            if logline.find(exclude) < 0:
-                result += [ logline ]
+            if logline.find(exclude) >= 0:
+                continue
+            elif white_line_matcher.match(logline):
+                continue
+            else:
+                result.append(logline.strip()+'\n')
         return result
 
     def insert_changelog (self, logfile, oldtag, newtag):
@@ -356,9 +427,9 @@ that for other purposes than tagging"""%topdir
                 if re.compile('%changelog').match(line):
                     dateformat="* %a %b %d %Y"
                     datepart=time.strftime(dateformat)
-                    logpart="%s <%s> - %s %s"%(Module.config['username'],
+                    logpart="%s <%s> - %s"%(Module.config['username'],
                                                  Module.config['email'],
-                                                 oldtag,newtag)
+                                                 newtag)
                     new.write(datepart+" "+logpart+"\n")
                     for logline in self.unignored_lines(logfile):
                         new.write("- " + logline)
@@ -376,10 +447,12 @@ that for other purposes than tagging"""%topdir
         return "%s/%s"%(Module.config['svnpath'],self.name)
 
     def edge_url (self):
-        if not self.branch:
-            return "%s/trunk"%(self.mod_url())
-        else:
+        if hasattr(self,'branch'):
             return "%s/branches/%s"%(self.mod_url(),self.branch)
+        elif hasattr(self,'tagname'):
+            return "%s/tags/%s"%(self.mod_url(),self.tagname)
+        else:
+            return "%s/trunk"%(self.mod_url())
 
     def tag_name (self, spec_dict):
         try:
@@ -388,8 +461,7 @@ that for other purposes than tagging"""%topdir
                 spec_dict[self.module_version_varname],
                 spec_dict[self.module_taglevel_varname])
         except KeyError,err:
-            print 'Something is wrong with module %s, cannot determine %s - exiting'%(self.name,err)
-            sys.exit(1)
+            raise Exception, 'Something is wrong with module %s, cannot determine %s - exiting'%(self.name,err)
 
     def tag_url (self, spec_dict):
         return "%s/tags/%s"%(self.mod_url(),self.tag_name(spec_dict))
@@ -404,8 +476,8 @@ that for other purposes than tagging"""%topdir
             if self.options.verbose: print 'exists - OK'
         else:
             if self.options.verbose: print 'KO'
-            print 'Could not find %s URL %s'%(message,url)
-            sys.exit(1)
+            raise Exception, 'Could not find %s URL %s'%(message,url)
+
     def check_svnpath_not_exists (self, url, message):
         if self.options.fast_checks:
             return
@@ -416,13 +488,13 @@ that for other purposes than tagging"""%topdir
             if self.options.verbose: print 'does not exist - OK'
         else:
             if self.options.verbose: print 'KO'
-            print '%s URL %s already exists - exiting'%(message,url)
-            sys.exit(1)
+            raise Exception, '%s URL %s already exists - exiting'%(message,url)
 
     # locate specfile, parse it, check it and show values
+
 ##############################
     def do_version (self):
-        self.init_moddir()
+        self.init_module_dir()
         self.init_edge_dir()
         self.revert_edge_dir()
         self.update_edge_dir()
@@ -432,25 +504,70 @@ that for other purposes than tagging"""%topdir
                 print 'Could not find %%define for %s'%varname
                 return
             else:
-                print varname+":",spec_dict[varname]
-        print 'edge url',self.edge_url()
-        print 'latest tag url',self.tag_url(spec_dict)
+                print "%-16s %s"%(varname,spec_dict[varname])
+        if self.options.show_urls:
+            print "%-16s %s"%('edge url',self.edge_url())
+            print "%-16s %s"%('latest tag url',self.tag_url(spec_dict))
         if self.options.verbose:
-            print 'main specfile:',self.main_specname()
-            print 'specfiles:',self.all_specnames()
+            print "%-16s %s"%('main specfile:',self.main_specname())
+            print "%-16s %s"%('specfiles:',self.all_specnames())
+
+##############################
+    def do_list (self):
+#        print 'verbose',self.options.verbose
+#        print 'list_tags',self.options.list_tags
+#        print 'list_branches',self.options.list_branches
+#        print 'all_modules',self.options.all_modules
+        
+        (verbose,branches,pattern,exact) = (self.options.verbose,self.options.list_branches,
+                                            self.options.list_pattern,self.options.list_exact)
+
+        extra_command=""
+        extra_message=""
+        if hasattr(self,'branch'):
+            pattern=self.branch
+        if pattern or exact:
+            if exact:
+                if verbose: grep="%s/$"%exact
+                else: grep="^%s$"%exact
+            else:
+                grep=pattern
+            extra_command=" | grep %s"%grep
+            extra_message=" matching %s"%grep
+
+        if not branches:
+            message="==================== tags for %s"%self.friendly_name()
+            command="svn list "
+            if verbose: command+="--verbose "
+            command += "%s/tags"%self.mod_url()
+            command += extra_command
+            message += extra_message
+            if verbose: print message
+            self.run(command)
 
-    init_warning="""WARNING
-The module-init function has the following limitations
+        else:
+            message="==================== branches for %s"%self.friendly_name()
+            command="svn list "
+            if verbose: command+="--verbose "
+            command += "%s/branches"%self.mod_url()
+            command += extra_command
+            message += extra_message
+            if verbose: print message
+            self.run(command)
+
+##############################
+    sync_warning="""*** WARNING
+The module-sync function has the following limitations
 * it does not handle changelogs
 * it does not scan the -tags*.mk files to adopt the new tags"""
-##############################
-    def do_init(self):
+
+    def do_sync(self):
         if self.options.verbose:
-            print Module.init_warning
+            print Module.sync_warning
             if not prompt('Want to proceed anyway'):
                 return
 
-        self.init_moddir()
+        self.init_module_dir()
         self.init_edge_dir()
         self.revert_edge_dir()
         self.update_edge_dir()
@@ -470,8 +587,8 @@ The module-init function has the following limitations
                         "svn copy %s %s %s"%(svnopt,edge_url,tag_url))
 
 ##############################
-    def do_diff (self):
-        self.init_moddir()
+    def do_diff (self,compute_only=False):
+        self.init_module_dir()
         self.init_edge_dir()
         self.revert_edge_dir()
         self.update_edge_dir()
@@ -482,7 +599,15 @@ The module-init function has the following limitations
         tag_url=self.tag_url(spec_dict)
         self.check_svnpath_exists(edge_url,"edge track")
         self.check_svnpath_exists(tag_url,"latest tag")
-        diff_output = Command("svn diff %s %s"%(tag_url,edge_url),self.options).output_of()
+        command="svn diff %s %s"%(tag_url,edge_url)
+        if compute_only:
+            if self.options.verbose:
+                print 'Getting diff with %s'%command
+        diff_output = Command(command,self.options).output_of()
+        # if used as a utility
+        if compute_only:
+            return (spec_dict,edge_url,tag_url,diff_output)
+        # otherwise print the result
         if self.options.list:
             if diff_output:
                 print self.name
@@ -494,25 +619,50 @@ The module-init function has the following limitations
                 print diff_output
 
 ##############################
-    def patch_tags_file (self, tagsfile, oldname, newname):
+    # using fine_grain means replacing only those instances that currently refer to this tag
+    # otherwise, <module>-SVNPATH is replaced unconditionnally
+    def patch_tags_file (self, tagsfile, oldname, newname,fine_grain=True):
         newtagsfile=tagsfile+".new"
-        if self.options.verbose:
-            print 'Replacing %s into %s in %s'%(oldname,newname,tagsfile)
         tags=open (tagsfile)
         new=open(newtagsfile,"w")
-        matcher=re.compile("^(.*)%s(.*)"%oldname)
-        for line in tags.readlines():
-            if not matcher.match(line):
-                new.write(line)
-            else:
-                (begin,end)=matcher.match(line).groups()
-                new.write(begin+newname+end+"\n")
+
+        matches=0
+        # fine-grain : replace those lines that refer to oldname
+        if fine_grain:
+            if self.options.verbose:
+                print 'Replacing %s into %s\n\tin %s .. '%(oldname,newname,tagsfile),
+            matcher=re.compile("^(.*)%s(.*)"%oldname)
+            for line in tags.readlines():
+                if not matcher.match(line):
+                    new.write(line)
+                else:
+                    (begin,end)=matcher.match(line).groups()
+                    new.write(begin+newname+end+"\n")
+                    matches += 1
+        # brute-force : change uncommented lines that define <module>-SVNPATH
+        else:
+            if self.options.verbose:
+                print 'Setting %s-SVNPATH for using %s\n\tin %s .. '%(self.name,newname,tagsfile),
+            pattern="\A\s*%s-SVNPATH\s*(=|:=)\s*(?P<url_main>[^\s]+)/%s/[^\s]+"\
+                                          %(self.name,self.name)
+            matcher_module=re.compile(pattern)
+            for line in tags.readlines():
+                attempt=matcher_module.match(line)
+                if attempt:
+                    svnpath="%s-SVNPATH"%self.name
+                    replacement = "%-32s:= %s/%s/tags/%s\n"%(svnpath,attempt.group('url_main'),self.name,newname)
+                    new.write(replacement)
+                    matches += 1
+                else:
+                    new.write(line)
         tags.close()
         new.close()
         os.rename(newtagsfile,tagsfile)
+        if self.options.verbose: print "%d changes"%matches
+        return matches
 
     def do_tag (self):
-        self.init_moddir()
+        self.init_module_dir()
         self.init_edge_dir()
         self.revert_edge_dir()
         self.update_edge_dir()
@@ -544,7 +694,7 @@ The module-init function has the following limitations
         diff_output=Command("svn diff %s %s"%(old_tag_url,edge_url),
                             self.options).output_of()
         if len(diff_output) == 0:
-            if not prompt ("No difference in trunk for module %s, want to tag anyway"%self.name,False):
+            if not prompt ("No pending difference in module %s, want to tag anyway"%self.name,False):
                 return
 
         # side effect in trunk's specfile
@@ -578,21 +728,56 @@ Please write a changelog for this new tag in the section above
             buildname=Module.config['build']
         except:
             buildname="build"
+        if self.options.build_branch:
+            buildname+=":"+self.options.build_branch
         build = Module(buildname,self.options)
-        build.init_moddir()
+        build.init_module_dir()
         build.init_edge_dir()
         build.revert_edge_dir()
         build.update_edge_dir()
         
-        for tagsfile in glob(build.edge_dir()+"/*-tags*.mk"):
-            if prompt("Want to adopt new tag in %s"%tagsfile):
-                self.patch_tags_file(tagsfile,old_tag_name,new_tag_name)
+        tagsfiles=glob(build.edge_dir()+"/*-tags*.mk")
+        tagsdict=dict( [ (x,'todo') for x in tagsfiles ] )
+        default_answer = 'y'
+        while True:
+            for (tagsfile,status) in tagsdict.iteritems():
+                basename=os.path.basename(tagsfile)
+                print ".................... Dealing with %s"%basename
+                while tagsdict[tagsfile] == 'todo' :
+                    choice = prompt ("insert %s in %s    "%(new_tag_name,basename),default_answer,
+                                     [ ('y','es'), ('n', 'ext'), ('f','orce'), 
+                                       ('d','iff'), ('r','evert'), ('h','elp') ] ,
+                                     allow_outside=True)
+                    if choice == 'y':
+                        self.patch_tags_file(tagsfile,old_tag_name,new_tag_name,fine_grain=True)
+                    elif choice == 'n':
+                        print 'Done with %s'%os.path.basename(tagsfile)
+                        tagsdict[tagsfile]='done'
+                    elif choice == 'f':
+                        self.patch_tags_file(tagsfile,old_tag_name,new_tag_name,fine_grain=False)
+                    elif choice == 'd':
+                        self.run("svn diff %s"%tagsfile)
+                    elif choice == 'r':
+                        self.run("svn revert %s"%tagsfile)
+                    else:
+                        name=self.name
+                        print """y: change %(name)s-SVNPATH only if it currently refers to %(old_tag_name)s
+f: unconditionnally change any line setting %(name)s-SVNPATH to using %(new_tag_name)s
+d: show current diff for this tag file
+r: revert that tag file
+n: move to next file"""%locals()
+
+            if prompt("Want to review changes on tags files",False):
+                tagsdict = dict ( [ (x, 'todo') for tagsfile in tagsfiles ] )
+                default_answer='d'
+            else:
+                break
 
         paths=""
         paths += self.edge_dir() + " "
         paths += build.edge_dir() + " "
-        self.run_prompt("Check","svn diff " + paths)
-        self.run_prompt("Commit","svn commit --file %s %s"%(changelog,paths))
+        self.run_prompt("Review module and build","svn diff " + paths)
+        self.run_prompt("Commit module and build","svn commit --file %s %s"%(changelog,paths))
         self.run_prompt("Create tag","svn copy --file %s %s %s"%(changelog,edge_url,new_tag_url))
 
         if self.options.debug:
@@ -603,162 +788,388 @@ Please write a changelog for this new tag in the section above
 ##############################
     def do_branch (self):
 
-        print 'module-branch is experimental - exiting'
-        sys.exit(1)
-
+        # save self.branch if any, as a hint for the new branch 
+        # do this before anything else and restore .branch to None, 
+        # as this is part of the class's logic
+        new_trunk_name=None
         if self.branch:
-            print 'Cannot create a branch from another branch - exiting'
-            sys.exit(1)
-        self.init_moddir()
-        
-        # xxx - tmp
-        import readline
-        answer = raw_input ("enter tag name [trunk]").strip()
-        if answer == "" or answer == "trunk":
-            ref="/trunk"
-            from_trunk=True
-        else:
-            ref="/tags/%s-%s"%(self.name,answer)
-            from_trunk=False
+            new_trunk_name=self.branch
+            self.branch=None
 
-        ref_url = "%s/%s"%(self.mod_url(),ref)
-        self.check_svnpath_exists (ref_url,"branch creation point")
-        print "Using starting point %s"%ref_url
-        
-        spec=self.main_specname()
-        if not from_trunk:
-            self.init_subdir(self.tags_dir())
-            workdir="%s/%s"%(self.moddir,ref)
-        else:
-            workdir=self.edge_dir()
+        # compute diff - a way to initialize the whole stuff
+        # do_diff already does edge_dir initialization
+        # and it checks that edge_url and tag_url exist as well
+        (spec_dict,edge_url,tag_url,diff_listing) = self.do_diff(compute_only=True)
 
-        self.init_subdir(workdir)
-        self.revert_subdir(workdir)
-        self.update_subdir(workdir)
+        # the version name in the trunk becomes the new branch name
+        branch_name = spec_dict[self.module_version_varname]
 
-        print 'got spec',spec
-        if not os.path.isfile(spec):
-            print 'cannot find spec'
-        
-        # read version & taglevel from the origin specfile
-        print 'parsing',spec
-        origin=self.spec_dict()
-        self.show_dict(origin)
-
-        default_branch=self.options.new_version
-        if not default_branch:
-#            try:
-                match=re.compile("\A(?P<main>.*[\.-_])(?P<subid>[0-9]+)\Z").match(origin['version'])
-                new_subid=int(match.group('subid'))+1
-                default_branch="%s%d"%(match.group('main'),new_subid)
-#            except:
-#                default_branch="not found"
-        new_branch_name=raw_input("Enter branch name [%s] "%default_branch) or default_branch
+        # figure new branch name (the one for the trunk) if not provided on the command line
+        if not new_trunk_name:
+            # heuristic is to assume 'version' is a dot-separated name
+            # we isolate the rightmost part and try incrementing it by 1
+            version=spec_dict[self.module_version_varname]
+            try:
+                m=re.compile("\A(?P<leftpart>.+)\.(?P<rightmost>[^\.]+)\Z")
+                (leftpart,rightmost)=m.match(version).groups()
+                incremented = int(rightmost)+1
+                new_trunk_name="%s.%d"%(leftpart,incremented)
+            except:
+                raise Exception, 'Cannot figure next branch name from %s - exiting'%version
+
+        # record starting point tagname
+        latest_tag_name = self.tag_name(spec_dict)
+
+        print "**********"
+        print "Using starting point %s (%s)"%(tag_url,latest_tag_name)
+        print "Creating branch %s  &  moving trunk to %s"%(branch_name,new_trunk_name)
+        print "**********"
+
+        # print warning if pending diffs
+        if diff_listing:
+            print """*** WARNING : Module %s has pending diffs on its trunk
+It is safe to proceed, but please note that branch %s
+will be based on latest tag %s and *not* on the current trunk"""%(self.name,branch_name,latest_tag_name)
+            while True:
+                answer = prompt ('Are you sure you want to proceed with branching',True,('d','iff'))
+                if answer is True:
+                    break
+                elif answer is False:
+                    raise Exception,"User quit"
+                elif answer == 'd':
+                    print '<<<< %s'%tag_url
+                    print '>>>> %s'%edge_url
+                    print diff_listing
+
+        branch_url = "%s/%s/branches/%s"%(Module.config['svnpath'],self.name,branch_name)
+        self.check_svnpath_not_exists (branch_url,"new branch")
         
-        new_branch_url="%s/branches/%s"%(self.mod_url(),new_branch_name)
-        self.check_svnpath_not_exists(new_branch_url,"new branch")
-        print new_branch_name
+        # patching trunk
+        spec_dict[self.module_version_varname]=new_trunk_name
+        spec_dict[self.module_taglevel_varname]='0'
+        # remember this in the trunk for easy location of the current branch
+        spec_dict['module_current_branch']=branch_name
+        self.patch_spec_var(spec_dict,True)
         
+        # create commit log file
+        tmp="/tmp/branching-%d"%os.getpid()
+        f=open(tmp,"w")
+        f.write("Branch %s for module %s created (as new trunk) from tag %s\n"%(new_trunk_name,self.name,latest_tag_name))
+        f.close()
+
+        # we're done, let's commit the stuff
+        command="svn diff %s"%self.edge_dir()
+        self.run_prompt("Review changes in trunk",command)
+        command="svn copy --file %s %s %s"%(tmp,self.edge_url(),branch_url)
+        self.run_prompt("Create branch",command)
+        command="svn commit --file %s %s"%(tmp,self.edge_dir())
+        self.run_prompt("Commit trunk",command)
+        new_tag_url=self.tag_url(spec_dict)
+        command="svn copy --file %s %s %s"%(tmp,self.edge_url(),new_tag_url)
+        self.run_prompt("Create initial tag in trunk",command)
+        os.unlink(tmp)
 
 ##############################
-usage="""Usage: %prog options module_desc [ .. module_desc ]
-Purpose:
-  manage subversion tags and specfile
-  requires the specfile to define *version* and *taglevel*
+class PackageSpec:
+
+    def __init__(self, package, module, svnpath, spec):
+        self.package=package
+        self.module=module
+        self.svnpath=svnpath
+        self.spec=spec
+        self.specpath="%s/%s"%(svnpath,spec)
+
+    def show(self):
+        print 'package=',self.package,'module=',self.module,'svnpath=',self.svnpath,'spec=',self.spec
+
+class Build (Module):
+    
+    def __init__ (self, buildtag,options):
+        self.buildtag=buildtag
+        Module.__init__(self,"build@%s"%buildtag,options)
+
+    # we cannot get build's svnpath as for other packages as we'd get something in svn+ssh
+    # xxx quick & dirty
+    def get_svnpath (self):
+        self.svnpath="http://svn.planet-lab.org/svn/build/tags/%s"%self.buildtag
+
+    def get_packages (self,distrotag):
+        # mhh: remove -tag* from distrotags to get distro
+        n=distrotag.find('-tag')
+        if n>0:
+            distro=distrotag[:n]
+        else:
+            distro='planetlab'
+        result={}
+        make_options="-C %s stage1=true DISTRO=%s PLDISTROTAGS=%s 2> /dev/null"%(self.edge_dir(),distro,distrotag)
+        command="make %s packages"%make_options
+        make_packages=Command(command,self.options).output_of()
+        pkg_line=re.compile("\Apackage=(?P<package>[^\s]+)\s+ref_module=(?P<module>[^\s]+)\s.*\Z")
+        for line in make_packages.split("\n"):
+            if not line:
+                continue
+            attempt=pkg_line.match(line)
+            if line and not attempt:
+                print "====="
+                print "WARNING: line not understood from make packages"
+                print "in dir %s"%self.edge_dir
+                print "with options",make_options
+                print 'line=',line
+                print "====="
+            else:
+                (package,module) = (attempt.group('package'),attempt.group('module')) 
+                command="make %s +%s-SVNPATH"%(make_options,module)
+                svnpath=Command(command,self.options).output_of().strip()
+                command="make %s +%s-SPEC"%(make_options,package)
+                spec=Command(command,self.options).output_of().strip()
+                result[package]=PackageSpec(package,module,svnpath,spec)
+        return result
+
+    def get_distrotags (self):
+        return [os.path.basename(p) for p in glob("%s/*tags*mk"%self.edge_dir())]
+
+
+class Release:
+
+    discard_matcher=re.compile("\A(\+\+\+|---).*")
+
+    # t1 is the most recent build tag, t2 should be older
+    @staticmethod
+    def do_changelog (t1,t2,options):
+        print "----"
+        print "= build tag %s to %s = #tag-%s-to-%s"%(t2,t1,t2,t1)
+        (b1,b2) = (Build (t1,options), Build (t2,options))
+        for b in (b1,b2):
+            b.init_module_dir()
+            b.init_edge_dir()
+            b.update_edge_dir()
+            b.get_svnpath()
+        # find out the tags files that are common, unless option was specified
+        if options.distrotags:
+            (d1s,d2s)=([options.distrotags],[options.distrotags])
+        else:
+            d1s=b1.get_distrotags()
+            d2s=b2.get_distrotags()
+        print 'd1s',d1s,'d2s',d2s
+        distrotags = list(set(d1s).intersection(set(d2s)))
+        print 'common distrotags',distrotags
+        distrotags.sort()
+        print 'after sort',distrotags
+        first_distrotag=True
+        for distrotag in distrotags:
+            if first_distrotag:
+                first_distrotag=False
+            else:
+                print '----'
+            print '== distro %s (%s to %s) == #distro-%s-%s-to-%s'%(distrotag,t2,t1,distrotag,t2,t1)
+            print ' * from %s/%s'%(b2.svnpath,distrotag)
+            print ' * to %s/%s'%(b1.svnpath,distrotag)
+            p1s=b1.get_packages(distrotag)
+            p1names=set(p1s.keys())
+            p2s=b2.get_packages(distrotag)
+            p2names=set(p2s.keys())
+            in1not2 = list(p1names-p2names)
+            in2not1 = list(p2names-p1names)
+            inboth = list(p1names.intersection(p2names))
+            in1not2.sort()
+            in2not1.sort()
+            inboth.sort()
+            for name in in1not2:
+                print '=== %s : new package %s -- appeared in %s === #pkg-%s-%s'%(distrotag,name,t1,name,t1)
+                obj=p1s[name]
+                print ' * svn link: %s'%obj.svnpath
+                print ' * spec: %s'%obj.specpath
+            for name in in2not1:
+                print '=== %s : package %s -- deprecated, last occurrence in %s === #pkg-%s-%s'%(distrotag,name,t2,name,t1)
+                obj=p2s[name]
+                print ' * svn link: %s'%obj.svnpath
+                print ' * spec: %s'%obj.specpath
+            for name in inboth:
+                (o1,o2)=(p1s[name],p2s[name])
+                if o1.specpath == o2.specpath:
+                    continue
+                command="svn diff %s %s"%(o2.specpath,o1.specpath)
+                specdiff=Command(command,options).output_of()
+                if not specdiff:
+                    continue
+                print '=== %s - %s to %s : package %s === #pkg-%s-%s'%(distrotag,t2,t1,name,name,t1)
+                print ' * from %s '%o2.specpath
+                print ' * to %s '%o1.specpath
+                print '{{{'
+                for line in specdiff.split('\n'):
+                    if not line:
+                        continue
+                    if Release.discard_matcher.match(line):
+                        continue
+                    if line[0] in ['@']:
+                        print '----------'
+                    elif line[0] in ['+','-']:
+                        print line
+                print '}}}'
+
+##############################
+class Main:
+
+    module_usage="""Usage: %prog [options] module_desc [ .. module_desc ]
+module-tools : a set of tools to manage subversion tags and specfile
+  requires the specfile to either
+  * define *version* and *taglevel*
   OR alternatively 
-  redirection variables module_version_varname / module_taglevel_varname
+  * define redirection variables module_version_varname / module_taglevel_varname
 Trunk:
   by default, the trunk of modules is taken into account
   in this case, just mention the module name as <module_desc>
 Branches:
   if you wish to work on a branch rather than on the trunk, 
-  you can use the following syntax for <module_desc>
-  Mom:2.1
-      works on Mom/branches/2.1 
+  you can use something like e.g. Mom:2.1 as <module_desc>
+"""
+    release_usage="""Usage: %prog [options] tag1 .. tagn
+  Extract release notes from the changes in specfiles between several build tags, latest first
 """
-# unsupported yet
-#"""
-#  branch:Mom
-#      the branch_id is deduced from the current *version* in the trunk's specfile
-#      e.g. if Mom/trunk/Mom.spec specifies %define version 2.3, then this script
-#      would use Mom/branches/2.2
-#      if if stated %define version 3.0, then the script fails
-#"""
-
-functions={ 
-    'diff' : "show difference between trunk and latest tag",
-    'tag'  : """increment taglevel in specfile, insert changelog in specfile,
-                create new tag and and adopt it in build/*-tags*.mk""",
-    'init' : "create initial tag",
-    'version' : "only check specfile and print out details",
-    'branch' : """create a branch for this module. 
-                either from trunk, or from a tag""",
-}
-
-def main():
-
-    mode=None
-    for function in functions.keys():
-        if sys.argv[0].find(function) >= 0:
-            mode = function
-            break
-    if not mode:
-        print "Unsupported command",sys.argv[0]
-        sys.exit(1)
-
-    global usage
-    usage += "module-%s.py : %s"%(mode,functions[mode])
-    all_modules=os.path.dirname(sys.argv[0])+"/modules.list"
-
-    parser=OptionParser(usage=usage,version=subversion_id)
-    parser.add_option("-a","--all",action="store_true",dest="all_modules",default=False,
-                      help="run on all modules as found in %s"%all_modules)
-    parser.add_option("-f","--fast-checks",action="store_true",dest="fast_checks",default=False,
-                      help="skip safety checks, such as svn updates -- use with care")
-    if mode == "tag" or mode == 'branch':
-        parser.add_option("-s","--set-version",action="store",dest="new_version",default=None,
-                          help="set new version and reset taglevel to 0")
-    if mode == "tag" :
-        parser.add_option("-c","--no-changelog", action="store_false", dest="changelog", default=True,
-                          help="do not update changelog section in specfile when tagging")
-    if mode == "tag" or mode == "init" :
-        parser.add_option("-e","--editor", action="store", dest="editor", default="emacs",
-                          help="specify editor")
-    if mode == "init" :
-        parser.add_option("-m","--message", action="store", dest="message", default=None,
-                          help="specify log message")
-    if mode == "diff" :
-        parser.add_option("-o","--only", action="store_true", dest="only", default=False,
-                          help="report diff only for modules that exhibit differences")
-    if mode == "diff" :
-        parser.add_option("-l","--list", action="store_true", dest="list", default=False,
-                          help="just list modules that exhibit differences")
-    parser.add_option("-w","--workdir", action="store", dest="workdir", 
-                      default="%s/%s"%(os.getenv("HOME"),"modules"),
-                      help="""name for dedicated working dir - defaults to ~/modules
+    common_usage="""More help:
+  see http://svn.planet-lab.org/wiki/ModuleTools"""
+
+    modes={ 
+        'list' : "displays a list of available tags or branches",
+        'version' : "check latest specfile and print out details",
+        'diff' : "show difference between module (trunk or branch) and latest tag",
+        'tag'  : """increment taglevel in specfile, insert changelog in specfile,
+                create new tag and and monitor its adoption in build/*-tags*.mk""",
+        'branch' : """create a branch for this module, from the latest tag on the trunk, 
+                  and change trunk's version number to reflect the new branch name;
+                  you can specify the new branch name by using module:branch""",
+        'sync' : """create a tag from the module
+                this is a last resort option, mostly for repairs""",
+        'changelog' : """extract changelog between build tags
+                expected arguments are a list of tags""",
+        }
+
+    silent_modes = ['list']
+    release_modes = ['changelog']
+
+    def run(self):
+
+        mode=None
+        for function in Main.modes.keys():
+            if sys.argv[0].find(function) >= 0:
+                mode = function
+                break
+        if not mode:
+            print "Unsupported command",sys.argv[0]
+            print "Supported commands:" + Modes.modes.keys.join(" ")
+            sys.exit(1)
+
+        if mode not in Main.release_modes:
+            usage = Main.module_usage
+            usage += Main.common_usage
+            usage += "\nmodule-%s : %s"%(mode,Main.modes[mode])
+        else:
+            usage = Main.release_usage
+            usage += Main.common_usage
+
+        parser=OptionParser(usage=usage,version=subversion_id)
+        
+        if mode == 'list':
+            parser.add_option("-b","--branches",action="store_true",dest="list_branches",default=False,
+                              help="list branches")
+            parser.add_option("-t","--tags",action="store_false",dest="list_branches",
+                              help="list tags")
+            parser.add_option("-m","--match",action="store",dest="list_pattern",default=None,
+                               help="grep pattern for filtering output")
+            parser.add_option("-x","--exact-match",action="store",dest="list_exact",default=None,
+                               help="exact grep pattern for filtering output")
+        if mode == "tag" or mode == 'branch':
+            parser.add_option("-s","--set-version",action="store",dest="new_version",default=None,
+                              help="set new version and reset taglevel to 0")
+        if mode == "tag" :
+            parser.add_option("-c","--no-changelog", action="store_false", dest="changelog", default=True,
+                              help="do not update changelog section in specfile when tagging")
+            parser.add_option("-b","--build-branch", action="store", dest="build_branch", default=None,
+                              help="specify a build branch; used for locating the *tags*.mk files where adoption is to take place")
+        if mode == "tag" or mode == "sync" :
+            parser.add_option("-e","--editor", action="store", dest="editor", default=default_editor(),
+                              help="specify editor")
+        if mode == "sync" :
+            parser.add_option("-m","--message", action="store", dest="message", default=None,
+                              help="specify log message")
+        if mode == "diff" :
+            parser.add_option("-o","--only", action="store_true", dest="only", default=False,
+                              help="report diff only for modules that exhibit differences")
+        if mode == "diff" :
+            parser.add_option("-l","--list", action="store_true", dest="list", default=False,
+                              help="just list modules that exhibit differences")
+
+        if mode  == 'version':
+            parser.add_option("-u","--url", action="store_true", dest="show_urls", default=False,
+                              help="display URLs")
+            
+        if mode not in Main.release_modes:
+            all_modules=os.path.dirname(sys.argv[0])+"/modules.list"
+            parser.add_option("-a","--all",action="store_true",dest="all_modules",default=False,
+                              help="run on all modules as found in %s"%all_modules)
+        else:
+            parser.add_option("-n","--dry-run",action="store_true",dest="dry_run",default=False,
+                              help="dry run - shell commands are only displayed")
+            parser.add_option("-t","--distrotags",action="store",dest="distrotags",default=None,
+                              help="specify a distro-tags file, e.g. onelab-tags-4.2.mk")
+
+        parser.add_option("-w","--workdir", action="store", dest="workdir", 
+                          default="%s/%s"%(os.getenv("HOME"),"modules"),
+                          help="""name for dedicated working dir - defaults to ~/modules
 ** THIS MUST NOT ** be your usual working directory""")
-    parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=True, 
-                      help="run in verbose mode")
-    parser.add_option("-q","--quiet", action="store_false", dest="verbose", 
-                      help="run in quiet (non-verbose) mode")
-    parser.add_option("-d","--debug", action="store_true", dest="debug", default=False, 
-                      help="debug mode - mostly more verbose")
-    (options, args) = parser.parse_args()
-
-    if len(args) == 0:
-        if options.all_modules:
-            args=Command("grep -v '#' %s"%all_modules,options).output_of().split()
+        parser.add_option("-f","--fast-checks",action="store_true",dest="fast_checks",default=False,
+                          help="skip safety checks, such as svn updates -- use with care")
+
+        # default verbosity depending on function - temp
+        verbose_modes= ['tag','sync']
+        
+        if mode not in verbose_modes:
+            parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False, 
+                              help="run in verbose mode")
         else:
-            parser.print_help()
-            sys.exit(1)
-    Module.init_homedir(options)
-    for modname in args:
-        module=Module(modname,options)
-        print '========================================',module.friendly_name()
-        # call the method called do_<mode>
-        method=Module.__dict__["do_%s"%mode]
-        method(module)
-
-# basically, we exit if anything goes wrong
+            parser.add_parser("-q","--quiet", action="store_false", dest="verbose", default=True,
+                              help="run in quiet (non-verbose) mode")
+#        parser.add_option("-d","--debug", action="store_true", dest="debug", default=False, 
+#                          help="debug mode - mostly more verbose")
+        (options, args) = parser.parse_args()
+        options.mode=mode
+        if not hasattr(options,'dry_run'):
+            options.dry_run=False
+        options.debug=False
+
+        ########## release-*
+        if mode in Main.release_modes :
+            ########## changelog
+            if len(args) <= 1:
+                parser.print_help()
+                sys.exit(1)
+            Module.init_homedir(options)
+            for n in range(len(args)-1):
+                [t_new,t_old]=args[n:n+2]
+                Release.do_changelog (t_new,t_old,options)
+        else:
+            ########## module-*
+            if len(args) == 0:
+                if options.all_modules:
+                    args=Command("grep -v '#' %s"%all_modules,options).output_of().split()
+                else:
+                    parser.print_help()
+                    sys.exit(1)
+            Module.init_homedir(options)
+            for modname in args:
+                module=Module(modname,options)
+                if len(args)>1 and mode not in Main.silent_modes:
+                    print '========================================',module.friendly_name()
+                # call the method called do_<mode>
+                method=Module.__dict__["do_%s"%mode]
+                try:
+                    method(module)
+                except Exception,e:
+                    print 'Skipping failed %s: '%modname,e
+
+####################
 if __name__ == "__main__" :
-    main()
+    try:
+        Main().run()
+    except KeyboardInterrupt:
+        print '\nBye'