contrib/genosxversion.py
author Matt Harbison <matt_harbison@yahoo.com>
Tue, 20 Aug 2019 18:05:07 -0400
changeset 42815 197e7326b8b8
parent 38734 25880ddf9a86
child 43076 2372284d9457
permissions -rwxr-xr-x
contrib: simplify the genosxversion.py command to find the hg libraries I forget what problem I ran into while trying to teach the makefile to use a non-system python. (It might have ben missing hg-evolve and/or keyring, but `check_output()` was raising an error.) This still isn't great because it will return non zero for something like the username not being set, even though we aren't asking for it. But I suppose it's still useful to simplify. Differential Revision: https://phab.mercurial-scm.org/D6753
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
from __future__ import absolute_import, print_function
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
import argparse
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
import os
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
import subprocess
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
import sys
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
# Always load hg libraries from the hg we can find on $PATH.
42815
197e7326b8b8 contrib: simplify the genosxversion.py command to find the hg libraries
Matt Harbison <matt_harbison@yahoo.com>
parents: 38734
diff changeset
    10
hglib = subprocess.check_output(
197e7326b8b8 contrib: simplify the genosxversion.py command to find the hg libraries
Matt Harbison <matt_harbison@yahoo.com>
parents: 38734
diff changeset
    11
    ['hg', 'debuginstall', '-T', '{hgmodules}'])
33594
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    12
sys.path.insert(0, os.path.dirname(hglib))
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
from mercurial import util
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    15
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
ap = argparse.ArgumentParser()
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    17
ap.add_argument('--paranoid',
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    18
                action='store_true',
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
                help=("Be paranoid about how version numbers compare and "
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
                      "produce something that's more likely to sort "
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
                      "reasonably."))
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
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
    23
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
    24
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    25
def paranoidver(ver):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    26
    """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
    27
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    28
    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
    29
    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
    30
    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
    31
    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
    32
    inspired this kludge.
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
    >>> paranoidver('3.4')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
    '3.4.0'
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
    >>> paranoidver('3.4.2')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
    '3.4.2'
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
    >>> paranoidver('3.0-rc+10')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
    '2.9.9999-rc+10'
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
    >>> paranoidver('4.2+483-5d44d7d4076e')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
    '4.2.0+483-5d44d7d4076e'
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
    >>> paranoidver('4.2.1+598-48d1e1214d8c')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
    '4.2.1+598-48d1e1214d8c'
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
    >>> paranoidver('4.3-rc')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
    '4.2.9999-rc'
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
    >>> paranoidver('4.3')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
    '4.3.0'
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
    >>> from distutils import version
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
    >>> class LossyPaddedVersion(version.LooseVersion):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    50
    ...     '''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
    51
    ...     "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
    52
    ...     def __init__(self, s):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    53
    ...             self.parse(s)
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
    ...
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
    ...     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
    56
    ...         '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
    57
    ...         # 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
    58
    ...         cmp_list = list(version_list)
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    59
    ...         while len(cmp_list) < max_length:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    60
    ...             cmp_list.append(0)
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    61
    ...         return cmp_list
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 __cmp__(self, other):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    64
    ...         if isinstance(other, str):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    65
    ...             other = MunkiLooseVersion(other)
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    66
    ...         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
    67
    ...         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
    68
    ...         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
    69
    ...         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
    70
    >>> def testver(older, newer):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    71
    ...   o = LossyPaddedVersion(paranoidver(older))
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    72
    ...   n = LossyPaddedVersion(paranoidver(newer))
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    73
    ...   return o < n
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    74
    >>> testver('3.4', '3.5')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    75
    True
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    76
    >>> 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
    77
    True
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    78
    >>> testver('3.4-rc', '3.5')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    79
    True
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    80
    >>> 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
    81
    True
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    82
    >>> 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
    83
    True
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    84
    >>> 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
    85
    True
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    86
    >>> 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
    87
    True
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    88
    >>> testver('4.3-rc', '4.3')
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('4.3', '4.3-rc')
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    91
    False
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    92
    """
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    93
    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
    94
    if micro is None:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    95
        micro = 0
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    96
    if extra:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    97
        if extra.startswith('rc'):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    98
            if minor == 0:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
    99
                major -= 1
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   100
                minor = 9
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   101
            else:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   102
                minor -= 1
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   103
            micro = 9999
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   104
            extra = '-' + extra
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   105
        else:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   106
            extra = '+' + extra
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   107
    else:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   108
        extra = ''
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   109
    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
   110
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   111
def main(argv):
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   112
    opts = ap.parse_args(argv[1:])
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   113
    if opts.selftest:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   114
        import doctest
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   115
        doctest.testmod()
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   116
        return
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   117
    with open(opts.versionfile) as f:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   118
        for l in f:
38734
25880ddf9a86 macosx: fixing macOS version generation after db9d1dd01bf0
Rodrigo Damazio <rdamazio@google.com>
parents: 33594
diff changeset
   119
            if l.startswith('version = b'):
33594
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   120
                # version number is entire line minus the quotes
38734
25880ddf9a86 macosx: fixing macOS version generation after db9d1dd01bf0
Rodrigo Damazio <rdamazio@google.com>
parents: 33594
diff changeset
   121
                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
   122
                break
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   123
    if opts.paranoid:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   124
        print(paranoidver(ver))
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   125
    else:
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   126
        print(ver)
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   127
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   128
if __name__ == '__main__':
283a7da602ae osx: new script for generating OS X package versions
Augie Fackler <augie@google.com>
parents:
diff changeset
   129
    main(sys.argv)