b839978e621bc0cc81faad69599aaccd8615a8b0
[build.git] / module-tag.py
1 #!/usr/bin/env python
2
3 subversion_id = "$Id: TestMain.py 7635 2008-01-04 09:46:06Z thierry $"
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 def prompt (question,default=True):
12     if default:
13         question += " [y]/n ? "
14     else:
15         question += " y/[n] ? "
16     try:
17         answer=raw_input(question)
18         if not answer:
19             return default
20         elif answer[0] in [ 'y','Y']:
21             return True
22         elif answer[0] in [ 'n','N']:
23             return False
24         else:
25             return prompt(question,default)
26     except KeyboardInterrupt:
27         print "Aborted"
28         return False
29     except:
30         raise
31
32 class Command:
33     def __init__ (self,command,options):
34         self.command=command
35         self.options=options
36         self.tmp="/tmp/command-%d"%os.getpid()
37
38     def run (self):
39         if self.options.verbose:
40             print '+',self.command
41             sys.stdout.flush()
42         return os.system(self.command)
43
44     def run_silent (self):
45         if self.options.verbose:
46             print '+',self.command,' .. ',
47             sys.stdout.flush()
48         retcod=os.system(self.command + " &> " + self.tmp)
49         if retcod != 0:
50             print "FAILED ! -- output quoted below "
51             os.system("cat " + self.tmp)
52             print "FAILED ! -- end of quoted output"
53         elif self.options.verbose:
54             print "OK"
55         os.unlink(self.tmp)
56         return retcod
57
58     def run_fatal(self):
59         if self.run_silent() !=0:
60             raise Exception,"Command %s failed"%self.command
61
62     # returns stdout, like bash's $(mycommand)
63     def output_of (self):
64         tmp="/tmp/status-%d"%os.getpid()
65         if self.options.debug:
66             print '+',self.command,' .. ',
67             sys.stdout.flush()
68         os.system(self.command + " &> " + tmp)
69         result=file(tmp).read()
70         os.unlink(tmp)
71         if self.options.debug:
72             print '+',self.command,'Done',
73         return result
74
75 class Svnpath:
76     def __init__(self,path,options):
77         self.path=path
78         self.options=options
79
80     def url_exists (self):
81         if self.options.verbose:
82             print 'Checking url',self.path
83         return os.system("svn list %s &> /dev/null"%self.path) == 0
84
85     def dir_needs_revert (self):
86         command="svn status %s"%self.path
87         return len(Command(command,self.options).output_of()) != 0
88     # turns out it's the same implem.
89     def file_needs_commit (self):
90         command="svn status %s"%self.path
91         return len(Command(command,self.options).output_of()) != 0
92
93 class Module:
94
95     # where to store user's config
96     config_storage="CONFIG"
97     # 
98     configKeys=[ ('svnpath',"Enter your toplevel svnpath (e.g. svn+ssh://thierry@svn.planet-lab.org/svn/)"),
99                  ('username',"Enter your firstname and lastname for changelogs"),
100                  ("email","Enter your email address for changelogs") ]
101     config={}
102
103     svn_magic_line="--This line, and those below, will be ignored--"
104
105     def __init__ (self,name,options):
106         self.name=name
107         self.options=options
108         self.moddir="%s/%s/%s"%(os.getenv("HOME"),options.modules,name)
109         self.trunkdir="%s/trunk"%(self.moddir)
110         # what to parse in a spec file
111         self.varnames = ["name",options.version,options.taglevel]
112         self.varmatcher=re.compile("%define\s+(\S+)\s+(.*)")
113
114
115     def run (self,command):
116         return Command(command,self.options).run()
117     def run_fatal (self,command):
118         return Command(command,self.options).run_fatal()
119     def run_prompt (self,message,command):
120         if not self.options.verbose:
121             question=message
122         else:
123             question="Want to run " + command
124         if prompt(question,True):
125             self.run(command)            
126
127     @staticmethod
128     def init_homedir (options):
129         topdir="%s/%s"%(os.getenv("HOME"),options.modules)
130         if options.verbose:
131             print 'Checking for',topdir
132         storage="%s/%s"%(topdir,Module.config_storage)
133         if not os.path.isdir (topdir):
134             # prompt for login or whatever svnpath
135             print "Cannot find",topdir,"let's create it"
136             for (key,message) in Module.configKeys:
137                 Module.config[key]=raw_input(message+" : ").strip()
138             Command("svn co -N %s %s"%(Module.config['svnpath'],topdir),options).run_fatal()
139             # store config
140             f=file(storage,"w")
141             for (key,message) in Module.configKeys:
142                 f.write("%s=%s\n"%(key,Module.config[key]))
143             f.close()
144             if options.debug:
145                 print 'Stored',storage
146                 Command("cat %s"%storage,options).run()
147         else:
148             # read config
149             f=open(storage)
150             for line in f.readlines():
151                 (key,value)=re.compile("^(.+)=(.+)$").match(line).groups()
152                 Module.config[key]=value                
153             f.close()
154             if options.debug:
155                 print 'Using config'
156                 for (key,message) in Module.configKeys:
157                     print key,'=',Module.config[key]
158
159     def init_moddir (self):
160         if self.options.verbose:
161             print 'Checking for',self.moddir
162         if not os.path.isdir (self.moddir):
163             self.run_fatal("svn up -N %s"%self.moddir)
164         if not os.path.isdir (self.moddir):
165             print 'Cannot find %s - check module name'%self.moddir
166             sys.exit(1)
167
168     def init_trunkdir (self):
169         if self.options.verbose:
170             print 'Checking for',self.trunkdir
171         if not os.path.isdir (self.trunkdir):
172             self.run_fatal("svn up %s"%self.trunkdir)
173
174     def revert_trunkdir (self):
175         if self.options.verbose:
176             print 'Checking whether',self.trunkdir,'needs being reverted'
177         if Svnpath(self.trunkdir,self.options).dir_needs_revert():
178             self.run_fatal("svn revert -R %s"%self.trunkdir)
179
180     def update_trunkdir (self):
181         if self.options.verbose:
182             print 'Updating',self.trunkdir
183         self.run_fatal("svn update %s"%self.trunkdir)
184
185     def guess_specname (self):
186         attempt="%s/%s.spec"%(self.trunkdir,self.name)
187         if os.path.isfile (attempt):
188             return attempt
189         else:
190             try:
191                 return glob("%s/*.spec"%self.trunkdir)[0]
192             except:
193                 print 'Cannot guess specfile for module %s'%self.name
194                 sys.exit(1)
195
196     def spec_dict (self):
197         specfile=self.guess_specname()
198         if self.options.verbose:
199             print 'Parsing',specfile,
200         result={}
201         f=open(specfile)
202         for line in f.readlines():
203             if self.varmatcher.match(line):
204                 (var,value)=self.varmatcher.match(line).groups()
205                 if var in self.varnames:
206                     result[var]=value
207         f.close()
208         if self.options.verbose:
209             print 'found',len(result),'keys'
210         return result
211
212     def patch_spec_var (self, patch_dict):
213         specfile=self.guess_specname()
214         newspecfile=specfile+".new"
215         if self.options.verbose:
216             print 'Patching',specfile,'for',patch_dict.keys()
217         spec=open (specfile)
218         new=open(newspecfile,"w")
219
220         for line in spec.readlines():
221             if self.varmatcher.match(line):
222                 (var,value)=self.varmatcher.match(line).groups()
223                 if var in patch_dict.keys():
224                     new.write('%%define %s %s\n'%(var,patch_dict[var]))
225                     continue
226             new.write(line)
227         spec.close()
228         new.close()
229         os.rename(newspecfile,specfile)
230
231     def unignored_lines (self, logfile):
232         result=[]
233         for logline in file(logfile).readlines():
234             if logline.strip() == Module.svn_magic_line:
235                 break
236             result += logline
237         return result
238
239     def insert_changelog (self, logfile, oldtag, newtag):
240         specfile=self.guess_specname()
241         newspecfile=specfile+".new"
242         if self.options.verbose:
243             print 'Inserting changelog from %s into %s'%(logfile,specfile)
244         spec=open (specfile)
245         new=open(newspecfile,"w")
246         for line in spec.readlines():
247             new.write(line)
248             if re.compile('%changelog').match(line):
249                 dateformat="* %a %b %d %Y"
250                 datepart=time.strftime(dateformat)
251                 logpart="%s <%s> - %s %s"%(Module.config['username'],
252                                              Module.config['email'],
253                                              oldtag,newtag)
254                 new.write(datepart+" "+logpart+"\n")
255                 for logline in self.unignored_lines(logfile):
256                     new.write(logline)
257                 new.write("\n")
258         spec.close()
259         new.close()
260         os.rename(newspecfile,specfile)
261             
262     def show_dict (self, spec_dict):
263         if self.options.verbose:
264             for (k,v) in spec_dict.iteritems():
265                 print k,'=',v
266
267     def trunk_url (self):
268         return "%s/%s/trunk"%(Module.config['svnpath'],self.name)
269     def tag_name (self, spec_dict):
270         return "%s-%s-%s"%(spec_dict['name'],spec_dict[self.options.version],spec_dict[self.options.taglevel])
271     def tag_url (self, spec_dict):
272         return "%s/%s/tags/%s"%(Module.config['svnpath'],self.name,self.tag_name(spec_dict))
273
274     # locate specfile, parse it, check it and show values
275     def do_version (self):
276         self.init_moddir()
277         self.init_trunkdir()
278         self.revert_trunkdir()
279         self.update_trunkdir()
280         for (key,message) in Module.configKeys:
281             print key,':',Module.config[key]
282         print 'module:',self.name
283         print 'specfile:',self.guess_specname()
284         spec_dict = self.spec_dict()
285         for varname in self.varnames:
286             if not spec_dict.has_key(varname):
287                 print 'Could not find %%define for %s'%varname
288                 return
289             else:
290                 print varname+":",spec_dict[varname]
291
292     init_warning="""WARNING
293 The module-init function has the following limitations
294 * it does not handle changelogs
295 * it does not scan the -tags.mk files to adopt the new tags"""
296     def do_init(self):
297         if self.options.verbose:
298             print Module.init_warning
299             if not prompt('Want to proceed anyway'):
300                 return
301
302         self.init_moddir()
303         self.init_trunkdir()
304         self.revert_trunkdir()
305         self.update_trunkdir()
306         spec_dict = self.spec_dict()
307
308         trunk_url=self.trunk_url()
309         tag_name=self.tag_name(spec_dict)
310         tag_url=self.tag_url(spec_dict)
311         # check the tag does not exist yet
312         if Svnpath(tag_url,self.options).url_exists():
313             print 'Module %s already has a tag %s'%(self.name,tag_name)
314             return
315
316         self.run_prompt("Create initial tag",
317                         "svn copy --editor-cmd=%s %s %s"%(self.options.editor,trunk_url,tag_url))
318
319     def do_diff (self):
320         self.init_moddir()
321         self.init_trunkdir()
322         self.revert_trunkdir()
323         self.update_trunkdir()
324         spec_dict = self.spec_dict()
325         self.show_dict(spec_dict)
326
327         trunk_url=self.trunk_url()
328         tag_url=self.tag_url(spec_dict)
329         for url in [ trunk_url, tag_url ] :
330             if not Svnpath(url,self.options).url_exists():
331                 print 'Could not find svn URL %s'%url
332                 sys.exit(1)
333
334         self.run("svn diff %s %s"%(tag_url,trunk_url))
335
336     def patch_tags_files (self, tagsfile, oldname, newname):
337         newtagsfile=tagsfile+".new"
338         if self.options.verbose:
339             print 'Replacing %s into %s in %s'%(oldname,newname,tagsfile)
340         tags=open (tagsfile)
341         new=open(newtagsfile,"w")
342         matcher=re.compile("^(.*)%s(.*)"%oldname)
343         for line in tags.readlines():
344             if not matcher.match(line):
345                 new.write(line)
346             else:
347                 (begin,end)=matcher.match(line).groups()
348                 new.write(begin+newname+end+"\n")
349         tags.close()
350         new.close()
351         os.rename(newtagsfile,tagsfile)
352
353     def do_tag (self):
354         self.init_moddir()
355         self.init_trunkdir()
356         self.revert_trunkdir()
357         self.update_trunkdir()
358         spec_dict = self.spec_dict()
359         self.show_dict(spec_dict)
360         
361         # parse specfile, check that the old tag exists and the new one does not
362         trunk_url=self.trunk_url()
363         old_tag_name = self.tag_name(spec_dict)
364         old_tag_url=self.tag_url(spec_dict)
365         # increment taglevel
366         new_taglevel = str ( int (spec_dict[self.options.taglevel]) + 1)
367         spec_dict[self.options.taglevel] = new_taglevel
368         new_tag_name = self.tag_name(spec_dict)
369         new_tag_url=self.tag_url(spec_dict)
370         for url in [ trunk_url, old_tag_url ] :
371             if not Svnpath(url,self.options).url_exists():
372                 print 'Could not find svn URL %s'%url
373                 sys.exit(1)
374         if Svnpath(new_tag_url,self.options).url_exists():
375             print 'New tag\'s svn URL %s already exists ! '%url
376             sys.exit(1)
377
378         # side effect in trunk's specfile
379         self.patch_spec_var({self.options.taglevel:new_taglevel})
380
381         # prepare changelog file 
382         # we use the standard subversion magic string (see svn_magic_line)
383         # so we can provide useful information, such as version numbers and diff
384         # in the same file
385         changelog="/tmp/%s-%d.txt"%(self.name,os.getpid())
386         file(changelog,"w").write("""
387 %s
388 module %s
389 old tag %s
390 new tag %s
391 """%(Module.svn_magic_line,self.name,old_tag_url,new_tag_url))
392
393         if not self.options.verbose or prompt('Want to run diff',True):
394             self.run("(echo 'DIFF========='; svn diff %s %s) >> %s"%(old_tag_url,trunk_url,changelog))
395         # edit it        
396         self.run("%s %s"%(self.options.editor,changelog))
397         # insert changelog in spec
398         if self.options.changelog:
399             self.insert_changelog (changelog,old_tag_name,new_tag_name)
400
401         ## update build
402         build = Module(self.options.build,self.options)
403         build.init_moddir()
404         build.init_trunkdir()
405         build.revert_trunkdir()
406         build.update_trunkdir()
407         
408         for tagsfile in glob(build.trunkdir+"/*-tags.mk"):
409             if prompt("Want to check %s"%tagsfile):
410                 self.patch_tags_files(tagsfile,old_tag_name,new_tag_name)
411
412         paths=""
413         paths += self.trunkdir + " "
414         paths += build.trunkdir + " "
415         self.run_prompt("Check","svn diff " + paths)
416         self.run_prompt("Commit","svn commit --file %s %s"%(changelog,paths))
417         self.run_prompt("Create tag","svn copy --file %s %s %s"%(changelog,trunk_url,new_tag_url))
418
419         if self.options.debug:
420             print 'Preserving',changelog
421         else:
422             os.unlink(changelog)
423             
424 usage="""Usage: %prog options module1 [ .. modulen ]
425 Purpose:
426   manage subversion tags and specfile
427   requires the specfile to define name, version and taglevel
428 Available functions:
429   module-diff : show difference between trunk and latest tag
430   module-tag  : increment taglevel in specfile, insert changelog in specfile,
431                 create new tag and and adopt it in build/*-tags.mk
432   module-init : create initial tag
433   module-version : only check specfile and print out details"""
434
435 def main():
436     parser=OptionParser(usage=usage,version=subversion_id)
437     parser.add_option("-e","--editor", action="store", dest="editor", default="emacs",
438                       help="Specify editor")
439     parser.add_option("-c","--changelog", action="store_false", dest="changelog", default=True,
440                       help="Does not update changelog section in specfile when tagging")
441     parser.add_option("-m","--modules", action="store", dest="modules", default="modules",
442                       help="Name for topdir - defaults to modules")
443     parser.add_option("-b","--build", action="store", dest="build", default="build",
444                       help="Set module name for build")
445     parser.add_option("-t","--taglevel",action="store",dest="taglevel",default="taglevel",
446                       help="Specify an alternate spec variable for storing taglevel")
447     parser.add_option("-s","--version-string",action="store",dest="version",default="version",
448                       help="Specify an alternate spec variable for storing version")
449     parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False, 
450                       help="Run in verbose mode")
451     parser.add_option("-d","--debug", action="store_true", dest="debug", default=False, 
452                       help="Debug mode - mostly more verbose")
453     (options, args) = parser.parse_args()
454     if options.debug: options.verbose=True
455
456     if len(args) == 0:
457         parser.print_help()
458         sys.exit(1)
459     else:
460         Module.init_homedir(options)
461         for modname in args:
462             module=Module(modname,options)
463             if sys.argv[0].find("diff") >= 0:
464                 module.do_diff()
465             elif sys.argv[0].find("tag") >= 0:
466                 module.do_tag()
467             elif sys.argv[0].find("init") >= 0:
468                 module.do_init()
469             elif sys.argv[0].find("version") >= 0:
470                 module.do_version()
471             else:
472                 print "Unsupported command",sys.argv[0]
473                 parser.print_help()
474                 sys.exit(1)
475
476 # basically, we exit if anything goes wrong
477 if __name__ == "__main__" :
478     main()