review tagsfiles in alphabetical order
[build.git] / module-tools.py
1 #!/usr/bin/python -u
2
3 subversion_id = "$Id$"
4
5 import sys, os, os.path
6 import re
7 import time
8 from glob import glob
9 from optparse import OptionParser
10
11 # e.g. other_choices = [ ('d','iff') , ('g','uess') ] - lowercase 
12 def prompt (question,default=True,other_choices=[],allow_outside=False):
13     if not isinstance (other_choices,list):
14         other_choices = [ other_choices ]
15     chars = [ c for (c,rest) in other_choices ]
16
17     choices = []
18     if 'y' not in chars:
19         if default is True: choices.append('[y]')
20         else : choices.append('y')
21     if 'n' not in chars:
22         if default is False: choices.append('[n]')
23         else : choices.append('n')
24
25     for (char,choice) in other_choices:
26         if default == char:
27             choices.append("["+char+"]"+choice)
28         else:
29             choices.append("<"+char+">"+choice)
30     try:
31         answer=raw_input(question + " " + "/".join(choices) + " ? ")
32         if not answer:
33             return default
34         answer=answer[0].lower()
35         if answer == 'y':
36             if 'y' in chars: return 'y'
37             else: return True
38         elif answer == 'n':
39             if 'n' in chars: return 'n'
40             else: return False
41         elif other_choices:
42             for (char,choice) in other_choices:
43                 if answer == char:
44                     return char
45             if allow_outside:
46                 return answer
47         return prompt(question,default,other_choices)
48     except:
49         raise
50
51 def default_editor():
52     try:
53         editor = os.environ['EDITOR']
54     except:
55         editor = "emacs"
56     return editor
57
58 ### fold long lines
59 fold_length=132
60
61 def print_fold (line):
62     while len(line) >= fold_length:
63         print line[:fold_length],'\\'
64         line=line[fold_length:]
65     print line
66
67 class Command:
68     def __init__ (self,command,options):
69         self.command=command
70         self.options=options
71         self.tmp="/tmp/command-%d"%os.getpid()
72
73     def run (self):
74         if self.options.dry_run:
75             print 'dry_run',self.command
76             return 0
77         if self.options.verbose and self.options.mode not in Main.silent_modes:
78             print '+',self.command
79             sys.stdout.flush()
80         return os.system(self.command)
81
82     def run_silent (self):
83         if self.options.dry_run:
84             print 'dry_run',self.command
85             return 0
86         if self.options.verbose:
87             print '+',self.command,' .. ',
88             sys.stdout.flush()
89         retcod=os.system(self.command + " &> " + self.tmp)
90         if retcod != 0:
91             print "FAILED ! -- out+err below (command was %s)"%self.command
92             os.system("cat " + self.tmp)
93             print "FAILED ! -- end of quoted output"
94         elif self.options.verbose:
95             print "OK"
96         os.unlink(self.tmp)
97         return retcod
98
99     def run_fatal(self):
100         if self.run_silent() !=0:
101             raise Exception,"Command %s failed"%self.command
102
103     # returns stdout, like bash's $(mycommand)
104     def output_of (self,with_stderr=False):
105         if self.options.dry_run:
106             print 'dry_run',self.command
107             return 'dry_run output'
108         tmp="/tmp/status-%d"%os.getpid()
109         if self.options.debug:
110             print '+',self.command,' .. ',
111             sys.stdout.flush()
112         command=self.command
113         if with_stderr:
114             command += " &> "
115         else:
116             command += " > "
117         command += tmp
118         os.system(command)
119         result=file(tmp).read()
120         os.unlink(tmp)
121         if self.options.debug:
122             print 'Done',
123         return result
124
125 class Svnpath:
126     def __init__(self,path,options):
127         self.path=path
128         self.options=options
129
130     def url_exists (self):
131         return os.system("svn list %s &> /dev/null"%self.path) == 0
132
133     def dir_needs_revert (self):
134         command="svn status %s"%self.path
135         return len(Command(command,self.options).output_of(True)) != 0
136     # turns out it's the same implem.
137     def file_needs_commit (self):
138         command="svn status %s"%self.path
139         return len(Command(command,self.options).output_of(True)) != 0
140
141 # support for tagged module is minimal, and is for the Build class only
142 class Module:
143
144     svn_magic_line="--This line, and those below, will be ignored--"
145     setting_tag_format = "Setting tag %s"
146     
147     redirectors=[ # ('module_name_varname','name'),
148                   ('module_version_varname','version'),
149                   ('module_taglevel_varname','taglevel'), ]
150
151     # where to store user's config
152     config_storage="CONFIG"
153     # 
154     config={}
155
156     import commands
157     configKeys=[ ('svnpath',"Enter your toplevel svnpath",
158                   "svn+ssh://%s@svn.planet-lab.org/svn/"%commands.getoutput("id -un")),
159                  ("build", "Enter the name of your build module","build"),
160                  ('username',"Enter your firstname and lastname for changelogs",""),
161                  ("email","Enter your email address for changelogs",""),
162                  ]
163
164     @staticmethod
165     def prompt_config ():
166         for (key,message,default) in Module.configKeys:
167             Module.config[key]=""
168             while not Module.config[key]:
169                 Module.config[key]=raw_input("%s [%s] : "%(message,default)).strip() or default
170
171
172     # for parsing module spec name:branch
173     matcher_branch_spec=re.compile("\A(?P<name>[\w\.-]+):(?P<branch>[\w\.-]+)\Z")
174     # special form for tagged module - for Build
175     matcher_tag_spec=re.compile("\A(?P<name>[\w-]+)@(?P<tagname>[\w\.-]+)\Z")
176     # parsing specfiles
177     matcher_rpm_define=re.compile("%(define|global)\s+(\S+)\s+(\S*)\s*")
178
179     def __init__ (self,module_spec,options):
180         # parse module spec
181         attempt=Module.matcher_branch_spec.match(module_spec)
182         if attempt:
183             self.name=attempt.group('name')
184             self.branch=attempt.group('branch')
185         else:
186             attempt=Module.matcher_tag_spec.match(module_spec)
187             if attempt:
188                 self.name=attempt.group('name')
189                 self.tagname=attempt.group('tagname')
190             else:
191                 self.name=module_spec
192
193         self.options=options
194         self.module_dir="%s/%s"%(options.workdir,self.name)
195
196     def friendly_name (self):
197         if hasattr(self,'branch'):
198             return "%s:%s"%(self.name,self.branch)
199         elif hasattr(self,'tagname'):
200             return "%s@%s"%(self.name,self.tagname)
201         else:
202             return self.name
203
204     def edge_dir (self):
205         if hasattr(self,'branch'):
206             return "%s/branches/%s"%(self.module_dir,self.branch)
207         elif hasattr(self,'tagname'):
208             return "%s/tags/%s"%(self.module_dir,self.tagname)
209         else:
210             return "%s/trunk"%(self.module_dir)
211
212     def tags_dir (self):
213         return "%s/tags"%(self.module_dir)
214
215     def run (self,command):
216         return Command(command,self.options).run()
217     def run_fatal (self,command):
218         return Command(command,self.options).run_fatal()
219     def run_prompt (self,message,command):
220         if not self.options.verbose:
221             while True:
222                 choice=prompt(message,True,('s','how'))
223                 if choice is True:
224                     self.run(command)
225                     return
226                 elif choice is False:
227                     return
228                 else:
229                     print 'About to run:',command
230         else:
231             question=message+" - want to run " + command
232             if prompt(question,True):
233                 self.run(command)            
234
235     ####################
236     # store and restitute html fragments
237     @staticmethod 
238     def html_href (url,text): return '<a href="%s">%s</a>'%(url,text)
239     @staticmethod 
240     def html_anchor (url,text): return '<a name="%s">%s</a>'%(url,text)
241     # there must be some smarter means to do that - dirty for now
242     @staticmethod
243     def html_quote (text):
244         return text.replace('&','&#38;').replace('<','&lt;').replace('>','&gt;')
245
246     # only the fake error module has multiple titles
247     def html_store_title (self, title):
248         if not hasattr(self,'titles'): self.titles=[]
249         self.titles.append(title)
250     def html_store_raw (self, html):
251         if not hasattr(self,'body'): self.body=''
252         self.body += html
253     def html_store_pre (self, text):
254         if not hasattr(self,'body'): self.body=''
255         self.body += '<pre>' + self.html_quote(text) + '</pre>'
256
257     def html_print (self, txt):
258         if not self.options.www:
259             print txt
260         else:
261             if not hasattr(self,'in_list') or not self.in_list:
262                 self.html_store_raw('<ul>')
263                 self.in_list=True
264             self.html_store_raw('<li>'+txt+'</li>')
265     def html_print_end (self):
266         if self.options.www:
267             self.html_store_raw ('</ul>')
268
269     @staticmethod
270     def html_dump_header(title):
271         nowdate=time.strftime("%Y-%m-%d")
272         nowtime=time.strftime("%H:%M")
273         print """
274 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
275 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
276 <head>
277 <title> %s </title>
278 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
279 <style type="text/css">
280 body { font-family:georgia, serif; }
281 h1 {font-size: large; }
282 p.title {font-size: x-large; }
283 span.error {text-weight:bold; color: red; }
284 </style>
285 </head>
286 <body>
287 <p class='title'> %s - status on %s at %s</p>
288 <ul>
289 """%(title,title,nowdate,nowtime)
290
291     @staticmethod
292     def html_dump_middle():
293         print "</ul>"
294
295     @staticmethod
296     def html_dump_footer():
297         print "</body></html"
298
299     def html_dump_toc(self):
300         if hasattr(self,'titles'):
301             for title in self.titles:
302                 print '<li>',self.html_href ('#'+self.friendly_name(),title),'</li>'
303
304     def html_dump_body(self):
305         if hasattr(self,'titles'):
306             for title in self.titles:
307                 print '<hr /><h1>',self.html_anchor(self.friendly_name(),title),'</h1>'
308         if hasattr(self,'body'):
309             print self.body
310             print '<p class="top">',self.html_href('#','Back to top'),'</p>'
311
312     ####################
313     @staticmethod
314     def init_homedir (options):
315         topdir=options.workdir
316         if options.verbose and options.mode not in Main.silent_modes:
317             print 'Checking for',topdir
318         storage="%s/%s"%(topdir,Module.config_storage)
319         # sanity check. Either the topdir exists AND we have a config/storage
320         # or topdir does not exist and we create it
321         # to avoid people use their own daily svn repo
322         if os.path.isdir(topdir) and not os.path.isfile(storage):
323             print """The directory %s exists and has no CONFIG file
324 If this is your regular working directory, please provide another one as the
325 module-* commands need a fresh working dir. Make sure that you do not use 
326 that for other purposes than tagging"""%topdir
327             sys.exit(1)
328         if not os.path.isdir (topdir):
329             print "Cannot find",topdir,"let's create it"
330             Module.prompt_config()
331             print "Checking ...",
332             Command("svn co -N %s %s"%(Module.config['svnpath'],topdir),options).run_fatal()
333             Command("svn co -N %s/%s %s/%s"%(Module.config['svnpath'],
334                                              Module.config['build'],
335                                              topdir,
336                                              Module.config['build']),options).run_fatal()
337             print "OK"
338             
339             # store config
340             f=file(storage,"w")
341             for (key,message,default) in Module.configKeys:
342                 f.write("%s=%s\n"%(key,Module.config[key]))
343             f.close()
344             if options.debug:
345                 print 'Stored',storage
346                 Command("cat %s"%storage,options).run()
347         else:
348             # read config
349             f=open(storage)
350             for line in f.readlines():
351                 (key,value)=re.compile("^(.+)=(.+)$").match(line).groups()
352                 Module.config[key]=value                
353             f.close()
354         if options.verbose and options.mode not in Main.silent_modes:
355             print '******** Using config'
356             for (key,message,default) in Module.configKeys:
357                 print '\t',key,'=',Module.config[key]
358
359     def init_module_dir (self):
360         if self.options.verbose:
361             print 'Checking for',self.module_dir
362         if not os.path.isdir (self.module_dir):
363             self.run_fatal("svn update -N %s"%self.module_dir)
364         if not os.path.isdir (self.module_dir):
365             raise Exception, 'Cannot find %s - check module name'%self.module_dir
366
367     def init_subdir (self,fullpath):
368         if self.options.verbose:
369             print 'Checking for',fullpath
370         if not os.path.isdir (fullpath):
371             self.run_fatal("svn update -N %s"%fullpath)
372
373     def revert_subdir (self,fullpath):
374         if self.options.fast_checks:
375             if self.options.verbose: print 'Skipping revert of %s'%fullpath
376             return
377         if self.options.verbose:
378             print 'Checking whether',fullpath,'needs being reverted'
379         if Svnpath(fullpath,self.options).dir_needs_revert():
380             self.run_fatal("svn revert -R %s"%fullpath)
381
382     def update_subdir (self,fullpath):
383         if self.options.fast_checks:
384             if self.options.verbose: print 'Skipping update of %s'%fullpath
385             return
386         if self.options.verbose:
387             print 'Updating',fullpath
388         self.run_fatal("svn update -N %s"%fullpath)
389
390     def init_edge_dir (self):
391         # if branch, edge_dir is two steps down
392         if hasattr(self,'branch'):
393             self.init_subdir("%s/branches"%self.module_dir)
394         elif hasattr(self,'tagname'):
395             self.init_subdir("%s/tags"%self.module_dir)
396         self.init_subdir(self.edge_dir())
397
398     def revert_edge_dir (self):
399         self.revert_subdir(self.edge_dir())
400
401     def update_edge_dir (self):
402         self.update_subdir(self.edge_dir())
403
404     def main_specname (self):
405         attempt="%s/%s.spec"%(self.edge_dir(),self.name)
406         if os.path.isfile (attempt):
407             return attempt
408         else:
409             pattern="%s/*.spec"%self.edge_dir()
410             try:
411                 return glob(pattern)[0]
412             except:
413                 raise Exception, 'Cannot guess specfile for module %s -- pattern was %s'%(self.name,pattern)
414
415     def all_specnames (self):
416         return glob("%s/*.spec"%self.edge_dir())
417
418     def parse_spec (self, specfile, varnames):
419         if self.options.verbose:
420             print 'Parsing',specfile,
421             for var in varnames:
422                 print "[%s]"%var,
423             print ""
424         result={}
425         f=open(specfile)
426         for line in f.readlines():
427             attempt=Module.matcher_rpm_define.match(line)
428             if attempt:
429                 (define,var,value)=attempt.groups()
430                 if var in varnames:
431                     result[var]=value
432         f.close()
433         if self.options.debug:
434             print 'found',len(result),'keys'
435             for (k,v) in result.iteritems():
436                 print k,'=',v
437         return result
438                 
439     # stores in self.module_name_varname the rpm variable to be used for the module's name
440     # and the list of these names in self.varnames
441     def spec_dict (self):
442         specfile=self.main_specname()
443         redirector_keys = [ varname for (varname,default) in Module.redirectors]
444         redirect_dict = self.parse_spec(specfile,redirector_keys)
445         if self.options.debug:
446             print '1st pass parsing done, redirect_dict=',redirect_dict
447         varnames=[]
448         for (varname,default) in Module.redirectors:
449             if redirect_dict.has_key(varname):
450                 setattr(self,varname,redirect_dict[varname])
451                 varnames += [redirect_dict[varname]]
452             else:
453                 setattr(self,varname,default)
454                 varnames += [ default ] 
455         self.varnames = varnames
456         result = self.parse_spec (specfile,self.varnames)
457         if self.options.debug:
458             print '2st pass parsing done, varnames=',varnames,'result=',result
459         return result
460
461     def patch_spec_var (self, patch_dict,define_missing=False):
462         for specfile in self.all_specnames():
463             # record the keys that were changed
464             changed = dict ( [ (x,False) for x in patch_dict.keys() ] )
465             newspecfile=specfile+".new"
466             if self.options.verbose:
467                 print 'Patching',specfile,'for',patch_dict.keys()
468             spec=open (specfile)
469             new=open(newspecfile,"w")
470
471             for line in spec.readlines():
472                 attempt=Module.matcher_rpm_define.match(line)
473                 if attempt:
474                     (define,var,value)=attempt.groups()
475                     if var in patch_dict.keys():
476                         if self.options.debug:
477                             print 'rewriting %s as %s'%(var,patch_dict[var])
478                         new.write('%%%s %s %s\n'%(define,var,patch_dict[var]))
479                         changed[var]=True
480                         continue
481                 new.write(line)
482             if define_missing:
483                 for (key,was_changed) in changed.iteritems():
484                     if not was_changed:
485                         if self.options.debug:
486                             print 'rewriting missing %s as %s'%(key,patch_dict[key])
487                         new.write('\n%%define %s %s\n'%(key,patch_dict[key]))
488             spec.close()
489             new.close()
490             os.rename(newspecfile,specfile)
491
492     # returns all lines until the magic line
493     def unignored_lines (self, logfile):
494         result=[]
495         white_line_matcher = re.compile("\A\s*\Z")
496         for logline in file(logfile).readlines():
497             if logline.strip() == Module.svn_magic_line:
498                 break
499             elif white_line_matcher.match(logline):
500                 continue
501             else:
502                 result.append(logline.strip()+'\n')
503         return result
504
505     # creates a copy of the input with only the unignored lines
506     def stripped_magic_line_filename (self, filein, fileout ,new_tag_name):
507        f=file(fileout,'w')
508        f.write(self.setting_tag_format%new_tag_name + '\n')
509        for line in self.unignored_lines(filein):
510            f.write(line)
511        f.close()
512
513     def insert_changelog (self, logfile, oldtag, newtag):
514         for specfile in self.all_specnames():
515             newspecfile=specfile+".new"
516             if self.options.verbose:
517                 print 'Inserting changelog from %s into %s'%(logfile,specfile)
518             spec=open (specfile)
519             new=open(newspecfile,"w")
520             for line in spec.readlines():
521                 new.write(line)
522                 if re.compile('%changelog').match(line):
523                     dateformat="* %a %b %d %Y"
524                     datepart=time.strftime(dateformat)
525                     logpart="%s <%s> - %s"%(Module.config['username'],
526                                                  Module.config['email'],
527                                                  newtag)
528                     new.write(datepart+" "+logpart+"\n")
529                     for logline in self.unignored_lines(logfile):
530                         new.write("- " + logline)
531                     new.write("\n")
532             spec.close()
533             new.close()
534             os.rename(newspecfile,specfile)
535             
536     def show_dict (self, spec_dict):
537         if self.options.verbose:
538             for (k,v) in spec_dict.iteritems():
539                 print k,'=',v
540
541     def mod_url (self):
542         return "%s/%s"%(Module.config['svnpath'],self.name)
543
544     def edge_url (self):
545         if hasattr(self,'branch'):
546             return "%s/branches/%s"%(self.mod_url(),self.branch)
547         elif hasattr(self,'tagname'):
548             return "%s/tags/%s"%(self.mod_url(),self.tagname)
549         else:
550             return "%s/trunk"%(self.mod_url())
551
552     def last_tag (self, spec_dict):
553         return "%s-%s"%(spec_dict[self.module_version_varname],spec_dict[self.module_taglevel_varname])
554
555     def tag_name (self, spec_dict):
556         try:
557             return "%s-%s"%(self.name,
558                             self.last_tag(spec_dict))
559         except KeyError,err:
560             raise Exception, 'Something is wrong with module %s, cannot determine %s - exiting'%(self.name,err)
561
562     def tag_url (self, spec_dict):
563         return "%s/tags/%s"%(self.mod_url(),self.tag_name(spec_dict))
564
565     def check_svnpath_exists (self, url, message):
566         if self.options.fast_checks:
567             return
568         if self.options.verbose:
569             print 'Checking url (%s) %s'%(url,message),
570         ok=Svnpath(url,self.options).url_exists()
571         if ok:
572             if self.options.verbose: print 'exists - OK'
573         else:
574             if self.options.verbose: print 'KO'
575             raise Exception, 'Could not find %s URL %s'%(message,url)
576
577     def check_svnpath_not_exists (self, url, message):
578         if self.options.fast_checks:
579             return
580         if self.options.verbose:
581             print 'Checking url (%s) %s'%(url,message),
582         ok=not Svnpath(url,self.options).url_exists()
583         if ok:
584             if self.options.verbose: print 'does not exist - OK'
585         else:
586             if self.options.verbose: print 'KO'
587             raise Exception, '%s URL %s already exists - exiting'%(message,url)
588
589     # locate specfile, parse it, check it and show values
590
591 ##############################
592     def do_version (self):
593         self.init_module_dir()
594         self.init_edge_dir()
595         self.revert_edge_dir()
596         self.update_edge_dir()
597         spec_dict = self.spec_dict()
598         if self.options.www:
599             self.html_store_title('Version for module %s (%s)' % (self.friendly_name(),self.last_tag(spec_dict)))
600         for varname in self.varnames:
601             if not spec_dict.has_key(varname):
602                 self.html_print ('Could not find %%define for %s'%varname)
603                 return
604             else:
605                 self.html_print ("%-16s %s"%(varname,spec_dict[varname]))
606         if self.options.show_urls:
607             self.html_print ("%-16s %s"%('edge url',self.edge_url()))
608             self.html_print ("%-16s %s"%('latest tag url',self.tag_url(spec_dict)))
609         if self.options.verbose:
610             self.html_print ("%-16s %s"%('main specfile:',self.main_specname()))
611             self.html_print ("%-16s %s"%('specfiles:',self.all_specnames()))
612         self.html_print_end()
613
614 ##############################
615     def do_list (self):
616 #        print 'verbose',self.options.verbose
617 #        print 'list_tags',self.options.list_tags
618 #        print 'list_branches',self.options.list_branches
619 #        print 'all_modules',self.options.all_modules
620         
621         (verbose,branches,pattern,exact) = (self.options.verbose,self.options.list_branches,
622                                             self.options.list_pattern,self.options.list_exact)
623
624         extra_command=""
625         extra_message=""
626         if hasattr(self,'branch'):
627             pattern=self.branch
628         if pattern or exact:
629             if exact:
630                 if verbose: grep="%s/$"%exact
631                 else: grep="^%s$"%exact
632             else:
633                 grep=pattern
634             extra_command=" | grep %s"%grep
635             extra_message=" matching %s"%grep
636
637         if not branches:
638             message="==================== tags for %s"%self.friendly_name()
639             command="svn list "
640             if verbose: command+="--verbose "
641             command += "%s/tags"%self.mod_url()
642             command += extra_command
643             message += extra_message
644             if verbose: print message
645             self.run(command)
646
647         else:
648             message="==================== branches for %s"%self.friendly_name()
649             command="svn list "
650             if verbose: command+="--verbose "
651             command += "%s/branches"%self.mod_url()
652             command += extra_command
653             message += extra_message
654             if verbose: print message
655             self.run(command)
656
657 ##############################
658     sync_warning="""*** WARNING
659 The module-sync function has the following limitations
660 * it does not handle changelogs
661 * it does not scan the -tags*.mk files to adopt the new tags"""
662
663     def do_sync(self):
664         if self.options.verbose:
665             print Module.sync_warning
666             if not prompt('Want to proceed anyway'):
667                 return
668
669         self.init_module_dir()
670         self.init_edge_dir()
671         self.revert_edge_dir()
672         self.update_edge_dir()
673         spec_dict = self.spec_dict()
674
675         edge_url=self.edge_url()
676         tag_name=self.tag_name(spec_dict)
677         tag_url=self.tag_url(spec_dict)
678         # check the tag does not exist yet
679         self.check_svnpath_not_exists(tag_url,"new tag")
680
681         if self.options.message:
682             svnopt='--message "%s"'%self.options.message
683         else:
684             svnopt='--editor-cmd=%s'%self.options.editor
685         self.run_prompt("Create initial tag",
686                         "svn copy %s %s %s"%(svnopt,edge_url,tag_url))
687
688 ##############################
689     def do_diff (self,compute_only=False):
690         self.init_module_dir()
691         self.init_edge_dir()
692         self.revert_edge_dir()
693         self.update_edge_dir()
694         spec_dict = self.spec_dict()
695         self.show_dict(spec_dict)
696
697         edge_url=self.edge_url()
698         tag_url=self.tag_url(spec_dict)
699         self.check_svnpath_exists(edge_url,"edge track")
700         self.check_svnpath_exists(tag_url,"latest tag")
701         command="svn diff %s %s"%(tag_url,edge_url)
702         if compute_only:
703             if self.options.verbose:
704                 print 'Getting diff with %s'%command
705         diff_output = Command(command,self.options).output_of()
706         # if used as a utility
707         if compute_only:
708             return (spec_dict,edge_url,tag_url,diff_output)
709         # otherwise print the result
710         if self.options.list:
711             if diff_output:
712                 print self.name
713         else:
714             thename=self.friendly_name()
715             do_print=False
716             if self.options.www and diff_output:
717                 self.html_store_title("Diffs in module %s (%s) : %d chars"%(\
718                         thename,self.last_tag(spec_dict),len(diff_output)))
719                 link=self.html_href(tag_url,tag_url)
720                 self.html_store_raw ('<p> &lt; (left) %s </p>'%link)
721                 link=self.html_href(edge_url,edge_url)
722                 self.html_store_raw ('<p> &gt; (right) %s </p>'%link)
723                 self.html_store_pre (diff_output)
724             elif not self.options.www:
725                 print 'x'*30,'module',thename
726                 print 'x'*20,'<',tag_url
727                 print 'x'*20,'>',edge_url
728                 print diff_output
729
730 ##############################
731     # using fine_grain means replacing only those instances that currently refer to this tag
732     # otherwise, <module>-SVNPATH is replaced unconditionnally
733     def patch_tags_file (self, tagsfile, oldname, newname,fine_grain=True):
734         newtagsfile=tagsfile+".new"
735         tags=open (tagsfile)
736         new=open(newtagsfile,"w")
737
738         matches=0
739         # fine-grain : replace those lines that refer to oldname
740         if fine_grain:
741             if self.options.verbose:
742                 print 'Replacing %s into %s\n\tin %s .. '%(oldname,newname,tagsfile),
743             matcher=re.compile("^(.*)%s(.*)"%oldname)
744             for line in tags.readlines():
745                 if not matcher.match(line):
746                     new.write(line)
747                 else:
748                     (begin,end)=matcher.match(line).groups()
749                     new.write(begin+newname+end+"\n")
750                     matches += 1
751         # brute-force : change uncommented lines that define <module>-SVNPATH
752         else:
753             if self.options.verbose:
754                 print 'Searching for -SVNPATH lines referring to /%s/\n\tin %s .. '%(self.name,tagsfile),
755             pattern="\A\s*(?P<make_name>[^\s]+)-SVNPATH\s*(=|:=)\s*(?P<url_main>[^\s]+)/%s/[^\s]+"\
756                                           %(self.name)
757             matcher_module=re.compile(pattern)
758             for line in tags.readlines():
759                 attempt=matcher_module.match(line)
760                 if attempt:
761                     svnpath="%s-SVNPATH"%(attempt.group('make_name'))
762                     if self.options.verbose:
763                         print ' '+svnpath, 
764                     replacement = "%-32s:= %s/%s/tags/%s\n"%(svnpath,attempt.group('url_main'),self.name,newname)
765                     new.write(replacement)
766                     matches += 1
767                 else:
768                     new.write(line)
769         tags.close()
770         new.close()
771         os.rename(newtagsfile,tagsfile)
772         if self.options.verbose: print "%d changes"%matches
773         return matches
774
775     def do_tag (self):
776         self.init_module_dir()
777         self.init_edge_dir()
778         self.revert_edge_dir()
779         self.update_edge_dir()
780         # parse specfile
781         spec_dict = self.spec_dict()
782         self.show_dict(spec_dict)
783         
784         # side effects
785         edge_url=self.edge_url()
786         old_tag_name = self.tag_name(spec_dict)
787         old_tag_url=self.tag_url(spec_dict)
788         if (self.options.new_version):
789             # new version set on command line
790             spec_dict[self.module_version_varname] = self.options.new_version
791             spec_dict[self.module_taglevel_varname] = 0
792         else:
793             # increment taglevel
794             new_taglevel = str ( int (spec_dict[self.module_taglevel_varname]) + 1)
795             spec_dict[self.module_taglevel_varname] = new_taglevel
796
797         # sanity check
798         new_tag_name = self.tag_name(spec_dict)
799         new_tag_url=self.tag_url(spec_dict)
800         self.check_svnpath_exists (edge_url,"edge track")
801         self.check_svnpath_exists (old_tag_url,"previous tag")
802         self.check_svnpath_not_exists (new_tag_url,"new tag")
803
804         # checking for diffs
805         diff_output=Command("svn diff %s %s"%(old_tag_url,edge_url),
806                             self.options).output_of()
807         if len(diff_output) == 0:
808             if not prompt ("No pending difference in module %s, want to tag anyway"%self.name,False):
809                 return
810
811         # side effect in trunk's specfile
812         self.patch_spec_var(spec_dict)
813
814         # prepare changelog file 
815         # we use the standard subversion magic string (see svn_magic_line)
816         # so we can provide useful information, such as version numbers and diff
817         # in the same file
818         changelog="/tmp/%s-%d.edit"%(self.name,os.getpid())
819         changelog_svn="/tmp/%s-%d.svn"%(self.name,os.getpid())
820         setting_tag_line=Module.setting_tag_format%new_tag_name
821         file(changelog,"w").write("""
822 %s
823 %s
824 Please write a changelog for this new tag in the section above
825 """%(Module.svn_magic_line,setting_tag_line))
826
827         if not self.options.verbose or prompt('Want to see diffs while writing changelog',True):
828             file(changelog,"a").write('DIFF=========\n' + diff_output)
829         
830         if self.options.debug:
831             prompt('Proceed ?')
832
833         # edit it        
834         self.run("%s %s"%(self.options.editor,changelog))
835         # strip magic line in second file - looks like svn has changed its magic line with 1.6
836         # so we do the job ourselves
837         self.stripped_magic_line_filename(changelog,changelog_svn,new_tag_name)
838         # insert changelog in spec
839         if self.options.changelog:
840             self.insert_changelog (changelog,old_tag_name,new_tag_name)
841
842         ## update build
843         try:
844             buildname=Module.config['build']
845         except:
846             buildname="build"
847         if self.options.build_branch:
848             buildname+=":"+self.options.build_branch
849         build = Module(buildname,self.options)
850         build.init_module_dir()
851         build.init_edge_dir()
852         build.revert_edge_dir()
853         build.update_edge_dir()
854         
855         tagsfiles=glob(build.edge_dir()+"/*-tags*.mk")
856         tagsdict=dict( [ (x,'todo') for x in tagsfiles ] )
857         default_answer = 'y'
858         tagsfiles.sort()
859         while True:
860             for tagsfile in tagsfiles:
861                 status=tagsdict[tagsfile]
862                 basename=os.path.basename(tagsfile)
863                 print ".................... Dealing with %s"%basename
864                 while tagsdict[tagsfile] == 'todo' :
865                     choice = prompt ("insert %s in %s    "%(new_tag_name,basename),default_answer,
866                                      [ ('y','es'), ('n', 'ext'), ('f','orce'), 
867                                        ('d','iff'), ('r','evert'), ('c', 'at'), ('h','elp') ] ,
868                                      allow_outside=True)
869                     if choice == 'y':
870                         self.patch_tags_file(tagsfile,old_tag_name,new_tag_name,fine_grain=True)
871                     elif choice == 'n':
872                         print 'Done with %s'%os.path.basename(tagsfile)
873                         tagsdict[tagsfile]='done'
874                     elif choice == 'f':
875                         self.patch_tags_file(tagsfile,old_tag_name,new_tag_name,fine_grain=False)
876                     elif choice == 'd':
877                         self.run("svn diff %s"%tagsfile)
878                     elif choice == 'r':
879                         self.run("svn revert %s"%tagsfile)
880                     elif choice == 'c':
881                         self.run("cat %s"%tagsfile)
882                     else:
883                         name=self.name
884                         print """y: change %(name)s-SVNPATH only if it currently refers to %(old_tag_name)s
885 f: unconditionnally change any line that assigns %(name)s-SVNPATH to using %(new_tag_name)s
886 d: show current diff for this tag file
887 r: revert that tag file
888 c: cat the current tag file
889 n: move to next file"""%locals()
890
891             if prompt("Want to review changes on tags files",False):
892                 tagsdict = dict ( [ (x, 'todo') for tagsfile in tagsfiles ] )
893                 default_answer='d'
894             else:
895                 break
896
897         paths=""
898         paths += self.edge_dir() + " "
899         paths += build.edge_dir() + " "
900         self.run_prompt("Review module and build","svn diff " + paths)
901         self.run_prompt("Commit module and build","svn commit --file %s %s"%(changelog_svn,paths))
902         self.run_prompt("Create tag","svn copy --file %s %s %s"%(changelog_svn,edge_url,new_tag_url))
903
904         if self.options.debug:
905             print 'Preserving',changelog,'and stripped',changelog_svn
906         else:
907             os.unlink(changelog)
908             os.unlink(changelog_svn)
909             
910 ##############################
911     def do_branch (self):
912
913         # save self.branch if any, as a hint for the new branch 
914         # do this before anything else and restore .branch to None, 
915         # as this is part of the class's logic
916         new_trunk_name=None
917         if hasattr(self,'branch'):
918             new_trunk_name=self.branch
919             del self.branch
920         elif self.options.new_version:
921             new_trunk_name = self.options.new_version
922
923         # compute diff - a way to initialize the whole stuff
924         # do_diff already does edge_dir initialization
925         # and it checks that edge_url and tag_url exist as well
926         (spec_dict,edge_url,tag_url,diff_listing) = self.do_diff(compute_only=True)
927
928         # the version name in the trunk becomes the new branch name
929         branch_name = spec_dict[self.module_version_varname]
930
931         # figure new branch name (the one for the trunk) if not provided on the command line
932         if not new_trunk_name:
933             # heuristic is to assume 'version' is a dot-separated name
934             # we isolate the rightmost part and try incrementing it by 1
935             version=spec_dict[self.module_version_varname]
936             try:
937                 m=re.compile("\A(?P<leftpart>.+)\.(?P<rightmost>[^\.]+)\Z")
938                 (leftpart,rightmost)=m.match(version).groups()
939                 incremented = int(rightmost)+1
940                 new_trunk_name="%s.%d"%(leftpart,incremented)
941             except:
942                 raise Exception, 'Cannot figure next branch name from %s - exiting'%version
943
944         # record starting point tagname
945         latest_tag_name = self.tag_name(spec_dict)
946
947         print "**********"
948         print "Using starting point %s (%s)"%(tag_url,latest_tag_name)
949         print "Creating branch %s  &  moving trunk to %s"%(branch_name,new_trunk_name)
950         print "**********"
951
952         # print warning if pending diffs
953         if diff_listing:
954             print """*** WARNING : Module %s has pending diffs on its trunk
955 It is safe to proceed, but please note that branch %s
956 will be based on latest tag %s and *not* on the current trunk"""%(self.name,branch_name,latest_tag_name)
957             while True:
958                 answer = prompt ('Are you sure you want to proceed with branching',True,('d','iff'))
959                 if answer is True:
960                     break
961                 elif answer is False:
962                     raise Exception,"User quit"
963                 elif answer == 'd':
964                     print '<<<< %s'%tag_url
965                     print '>>>> %s'%edge_url
966                     print diff_listing
967
968         branch_url = "%s/%s/branches/%s"%(Module.config['svnpath'],self.name,branch_name)
969         self.check_svnpath_not_exists (branch_url,"new branch")
970         
971         # patching trunk
972         spec_dict[self.module_version_varname]=new_trunk_name
973         spec_dict[self.module_taglevel_varname]='0'
974         # remember this in the trunk for easy location of the current branch
975         spec_dict['module_current_branch']=branch_name
976         self.patch_spec_var(spec_dict,True)
977         
978         # create commit log file
979         tmp="/tmp/branching-%d"%os.getpid()
980         f=open(tmp,"w")
981         f.write("Branch %s for module %s created (as new trunk) from tag %s\n"%(new_trunk_name,self.name,latest_tag_name))
982         f.close()
983
984         # we're done, let's commit the stuff
985         command="svn diff %s"%self.edge_dir()
986         self.run_prompt("Review changes in trunk",command)
987         command="svn copy --file %s %s %s"%(tmp,self.edge_url(),branch_url)
988         self.run_prompt("Create branch",command)
989         command="svn commit --file %s %s"%(tmp,self.edge_dir())
990         self.run_prompt("Commit trunk",command)
991         new_tag_url=self.tag_url(spec_dict)
992         command="svn copy --file %s %s %s"%(tmp,self.edge_url(),new_tag_url)
993         self.run_prompt("Create initial tag in trunk",command)
994         os.unlink(tmp)
995
996 ##############################
997 class Package:
998
999     def __init__(self, package, module, svnpath, spec):
1000         self.package=package
1001         self.module=module
1002         self.svnpath=svnpath
1003         self.spec=spec
1004         self.specpath="%s/%s"%(svnpath,spec)
1005         self.basename=os.path.basename(svnpath)
1006
1007     # returns a http URL to the trac path where full diff can be viewed (between self and pkg)
1008     # typically http://svn.planet-lab.org/changeset?old_path=Monitor%2Ftags%2FMonitor-1.0-7&new_path=Monitor%2Ftags%2FMonitor-1.0-13
1009     # xxx quick & dirty: rough url parsing 
1010     def trac_full_diff (self, pkg):
1011         matcher=re.compile("\A(?P<method>.*)://(?P<hostname>[^/]+)/(svn/)?(?P<path>.*)\Z")
1012         self_match=matcher.match(self.svnpath)
1013         pkg_match=matcher.match(pkg.svnpath)
1014         if self_match and pkg_match:
1015             (method,hostname,svn,path)=self_match.groups()
1016             self_path=path.replace("/","%2F")
1017             pkg_path=pkg_match.group('path').replace("/","%2F")
1018             return "%s://%s/changeset?old_path=%s&new_path=%s"%(method,hostname,self_path,pkg_path)
1019         else:
1020             return None
1021
1022     def details (self):
1023         return "[%s %s] [%s (spec)]"%(self.svnpath,self.basename,self.specpath)
1024
1025 class Build (Module):
1026     
1027     # we cannot get build's svnpath as for other packages as we'd get something in svn+ssh
1028     # xxx quick & dirty
1029     def __init__ (self, buildtag,options):
1030         self.buildtag=buildtag
1031         # if the buildtag start with a : (to use a branch rather than a tag)
1032         if buildtag.find(':') == 0 : 
1033             module_name="build%(buildtag)s"%locals()
1034             self.display=buildtag[1:]
1035             self.svnpath="http://svn.planet-lab.org/svn/build/branches/%s"%self.display
1036         else : 
1037             module_name="build@%(buildtag)s"%locals()
1038             self.display=buildtag
1039             self.svnpath="http://svn.planet-lab.org/svn/build/tags/%s"%self.buildtag
1040         Module.__init__(self,module_name,options)
1041
1042     @staticmethod
1043     def get_distro_from_distrotag (distrotag):
1044         # mhh: remove -tag* from distrotags to get distro
1045         n=distrotag.find('-tag')
1046         if n>0:
1047             return distrotag[:n]
1048         else:
1049             return None
1050
1051     def get_packages (self,distrotag):
1052         result={}
1053         distro=Build.get_distro_from_distrotag(distrotag)
1054         if not distro:
1055             return result
1056         make_options="--no-print-directory -C %s stage1=true PLDISTRO=%s PLDISTROTAGS=%s 2> /dev/null"%(self.edge_dir(),distro,distrotag)
1057         command="make %s packages"%make_options
1058         make_packages=Command(command,self.options).output_of()
1059         pkg_line=re.compile("\Apackage=(?P<package>[^\s]+)\s+ref_module=(?P<module>[^\s]+)\s.*\Z")
1060         for line in make_packages.split("\n"):
1061             if not line:
1062                 continue
1063             attempt=pkg_line.match(line)
1064             if line and not attempt:
1065                 print "====="
1066                 print "WARNING: line not understood from make packages"
1067                 print "in dir %s"%self.edge_dir
1068                 print "with options",make_options
1069                 print 'line=',line
1070                 print "====="
1071             else:
1072                 (package,module) = (attempt.group('package'),attempt.group('module')) 
1073                 command="make %s +%s-SVNPATH"%(make_options,module)
1074                 svnpath=Command(command,self.options).output_of().strip()
1075                 command="make %s +%s-SPEC"%(make_options,package)
1076                 spec=Command(command,self.options).output_of().strip()
1077                 result[package]=Package(package,module,svnpath,spec)
1078         return result
1079
1080     def get_distrotags (self):
1081         return [os.path.basename(p) for p in glob("%s/*tags*mk"%self.edge_dir())]
1082
1083 class DiffCache:
1084
1085     def __init__ (self):
1086         self._cache={}
1087
1088     def key(self, frompath,topath):
1089         return frompath+'-to-'+topath
1090
1091     def fetch (self, frompath, topath):
1092         key=self.key(frompath,topath)
1093         if not self._cache.has_key(key):
1094             return None
1095         return self._cache[key]
1096
1097     def store (self, frompath, topath, diff):
1098         key=self.key(frompath,topath)
1099         self._cache[key]=diff
1100
1101 class Release:
1102
1103     # header in diff output
1104     discard_matcher=re.compile("\A(\+\+\+|---).*")
1105
1106     @staticmethod
1107     def do_changelog (buildtag_new,buildtag_old,options):
1108         print "----"
1109         print "----"
1110         print "----"
1111         (build_new,build_old) = (Build (buildtag_new,options), Build (buildtag_old,options))
1112         print "= build tag %s to %s = #build-%s"%(build_old.display,build_new.display,build_new.display)
1113         for b in (build_new,build_old):
1114             b.init_module_dir()
1115             b.init_edge_dir()
1116             b.update_edge_dir()
1117         # find out the tags files that are common, unless option was specified
1118         if options.distrotags:
1119             distrotags=options.distrotags
1120         else:
1121             distrotags_new=build_new.get_distrotags()
1122             distrotags_old=build_old.get_distrotags()
1123             distrotags = list(set(distrotags_new).intersection(set(distrotags_old)))
1124             distrotags.sort()
1125         if options.verbose: print "Found distrotags",distrotags
1126         first_distrotag=True
1127         diffcache = DiffCache()
1128         for distrotag in distrotags:
1129             distro=Build.get_distro_from_distrotag(distrotag)
1130             if not distro:
1131                 continue
1132             if first_distrotag:
1133                 first_distrotag=False
1134             else:
1135                 print '----'
1136             print '== distro %s (%s to %s) == #distro-%s-%s'%(distrotag,build_old.display,build_new.display,distro,build_new.display)
1137             print ' * from %s/%s'%(build_old.svnpath,distrotag)
1138             print ' * to %s/%s'%(build_new.svnpath,distrotag)
1139
1140             # parse make packages
1141             packages_new=build_new.get_packages(distrotag)
1142             pnames_new=set(packages_new.keys())
1143             if options.verbose: print 'got packages for ',build_new.display
1144             packages_old=build_old.get_packages(distrotag)
1145             pnames_old=set(packages_old.keys())
1146             if options.verbose: print 'got packages for ',build_old.display
1147
1148             # get created, deprecated, and preserved package names
1149             pnames_created = list(pnames_new-pnames_old)
1150             pnames_created.sort()
1151             pnames_deprecated = list(pnames_old-pnames_new)
1152             pnames_deprecated.sort()
1153             pnames = list(pnames_new.intersection(pnames_old))
1154             pnames.sort()
1155
1156             if options.verbose: print "Found new/deprecated/preserved pnames",pnames_new,pnames_deprecated,pnames
1157
1158             # display created and deprecated 
1159             for name in pnames_created:
1160                 print '=== %s : new package %s -- appeared in %s === #package-%s-%s-%s'%(
1161                     distrotag,name,build_new.display,name,distro,build_new.display)
1162                 pobj=packages_new[name]
1163                 print ' * %s'%pobj.details()
1164             for name in pnames_deprecated:
1165                 print '=== %s : package %s -- deprecated, last occurrence in %s === #package-%s-%s-%s'%(
1166                     distrotag,name,build_old.display,name,distro,build_new.display)
1167                 pobj=packages_old[name]
1168                 if not pobj.svnpath:
1169                     print ' * codebase stored in CVS, specfile is %s'%pobj.spec
1170                 else:
1171                     print ' * %s'%pobj.details()
1172
1173             # display other packages
1174             for name in pnames:
1175                 (pobj_new,pobj_old)=(packages_new[name],packages_old[name])
1176                 if options.verbose: print "Dealing with package",name
1177                 if pobj_old.specpath == pobj_new.specpath:
1178                     continue
1179                 specdiff = diffcache.fetch(pobj_old.specpath,pobj_new.specpath)
1180                 if specdiff is None:
1181                     command="svn diff %s %s"%(pobj_old.specpath,pobj_new.specpath)
1182                     specdiff=Command(command,options).output_of()
1183                     diffcache.store(pobj_old.specpath,pobj_new.specpath,specdiff)
1184                 else:
1185                     if options.verbose: print 'got diff from cache'
1186                 if not specdiff:
1187                     continue
1188                 print '=== %s - %s to %s : package %s === #package-%s-%s-%s'%(
1189                     distrotag,build_old.display,build_new.display,name,name,distro,build_new.display)
1190                 print ' * from %s to %s'%(pobj_old.details(),pobj_new.details())
1191                 trac_diff_url=pobj_old.trac_full_diff(pobj_new)
1192                 if trac_diff_url:
1193                     print ' * [%s View full diff]'%trac_diff_url
1194                 print '{{{'
1195                 for line in specdiff.split('\n'):
1196                     if not line:
1197                         continue
1198                     if Release.discard_matcher.match(line):
1199                         continue
1200                     if line[0] in ['@']:
1201                         print '----------'
1202                     elif line[0] in ['+','-']:
1203                         print_fold(line)
1204                 print '}}}'
1205
1206 ##############################
1207 class Main:
1208
1209     module_usage="""Usage: %prog [options] module_desc [ .. module_desc ]
1210 module-tools : a set of tools to manage subversion tags and specfile
1211   requires the specfile to either
1212   * define *version* and *taglevel*
1213   OR alternatively 
1214   * define redirection variables module_version_varname / module_taglevel_varname
1215 Trunk:
1216   by default, the trunk of modules is taken into account
1217   in this case, just mention the module name as <module_desc>
1218 Branches:
1219   if you wish to work on a branch rather than on the trunk, 
1220   you can use something like e.g. Mom:2.1 as <module_desc>
1221 """
1222     release_usage="""Usage: %prog [options] tag1 .. tagn
1223   Extract release notes from the changes in specfiles between several build tags, latest first
1224   Examples:
1225       release-changelog 4.2-rc25 4.2-rc24 4.2-rc23 4.2-rc22
1226   You can refer to a (build) branch by prepending a colon, like in
1227       release-changelog :4.2 4.2-rc25
1228 """
1229     common_usage="""More help:
1230   see http://svn.planet-lab.org/wiki/ModuleTools"""
1231
1232     modes={ 
1233         'list' : "displays a list of available tags or branches",
1234         'version' : "check latest specfile and print out details",
1235         'diff' : "show difference between module (trunk or branch) and latest tag",
1236         'tag'  : """increment taglevel in specfile, insert changelog in specfile,
1237                 create new tag and and monitor its adoption in build/*-tags*.mk""",
1238         'branch' : """create a branch for this module, from the latest tag on the trunk, 
1239                   and change trunk's version number to reflect the new branch name;
1240                   you can specify the new branch name by using module:branch""",
1241         'sync' : """create a tag from the module
1242                 this is a last resort option, mostly for repairs""",
1243         'changelog' : """extract changelog between build tags
1244                 expected arguments are a list of tags""",
1245         }
1246
1247     silent_modes = ['list']
1248     release_modes = ['changelog']
1249
1250     @staticmethod
1251     def optparse_list (option, opt, value, parser):
1252         try:
1253             setattr(parser.values,option.dest,getattr(parser.values,option.dest)+value.split())
1254         except:
1255             setattr(parser.values,option.dest,value.split())
1256
1257     def run(self):
1258
1259         mode=None
1260         for function in Main.modes.keys():
1261             if sys.argv[0].find(function) >= 0:
1262                 mode = function
1263                 break
1264         if not mode:
1265             print "Unsupported command",sys.argv[0]
1266             print "Supported commands:" + Modes.modes.keys.join(" ")
1267             sys.exit(1)
1268
1269         if mode not in Main.release_modes:
1270             usage = Main.module_usage
1271             usage += Main.common_usage
1272             usage += "\nmodule-%s : %s"%(mode,Main.modes[mode])
1273         else:
1274             usage = Main.release_usage
1275             usage += Main.common_usage
1276
1277         parser=OptionParser(usage=usage,version=subversion_id)
1278         
1279         if mode == 'list':
1280             parser.add_option("-b","--branches",action="store_true",dest="list_branches",default=False,
1281                               help="list branches")
1282             parser.add_option("-t","--tags",action="store_false",dest="list_branches",
1283                               help="list tags")
1284             parser.add_option("-m","--match",action="store",dest="list_pattern",default=None,
1285                                help="grep pattern for filtering output")
1286             parser.add_option("-x","--exact-match",action="store",dest="list_exact",default=None,
1287                                help="exact grep pattern for filtering output")
1288         if mode == "tag" or mode == 'branch':
1289             parser.add_option("-s","--set-version",action="store",dest="new_version",default=None,
1290                               help="set new version and reset taglevel to 0")
1291         if mode == "tag" :
1292             parser.add_option("-c","--no-changelog", action="store_false", dest="changelog", default=True,
1293                               help="do not update changelog section in specfile when tagging")
1294             parser.add_option("-b","--build-branch", action="store", dest="build_branch", default=None,
1295                               help="specify a build branch; used for locating the *tags*.mk files where adoption is to take place")
1296         if mode == "tag" or mode == "sync" :
1297             parser.add_option("-e","--editor", action="store", dest="editor", default=default_editor(),
1298                               help="specify editor")
1299         if mode == "sync" :
1300             parser.add_option("-m","--message", action="store", dest="message", default=None,
1301                               help="specify log message")
1302         if mode in ["diff","version"] :
1303             parser.add_option("-W","--www", action="store", dest="www", default=False,
1304                               help="export diff in html format, e.g. -W trunk")
1305         if mode == "diff" :
1306             parser.add_option("-l","--list", action="store_true", dest="list", default=False,
1307                               help="just list modules that exhibit differences")
1308
1309         if mode  == 'version':
1310             parser.add_option("-u","--url", action="store_true", dest="show_urls", default=False,
1311                               help="display URLs")
1312             
1313         default_modules_list=os.path.dirname(sys.argv[0])+"/modules.list"
1314         if mode not in Main.release_modes:
1315             parser.add_option("-a","--all",action="store_true",dest="all_modules",default=False,
1316                               help="run on all modules as found in %s"%default_modules_list)
1317             parser.add_option("-f","--file",action="store",dest="modules_list",default=None,
1318                               help="run on all modules found in specified file")
1319         else:
1320             parser.add_option("-n","--dry-run",action="store_true",dest="dry_run",default=False,
1321                               help="dry run - shell commands are only displayed")
1322             parser.add_option("-t","--distrotags",action="callback",callback=Main.optparse_list, dest="distrotags",
1323                               default=[], nargs=1,type="string",
1324                               help="""specify distro-tags files, e.g. onelab-tags-4.2.mk
1325 -- can be set multiple times, or use quotes""")
1326
1327         parser.add_option("-w","--workdir", action="store", dest="workdir", 
1328                           default="%s/%s"%(os.getenv("HOME"),"modules"),
1329                           help="""name for dedicated working dir - defaults to ~/modules
1330 ** THIS MUST NOT ** be your usual working directory""")
1331         parser.add_option("-F","--fast-checks",action="store_true",dest="fast_checks",default=False,
1332                           help="skip safety checks, such as svn updates -- use with care")
1333
1334         # default verbosity depending on function - temp
1335         verbose_modes= ['tag','sync']
1336         
1337         if mode not in verbose_modes:
1338             parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False, 
1339                               help="run in verbose mode")
1340         else:
1341             parser.add_option("-q","--quiet", action="store_false", dest="verbose", default=True,
1342                               help="run in quiet (non-verbose) mode")
1343 #        parser.add_option("-d","--debug", action="store_true", dest="debug", default=False, 
1344 #                          help="debug mode - mostly more verbose")
1345         (options, args) = parser.parse_args()
1346         options.mode=mode
1347         if not hasattr(options,'dry_run'):
1348             options.dry_run=False
1349         if not hasattr(options,'www'):
1350             options.www=False
1351         options.debug=False
1352
1353         ########## release-*
1354         if mode in Main.release_modes :
1355             ########## changelog
1356             if len(args) <= 1:
1357                 parser.print_help()
1358                 sys.exit(1)
1359             Module.init_homedir(options)
1360             for n in range(len(args)-1):
1361                 [t_new,t_old]=args[n:n+2]
1362                 Release.do_changelog (t_new,t_old,options)
1363         else:
1364             ########## module-*
1365             if len(args) == 0:
1366                 if options.all_modules:
1367                     options.modules_list=default_modules_list
1368                 if options.modules_list:
1369                     args=Command("grep -v '#' %s"%options.modules_list,options).output_of().split()
1370                 else:
1371                     parser.print_help()
1372                     sys.exit(1)
1373             Module.init_homedir(options)
1374
1375             # 2 passes for www output
1376             modules=[ Module(modname,options) for modname in args ]
1377             # hack: create a dummy Module to store errors/warnings
1378             error_module = Module('__errors__',options)
1379
1380             # pass 1 : do it, except if options.www
1381             for module in modules:
1382                 if len(args)>1 and mode not in Main.silent_modes and not options.www:
1383                     print '========================================',module.friendly_name()
1384                 # call the method called do_<mode>
1385                 method=Module.__dict__["do_%s"%mode]
1386                 try:
1387                     method(module)
1388                 except Exception,e:
1389                     if options.www:
1390                         title='<span class="error"> Skipping module %s - failure: %s </span>'%\
1391                             (module.friendly_name(), str(e))
1392                         error_module.html_store_title(title)
1393                     else:
1394                         print 'Skipping module %s: '%modname,e
1395
1396             # in which case we do the actual printing in the second pass
1397             if options.www:
1398                 if mode == "diff":
1399                     modetitle="Changes to tag in %s"%options.www
1400                 elif mode == "version":
1401                     modetitle="Latest tags in %s"%options.www
1402                 modules.append(error_module)
1403                 error_module.html_dump_header(modetitle)
1404                 for module in modules:
1405                     module.html_dump_toc()
1406                 Module.html_dump_middle()
1407                 for module in modules:
1408                     module.html_dump_body()
1409                 Module.html_dump_footer()
1410
1411 ####################
1412 if __name__ == "__main__" :
1413     try:
1414         Main().run()
1415     except KeyboardInterrupt:
1416         print '\nBye'