tests/hghave.py
changeset 16966 23f621ca04b5
parent 16891 b0e8afdfa970
child 16968 456f457e376d
equal deleted inserted replaced
16965:91284af53508 16966:23f621ca04b5
       
     1 import os, stat
       
     2 import re
       
     3 import sys
       
     4 import tempfile
       
     5 
       
     6 tempprefix = 'hg-hghave-'
       
     7 
       
     8 def matchoutput(cmd, regexp, ignorestatus=False):
       
     9     """Return True if cmd executes successfully and its output
       
    10     is matched by the supplied regular expression.
       
    11     """
       
    12     r = re.compile(regexp)
       
    13     fh = os.popen(cmd)
       
    14     s = fh.read()
       
    15     try:
       
    16         ret = fh.close()
       
    17     except IOError:
       
    18         # Happen in Windows test environment
       
    19         ret = 1
       
    20     return (ignorestatus or ret is None) and r.search(s)
       
    21 
       
    22 def has_baz():
       
    23     return matchoutput('baz --version 2>&1', r'baz Bazaar version')
       
    24 
       
    25 def has_bzr():
       
    26     try:
       
    27         import bzrlib
       
    28         return bzrlib.__doc__ is not None
       
    29     except ImportError:
       
    30         return False
       
    31 
       
    32 def has_bzr114():
       
    33     try:
       
    34         import bzrlib
       
    35         return (bzrlib.__doc__ is not None
       
    36                 and bzrlib.version_info[:2] >= (1, 14))
       
    37     except ImportError:
       
    38         return False
       
    39 
       
    40 def has_cvs():
       
    41     re = r'Concurrent Versions System.*?server'
       
    42     return matchoutput('cvs --version 2>&1', re) and not has_msys()
       
    43 
       
    44 def has_darcs():
       
    45     return matchoutput('darcs --version', r'2\.[2-9]', True)
       
    46 
       
    47 def has_mtn():
       
    48     return matchoutput('mtn --version', r'monotone', True) and not matchoutput(
       
    49         'mtn --version', r'monotone 0\.', True)
       
    50 
       
    51 def has_eol_in_paths():
       
    52     try:
       
    53         fd, path = tempfile.mkstemp(prefix=tempprefix, suffix='\n\r')
       
    54         os.close(fd)
       
    55         os.remove(path)
       
    56         return True
       
    57     except (IOError, OSError):
       
    58         return False
       
    59 
       
    60 def has_executablebit():
       
    61     try:
       
    62         EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
       
    63         fh, fn = tempfile.mkstemp(dir=".", prefix='hg-checkexec-')
       
    64         try:
       
    65             os.close(fh)
       
    66             m = os.stat(fn).st_mode & 0777
       
    67             new_file_has_exec = m & EXECFLAGS
       
    68             os.chmod(fn, m ^ EXECFLAGS)
       
    69             exec_flags_cannot_flip = ((os.stat(fn).st_mode & 0777) == m)
       
    70         finally:
       
    71             os.unlink(fn)
       
    72     except (IOError, OSError):
       
    73         # we don't care, the user probably won't be able to commit anyway
       
    74         return False
       
    75     return not (new_file_has_exec or exec_flags_cannot_flip)
       
    76 
       
    77 def has_icasefs():
       
    78     # Stolen from mercurial.util
       
    79     fd, path = tempfile.mkstemp(prefix=tempprefix, dir='.')
       
    80     os.close(fd)
       
    81     try:
       
    82         s1 = os.stat(path)
       
    83         d, b = os.path.split(path)
       
    84         p2 = os.path.join(d, b.upper())
       
    85         if path == p2:
       
    86             p2 = os.path.join(d, b.lower())
       
    87         try:
       
    88             s2 = os.stat(p2)
       
    89             return s2 == s1
       
    90         except OSError:
       
    91             return False
       
    92     finally:
       
    93         os.remove(path)
       
    94 
       
    95 def has_inotify():
       
    96     try:
       
    97         import hgext.inotify.linux.watcher
       
    98         return True
       
    99     except ImportError:
       
   100         return False
       
   101 
       
   102 def has_fifo():
       
   103     return getattr(os, "mkfifo", None) is not None
       
   104 
       
   105 def has_cacheable_fs():
       
   106     from mercurial import util
       
   107 
       
   108     fd, path = tempfile.mkstemp(prefix=tempprefix)
       
   109     os.close(fd)
       
   110     try:
       
   111         return util.cachestat(path).cacheable()
       
   112     finally:
       
   113         os.remove(path)
       
   114 
       
   115 def has_lsprof():
       
   116     try:
       
   117         import _lsprof
       
   118         return True
       
   119     except ImportError:
       
   120         return False
       
   121 
       
   122 def has_gettext():
       
   123     return matchoutput('msgfmt --version', 'GNU gettext-tools')
       
   124 
       
   125 def has_git():
       
   126     return matchoutput('git --version 2>&1', r'^git version')
       
   127 
       
   128 def has_docutils():
       
   129     try:
       
   130         from docutils.core import publish_cmdline
       
   131         return True
       
   132     except ImportError:
       
   133         return False
       
   134 
       
   135 def getsvnversion():
       
   136     m = matchoutput('svn --version 2>&1', r'^svn,\s+version\s+(\d+)\.(\d+)')
       
   137     if not m:
       
   138         return (0, 0)
       
   139     return (int(m.group(1)), int(m.group(2)))
       
   140 
       
   141 def has_svn15():
       
   142     return getsvnversion() >= (1, 5)
       
   143 
       
   144 def has_svn13():
       
   145     return getsvnversion() >= (1, 3)
       
   146 
       
   147 def has_svn():
       
   148     return matchoutput('svn --version 2>&1', r'^svn, version') and \
       
   149         matchoutput('svnadmin --version 2>&1', r'^svnadmin, version')
       
   150 
       
   151 def has_svn_bindings():
       
   152     try:
       
   153         import svn.core
       
   154         version = svn.core.SVN_VER_MAJOR, svn.core.SVN_VER_MINOR
       
   155         if version < (1, 4):
       
   156             return False
       
   157         return True
       
   158     except ImportError:
       
   159         return False
       
   160 
       
   161 def has_p4():
       
   162     return (matchoutput('p4 -V', r'Rev\. P4/') and
       
   163             matchoutput('p4d -V', r'Rev\. P4D/'))
       
   164 
       
   165 def has_symlink():
       
   166     if getattr(os, "symlink", None) is None:
       
   167         return False
       
   168     name = tempfile.mktemp(dir=".", prefix='hg-checklink-')
       
   169     try:
       
   170         os.symlink(".", name)
       
   171         os.unlink(name)
       
   172         return True
       
   173     except (OSError, AttributeError):
       
   174         return False
       
   175 
       
   176 def has_tla():
       
   177     return matchoutput('tla --version 2>&1', r'The GNU Arch Revision')
       
   178 
       
   179 def has_gpg():
       
   180     return matchoutput('gpg --version 2>&1', r'GnuPG')
       
   181 
       
   182 def has_unix_permissions():
       
   183     d = tempfile.mkdtemp(prefix=tempprefix, dir=".")
       
   184     try:
       
   185         fname = os.path.join(d, 'foo')
       
   186         for umask in (077, 007, 022):
       
   187             os.umask(umask)
       
   188             f = open(fname, 'w')
       
   189             f.close()
       
   190             mode = os.stat(fname).st_mode
       
   191             os.unlink(fname)
       
   192             if mode & 0777 != ~umask & 0666:
       
   193                 return False
       
   194         return True
       
   195     finally:
       
   196         os.rmdir(d)
       
   197 
       
   198 def has_pyflakes():
       
   199     return matchoutput("sh -c \"echo 'import re' 2>&1 | pyflakes\"",
       
   200                        r"<stdin>:1: 're' imported but unused",
       
   201                        True)
       
   202 
       
   203 def has_pygments():
       
   204     try:
       
   205         import pygments
       
   206         return True
       
   207     except ImportError:
       
   208         return False
       
   209 
       
   210 def has_outer_repo():
       
   211     return matchoutput('hg root 2>&1', r'')
       
   212 
       
   213 def has_ssl():
       
   214     try:
       
   215         import ssl
       
   216         import OpenSSL
       
   217         OpenSSL.SSL.Context
       
   218         return True
       
   219     except ImportError:
       
   220         return False
       
   221 
       
   222 def has_windows():
       
   223     return os.name == 'nt'
       
   224 
       
   225 def has_system_sh():
       
   226     return os.name != 'nt'
       
   227 
       
   228 def has_serve():
       
   229     return os.name != 'nt' # gross approximation
       
   230 
       
   231 def has_tic():
       
   232     return matchoutput('test -x "`which tic`"', '')
       
   233 
       
   234 def has_msys():
       
   235     return os.getenv('MSYSTEM')
       
   236 
       
   237 checks = {
       
   238     "true": (lambda: True, "yak shaving"),
       
   239     "false": (lambda: False, "nail clipper"),
       
   240     "baz": (has_baz, "GNU Arch baz client"),
       
   241     "bzr": (has_bzr, "Canonical's Bazaar client"),
       
   242     "bzr114": (has_bzr114, "Canonical's Bazaar client >= 1.14"),
       
   243     "cacheable": (has_cacheable_fs, "cacheable filesystem"),
       
   244     "cvs": (has_cvs, "cvs client/server"),
       
   245     "darcs": (has_darcs, "darcs client"),
       
   246     "docutils": (has_docutils, "Docutils text processing library"),
       
   247     "eol-in-paths": (has_eol_in_paths, "end-of-lines in paths"),
       
   248     "execbit": (has_executablebit, "executable bit"),
       
   249     "fifo": (has_fifo, "named pipes"),
       
   250     "gettext": (has_gettext, "GNU Gettext (msgfmt)"),
       
   251     "git": (has_git, "git command line client"),
       
   252     "gpg": (has_gpg, "gpg client"),
       
   253     "icasefs": (has_icasefs, "case insensitive file system"),
       
   254     "inotify": (has_inotify, "inotify extension support"),
       
   255     "lsprof": (has_lsprof, "python lsprof module"),
       
   256     "mtn": (has_mtn, "monotone client (>= 1.0)"),
       
   257     "outer-repo": (has_outer_repo, "outer repo"),
       
   258     "p4": (has_p4, "Perforce server and client"),
       
   259     "pyflakes": (has_pyflakes, "Pyflakes python linter"),
       
   260     "pygments": (has_pygments, "Pygments source highlighting library"),
       
   261     "serve": (has_serve, "platform and python can manage 'hg serve -d'"),
       
   262     "ssl": (has_ssl, "python >= 2.6 ssl module and python OpenSSL"),
       
   263     "svn": (has_svn, "subversion client and admin tools"),
       
   264     "svn13": (has_svn13, "subversion client and admin tools >= 1.3"),
       
   265     "svn15": (has_svn15, "subversion client and admin tools >= 1.5"),
       
   266     "svn-bindings": (has_svn_bindings, "subversion python bindings"),
       
   267     "symlink": (has_symlink, "symbolic links"),
       
   268     "system-sh": (has_system_sh, "system() uses sh"),
       
   269     "tic": (has_tic, "terminfo compiler"),
       
   270     "tla": (has_tla, "GNU Arch tla client"),
       
   271     "unix-permissions": (has_unix_permissions, "unix-style permissions"),
       
   272     "windows": (has_windows, "Windows"),
       
   273     "msys": (has_msys, "Windows with MSYS"),
       
   274 }