Mercurial > hg-stable
annotate mercurial/ui.py @ 508:42a660abaf75
[PATCH] Harden os.system
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[PATCH] Harden os.system
From: Bryan O'Sullivan <bos@serpentine.com>
Add util.system function. This is similar to os.system, but will
either succeed (if the process finishes with a zero exit code) or raise
a util.CommandError (if the process exits uncleanly or is killed by
a signal).
Add util.explain_exit function. This tends to be ubiquitous in code
that calls other processes, and must describe what has gone wrong.
Change some uses of os.system over to util.system.
manifest hash: e3bf4adcac5b915432ec0af00efdbcef86bea4b1
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)
iD8DBQFCwSipywK+sNU5EO8RAr0RAJkBDt8XQ7mYQAWNHNgTOVt1eyWU1QCfe1oO
2OwxyWqpbRNACVJHHfZ3/Xw=
=OaRX
-----END PGP SIGNATURE-----
author | mpm@selenic.com |
---|---|
date | Tue, 28 Jun 2005 02:38:33 -0800 |
parents | 1f81ebff98c9 |
children | 03f27b1381f9 |
rev | line source |
---|---|
207 | 1 # ui.py - user interface bits for mercurial |
2 # | |
3 # Copyright 2005 Matt Mackall <mpm@selenic.com> | |
4 # | |
5 # This software may be used and distributed according to the terms | |
6 # of the GNU General Public License, incorporated herein by reference. | |
7 | |
508 | 8 import os, sys, re, ConfigParser, util |
207 | 9 |
10 class ui: | |
11 def __init__(self, verbose=False, debug=False, quiet=False, | |
12 interactive=True): | |
285 | 13 self.cdata = ConfigParser.SafeConfigParser() |
14 self.cdata.read(os.path.expanduser("~/.hgrc")) | |
15 | |
16 self.quiet = self.configbool("ui", "quiet") | |
17 self.verbose = self.configbool("ui", "verbose") | |
18 self.debugflag = self.configbool("ui", "debug") | |
19 self.interactive = self.configbool("ui", "interactive", True) | |
20 | |
21 self.quiet = (self.quiet or quiet) and not verbose and not debug | |
22 self.verbose = (self.verbose or verbose) or debug | |
23 self.debugflag = (self.debugflag or debug) | |
24 self.interactive = (self.interactive and interactive) | |
25 | |
337 | 26 def readconfig(self, fp): |
27 self.cdata.readfp(fp) | |
28 | |
285 | 29 def config(self, section, val, default=None): |
30 if self.cdata.has_option(section, val): | |
31 return self.cdata.get(section, val) | |
32 return default | |
33 | |
34 def configbool(self, section, val, default=False): | |
35 if self.cdata.has_option(section, val): | |
36 return self.cdata.getboolean(section, val) | |
37 return default | |
38 | |
39 def configitems(self, section): | |
40 if self.cdata.has_section(section): | |
41 return self.cdata.items(section) | |
42 return [] | |
43 | |
506 | 44 def expandpath(self, loc): |
45 paths = {} | |
46 for name, path in self.configitems("paths"): | |
47 paths[name] = path | |
48 | |
49 return paths.get(loc, loc) | |
50 | |
207 | 51 def write(self, *args): |
52 for a in args: | |
53 sys.stdout.write(str(a)) | |
54 def readline(self): | |
55 return sys.stdin.readline()[:-1] | |
56 def prompt(self, msg, pat, default = "y"): | |
57 if not self.interactive: return default | |
58 while 1: | |
59 self.write(msg, " ") | |
60 r = self.readline() | |
61 if re.match(pat, r): | |
62 return r | |
63 else: | |
64 self.write("unrecognized response\n") | |
65 def status(self, *msg): | |
66 if not self.quiet: self.write(*msg) | |
234
3427806d5ab9
ui.warn can use more than one argument like the other ui methods.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
207
diff
changeset
|
67 def warn(self, *msg): |
207 | 68 self.write(*msg) |
69 def note(self, *msg): | |
70 if self.verbose: self.write(*msg) | |
71 def debug(self, *msg): | |
72 if self.debugflag: self.write(*msg) | |
73 def edit(self, text): | |
249 | 74 import tempfile |
207 | 75 (fd, name) = tempfile.mkstemp("hg") |
76 f = os.fdopen(fd, "w") | |
77 f.write(text) | |
78 f.close() | |
79 | |
80 editor = os.environ.get("HGEDITOR") or os.environ.get("EDITOR", "vi") | |
508 | 81 util.system("%s %s" % (editor, name), errprefix = "edit failed") |
207 | 82 |
83 t = open(name).read() | |
84 t = re.sub("(?m)^HG:.*\n", "", t) | |
85 | |
86 return t | |
87 |