setup.py
branchstable
changeset 50092 010a1e73f69e
parent 49971 ac93876ea2df
child 50099 0f0880c8a7e5
child 50168 593f5e4076ff
equal deleted inserted replaced
50089:5fb2546d0df1 50092:010a1e73f69e
    17 import sysconfig
    17 import sysconfig
    18 
    18 
    19 
    19 
    20 def sysstr(s):
    20 def sysstr(s):
    21     return s.decode('latin-1')
    21     return s.decode('latin-1')
       
    22 
       
    23 
       
    24 def eprint(*args, **kwargs):
       
    25     kwargs['file'] = sys.stderr
       
    26     print(*args, **kwargs)
    22 
    27 
    23 
    28 
    24 import ssl
    29 import ssl
    25 
    30 
    26 # ssl.HAS_TLSv1* are preferred to check support but they were added in Python
    31 # ssl.HAS_TLSv1* are preferred to check support but they were added in Python
   290     except EnvironmentError:
   295     except EnvironmentError:
   291         retcode = -1
   296         retcode = -1
   292     if retcode == 0 and not filterhgerr(err):
   297     if retcode == 0 and not filterhgerr(err):
   293         return hgcommand(hgcmd, hgenv)
   298         return hgcommand(hgcmd, hgenv)
   294 
   299 
   295     raise SystemExit(
   300     eprint("/!\\")
   296         'Unable to find a working hg binary to extract the '
   301     eprint(r"/!\ Unable to find a working hg binary")
   297         'version from the repository tags'
   302     eprint(r"/!\ Version cannot be extract from the repository")
   298     )
   303     eprint(r"/!\ Re-run the setup once a first version is built")
       
   304     return None
   299 
   305 
   300 
   306 
   301 def localhgenv():
   307 def localhgenv():
   302     """Get an environment dictionary to use for invoking or importing
   308     """Get an environment dictionary to use for invoking or importing
   303     mercurial from the local repository."""
   309     mercurial from the local repository."""
   318     return env
   324     return env
   319 
   325 
   320 
   326 
   321 version = ''
   327 version = ''
   322 
   328 
   323 if os.path.isdir('.hg'):
   329 
       
   330 def _try_get_version():
   324     hg = findhg()
   331     hg = findhg()
       
   332     if hg is None:
       
   333         return ''
       
   334     hgid = None
       
   335     numerictags = []
   325     cmd = ['log', '-r', '.', '--template', '{tags}\n']
   336     cmd = ['log', '-r', '.', '--template', '{tags}\n']
   326     numerictags = [t for t in sysstr(hg.run(cmd)).split() if t[0:1].isdigit()]
   337     pieces = sysstr(hg.run(cmd)).split()
       
   338     numerictags = [t for t in pieces if t[0:1].isdigit()]
   327     hgid = sysstr(hg.run(['id', '-i'])).strip()
   339     hgid = sysstr(hg.run(['id', '-i'])).strip()
   328     if not hgid:
   340     if not hgid:
   329         # Bail out if hg is having problems interacting with this repository,
   341         eprint("/!\\")
   330         # rather than falling through and producing a bogus version number.
   342         eprint(r"/!\ Unable to determine hg version from local repository")
   331         # Continuing with an invalid version number will break extensions
   343         eprint(r"/!\ Failed to retrieve current revision tags")
   332         # that define minimumhgversion.
   344         return ''
   333         raise SystemExit('Unable to determine hg version from local repository')
       
   334     if numerictags:  # tag(s) found
   345     if numerictags:  # tag(s) found
   335         version = numerictags[-1]
   346         version = numerictags[-1]
   336         if hgid.endswith('+'):  # propagate the dirty status to the tag
   347         if hgid.endswith('+'):  # propagate the dirty status to the tag
   337             version += '+'
   348             version += '+'
   338     else:  # no tag found
   349     else:  # no tag found on the checked out revision
   339         ltagcmd = ['parents', '--template', '{latesttag}']
   350         ltagcmd = ['parents', '--template', '{latesttag}']
   340         ltag = sysstr(hg.run(ltagcmd))
   351         ltag = sysstr(hg.run(ltagcmd))
   341         if not ltag:
   352         if not ltag:
   342             ltag = 'null'
   353             eprint("/!\\")
       
   354             eprint(r"/!\ Unable to determine hg version from local repository")
       
   355             eprint(
       
   356                 r"/!\ Failed to retrieve current revision distance to lated tag"
       
   357             )
       
   358             return ''
   343         changessincecmd = ['log', '-T', 'x\n', '-r', "only(.,'%s')" % ltag]
   359         changessincecmd = ['log', '-T', 'x\n', '-r', "only(.,'%s')" % ltag]
   344         changessince = len(hg.run(changessincecmd).splitlines())
   360         changessince = len(hg.run(changessincecmd).splitlines())
   345         if ltag == 'null':
       
   346             ltag = '0.0'
       
   347         version = '%s+hg%s.%s' % (ltag, changessince, hgid)
   361         version = '%s+hg%s.%s' % (ltag, changessince, hgid)
   348     if version.endswith('+'):
   362     if version.endswith('+'):
   349         version = version[:-1] + 'local' + time.strftime('%Y%m%d')
   363         version = version[:-1] + 'local' + time.strftime('%Y%m%d')
       
   364     return version
       
   365 
       
   366 
       
   367 if os.path.isdir('.hg'):
       
   368     version = _try_get_version()
   350 elif os.path.exists('.hg_archival.txt'):
   369 elif os.path.exists('.hg_archival.txt'):
   351     kw = dict(
   370     kw = dict(
   352         [[t.strip() for t in l.split(':', 1)] for l in open('.hg_archival.txt')]
   371         [[t.strip() for t in l.split(':', 1)] for l in open('.hg_archival.txt')]
   353     )
   372     )
   354     if 'tag' in kw:
   373     if 'tag' in kw:
   364         version = '0+hg' + kw.get('node', '')[:12]
   383         version = '0+hg' + kw.get('node', '')[:12]
   365 elif os.path.exists('mercurial/__version__.py'):
   384 elif os.path.exists('mercurial/__version__.py'):
   366     with open('mercurial/__version__.py') as f:
   385     with open('mercurial/__version__.py') as f:
   367         data = f.read()
   386         data = f.read()
   368     version = re.search('version = b"(.*)"', data).group(1)
   387     version = re.search('version = b"(.*)"', data).group(1)
   369 
   388 if not version:
   370 if version:
   389     if os.environ.get("MERCURIAL_SETUP_MAKE_LOCAL") == "1":
   371     versionb = version
   390         version = "0.0+0"
   372     if not isinstance(versionb, bytes):
   391         eprint("/!\\")
   373         versionb = versionb.encode('ascii')
   392         eprint(r"/!\ Using '0.0+0' as the default version")
   374 
   393         eprint(r"/!\ Re-run make local once that first version is built")
   375     write_if_changed(
   394         eprint("/!\\")
   376         'mercurial/__version__.py',
   395     else:
   377         b''.join(
   396         eprint("/!\\")
   378             [
   397         eprint(r"/!\ Could not determine the Mercurial version")
   379                 b'# this file is autogenerated by setup.py\n'
   398         eprint(r"/!\ You need to build a local version first")
   380                 b'version = b"%s"\n' % versionb,
   399         eprint(r"/!\ Run `make local` and try again")
   381             ]
   400         eprint("/!\\")
   382         ),
   401         msg = "Run `make local` first to get a working local version"
   383     )
   402         raise SystemExit(msg)
       
   403 
       
   404 versionb = version
       
   405 if not isinstance(versionb, bytes):
       
   406     versionb = versionb.encode('ascii')
       
   407 
       
   408 write_if_changed(
       
   409     'mercurial/__version__.py',
       
   410     b''.join(
       
   411         [
       
   412             b'# this file is autogenerated by setup.py\n'
       
   413             b'version = b"%s"\n' % versionb,
       
   414         ]
       
   415     ),
       
   416 )
   384 
   417 
   385 
   418 
   386 class hgbuild(build):
   419 class hgbuild(build):
   387     # Insert hgbuildmo first so that files in mercurial/locale/ are found
   420     # Insert hgbuildmo first so that files in mercurial/locale/ are found
   388     # when build_py is run next.
   421     # when build_py is run next.