update_versions.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import io
  4. import os
  5. import re
  6. import sys
  7. import time
  8. def P(path):
  9. """
  10. Give 'path' as a path relative to the abs_top_srcdir environment
  11. variable.
  12. """
  13. return os.path.join(
  14. os.environ.get('abs_top_srcdir', "."),
  15. path)
  16. def warn(msg):
  17. """
  18. Print an warning message.
  19. """
  20. print("WARNING: {}".format(msg), file=sys.stderr)
  21. def find_version(infile):
  22. """
  23. Given an open file (or some other iterator of lines) holding a
  24. configure.ac file, find the current version line.
  25. """
  26. for line in infile:
  27. m = re.search(r'AC_INIT\(\[tor\],\s*\[([^\]]*)\]\)', line)
  28. if m:
  29. return m.group(1)
  30. return None
  31. def update_version_in(infile, outfile, regex, versionline):
  32. """
  33. Copy every line from infile to outfile. If any line matches 'regex',
  34. replace it with 'versionline'. Return True if any line was changed;
  35. false otherwise.
  36. 'versionline' is either a string -- in which case it is used literally,
  37. or a function that receives the output of 'regex.match'.
  38. """
  39. found = False
  40. have_changed = False
  41. for line in infile:
  42. m = regex.match(line)
  43. if m:
  44. found = True
  45. oldline = line
  46. if type(versionline) == type(u""):
  47. line = versionline
  48. else:
  49. line = versionline(m)
  50. if not line.endswith("\n"):
  51. line += "\n"
  52. if oldline != line:
  53. have_changed = True
  54. outfile.write(line)
  55. if not found:
  56. warn("didn't find any version line to replace in {}".format(infile.name))
  57. return have_changed
  58. def replace_on_change(fname, change):
  59. """
  60. If "change" is true, replace fname with fname.tmp. Otherwise,
  61. delete fname.tmp. Log what we're doing to stderr.
  62. """
  63. if not change:
  64. print("No change in {}".format(fname))
  65. os.unlink(fname+".tmp")
  66. else:
  67. print("Updating {}".format(fname))
  68. os.rename(fname+".tmp", fname)
  69. def update_file(fname,
  70. regex,
  71. versionline,
  72. encoding="utf-8"):
  73. """
  74. Replace any line matching 'regex' in 'fname' with 'versionline'.
  75. Do not modify 'fname' if there are no changes made. Use the
  76. provided encoding to read and write.
  77. """
  78. with io.open(fname, "r", encoding=encoding) as f, \
  79. io.open(fname+".tmp", "w", encoding=encoding) as outf:
  80. have_changed = update_version_in(f, outf, regex, versionline)
  81. replace_on_change(fname, have_changed)
  82. # Find out our version
  83. with open("configure.ac") as f:
  84. version = find_version(f)
  85. # If we have no version, we can't proceed.
  86. if version == None:
  87. print("No version found in configure.ac", file=sys.stderr())
  88. sys.exit(1)
  89. print("The version is {}".format(version))
  90. today = time.strftime("%Y-%m-%d", time.gmtime())
  91. # In configure.ac, we replace the definition of APPROX_RELEASE_DATE
  92. # with "{today} for {version}", but only if the version does not match
  93. # what is already there.
  94. def replace_fn(m):
  95. if m.group(1) != version:
  96. # The version changed -- we change the date.
  97. return u'AC_DEFINE(APPROX_RELEASE_DATE, ["{}"], # for {}'.format(today, version)
  98. else:
  99. # No changes.
  100. return m.group(0)
  101. update_file(P("configure.ac"),
  102. re.compile(r'AC_DEFINE\(APPROX_RELEASE_DATE.* for (.*)'),
  103. replace_fn)
  104. # In tor-mingw.nsi.in, we replace the definition of VERSION.
  105. update_file(P("contrib/win32build/tor-mingw.nsi.in"),
  106. re.compile(r'!define VERSION .*'),
  107. u'!define VERSION "{}"'.format(version),
  108. encoding="iso-8859-1")
  109. # In src/win32/orconfig.h, we replace the definition of VERSION.
  110. update_file(P("src/win32/orconfig.h"),
  111. re.compile(r'#define VERSION .*'),
  112. u'#define VERSION "{}"'.format(version))