Safe-ish version number list parsing from stdin
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
#!/usr/bin/env python | |
from __future__ import print_function | |
import sys | |
def _leloo_multiparse(v): | |
parse_fn = None | |
try: | |
import pkg_resources | |
parse_fn = pkg_resources.parse_version | |
except ImportError as ex_a: | |
try: | |
import distutils.version | |
parse_fn = distutils.version.StrictVersion | |
except ImportError as ex_b: | |
sys.stderr.writelines([ | |
'ERROR: %s\n' % ex_a.message, | |
' %s\n' % ex_b.message, | |
' The sky is falling! No suitable version parsers!\n']) | |
sys.exit(1) | |
return parse_fn(v) | |
def sort_versions(x): | |
return sorted(x, key=_leloo_multiparse) | |
if __name__ == '__main__': | |
lines = map(str.strip, sys.stdin.readlines()) | |
print(lines) | |
print(sort_versions(lines)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment