summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEdwin Mons <edwin.mons@isode.com>2019-01-21 09:09:48 (GMT)
committerKevin Smith <kevin.smith@isode.com>2019-01-21 16:51:15 (GMT)
commit9a27bc87984613a63cb6cfb651282b824df51c81 (patch)
tree4fc1c445bbc27cd7df2645270585105faca69c5e /BuildTools/SCons/Version.py
parent54c71ab51b6c8d94492168e9cf6cf6045d7794f3 (diff)
downloadswift-9a27bc87984613a63cb6cfb651282b824df51c81.zip
swift-9a27bc87984613a63cb6cfb651282b824df51c81.tar.bz2
Make Version.py compatible with both Py2 and Py3
The merge of 4.x changes onto master introduced changes to Pyhon scripts that weren't compatible with Python 3. The Version.py script is now compatible with both Python 2 and Python 3, and a slightly underimplemented test has been expanded to actually live up to its name. getGitBuildVersion has been refactored completely. It has been simplified to require only one invocation of git describe, unit tests for the parsing have been added, and a way to call getGitBuildVersion from the commandline has been added to Version.py (to avoid adding a unit test that would call out to an external tool). DocBook.py has been made compatible with Python3, and some additional logic to prevent emitting b'path/to/docbook/xml' instead of the desired 'path/to/docbook/xml' has been added. Generation of COPYING in Swift/QtUI now uses the upstream version provided with SCons 3, and our custom version has been removed. Unused code in the SwiftenDevelopersGuide SConscript with invalid regular expressions has been removed, and the remaining regular expressions in SConscripts have been fixed. Test-Information Code has been tested on macOS 10.14. Scons now completes a build on a clean tree using either Python 2 or Python 3. Running it on a previouly built tree works with either, as well, mixing versions between invocations is not an issue. Swift unit tests pass with Python 3.7.0. Version.py unit tests pass with Python 2.7.15 and 3.7.0. Running with doc=1 works with both Python 2 and Python 3, even if the docbook points to a path with unicode characters in it. Resulting COPYING file has been verified both visually and against a Python2 generated one on master. Resulting XML files for documentation have been inspected. Resulting manual HTML and PDF files have been inspected. Change-Id: I54de909d80b8e35a8c351261ae10ce3537729c84
Diffstat (limited to 'BuildTools/SCons/Version.py')
-rw-r--r--BuildTools/SCons/Version.py72
1 files changed, 51 insertions, 21 deletions
diff --git a/BuildTools/SCons/Version.py b/BuildTools/SCons/Version.py
index 001374a..23cd850 100644
--- a/BuildTools/SCons/Version.py
+++ b/BuildTools/SCons/Version.py
@@ -1,26 +1,34 @@
+from __future__ import print_function
import subprocess, os, datetime, re, os.path, sys, unittest
-def getGitBuildVersion(root, project) :
- tag = git("describe --tags --exact --match \"" + project + "-*\"", root)
- if tag :
- return tag.rstrip()[len(project)+1:]
- tag = git("describe --tags --match \"" + project + "-*\"", root)
- if tag :
- m = re.match(project + "-(.*)-(.*)-(.*)", tag)
- if m :
- return m.group(1) + "-dev" + m.group(2)
+def gitDescribeToVersion(tag, tagPrefix):
+ m = re.match(r'^' + tagPrefix + r'-(.+?)(?:-(.+?)-(.+?))?$', tag)
+ if not m:
+ return None
+ result = m.group(1)
+ if m.group(2):
+ result += "-dev" + m.group(2)
+ return result
+
+def getGitBuildVersion(root, tagPrefix) :
+ tag = git("describe --tags --match \"" + tagPrefix + "-*\"", root)
+ if tag:
+ return gitDescribeToVersion(tag, tagPrefix)
return None
-def git(cmd, root) :
+def git(cmd, root):
full_cmd = "git " + cmd
+ # Would've used with .. as p, but that's not supported by Python 2.7
p = subprocess.Popen(full_cmd, cwd=root, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=(os.name != "nt"))
gitVersion = p.stdout.read()
- # error = p.stderr.read()
- # if error:
- # print("Git error: " + error)
- p.stdin.close()
- if p.wait() == 0 :
- return gitVersion
+ try:
+ p.stdin.close()
+ p.stdout.close()
+ p.stderr.close()
+ except:
+ pass
+ if p.wait() == 0:
+ return bytes(gitVersion).decode('utf-8')
return None
def getBuildVersion(root, project) :
@@ -95,7 +103,7 @@ def convertToWindowsVersion(version):
return (major, minor, patch)
# Test Windows version mapping scheme
-class convertToWindowsVersionTest(unittest.TestCase):
+class TestCases(unittest.TestCase):
def testWindowsVersionsAreDescending(self):
versionStringsWithOldVersions = [
("5.0rc11", None),
@@ -111,13 +119,13 @@ class convertToWindowsVersionTest(unittest.TestCase):
("4.0rc5", (4, 0, 50000)),
("4.0rc4", (4, 0, 40000)),
("4.0rc3", (4, 0, 30000)),
+ ('4.0rc2-dev39', (4, 0, 20039)),
('4.0rc2-dev34', (4, 0, 20034)),
('4.0rc2-dev33', (4, 0, 20033)),
('4.0rc2-dev31', (4, 0, 20031)),
('4.0rc2-dev30', (4, 0, 20030)),
('4.0rc2-dev29', (4, 0, 20029)),
('4.0rc2-dev27', (4, 0, 20027)),
- ('4.0rc2-dev39', (4, 0, 20039)),
('4.0rc2', (4, 0, 20000)),
('4.0rc1', (4, 0, 10000)),
('4.0beta2-dev203', (4, 0, 2203)),
@@ -163,7 +171,17 @@ class convertToWindowsVersionTest(unittest.TestCase):
('3.0alpha-dev524', (3, 0, 524)),
('3.0alpha-dev515', (3, 0, 515)),
]
- windowsVersionMapping = list(map(lambda (x,y): (x, convertToWindowsVersion(x)), versionStringsWithOldVersions))
+ prevParsed = None
+ prevOldVersion = None
+ for string, oldVersion in versionStringsWithOldVersions:
+ parsed = convertToWindowsVersion(string)
+ if prevOldVersion and oldVersion:
+ self.assertTrue(oldVersion <= prevOldVersion, "Old version %r must come before %r" % (oldVersion, prevOldVersion))
+ if prevParsed:
+ self.assertTrue(parsed < prevParsed, "%s => %r must be smaller than %s => %r" % (string, parsed, prevString, prevParsed))
+ prevString = string
+ prevParsed = parsed
+ prevOldVersion = oldVersion
def testThatBetaIsHigherThanAlpha(self):
self.assertTrue(convertToWindowsVersion("3.0beta0") > convertToWindowsVersion("3.0alpha0"))
@@ -190,12 +208,24 @@ class convertToWindowsVersionTest(unittest.TestCase):
self.assertTrue(convertToWindowsVersion("3.0") > convertToWindowsVersion("3.0rc1"))
self.assertTrue(convertToWindowsVersion("3.0") > convertToWindowsVersion("3.0rc11"))
+ def testGitDescribeToVersion(self):
+ self.assertEqual("5.0alpha2-dev100", gitDescribeToVersion("swift-5.0alpha2-100-g33d5f6571", "swift"))
+ self.assertEqual("5.0alpha2", gitDescribeToVersion("swift-5.0alpha2", "swift"))
+ self.assertEqual("5.0-dev1", gitDescribeToVersion("swift-5.0-1-g012312312", "swift"))
+ self.assertIsNone(gitDescribeToVersion("swiften-5.0-1-g012312312", "swift"))
+ self.assertIsNone(gitDescribeToVersion("quickly-5.0-1-g012312312", "swift"))
+ self.assertIsNone(gitDescribeToVersion("swift-", "swift"))
+ self.assertIsNone(gitDescribeToVersion("swift", "swift"))
+
if __name__ == '__main__':
if len(sys.argv) == 1:
unittest.main()
elif len(sys.argv) == 2:
- print convertToWindowsVersion(sys.argv[1])
+ if sys.argv[1] == "--git-build-version":
+ print(getGitBuildVersion(os.path.dirname(os.path.dirname(__file__)), "swift"))
+ else:
+ print(convertToWindowsVersion(sys.argv[1]))
sys.exit(0)
else:
- print "Error: Simply run the script without arguments or pass a single argument."
+ print("Error: Simply run the script without arguments or pass a single argument.")
sys.exit(-1)