Skip to content

Commit 46e554e

Browse files
utziggustavonihei
authored andcommitted
ci: Fix compatibility with packaging==22
packaging >= 22 dropped support for LegacyVersion, which was the usual result of an invalid version number being parsed. Now it is PEP-440 strict and throws an exception on fails. This fixes the script to work with both older and newer releases. Signed-off-by: Fabio Utzig <[email protected]>
1 parent 9d3fd7f commit 46e554e

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

ci/compare_versions.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from packaging.version import parse, LegacyVersion
15+
from packaging.version import parse, InvalidVersion
1616
import argparse
1717
import sys
1818

19+
try:
20+
from packaging.version import LegacyVersion
21+
except ImportError:
22+
LegacyVersion = () # trick isinstance!
23+
1924
# exit with 0 if --new is equal to --old
2025
# exit with 1 on errors
2126
# exit with 2 if --new is newer than --old
@@ -30,9 +35,20 @@
3035
parser.print_help()
3136
exit(1)
3237

33-
old, new = parse(args.old), parse(args.new)
38+
# packaging>=22 only supports PEP-440 version numbers, and a non-valid version
39+
# will throw InvalidVersion. Previous packaging releases would create a
40+
# LegacyVersion object if the given version string failed to parse as PEP-440,
41+
# and since we use versions closer to semver, we want to fail in that case.
42+
43+
versions = []
44+
for version in [args.old, args.new]:
45+
try:
46+
versions.append(parse(version))
47+
except InvalidVersion:
48+
print("Invalid version parsed: {}".format(version))
49+
sys.exit(1)
3450

35-
# only accept versions that were correctly parsed
51+
old, new = versions[0], versions[1]
3652
for version in [old, new]:
3753
if isinstance(version, LegacyVersion):
3854
print("Invalid version parsed: {}".format(version))

0 commit comments

Comments
 (0)