Disable openvswitch for rc26 build
[build.git] / norm-tags.py
1 #!/usr/bin/env python3.2
2 #
3 # perform comparison of tags contents between planet-lab and onelab tags
4 #
5
6 import sys
7 import re
8 from argparse import ArgumentParser
9 import subprocess
10
11 class TagsFile:
12     
13     def __init__ (self, filename):
14         self.filename=filename
15
16     m_comment =         re.compile('^\s*#')
17     m_empty =           re.compile('^\s*$')
18     m_gitpath =         re.compile(\
19         '^\s*(?P<module>[\w\.\-]+)-GITPATH\s*'+
20         ':=\s*git://(?P<host>[\w\.\-]+)/'+
21         '(?P<repo>[\w\.\-]+)(?P<tag>[@\w/:_\.\-]+)?\s*$')
22     m_svnpath =         re.compile(\
23         '^\s*(?P<module>[\w\-]+)-SVNPATH\s*'+
24         ':=\s*http://(?P<host>[\w\.\-]+)/'+
25         '(?P<svnpath>[\w/:_\.\-]+)\s*$')
26     m_branch =          re.compile(\
27         '^\s*(?P<module>[\w\.\-]+)-BRANCH\s*:=\s*(?P<branch>[\w]+)\s*$')
28
29     def _parse (self,o):
30         self.git={}
31         self.svn={}
32         self.branch={}
33         with open(self.filename) as i:
34             for line in i.readlines():
35                 line=line.strip()
36                 match=TagsFile.m_empty.match(line)
37                 if match: continue
38                 match=TagsFile.m_comment.match(line)
39                 if match: continue
40                 match=TagsFile.m_gitpath.match(line)
41                 if match:
42                     (module,host,repo,tag)=match.groups()
43                     if tag: tag=tag.replace('@','')
44                     if module in self.git: print ('Warning: duplicate GITPATH for',module,file=o)
45                     self.git[module]=tag
46                     continue
47                 match=TagsFile.m_svnpath.match(line)
48                 if match:
49                     (module,host,svnpath)=match.groups()
50                     tag=svnpath.split('/')[-1]
51                     if module in self.svn: print ('Warning: duplicate SVNPATH for',module,file=o)
52                     self.svn[module]=tag
53                     continue
54                 match=TagsFile.m_branch.match(line)
55                 if match:
56                     (module,branch)=match.groups()
57                     if module in self.branch: print ('Warning: duplicate BRANCH for',module,file=o)
58                     self.branch[module]=branch
59                     continue
60                 print ("%-020s"%"ignored",line,file=o)
61         # outputs relevant info
62         for n in ['branch','git','svn']:
63             d=getattr(self,n)
64             keys=list(d.keys())
65             keys.sort()
66             for key in keys: print ("%-020s %-20s %s"%(n,key,d[key]),file=o)
67
68     def norm(self): return self.filename+'.norm'
69                 
70     def parse(self):
71         with open(self.norm(),'w') as f:
72             self._parse(f)
73         print ("(Over)wrote",self.norm())
74
75 # basic usage for now 
76 # $0 tagsfile1 tagsfile2
77 def main ():
78     parser=ArgumentParser(description="Create a normalized (.norm) file for each input, may run diff on them")
79     parser.add_argument('tagsnames',
80                         metavar='tagsfile',
81                         nargs='+',
82                         help="tags file names")
83     parser.add_argument("-d","--diff",action="store_true",dest="run_diff",default=False,
84                         help="runs diff on the normalized outputs - requires exactly 2 args")
85
86     apres=parser.parse_args()
87     for tagsname in apres.tagsnames: 
88         TagsFile(tagsname).parse()
89     if apres.run_diff and len(apres.tagsnames)==2:
90         tf1=TagsFile(apres.tagsnames[0])
91         tf2=TagsFile(apres.tagsnames[1])
92         print ("%s <-- --> %s"%(tf1.norm(),tf2.norm()))
93         command = "diff %s %s"%(tf1.norm(),tf2.norm())
94         subprocess.Popen(command,shell=True)
95
96 if __name__ == '__main__': 
97     main()