gooderp18绿色标准版
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

81 linhas
3.4KB

  1. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  2. ## this functions are taken from the setuptools package (version 0.6c8)
  3. ## http://peak.telecommunity.com/DevCenter/PkgResources#parsing-utilities
  4. import re
  5. component_re = re.compile(r'(\d+ | [a-z]+ | \.| -)', re.VERBOSE)
  6. replace = {'pre':'c', 'preview':'c','-':'final-','_':'final-','rc':'c','dev':'@','saas':'','~':''}.get
  7. def _parse_version_parts(s):
  8. for part in component_re.split(s):
  9. part = replace(part,part)
  10. if not part or part=='.':
  11. continue
  12. if part[:1] in '0123456789':
  13. yield part.zfill(8) # pad for numeric comparison
  14. else:
  15. yield '*'+part
  16. yield '*final' # ensure that alpha/beta/candidate are before final
  17. def parse_version(s: str) -> tuple[str, ...]:
  18. """Convert a version string to a chronologically-sortable key
  19. This is a rough cross between distutils' StrictVersion and LooseVersion;
  20. if you give it versions that would work with StrictVersion, then it behaves
  21. the same; otherwise it acts like a slightly-smarter LooseVersion. It is
  22. *possible* to create pathological version coding schemes that will fool
  23. this parser, but they should be very rare in practice.
  24. The returned value will be a tuple of strings. Numeric portions of the
  25. version are padded to 8 digits so they will compare numerically, but
  26. without relying on how numbers compare relative to strings. Dots are
  27. dropped, but dashes are retained. Trailing zeros between alpha segments
  28. or dashes are suppressed, so that e.g. "2.4.0" is considered the same as
  29. "2.4". Alphanumeric parts are lower-cased.
  30. The algorithm assumes that strings like "-" and any alpha string that
  31. alphabetically follows "final" represents a "patch level". So, "2.4-1"
  32. is assumed to be a branch or patch of "2.4", and therefore "2.4.1" is
  33. considered newer than "2.4-1", which in turn is newer than "2.4".
  34. Strings like "a", "b", "c", "alpha", "beta", "candidate" and so on (that
  35. come before "final" alphabetically) are assumed to be pre-release versions,
  36. so that the version "2.4" is considered newer than "2.4a1".
  37. Finally, to handle miscellaneous cases, the strings "pre", "preview", and
  38. "rc" are treated as if they were "c", i.e. as though they were release
  39. candidates, and therefore are not as new as a version string that does not
  40. contain them.
  41. """
  42. parts: list[str] = []
  43. for part in _parse_version_parts((s or '0.1').lower()):
  44. if part.startswith('*'):
  45. if part<'*final': # remove '-' before a prerelease tag
  46. while parts and parts[-1]=='*final-': parts.pop()
  47. # remove trailing zeros from each series of numeric parts
  48. while parts and parts[-1]=='00000000':
  49. parts.pop()
  50. parts.append(part)
  51. return tuple(parts)
  52. if __name__ == '__main__':
  53. def chk(lst, verbose=False):
  54. pvs = []
  55. for v in lst:
  56. pv = parse_version(v)
  57. pvs.append(pv)
  58. if verbose:
  59. print(v, pv)
  60. for a, b in zip(pvs, pvs[1:]):
  61. assert a < b, '%s < %s == %s' % (a, b, a < b)
  62. chk(('0', '4.2', '4.2.3.4', '5.0.0-alpha', '5.0.0-rc1', '5.0.0-rc1.1', '5.0.0_rc2', '5.0.0_rc3', '5.0.0'), False)
  63. chk(('5.0.0-0_rc3', '5.0.0-1dev', '5.0.0-1'), False)
上海开阖软件有限公司 沪ICP备12045867号-1