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