setup.py
changeset 9615 f51d1822d6fd
parent 9539 c904e76e3834
child 9807 f359d4f528aa
equal deleted inserted replaced
9614:58edd448da4f 9615:f51d1822d6fd
    95     extra['console'] = ['hg']
    95     extra['console'] = ['hg']
    96 
    96 
    97 except ImportError:
    97 except ImportError:
    98     pass
    98     pass
    99 
    99 
   100 version = None
   100 def runcmd(cmd):
       
   101     p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
       
   102                          stderr=subprocess.PIPE, env=env)
       
   103     out, err = p.communicate()
       
   104     # If root is executing setup.py, but the repository is owned by
       
   105     # another user (as in "sudo python setup.py install") we will get
       
   106     # trust warnings since the .hg/hgrc file is untrusted. That is
       
   107     # fine, we don't want to load it anyway.
       
   108     err = [e for e in err.splitlines()
       
   109            if not e.startswith('Not trusting file')]
       
   110     if err:
       
   111         return ''
       
   112     return out
       
   113 
       
   114 version = ''
   101 
   115 
   102 if os.path.isdir('.hg'):
   116 if os.path.isdir('.hg'):
   103     # Execute hg out of this directory with a custom environment which
   117     # Execute hg out of this directory with a custom environment which
   104     # includes the pure Python modules in mercurial/pure. We also take
   118     # includes the pure Python modules in mercurial/pure. We also take
   105     # care to not use any hgrc files and do no localization.
   119     # care to not use any hgrc files and do no localization.
   111         # Copy SystemRoot into the custom environment for Python 2.6
   125         # Copy SystemRoot into the custom environment for Python 2.6
   112         # under Windows. Otherwise, the subprocess will fail with
   126         # under Windows. Otherwise, the subprocess will fail with
   113         # error 0xc0150004. See: http://bugs.python.org/issue3440
   127         # error 0xc0150004. See: http://bugs.python.org/issue3440
   114         env['SystemRoot'] = os.environ['SystemRoot']
   128         env['SystemRoot'] = os.environ['SystemRoot']
   115     cmd = [sys.executable, 'hg', 'id', '-i', '-t']
   129     cmd = [sys.executable, 'hg', 'id', '-i', '-t']
   116 
   130     l = runcmd(cmd).split()
   117     p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
   131     while len(l) > 1 and l[-1][0].isalpha(): # remove non-numbered tags
   118                          stderr=subprocess.PIPE, env=env)
   132         l.pop()
   119     out, err = p.communicate()
   133     if len(l) > 1: # tag found
   120 
   134         version = l[-1]
   121     # If root is executing setup.py, but the repository is owned by
   135         if l[0].endswith('+'): # propagate the dirty status to the tag
   122     # another user (as in "sudo python setup.py install") we will get
   136             version += '+'
   123     # trust warnings since the .hg/hgrc file is untrusted. That is
   137     elif len(l) == 1: # no tag found
   124     # fine, we don't want to load it anyway.
   138         cmd = [sys.executable, 'hg', 'parents', '--template',
   125     err = [e for e in err.splitlines()
   139                '{latesttag}+{latesttagdistance}-']
   126            if not e.startswith('Not trusting file')]
   140         version = runcmd(cmd) + l[0]
   127     if err:
   141     if version.endswith('+'):
   128         sys.stderr.write('warning: could not establish Mercurial '
   142         version += time.strftime('%Y%m%d')
   129                          'version:\n%s\n' % '\n'.join(err))
   143 elif os.path.exists('.hg_archival.txt'):
       
   144     kw = dict([t.strip() for t in l.split(':', 1)]
       
   145               for l in open('.hg_archival.txt'))
       
   146     if 'tag' in kw:
       
   147         version =  kw['tag']
       
   148     elif 'latesttag' in kw:
       
   149         version = '%(latesttag)s+%(latesttagdistance)s-%(node).12s' % kw
   130     else:
   150     else:
   131         l = out.split()
   151         version = kw.get('node', '')[:12]
   132         while len(l) > 1 and l[-1][0].isalpha(): # remove non-numbered tags
       
   133             l.pop()
       
   134         if l:
       
   135             version = l[-1] # latest tag or revision number
       
   136             if version.endswith('+'):
       
   137                 version += time.strftime('%Y%m%d')
       
   138 elif os.path.exists('.hg_archival.txt'):
       
   139     hgarchival = open('.hg_archival.txt')
       
   140     for line in hgarchival:
       
   141         if line.startswith('node:'):
       
   142             version = line.split(':')[1].strip()[:12]
       
   143             break
       
   144 
   152 
   145 if version:
   153 if version:
   146     f = open("mercurial/__version__.py", "w")
   154     f = open("mercurial/__version__.py", "w")
   147     f.write('# this file is autogenerated by setup.py\n')
   155     f.write('# this file is autogenerated by setup.py\n')
   148     f.write('version = "%s"\n' % version)
   156     f.write('version = "%s"\n' % version)