~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Tools/SubWCRev.py

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
ImportĀ upstreamĀ versionĀ 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# FreeCAD RevInfo script to get the revision information from Subversion.
 
2
# (c) 2006 Werner Mayer
 
3
#
 
4
# Under Linux the Subversion tool SubWCRev shipped with TortoiseSVN isn't 
 
5
# available which is provided by this script. 
 
6
 
 
7
#***************************************************************************
 
8
#*   Copyright (c) 2006 Werner Mayer <werner.wm.mayer@gmx.de>              *
 
9
#*                                                                         *
 
10
#*   This file is part of the FreeCAD CAx development system.              *
 
11
#*                                                                         *
 
12
#*   This program is free software; you can redistribute it and/or modify  *
 
13
#*   it under the terms of the GNU General Public License (GPL)            *
 
14
#*   as published by the Free Software Foundation; either version 2 of     *
 
15
#*   the License, or (at your option) any later version.                   *
 
16
#*   for detail see the LICENCE text file.                                 *
 
17
#*                                                                         *
 
18
#*   FreeCAD is distributed in the hope that it will be useful,            *
 
19
#*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
20
#*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
21
#*   GNU Library General Public License for more details.                  *
 
22
#*                                                                         *
 
23
#*   You should have received a copy of the GNU Library General Public     *
 
24
#*   License along with FreeCAD; if not, write to the Free Software        *
 
25
#*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
 
26
#*   USA                                                                   *
 
27
#*                                                                         *
 
28
#***************************************************************************
 
29
 
 
30
#!/usr/bin/python
 
31
import os,sys,string,re,time
 
32
import xml.sax
 
33
import xml.sax.handler
 
34
import xml.sax.xmlreader
 
35
import StringIO
 
36
 
 
37
# SAX handler to parse the subversion output
 
38
class SvnHandler(xml.sax.handler.ContentHandler):
 
39
  def __init__(self):
 
40
    self.inUrl = 0
 
41
    self.inDate = 0
 
42
    self.mapping = {}
 
43
 
 
44
  def startElement(self, name, attributes):
 
45
    if name == "entry":
 
46
      self.buffer = ""
 
47
      self.mapping["Rev"] = attributes["revision"]
 
48
    elif name == "url":
 
49
      self.inUrl = 1
 
50
    elif name == "date":
 
51
      self.inDate = 1
 
52
 
 
53
  def characters(self, data):
 
54
    if self.inUrl:
 
55
      self.buffer += data
 
56
    elif self.inDate:
 
57
      self.buffer += data
 
58
 
 
59
  def endElement(self, name):
 
60
    if name == "url":
 
61
      self.inUrl = 0
 
62
      self.mapping["Url"] = self.buffer
 
63
      self.buffer = ""
 
64
    elif name == "date":
 
65
      self.inDate = 0
 
66
      self.mapping["Date"] = self.buffer
 
67
      self.buffer = ""
 
68
 
 
69
 
 
70
def main():
 
71
        #if(len(sys.argv) != 2):
 
72
        #    sys.stderr.write("Usage:  SubWCRev \"`svn info .. --xml`\"\n")
 
73
 
 
74
        parser=xml.sax.make_parser()
 
75
        handler=SvnHandler()
 
76
        parser.setContentHandler(handler)
 
77
 
 
78
        #Create an XML stream with the required information and read in with a SAX parser
 
79
        Ver=os.popen("svnversion . -n").read()
 
80
        Info=os.popen("svn info . --xml").read()
 
81
        try:
 
82
                inpsrc = xml.sax.InputSource()
 
83
                strio=StringIO.StringIO(Info)
 
84
                inpsrc.setByteStream(strio)
 
85
                parser.parse(inpsrc)
 
86
        except:
 
87
                sys.stderr.write("No svn repository\n")
 
88
                sys.exit(2)
 
89
 
 
90
        #Information of the Subversion stuff
 
91
        Url = handler.mapping["Url"]
 
92
        Rev = handler.mapping["Rev"]
 
93
        Date = handler.mapping["Date"]
 
94
        Date = Date[:19]
 
95
        #Same format as SubWCRev does
 
96
        Date = string.replace(Date,'T',' ')
 
97
        Date = string.replace(Date,'-','/')
 
98
 
 
99
        #Date is given as GMT. Now we must convert to local date.
 
100
        m=time.strptime(Date,"%Y/%m/%d %H:%M:%S")
 
101
        #Copy the tuple and set tm_isdst to 0 because it's GMT
 
102
        l=(m.tm_year,m.tm_mon,m.tm_mday,m.tm_hour,m.tm_min,m.tm_sec,m.tm_wday,m.tm_yday,0)
 
103
        #Take timezone into account
 
104
        t=time.mktime(l)-time.timezone
 
105
        Date=time.strftime("%Y/%m/%d %H:%M:%S",time.localtime(t))
 
106
 
 
107
        #Get the current local date
 
108
        Time = time.strftime("%Y/%m/%d %H:%M:%S")
 
109
 
 
110
        Mods = 'Src not modified'
 
111
        Mixed = 'Src not mixed'
 
112
        Range = Rev
 
113
 
 
114
        # if version string ends with an 'M'
 
115
        r=re.search("M$",Ver)
 
116
        if r != None:
 
117
            Mods = 'Src modified'
 
118
 
 
119
        # if version string contains a range
 
120
        r=re.match("^\\d+\\:\\d+",Ver)
 
121
        if r != None:
 
122
            Mixed = 'Src mixed'
 
123
            Range = Ver[:r.end()]
 
124
 
 
125
        # Open the template file and the version file
 
126
        file = open("./src/Build/Version.h.in")
 
127
        lines = file.readlines()
 
128
        file.close()
 
129
        out  = open("./src/Build/Version.h","w");
 
130
 
 
131
        for line in lines:
 
132
            line = string.replace(line,'$WCREV$',Rev)
 
133
            line = string.replace(line,'$WCDATE$',Date)
 
134
            line = string.replace(line,'$WCRANGE$',Range)
 
135
            line = string.replace(line,'$WCURL$',Url)
 
136
            line = string.replace(line,'$WCNOW$',Time)
 
137
            line = string.replace(line,'$WCMODS?Src modified:Src not modified$',Mods)
 
138
            line = string.replace(line,'$WCMIXED?Src mixed:Src not mixed$',Mixed)
 
139
            # output
 
140
            out.write(line)
 
141
 
 
142
        out.write('\n')
 
143
if __name__ == "__main__":
 
144
        main()