]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - misc/make-version.py
a4f68164447dec03f32bc53b508ea762b046943d
[tahoe-lafs/tahoe-lafs.git] / misc / make-version.py
1 #! /usr/bin/python
2
3 """
4 Create src/allmydata/version.py, based upon the latest darcs release tag.
5
6 If your source tree is coming from darcs (i.e. there exists a _darcs
7 directory), this tool will determine the most recent release tag, count the
8 patches that have been applied since then, and compute a version number to be
9 written into version.py . This version number will be available by doing:
10
11  from allmydata import __version__
12
13 Source trees that do not come from darcs (release tarballs, nightly tarballs)
14 do not have a _darcs directory. Instead, they should have a version.py that
15 was generated before the tarball was produced. In this case, this script will
16 quietly exit without modifying the existing version.py .
17
18 FYI, src/allmydata/__init__.py will attempt to import version.py and use the
19 version number therein. If it cannot, it will announce a version of
20 'UNKNOWN'. This should only happen if someone manages to get hold of a
21 non-_darcs/ source tree.
22
23 'release tags' are tags in the tahoe source tree that match the following
24 regexp:
25
26  ^allmydata-tahoe-\d+\.\d+\.\d+\w*$
27
28 This excludes zfec tags (which start with 'zfec '). It also excludes
29 'developer convenience tags', which look like 'hoping to fix bug -warner'.
30 (the original goal was to use release tags that lacked the 'allmydata-tahoe-'
31 prefix, but it turns out to be more efficient to keep it in, because I can't
32 get 'darcs changes --from-tag=' to accept real regexps).
33
34 """
35
36 import os, sys, commands, re
37 import xml.dom.minidom
38
39 def get_text(nodelist):
40     rc = ""
41     for node in nodelist:
42         if node.nodeType == node.TEXT_NODE:
43             rc = rc + node.data
44     return rc
45
46 VERSION_BODY = '''
47 from util.version import Version
48
49 # This is the version of this tree, as created by misc/make-version.py from
50 # the Darcs patch information: the main version number is taken from the most
51 # recent release tag. If some patches have been added since the last release,
52 # this will have a -NN "build number" suffix. Please see
53 # allmydata.util.version for a description of what the different fields mean.
54
55 verstr = "%s"
56 __version__ = Version(verstr)
57 '''
58
59 def write_version_py(verstr):
60     f = open("src/allmydata/version.py", "wt")
61     f.write(VERSION_BODY % (verstr,))
62     f.close()
63
64 def update():
65     if not os.path.exists("_darcs") or not os.path.isdir("_darcs"):
66         if os.path.exists("src/allmydata/version.py"):
67             print "no _darcs/ and version.py exists, leaving it alone"
68             return 0
69         print "no _darcs/ but no version.py either: how did you get this tree?"
70         return 0
71     cmd = "darcs changes --from-tag=^allmydata-tahoe --xml-output"
72     (rc, output) = commands.getstatusoutput(cmd)
73     if rc != 0:
74         print "unable to run 'darcs changes':"
75         print output
76         print "so I'm leaving version.py alone"
77         return 0
78
79     # windows' weird ssh process prepends some 'plink: unknown option "-O"'
80     # junk to the beginning of the otput. To overcome this, manually scan for
81     # the opening <changelog> tag before giving anything to the xml parser.
82
83     output = output[output.find("<changelog>"):]
84
85     try:
86         doc = xml.dom.minidom.parseString(output)
87     except xml.parsers.expat.ExpatError:
88         print "unable to parse darcs XML output:"
89         print output
90         raise
91     changelog = doc.getElementsByTagName("changelog")[0]
92     patches = changelog.getElementsByTagName("patch")
93     count = 0
94     version_re = re.compile("^TAG allmydata-tahoe-(\d+\.\d+\.\d+\w*)$")
95     for patch in patches:
96         name = get_text(patch.getElementsByTagName("name")[0].childNodes)
97         m = version_re.match(name)
98         if m:
99             last_tag = m.group(1)
100             last_tag = last_tag.encode("ascii")
101             break
102         count += 1
103     else:
104         print "unable to find a matching tag"
105         print output
106         print "so I'm leaving version.py alone"
107         return 0
108
109     if count:
110         # this is an interim version
111         verstr = "%s-%d" % (last_tag, count)
112     else:
113         # this is a release
114         verstr = last_tag
115
116     write_version_py(verstr)
117     print "wrote '%s' into src/allmydata/version.py" % (verstr,)
118     return 0
119
120 if __name__ == '__main__':
121     rc = update()
122     sys.exit(rc)
123