3 # Future imports for Python 2.7, mandatory in 3.0
4 from __future__
import division
5 from __future__
import print_function
6 from __future__
import unicode_literals
16 Give 'path' as a path relative to the abs_top_srcdir environment
20 os
.environ
.get('abs_top_srcdir', "."),
25 Print an warning message.
27 print("WARNING: {}".format(msg
), file=sys
.stderr
)
29 def find_version(infile
):
31 Given an open file (or some other iterator of lines) holding a
32 configure.ac file, find the current version line.
35 m
= re
.search(r
'AC_INIT\(\[tor\],\s*\[([^\]]*)\]\)', line
)
41 def update_version_in(infile
, outfile
, regex
, versionline
):
43 Copy every line from infile to outfile. If any line matches 'regex',
44 replace it with 'versionline'. Return True if any line was changed;
47 'versionline' is either a string -- in which case it is used literally,
48 or a function that receives the output of 'regex.match'.
57 if type(versionline
) == type(u
""):
61 if not line
.endswith("\n"):
68 warn("didn't find any version line to replace in {}".format(infile
.name
))
72 def replace_on_change(fname
, change
):
74 If "change" is true, replace fname with fname.tmp. Otherwise,
75 delete fname.tmp. Log what we're doing to stderr.
78 print("No change in {}".format(fname
))
79 os
.unlink(fname
+".tmp")
81 print("Updating {}".format(fname
))
82 os
.rename(fname
+".tmp", fname
)
85 def update_file(fname
,
90 Replace any line matching 'regex' in 'fname' with 'versionline'.
91 Do not modify 'fname' if there are no changes made. Use the
92 provided encoding to read and write.
94 with io
.open(fname
, "r", encoding
=encoding
) as f
, \
95 io
.open(fname
+".tmp", "w", encoding
=encoding
) as outf
:
96 have_changed
= update_version_in(f
, outf
, regex
, versionline
)
98 replace_on_change(fname
, have_changed
)
100 # Find out our version
101 with
open(P("configure.ac")) as f
:
102 version
= find_version(f
)
104 # If we have no version, we can't proceed.
106 print("No version found in configure.ac", file=sys
.stderr())
109 print("The version is {}".format(version
))
111 today
= time
.strftime("%Y-%m-%d", time
.gmtime())
113 # In configure.ac, we replace the definition of APPROX_RELEASE_DATE
114 # with "{today} for {version}", but only if the version does not match
115 # what is already there.
117 if m
.group(1) != version
:
118 # The version changed -- we change the date.
119 return u
'AC_DEFINE(APPROX_RELEASE_DATE, ["{}"], # for {}'.format(today
, version
)
123 update_file(P("configure.ac"),
124 re
.compile(r
'AC_DEFINE\(APPROX_RELEASE_DATE.* for (.*)'),
127 # In tor-mingw.nsi.in, we replace the definition of VERSION.
128 update_file(P("contrib/win32build/tor-mingw.nsi.in"),
129 re
.compile(r
'!define VERSION .*'),
130 u
'!define VERSION "{}"'.format(version
),
131 encoding
="iso-8859-1")