contrib/genosxversion.py
author Manuel Jacob <me@manueljacob.de>
Tue, 31 May 2022 04:11:34 +0200
changeset 49302 ee4537e365c8
parent 48875 6000f5b25c9b
permissions -rwxr-xr-x
py3: remove retry on EINTR errno Since the implementation of PEP 475 (Python 3.5), Python retries system calls failing with EINTR. Therefore we don’t need the logic that retries it in Python code.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33594
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
#!/usr/bin/env python2
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
import argparse
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
import os
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
import subprocess
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
import sys
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
45397
148d177a4f2d genosxversion: don't give up if we can't find a path to hg libraries
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
     8
try:
148d177a4f2d genosxversion: don't give up if we can't find a path to hg libraries
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
     9
    # Always load hg libraries from the hg we can find on $PATH.
148d177a4f2d genosxversion: don't give up if we can't find a path to hg libraries
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    10
    hglib = subprocess.check_output(['hg', 'debuginstall', '-T', '{hgmodules}'])
148d177a4f2d genosxversion: don't give up if we can't find a path to hg libraries
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    11
    sys.path.insert(0, os.path.dirname(hglib))
148d177a4f2d genosxversion: don't give up if we can't find a path to hg libraries
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    12
except subprocess.CalledProcessError:
148d177a4f2d genosxversion: don't give up if we can't find a path to hg libraries
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    13
    # We're probably running with a PyOxidized Mercurial, so just
148d177a4f2d genosxversion: don't give up if we can't find a path to hg libraries
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    14
    # proceed and hope it works out okay.
148d177a4f2d genosxversion: don't give up if we can't find a path to hg libraries
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    15
    pass
33594
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    17
from mercurial import util
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    18
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
ap = argparse.ArgumentParser()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42815
diff changeset
    20
ap.add_argument(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42815
diff changeset
    21
    '--paranoid',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42815
diff changeset
    22
    action='store_true',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42815
diff changeset
    23
    help=(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42815
diff changeset
    24
        "Be paranoid about how version numbers compare and "
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42815
diff changeset
    25
        "produce something that's more likely to sort "
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42815
diff changeset
    26
        "reasonably."
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42815
diff changeset
    27
    ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42815
diff changeset
    28
)
33594
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
ap.add_argument('--selftest', action='store_true', help='Run self-tests.')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
ap.add_argument('versionfile', help='Path to a valid mercurial __version__.py')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    31
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42815
diff changeset
    32
33594
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
def paranoidver(ver):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
    """Given an hg version produce something that distutils can sort.
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
    Some Mac package management systems use distutils code in order to
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
    figure out upgrades, which makes life difficult. The test case is
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
    a reduced version of code in the Munki tool used by some large
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
    organizations to centrally manage OS X packages, which is what
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
    inspired this kludge.
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
    >>> paranoidver('3.4')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
    '3.4.0'
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
    >>> paranoidver('3.4.2')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
    '3.4.2'
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
    >>> paranoidver('3.0-rc+10')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
    '2.9.9999-rc+10'
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
    >>> paranoidver('4.2+483-5d44d7d4076e')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
    '4.2.0+483-5d44d7d4076e'
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    50
    >>> paranoidver('4.2.1+598-48d1e1214d8c')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    51
    '4.2.1+598-48d1e1214d8c'
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    52
    >>> paranoidver('4.3-rc')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    53
    '4.2.9999-rc'
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
    >>> paranoidver('4.3')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
    '4.3.0'
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    56
    >>> from distutils import version
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    57
    >>> class LossyPaddedVersion(version.LooseVersion):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    58
    ...     '''Subclass version.LooseVersion to compare things like
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    59
    ...     "10.6" and "10.6.0" as equal'''
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    60
    ...     def __init__(self, s):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    61
    ...             self.parse(s)
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    62
    ...
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    63
    ...     def _pad(self, version_list, max_length):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    64
    ...         'Pad a version list by adding extra 0 components to the end'
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    65
    ...         # copy the version_list so we don't modify it
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    66
    ...         cmp_list = list(version_list)
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    67
    ...         while len(cmp_list) < max_length:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    68
    ...             cmp_list.append(0)
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    69
    ...         return cmp_list
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    70
    ...
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    71
    ...     def __cmp__(self, other):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    72
    ...         if isinstance(other, str):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    73
    ...             other = MunkiLooseVersion(other)
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    74
    ...         max_length = max(len(self.version), len(other.version))
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    75
    ...         self_cmp_version = self._pad(self.version, max_length)
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    76
    ...         other_cmp_version = self._pad(other.version, max_length)
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    77
    ...         return cmp(self_cmp_version, other_cmp_version)
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    78
    >>> def testver(older, newer):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    79
    ...   o = LossyPaddedVersion(paranoidver(older))
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    80
    ...   n = LossyPaddedVersion(paranoidver(newer))
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    81
    ...   return o < n
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    82
    >>> testver('3.4', '3.5')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    83
    True
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    84
    >>> testver('3.4.0', '3.5-rc')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    85
    True
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    86
    >>> testver('3.4-rc', '3.5')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    87
    True
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    88
    >>> testver('3.4-rc+10-deadbeef', '3.5')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    89
    True
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    90
    >>> testver('3.4.2', '3.5-rc')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    91
    True
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    92
    >>> testver('3.4.2', '3.5-rc+10-deadbeef')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    93
    True
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    94
    >>> testver('4.2+483-5d44d7d4076e', '4.2.1+598-48d1e1214d8c')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    95
    True
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    96
    >>> testver('4.3-rc', '4.3')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    97
    True
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    98
    >>> testver('4.3', '4.3-rc')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    99
    False
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   100
    """
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   101
    major, minor, micro, extra = util.versiontuple(ver, n=4)
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   102
    if micro is None:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   103
        micro = 0
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   104
    if extra:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   105
        if extra.startswith('rc'):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   106
            if minor == 0:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   107
                major -= 1
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   108
                minor = 9
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   109
            else:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   110
                minor -= 1
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   111
            micro = 9999
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   112
            extra = '-' + extra
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   113
        else:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   114
            extra = '+' + extra
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   115
    else:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   116
        extra = ''
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   117
    return '%d.%d.%d%s' % (major, minor, micro, extra)
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   118
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42815
diff changeset
   119
33594
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   120
def main(argv):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   121
    opts = ap.parse_args(argv[1:])
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   122
    if opts.selftest:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   123
        import doctest
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42815
diff changeset
   124
33594
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   125
        doctest.testmod()
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   126
        return
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   127
    with open(opts.versionfile) as f:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   128
        for l in f:
38734
25880ddf9a86 macosx: fixing macOS version generation after db9d1dd01bf0
Rodrigo Damazio <rdamazio@google.com>
parents: 33594
diff changeset
   129
            if l.startswith('version = b'):
33594
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   130
                # version number is entire line minus the quotes
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42815
diff changeset
   131
                ver = l[len('version = b') + 1 : -2]
33594
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   132
                break
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   133
    if opts.paranoid:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   134
        print(paranoidver(ver))
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   135
    else:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   136
        print(ver)
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   137
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42815
diff changeset
   138
33594
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   139
if __name__ == '__main__':
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   140
    main(sys.argv)