-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change install prefix and cmake version parsing (#2530)
- Loading branch information
1 parent
6327f90
commit 96e197b
Showing
7 changed files
with
143 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the Apache 2.0 License. | ||
|
||
# pylint: disable=import-error, no-name-in-module | ||
from setuptools.extern.packaging.version import ( # type: ignore | ||
Version, | ||
InvalidVersion, | ||
) | ||
|
||
|
||
def remove_prefix(s, prefix): | ||
if s.startswith(prefix): | ||
return s[len(prefix) :] | ||
return s | ||
|
||
|
||
def replace_char(s, n, c): | ||
return s[:n] + str(c) + s[n + 1 :] | ||
|
||
|
||
def to_python_version(original): | ||
unprefixed = remove_prefix(original, "ccf-") | ||
|
||
# Try to parse this as a Version (with automatic normalisation). | ||
# If it fails, try making part of the suffix a local version specifier (+foo). | ||
# Keep expanding this suffix until you get a valid version, or run out of attempts. | ||
next_attempt = unprefixed | ||
next_replace = len(next_attempt) | ||
while True: | ||
try: | ||
version = Version(next_attempt) | ||
return version | ||
except InvalidVersion: | ||
next_replace = unprefixed.rfind("-", 0, next_replace) | ||
if next_replace == -1: | ||
break | ||
next_attempt = replace_char(unprefixed, next_replace, "+") | ||
|
||
raise ValueError(f"Cannot convert '{original}' to a Version") | ||
|
||
|
||
if __name__ == "__main__": | ||
# Run some tests that expected versions are correctly parsed | ||
v = to_python_version("1") | ||
assert v.release == (1,) | ||
|
||
v = to_python_version("1.2.3") | ||
assert v.release == (1, 2, 3) | ||
|
||
v = to_python_version("ccf-1.2.3") | ||
assert v.release == (1, 2, 3) | ||
|
||
v = to_python_version("ccf-1.2.3-a42") | ||
assert v.release == (1, 2, 3) | ||
assert v.pre == ("a", 42) | ||
|
||
v = to_python_version("ccf-1.2.3-rc1") | ||
assert v.release == (1, 2, 3) | ||
assert v.pre == ("rc", 1) | ||
|
||
v = to_python_version("ccf-1.2.3-dev2") | ||
assert v.release == (1, 2, 3) | ||
assert v.dev == 2 | ||
|
||
v = to_python_version("ccf-1.2.3-dev3-5-deadbeef") | ||
assert v.release == (1, 2, 3) | ||
assert v.dev == 3 | ||
assert v.local == "5.deadbeef" | ||
|
||
v = to_python_version("ccf-1.2.3-42-deadbeef") | ||
assert v.release == (1, 2, 3) | ||
assert v.post == 42 | ||
assert v.local == "deadbeef" |