from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import io
import os
import re
import sys
import time
def P(path):
return os.path.join(
os.environ.get('abs_top_srcdir', "."),
path)
def warn(msg):
print("WARNING: {}".format(msg), file=sys.stderr)
def find_version(infile):
for line in infile:
m = re.search(r'AC_INIT\(\[tor\],\s*\[([^\]]*)\]\)', line)
if m:
return m.group(1)
return None
def update_version_in(infile, outfile, regex, versionline):
found = False
have_changed = False
for line in infile:
m = regex.match(line)
if m:
found = True
oldline = line
if type(versionline) == type(u""):
line = versionline
else:
line = versionline(m)
if not line.endswith("\n"):
line += "\n"
if oldline != line:
have_changed = True
outfile.write(line)
if not found:
warn("didn't find any version line to replace in {}".format(infile.name))
return have_changed
def replace_on_change(fname, change):
if not change:
print("No change in {}".format(fname))
os.unlink(fname+".tmp")
else:
print("Updating {}".format(fname))
os.rename(fname+".tmp", fname)
def update_file(fname,
regex,
versionline,
encoding="utf-8"):
with io.open(fname, "r", encoding=encoding) as f, \
io.open(fname+".tmp", "w", encoding=encoding) as outf:
have_changed = update_version_in(f, outf, regex, versionline)
replace_on_change(fname, have_changed)
with open(P("configure.ac")) as f:
version = find_version(f)
if version == None:
print("No version found in configure.ac", file=sys.stderr())
sys.exit(1)
print("The version is {}".format(version))
today = time.strftime("%Y-%m-%d", time.gmtime())
def replace_fn(m):
if m.group(1) != version:
return u'AC_DEFINE(APPROX_RELEASE_DATE, ["{}"], # for {}'.format(today, version)
else:
return m.group(0)
update_file(P("configure.ac"),
re.compile(r'AC_DEFINE\(APPROX_RELEASE_DATE.* for (.*)'),
replace_fn)
update_file(P("contrib/win32build/tor-mingw.nsi.in"),
re.compile(r'!define VERSION .*'),
u'!define VERSION "{}"'.format(version),
encoding="iso-8859-1")
update_file(P("src/win32/orconfig.h"),
re.compile(r'#define VERSION .*'),
u'#define VERSION "{}"'.format(version))