##// END OF EJS Templates
hghave: wrap command output matching
Patrick Mezard -
r5252:c0281c6b default
parent child Browse files
Show More
@@ -1,115 +1,123 b''
1 1 #!/usr/bin/env python
2 2 """Test the running system for features availability. Exit with zero
3 3 if all features are there, non-zero otherwise. If a feature name is
4 4 prefixed with "no-", the absence of feature is tested.
5 5 """
6 6 import optparse
7 7 import os
8 import re
8 9 import sys
9 10 import tempfile
10 11
11 12 tempprefix = 'hg-hghave-'
12 13
14 def matchoutput(cmd, regexp):
15 """Return True if cmd executes successfully and its output
16 is matched by the supplied regular expression.
17 """
18 r = re.compile(regexp)
19 fh = os.popen(cmd)
20 s = fh.read()
21 ret = fh.close()
22 return ret is None and r.search(s)
23
13 24 def has_symlink():
14 25 return hasattr(os, "symlink")
15 26
16 27 def has_fifo():
17 28 return hasattr(os, "mkfifo")
18 29
19 30 def has_executablebit():
20 31 fd, path = tempfile.mkstemp(prefix=tempprefix)
21 32 os.close(fd)
22 33 try:
23 34 s = os.lstat(path).st_mode
24 35 os.chmod(path, s | 0100)
25 36 return (os.lstat(path).st_mode & 0100 != 0)
26 37 finally:
27 38 os.remove(path)
28 39
29 40 def has_eol_in_paths():
30 41 try:
31 42 fd, path = tempfile.mkstemp(prefix=tempprefix, suffix='\n\r')
32 43 os.close(fd)
33 44 os.remove(path)
34 45 return True
35 46 except:
36 47 return False
37 48
38 49 def has_hotshot():
39 50 try:
40 51 # hotshot.stats tests hotshot and many problematic dependencies
41 52 # like profile.
42 53 import hotshot.stats
43 54 return True
44 55 except ImportError:
45 56 return False
46 57
47 58 def has_lsprof():
48 59 try:
49 60 import _lsprof
50 61 return True
51 62 except ImportError:
52 63 return False
53 64
54 65 def has_git():
55 fh = os.popen('git --version 2>&1')
56 s = fh.read()
57 ret = fh.close()
58 return ret is None and s.startswith('git version')
66 return matchoutput('git --version 2>&1', r'^git version')
59 67
60 68 checks = {
61 69 "eol-in-paths": (has_eol_in_paths, "end-of-lines in paths"),
62 70 "execbit": (has_executablebit, "executable bit"),
63 71 "git": (has_git, "git command line client"),
64 72 "fifo": (has_fifo, "named pipes"),
65 73 "hotshot": (has_hotshot, "python hotshot module"),
66 74 "lsprof": (has_lsprof, "python lsprof module"),
67 75 "symlink": (has_symlink, "symbolic links"),
68 76 }
69 77
70 78 def list_features():
71 79 for name, feature in checks.iteritems():
72 80 desc = feature[1]
73 81 print name + ':', desc
74 82
75 83 parser = optparse.OptionParser("%prog [options] [features]")
76 84 parser.add_option("--list-features", action="store_true",
77 85 help="list available features")
78 86 parser.add_option("-q", "--quiet", action="store_true",
79 87 help="check features silently")
80 88
81 89 if __name__ == '__main__':
82 90 options, args = parser.parse_args()
83 91 if options.list_features:
84 92 list_features()
85 93 sys.exit(0)
86 94
87 95 quiet = options.quiet
88 96
89 97 failures = 0
90 98
91 99 def error(msg):
92 100 global failures
93 101 if not quiet:
94 102 sys.stderr.write(msg + '\n')
95 103 failures += 1
96 104
97 105 for feature in args:
98 106 negate = feature.startswith('no-')
99 107 if negate:
100 108 feature = feature[3:]
101 109
102 110 if feature not in checks:
103 111 error('hghave: unknown feature: ' + feature)
104 112 continue
105 113
106 114 check, desc = checks[feature]
107 115 if not negate and not check():
108 116 error('hghave: missing feature: ' + desc)
109 117 elif negate and check():
110 118 error('hghave: system supports %s' % desc)
111 119
112 120 if failures != 0:
113 121 sys.exit(1)
114 122
115 123
General Comments 0
You need to be logged in to leave comments. Login now