setup.py 4.74 KB
Newer Older
Qijiang Fan's avatar
Qijiang Fan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import subprocess
import sys
import time
if not hasattr(sys, 'version_info') or sys.version_info < (2, 4, 0, 'final'):
    raise SystemExit("Mercurial requires python 2.4 or later.")

try:
    from distutils.command.build_py import build_py_2to3 as build_py
except ImportError:
    from distutils.command.build_py import build_py
try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

def runcmd(cmd, env):
Qijiang Fan's avatar
Qijiang Fan committed
21 22
    shell = os.name == 'nt'
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=shell,
Qijiang Fan's avatar
Qijiang Fan committed
23 24 25 26 27 28 29
                         stderr=subprocess.PIPE, env=env)
    out, err = p.communicate()
    # If root is executing setup.py, but the repository is owned by
    # another user (as in "sudo python setup.py install") we will get
    # trust warnings since the .hg/hgrc file is untrusted. That is
    # fine, we don't want to load it anyway.
    err = [e for e in err.splitlines()
Qijiang Fan's avatar
Qijiang Fan committed
30 31
           if not (e.startswith('Not trusting file')
                   or e.startswith('obsolete feature not enabled'))]
Qijiang Fan's avatar
Qijiang Fan committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    if err:
        return ''
    return out


version = ''

if os.path.isdir('.hg'):
    # Execute hg out of this directory with a custom environment which
    # includes the pure Python modules in mercurial/pure. We also take
    # care to not use any hgrc files and do no localization.
    env = {'HGRCPATH': '',
           'LANGUAGE': 'C'}
    for copyenv in ('LD_LIBRARY_PATH', 'PYTHONPATH', 'PATH'):
        if copyenv in os.environ:
            env[copyenv] = os.environ[copyenv]
    if 'SystemRoot' in os.environ:
        # Copy SystemRoot into the custom environment for Python 2.6
        # under Windows. Otherwise, the subprocess will fail with
        # error 0xc0150004. See: http://bugs.python.org/issue3440
        env['SystemRoot'] = os.environ['SystemRoot']
    cmd = ['hg', 'id', '-i', '-t']
    l = runcmd(cmd, env).split()
    while len(l) > 1 and l[-1][0].isalpha(): # remove non-numbered tags
        l.pop()
    if len(l) > 1: # tag found
        version = l[-1]
        if l[0].endswith('+'): # propagate the dirty status to the tag
            version += '+'
    elif len(l) == 1: # no tag found
        cmd = ['hg', 'parents', '--template',
               '{latesttag}+{latesttagdistance}-']
        version = runcmd(cmd, env) + l[0]
    if not version:
        version = runcmd(['hg', 'parents', '--template' '{node|short}\n'],
                         env)
        if version:
            version = version.split()[0]
    if version.endswith('+'):
        version += time.strftime('%Y%m%d')
elif os.path.exists('.hg_archival.txt'):
    kw = dict([t.strip() for t in l.split(':', 1)]
              for l in open('.hg_archival.txt'))
    if 'tag' in kw:
Qijiang Fan's avatar
Qijiang Fan committed
76
        version = kw['tag']
Qijiang Fan's avatar
Qijiang Fan committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    elif 'latesttag' in kw:
        version = '%(latesttag)s+%(latesttagdistance)s-%(node).12s' % kw
    else:
        version = kw.get('node', '')[:12]

verfile = os.path.join("hgsubversion", "__version__.py")
if version:
    f = open(verfile, "w")
    f.write('# this file is autogenerated by setup.py\n')
    f.write('version = "%s"\n' % version)
    f.close()

if os.path.exists(verfile):
    # scrape the version out with a regex because setuptools
    # needlessly swaps out file() for some non-object thing
    # and breaks importing hgsubversion entirely
    mat = re.findall('.*"(.*)"', open(verfile).read())
    version = mat[0]
if not version:
    version = 'unknown'

requires = []
try:
    import mercurial
except ImportError:
    requires.append('mercurial')

# If the Subversion SWIG bindings aren't present, require Subvertpy
try:
    from hgsubversion.svnwrap import svn_swig_wrapper
except ImportError:
    requires.append('subvertpy>=0.7.4')

setup(
Qijiang Fan's avatar
Qijiang Fan committed
111 112 113 114 115 116 117
    name='hgsubversion',
    version=version,
    url='http://bitbucket.org/durin42/hgsubversion',
    license='GNU GPL',
    author='Augie Fackler, others',
    author_email='durin42@gmail.com',
    description=('hgsubversion is a Mercurial extension for working with '
Qijiang Fan's avatar
Qijiang Fan committed
118
                   'Subversion repositories.'),
Qijiang Fan's avatar
Qijiang Fan committed
119
    long_description=open(os.path.join(os.path.dirname(__file__),
Qijiang Fan's avatar
Qijiang Fan committed
120
                                         'README')).read(),
Qijiang Fan's avatar
Qijiang Fan committed
121
    keywords='mercurial',
Qijiang Fan's avatar
Qijiang Fan committed
122 123
    packages=('hgsubversion', 'hgsubversion.hooks', 'hgsubversion.layouts',
              'hgsubversion.svnwrap'),
Qijiang Fan's avatar
Qijiang Fan committed
124 125
    package_data={ 'hgsubversion': ['help/subversion.rst'] },
    platforms='any',
Qijiang Fan's avatar
Qijiang Fan committed
126
    install_requires=requires,
Qijiang Fan's avatar
Qijiang Fan committed
127
    classifiers=[
Qijiang Fan's avatar
Qijiang Fan committed
128 129 130 131 132 133 134
        'License :: OSI Approved :: GNU General Public License (GPL)',
        'Intended Audience :: Developers',
        'Topic :: Software Development :: Version Control',
        'Development Status :: 4 - Beta',
        'Programming Language :: Python',
        'Operating System :: OS Independent',
    ],
Qijiang Fan's avatar
Qijiang Fan committed
135
    cmdclass={'build_py': build_py},
Qijiang Fan's avatar
Qijiang Fan committed
136
)