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