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