Show More
@@ -1,222 +1,222 b'' | |||
|
1 | 1 | """ |
|
2 | 2 | posix.py - Posix utility function implementations for Mercurial |
|
3 | 3 | |
|
4 | 4 | Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others |
|
5 | 5 | |
|
6 | 6 | This software may be used and distributed according to the terms of |
|
7 | 7 | the GNU General Public License version 2, incorporated herein by |
|
8 | 8 | reference. |
|
9 | 9 | """ |
|
10 | 10 | |
|
11 | 11 | from i18n import _ |
|
12 | import os, sys, osutil, errno, stat, getpass | |
|
12 | import os, sys, osutil, errno, stat, getpass, pwd, grp | |
|
13 | 13 | |
|
14 | 14 | posixfile = file |
|
15 | 15 | nulldev = '/dev/null' |
|
16 | 16 | normpath = os.path.normpath |
|
17 | 17 | samestat = os.path.samestat |
|
18 | 18 | |
|
19 | 19 | umask = os.umask(0) |
|
20 | 20 | os.umask(umask) |
|
21 | 21 | |
|
22 | 22 | def openhardlinks(): |
|
23 | 23 | '''return true if it is safe to hold open file handles to hardlinks''' |
|
24 | 24 | return True |
|
25 | 25 | |
|
26 | 26 | def rcfiles(path): |
|
27 | 27 | rcs = [os.path.join(path, 'hgrc')] |
|
28 | 28 | rcdir = os.path.join(path, 'hgrc.d') |
|
29 | 29 | try: |
|
30 | 30 | rcs.extend([os.path.join(rcdir, f) |
|
31 | 31 | for f, kind in osutil.listdir(rcdir) |
|
32 | 32 | if f.endswith(".rc")]) |
|
33 | 33 | except OSError: |
|
34 | 34 | pass |
|
35 | 35 | return rcs |
|
36 | 36 | |
|
37 | 37 | def system_rcpath(): |
|
38 | 38 | path = [] |
|
39 | 39 | # old mod_python does not set sys.argv |
|
40 | 40 | if len(getattr(sys, 'argv', [])) > 0: |
|
41 | 41 | path.extend(rcfiles(os.path.dirname(sys.argv[0]) + |
|
42 | 42 | '/../etc/mercurial')) |
|
43 | 43 | path.extend(rcfiles('/etc/mercurial')) |
|
44 | 44 | return path |
|
45 | 45 | |
|
46 | 46 | def user_rcpath(): |
|
47 | 47 | return [os.path.expanduser('~/.hgrc')] |
|
48 | 48 | |
|
49 | 49 | def parse_patch_output(output_line): |
|
50 | 50 | """parses the output produced by patch and returns the file name""" |
|
51 | 51 | pf = output_line[14:] |
|
52 | 52 | if os.sys.platform == 'OpenVMS': |
|
53 | 53 | if pf[0] == '`': |
|
54 | 54 | pf = pf[1:-1] # Remove the quotes |
|
55 | 55 | else: |
|
56 | 56 | if pf.startswith("'") and pf.endswith("'") and " " in pf: |
|
57 | 57 | pf = pf[1:-1] # Remove the quotes |
|
58 | 58 | return pf |
|
59 | 59 | |
|
60 | 60 | def sshargs(sshcmd, host, user, port): |
|
61 | 61 | '''Build argument list for ssh''' |
|
62 | 62 | args = user and ("%s@%s" % (user, host)) or host |
|
63 | 63 | return port and ("%s -p %s" % (args, port)) or args |
|
64 | 64 | |
|
65 | 65 | def is_exec(f): |
|
66 | 66 | """check whether a file is executable""" |
|
67 | 67 | return (os.lstat(f).st_mode & 0100 != 0) |
|
68 | 68 | |
|
69 | 69 | def set_flags(f, l, x): |
|
70 | 70 | s = os.lstat(f).st_mode |
|
71 | 71 | if l: |
|
72 | 72 | if not stat.S_ISLNK(s): |
|
73 | 73 | # switch file to link |
|
74 | 74 | data = file(f).read() |
|
75 | 75 | os.unlink(f) |
|
76 | 76 | try: |
|
77 | 77 | os.symlink(data, f) |
|
78 | 78 | except: |
|
79 | 79 | # failed to make a link, rewrite file |
|
80 | 80 | file(f, "w").write(data) |
|
81 | 81 | # no chmod needed at this point |
|
82 | 82 | return |
|
83 | 83 | if stat.S_ISLNK(s): |
|
84 | 84 | # switch link to file |
|
85 | 85 | data = os.readlink(f) |
|
86 | 86 | os.unlink(f) |
|
87 | 87 | file(f, "w").write(data) |
|
88 | 88 | s = 0666 & ~umask # avoid restatting for chmod |
|
89 | 89 | |
|
90 | 90 | sx = s & 0100 |
|
91 | 91 | if x and not sx: |
|
92 | 92 | # Turn on +x for every +r bit when making a file executable |
|
93 | 93 | # and obey umask. |
|
94 | 94 | os.chmod(f, s | (s & 0444) >> 2 & ~umask) |
|
95 | 95 | elif not x and sx: |
|
96 | 96 | # Turn off all +x bits |
|
97 | 97 | os.chmod(f, s & 0666) |
|
98 | 98 | |
|
99 | 99 | def set_binary(fd): |
|
100 | 100 | pass |
|
101 | 101 | |
|
102 | 102 | def pconvert(path): |
|
103 | 103 | return path |
|
104 | 104 | |
|
105 | 105 | def localpath(path): |
|
106 | 106 | return path |
|
107 | 107 | |
|
108 | 108 | def shellquote(s): |
|
109 | 109 | if os.sys.platform == 'OpenVMS': |
|
110 | 110 | return '"%s"' % s |
|
111 | 111 | else: |
|
112 | 112 | return "'%s'" % s.replace("'", "'\\''") |
|
113 | 113 | |
|
114 | 114 | def quotecommand(cmd): |
|
115 | 115 | return cmd |
|
116 | 116 | |
|
117 | 117 | def popen(command, mode='r'): |
|
118 | 118 | return os.popen(command, mode) |
|
119 | 119 | |
|
120 | 120 | def testpid(pid): |
|
121 | 121 | '''return False if pid dead, True if running or not sure''' |
|
122 | 122 | if os.sys.platform == 'OpenVMS': |
|
123 | 123 | return True |
|
124 | 124 | try: |
|
125 | 125 | os.kill(pid, 0) |
|
126 | 126 | return True |
|
127 | 127 | except OSError, inst: |
|
128 | 128 | return inst.errno != errno.ESRCH |
|
129 | 129 | |
|
130 | 130 | def explain_exit(code): |
|
131 | 131 | """return a 2-tuple (desc, code) describing a process's status""" |
|
132 | 132 | if os.WIFEXITED(code): |
|
133 | 133 | val = os.WEXITSTATUS(code) |
|
134 | 134 | return _("exited with status %d") % val, val |
|
135 | 135 | elif os.WIFSIGNALED(code): |
|
136 | 136 | val = os.WTERMSIG(code) |
|
137 | 137 | return _("killed by signal %d") % val, val |
|
138 | 138 | elif os.WIFSTOPPED(code): |
|
139 | 139 | val = os.WSTOPSIG(code) |
|
140 | 140 | return _("stopped by signal %d") % val, val |
|
141 | 141 | raise ValueError(_("invalid exit code")) |
|
142 | 142 | |
|
143 | 143 | def isowner(fp, st=None): |
|
144 | 144 | """Return True if the file object f belongs to the current user. |
|
145 | 145 | |
|
146 | 146 | The return value of a util.fstat(f) may be passed as the st argument. |
|
147 | 147 | """ |
|
148 | 148 | if st is None: |
|
149 | 149 | st = fstat(fp) |
|
150 | 150 | return st.st_uid == os.getuid() |
|
151 | 151 | |
|
152 | 152 | def find_exe(command): |
|
153 | 153 | '''Find executable for command searching like which does. |
|
154 | 154 | If command is a basename then PATH is searched for command. |
|
155 | 155 | PATH isn't searched if command is an absolute or relative path. |
|
156 | 156 | If command isn't found None is returned.''' |
|
157 | 157 | if sys.platform == 'OpenVMS': |
|
158 | 158 | return command |
|
159 | 159 | |
|
160 | 160 | def findexisting(executable): |
|
161 | 161 | 'Will return executable if existing file' |
|
162 | 162 | if os.path.exists(executable): |
|
163 | 163 | return executable |
|
164 | 164 | return None |
|
165 | 165 | |
|
166 | 166 | if os.sep in command: |
|
167 | 167 | return findexisting(command) |
|
168 | 168 | |
|
169 | 169 | for path in os.environ.get('PATH', '').split(os.pathsep): |
|
170 | 170 | executable = findexisting(os.path.join(path, command)) |
|
171 | 171 | if executable is not None: |
|
172 | 172 | return executable |
|
173 | 173 | return None |
|
174 | 174 | |
|
175 | 175 | def set_signal_handler(): |
|
176 | 176 | pass |
|
177 | 177 | |
|
178 | 178 | def statfiles(files): |
|
179 | 179 | 'Stat each file in files and yield stat or None if file does not exist.' |
|
180 | 180 | lstat = os.lstat |
|
181 | 181 | for nf in files: |
|
182 | 182 | try: |
|
183 | 183 | st = lstat(nf) |
|
184 | 184 | except OSError, err: |
|
185 | 185 | if err.errno not in (errno.ENOENT, errno.ENOTDIR): |
|
186 | 186 | raise |
|
187 | 187 | st = None |
|
188 | 188 | yield st |
|
189 | 189 | |
|
190 | 190 | def getuser(): |
|
191 | 191 | '''return name of current user''' |
|
192 | 192 | return getpass.getuser() |
|
193 | 193 | |
|
194 | 194 | def expand_glob(pats): |
|
195 | 195 | '''On Windows, expand the implicit globs in a list of patterns''' |
|
196 | 196 | return list(pats) |
|
197 | 197 | |
|
198 | 198 | def username(uid=None): |
|
199 | 199 | """Return the name of the user with the given uid. |
|
200 | 200 | |
|
201 | 201 | If uid is None, return the name of the current user.""" |
|
202 | 202 | |
|
203 | 203 | if uid is None: |
|
204 | 204 | uid = os.getuid() |
|
205 | 205 | try: |
|
206 | 206 | return pwd.getpwuid(uid)[0] |
|
207 | 207 | except KeyError: |
|
208 | 208 | return str(uid) |
|
209 | 209 | |
|
210 | 210 | def groupname(gid=None): |
|
211 | 211 | """Return the name of the group with the given gid. |
|
212 | 212 | |
|
213 | 213 | If gid is None, return the name of the current group.""" |
|
214 | 214 | |
|
215 | 215 | if gid is None: |
|
216 | 216 | gid = os.getgid() |
|
217 | 217 | try: |
|
218 | 218 | return grp.getgrgid(gid)[0] |
|
219 | 219 | except KeyError: |
|
220 | 220 | return str(gid) |
|
221 | 221 | |
|
222 | 222 |
General Comments 0
You need to be logged in to leave comments.
Login now