Elffuss BraidHub

kikocis/lancedb public

main · 966 BRawPermalink
1"""
2Takes a list of semver strings and sorts them in ascending order.
3"""
4
5import sys
6from packaging.version import parse, InvalidVersion
7
8if __name__ == "__main__":
9 import argparse
10 parser = argparse.ArgumentParser()
11 parser.add_argument("prefix", default="v")
12 args = parser.parse_args()
13
14 # Read the input from stdin
15 lines = sys.stdin.readlines()
16
17 # Parse the versions
18 versions = []
19 for line in lines:
20 line = line.strip()
21 try:
22 version_str = line.removeprefix(args.prefix)
23 version = parse(version_str)
24 except InvalidVersion:
25 # There are old tags that don't follow the semver format
26 print(f"Invalid version: {line}", file=sys.stderr)
27 continue
28 versions.append((line, version))
29
30 # Sort the versions
31 versions.sort(key=lambda x: x[1])
32
33 # Print the sorted versions as original strings
34 for line, _ in versions:
35 print(line)