-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathversion_chk.py
executable file
·109 lines (88 loc) · 2.83 KB
/
version_chk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
#
# Copyright (C) [2020] Futurewei Technologies, Inc.
#
# FORCE-RISCV is licensed under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
# OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
# NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Compares dot-separated version strings for testing min version requirements
Usage:
version_chk [-v | --verbose] <version_str> <required_version_str>
version_chk -t | --test
version_chk -h | --help
Options:
-v --verbose output intermediate results
-t --test Run self tests
-h --help Show this screen
"""
import re
from docopt.docopt import docopt
PASS = 0
FAIL = 1
RC = {PASS: "Passed", FAIL: "Failed"}
def compare_versions(value, required, verbose):
"""Compares the numerical value of two dot-separated version strings
:type value: str
:type required: str
"""
# allow for unexpected input for value.
# required is within our control.
val = [int(x) for x in re.split(r"\D+", value) if x]
req = [int(x) for x in required.split(sep=".")]
def log_return(result):
if verbose:
print("value = %14s, required = %14s, result = %s" % (val, req, RC[result]))
return result
for i, rev in enumerate(req):
try:
test_val = val[i]
except IndexError:
return log_return(FAIL)
if test_val == rev:
continue
elif test_val < rev:
return log_return(FAIL)
elif test_val > rev:
return log_return(PASS)
return log_return(PASS)
def self_test():
test_data = [
[" 2.3-23f", "2.3.4"],
[" 10.2.3", "2.3.4"],
[" 10.2.3", "2.3.4"],
["2.3.5", "2.3.4"],
["2.3.4", " 2.3.4"],
["2.3.3", "2.3.4"],
["2.4", "2.3.4"],
["2.3", "2.3.4"],
[" 2.3.4", "2"],
["2.2.4", "2. 3.4"],
["1.3.4", "2.3.4"],
[" 21.3.4", "2.3.4"],
["2.30.4", "2.3.4"],
]
for v, r in test_data:
compare_versions(v, r, True)
def main():
arguments = docopt(__doc__, version="version_chk.py v0.9")
verbose = arguments["--verbose"]
if arguments["--test"]:
print(arguments)
self_test()
exit(0)
elif arguments["<required_version_str>"] and arguments["<version_str>"]:
return compare_versions(
arguments["<version_str>"], arguments["<required_version_str>"], verbose
)
if __name__ == "__main__":
exit(main())