tests/hghave
author Patrick Mezard <pmezard@gmail.com>
Mon, 06 Aug 2007 10:26:04 +0200
changeset 5072 7e2385a31933
parent 5070 4cf6f8dbd1b4
child 5074 e86788af599a
permissions -rwxr-xr-x
hghave: detect executable permission availability.

#!/usr/bin/env python
"""Test the running system for features availability. Exit with zero
if all features are there, non-zero otherwise.
"""
import optparse
import os
import sys
import tempfile

def has_symlink():
    return hasattr(os, "symlink")

def has_fifo():
    return hasattr(os, "mkfifo")

def has_executablebit():
    fd, path = tempfile.mkstemp()
    os.close(fd)
    try:
        s = os.lstat(path).st_mode
        os.chmod(path, s | 0100)
        return (os.lstat(path).st_mode & 0100 != 0)
    finally:
        os.remove(path)

checks = {
    "symlink": (has_symlink, "symbolic links"),
    "fifo": (has_fifo, "named pipes"),
    "execbit": (has_executablebit, "executable bit"),
}

def list_features():
    for name, feature in checks.iteritems():
        desc = feature[1]
        print name + ':', desc

parser = optparse.OptionParser("%prog [options] [features]")
parser.add_option("--list-features", action="store_true",
                  help="list available features")
parser.add_option("-q", "--quiet", action="store_true",
                  help="check features silently")

if __name__ == '__main__':
    options, args = parser.parse_args()
    if options.list_features:
        list_features()
        sys.exit(0)
        
    quiet = options.quiet

    failures = 0

    def error(msg):
        global failures
        if not quiet:
            sys.stderr.write(msg + '\n')
        failures += 1
    
    for feature in args:
        if feature not in checks:
            error('hghave: unknown feature: ' + feature)
            continue
        
        check, desc = checks[feature]       
        if not check():
            error('hghave: missing feature: ' + desc)

    if failures != 0:
        sys.exit(1)