root/branches/Carsten_PtrWork2/Tools/scons-build/RevisionTagWriter.py

Revision 563, 4.1 kB (checked in by cneumann, 2 years ago)

changed: moved some utility code to separate files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1
2 import os;
3 import os.path;
4
5 pj = os.path.join;
6
7 # If we have pysvn, load it
8 try:
9     import pysvn
10     have_pysvn = True
11 except:
12     have_pysvn = False
13
14
15 class RevisionTagWriter(object):
16     """
17     For each library the corresponding <Libname>Def.cpp file is located and
18     the highest revision number of any of the lib's source files is recorded
19     together with a marker if the lib contains local changes.
20     """
21     def __init__(self, libMap):
22         """
23         Create a new tag writer that uses the libraries from the given libMap.
24         Also checks if pysvn is available - without it tagging is not possible.
25         """
26         if not have_pysvn:
27             raise "The module pysvn is required to perform revision tagging!"
28        
29         self.libMap        = libMap
30         self.maxRev        = 0
31         self.svnClient     = pysvn.Client()
32         self.versionString = open("VERSION").readline().strip()
33    
34     def run(self):
35         """
36         Write revision tags.
37         """
38         for (libName, libObj) in self.libMap.iteritems():
39             libMaxRev   = 0
40             modifiedStr = ""
41            
42             for file in libObj.source_files + libObj.header_files:
43                 filename = pj("Source", file)
44                
45                 fileRev, fileModified = self._getSVNInfo(filename)
46                
47                 if libMaxRev < fileRev:
48                     libMaxRev = fileRev
49                
50                 # we modify the <libname>Def.cpp files ourselves
51                 if fileModified and filename[-7:] != "Def.cpp":
52                     print "%s: file %s is modified!" % (libName, filename)
53                     modifiedStr = " !MODIFIED!"
54            
55             if modifiedStr != "":
56                 print "%s: There are modified files, revision might be inaccurate!" % libName
57             print "%s: Highest revision: %d." % (libName, libMaxRev)
58            
59             if self.maxRev < libMaxRev:
60                 self.maxRev = libMaxRev
61            
62             for file in libObj.source_files:
63                 if file[-7:] == "Def.cpp":
64                     filename = pj("Source", file)
65                     fileInfo = self.svnClient.info(filename)
66                     repoPath = fileInfo.url[len(fileInfo.repos):-len(filename)-1]
67                    
68                     fileContents = open(filename).readlines()
69                     for i in range(len(fileContents)):
70                         if fileContents[i][:22] == '#define SVN_REVISION "':
71                             fileContents[i] = '#define SVN_REVISION "%d  (%s)%s"\n' % (libMaxRev, repoPath, modifiedStr)
72                             break
73                     open(filename, "w").writelines(fileContents)
74        
75         # Update the documentation mainfile
76         # Find the high version for stuff in Doc/, too
77         modifiedStr = ""
78         for file in glob.glob("Doc/*"):
79             print file
80             fileRev, fileMod = self._getSVNInfo(file)
81            
82             if self.maxRev < fileRev:
83                 self.maxRev = fileRev
84             if fileMod:
85                 modifiedStr = " !Modified!"
86        
87         filename     = "Doc/mainpage.dox"
88         fileContents = open(filename).readlines()
89         for i in range(len(fileContents)):
90             if fileContents[i][:7] == 'version':
91                 fileContents[i] = 'version %s r%d %s\n' % (self.versionString, self.maxRev, modifiedStr)
92                 break
93         open(filename,'w').writelines(fileContents)
94
95     def _getSVNInfo(self, filename):
96         """
97            Get the svn info for the given file.
98            Returns a version, modified tuple.
99         """
100         fileInfo   = self.svnClient.info  (filename)
101         fileStatus = self.svnClient.status(filename)
102        
103         fileRev = 0
104         fileMod = False
105        
106         # Ignore unversioned files
107         if pysvn.wc_status_kind.unversioned != fileStatus[0].text_status and \
108            pysvn.wc_status_kind.ignored     != fileStatus[0].text_status:
109             fileRev = fileInfo.revision.number
110             fileMod = pysvn.wc_status_kind.modified == fileStatus[0].text_status
111
112         return fileRev, fileMod
Note: See TracBrowser for help on using the browser.