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