Show More
@@ -1,87 +1,89 b'' | |||
|
1 | 1 | # util.py - utility functions and platform specfic implementations |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005 K. Thananchayan <thananck@yahoo.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms |
|
6 | 6 | # of the GNU General Public License, incorporated herein by reference. |
|
7 | 7 | |
|
8 | 8 | import os |
|
9 | 9 | |
|
10 | 10 | class CommandError(Exception): pass |
|
11 | 11 | |
|
12 | 12 | def explain_exit(code): |
|
13 | 13 | """return a 2-tuple (desc, code) describing a process's status""" |
|
14 | 14 | if os.WIFEXITED(code): |
|
15 | 15 | val = os.WEXITSTATUS(code) |
|
16 | 16 | return "exited with status %d" % val, val |
|
17 | 17 | elif os.WIFSIGNALED(code): |
|
18 | 18 | val = os.WTERMSIG(code) |
|
19 | 19 | return "killed by signal %d" % val, val |
|
20 | 20 | elif os.WIFSTOPPED(code): |
|
21 | 21 | val = os.STOPSIG(code) |
|
22 | 22 | return "stopped by signal %d" % val, val |
|
23 | 23 | raise ValueError("invalid exit code") |
|
24 | 24 | |
|
25 |
def system(cmd, errprefix |
|
|
25 | def system(cmd, errprefix=None): | |
|
26 | 26 | """execute a shell command that must succeed""" |
|
27 | 27 | rc = os.system(cmd) |
|
28 | 28 | if rc: |
|
29 |
errmsg = "%s |
|
|
30 |
|
|
|
29 | errmsg = "%s %s" % (os.path.basename(cmd.split(None, 1)[0]), | |
|
30 | explain_exit(rc)[0]) | |
|
31 | if errprefix: | |
|
32 | errmsg = "%s: %s" % (errprefix, errmsg) | |
|
31 | 33 | raise CommandError(errmsg) |
|
32 | 34 | |
|
33 | 35 | def rename(src, dst): |
|
34 | 36 | try: |
|
35 | 37 | os.rename(src, dst) |
|
36 | 38 | except: |
|
37 | 39 | os.unlink(dst) |
|
38 | 40 | os.rename(src, dst) |
|
39 | 41 | |
|
40 | 42 | # Platfor specific varients |
|
41 | 43 | if os.name == 'nt': |
|
42 | 44 | nulldev = 'NUL:' |
|
43 | 45 | |
|
44 | 46 | def is_exec(f, last): |
|
45 | 47 | return last |
|
46 | 48 | |
|
47 | 49 | def set_exec(f, mode): |
|
48 | 50 | pass |
|
49 | 51 | |
|
50 | 52 | def pconvert(path): |
|
51 | 53 | return path.replace("\\", "/") |
|
52 | 54 | |
|
53 | 55 | def makelock(info, pathname): |
|
54 | 56 | ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL) |
|
55 | 57 | os.write(ld, info) |
|
56 | 58 | os.close(ld) |
|
57 | 59 | |
|
58 | 60 | def readlock(pathname): |
|
59 | 61 | return file(pathname).read() |
|
60 | 62 | |
|
61 | 63 | else: |
|
62 | 64 | nulldev = '/dev/null' |
|
63 | 65 | |
|
64 | 66 | def is_exec(f, last): |
|
65 | 67 | return (os.stat(f).st_mode & 0100 != 0) |
|
66 | 68 | |
|
67 | 69 | def set_exec(f, mode): |
|
68 | 70 | s = os.stat(f).st_mode |
|
69 | 71 | if (s & 0100 != 0) == mode: |
|
70 | 72 | return |
|
71 | 73 | if mode: |
|
72 | 74 | # Turn on +x for every +r bit when making a file executable |
|
73 | 75 | # and obey umask. |
|
74 | 76 | umask = os.umask(0) |
|
75 | 77 | os.umask(umask) |
|
76 | 78 | os.chmod(f, s | (s & 0444) >> 2 & ~umask) |
|
77 | 79 | else: |
|
78 | 80 | os.chmod(f, s & 0666) |
|
79 | 81 | |
|
80 | 82 | def pconvert(path): |
|
81 | 83 | return path |
|
82 | 84 | |
|
83 | 85 | def makelock(info, pathname): |
|
84 | 86 | os.symlink(info, pathname) |
|
85 | 87 | |
|
86 | 88 | def readlock(pathname): |
|
87 | 89 | return os.readlink(pathname) |
General Comments 0
You need to be logged in to leave comments.
Login now