1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import bdb
16 import bz2
17 import debugger
18 import fcntl
19 import errno
20 import itertools
21 import log
22 import misc
23 import os
24 import re
25 import select
26 import shutil
27 import signal
28 import stat
29 import string
30 import StringIO
31 import subprocess
32 import struct
33 import sys
34 import tempfile
35 import time
36 import urllib
37 import urlparse
38 import weakref
39 import xmlrpclib
40 import zlib
41
42 from conary.lib import fixedglob, graph, log
43
44
45
47 s = os.path.normpath(path)
48 if s.startswith(os.sep + os.sep):
49 return s[1:]
50 return s
51
60
62 return stat.S_ISREG(os.lstat(path)[stat.ST_MODE])
63
79
81 file = arg[0]
82 path = arg[1]
83 testname = '%s%s%s' %(dirname, os.sep, file)
84 if file in names:
85 path[0] = testname
86 del names
87
93
95 for dir in searchdirs:
96 s = "%s%s%s" %(dir, os.sep, file)
97 if os.path.exists(s):
98 return s
99 if error:
100 raise OSError, (errno.ENOENT, os.strerror(errno.ENOENT))
101 return None
102
105
107 if not os.environ.has_key('PATH') or os.environ['PATH'] == '':
108 p = os.defpath
109 else:
110 p = os.environ['PATH']
111
112 pathlist = p.split (os.pathsep)
113
114 for path in pathlist:
115 f = os.path.join(path, filename)
116 if os.access(f, os.X_OK):
117 return f
118 return None
119
121 """Recursively list all files in the directory"""
122 items = [topdir]
123 while items:
124 item = items.pop()
125 if os.path.islink(item) or os.path.isfile(item):
126 yield item
127 continue
128
129 listdir = os.listdir(item)
130
131
132 listdir.sort()
133 listdir.reverse()
134 listdir = [ os.path.join(item, x) for x in listdir ]
135 items.extend(listdir)
136
137 if withDirs:
138
139 yield item
140
142 surl = list(urlparse.urlsplit(url))
143 if surl[2] == '':
144 surl[2] = '/'
145 elif surl[2] != '/':
146 tail = ''
147 if surl[2].endswith('/'):
148 tail = '/'
149 surl[2] = normpath(surl[2]) + tail
150 return urlparse.urlunsplit(surl)
151
152 errorMessage = '''
153 ERROR: An unexpected condition has occurred in Conary. This is
154 most likely due to insufficient handling of erroneous input, but
155 may be some other bug. In either case, please report the error at
156 http://issues.rpath.com/ and attach to the issue the file
157 %(stackfile)s
158
159 Then, for more complete information, please run the following script:
160 conary-debug "%(command)s"
161 You can attach the resulting archive to your issue report at
162 http://issues.rpath.com/ For more information, or if you have
163 trouble with the conary-debug command, go to:
164 http://wiki.rpath.com/wiki/Conary:How_To_File_An_Effective_Bug_Report
165
166 To get a debug prompt, rerun the command with --debug-all
167
168 Error details follow:
169
170 %(filename)s:%(lineno)s
171 %(errtype)s: %(errmsg)s
172
173 The complete related traceback has been saved as %(stackfile)s
174 '''
175 _debugAll = False
176
180 def SIGUSR1Handler(signum, frame):
181 global _debugAll
182 _debugAll = True
183 print >>sys.stderr, '<Turning on KeyboardInterrupt catching>'
184
185 def excepthook(typ, value, tb):
186 if typ is bdb.BdbQuit:
187 sys.exit(1)
188 sys.excepthook = sys.__excepthook__
189 if not _debugAll and (typ == KeyboardInterrupt and not debugCtrlC):
190 sys.exit(1)
191
192 out = BoundedStringIO()
193 formatTrace(typ, value, tb, stream = out, withLocals = False)
194 out.write("\nFull stack:\n")
195 formatTrace(typ, value, tb, stream = out, withLocals = True)
196 out.seek(0)
197 tbString = out.read()
198 del out
199 if log.syslog is not None:
200 log.syslog("command failed\n%s", tbString)
201
202 if debug or _debugAll:
203 formatTrace(typ, value, tb, stream = sys.stderr,
204 withLocals = False)
205 if sys.stdout.isatty() and sys.stdin.isatty():
206 debugger.post_mortem(tb, typ, value)
207 else:
208 sys.exit(1)
209 elif log.getVerbosity() is log.DEBUG:
210 log.debug(tbString)
211 else:
212 cmd = sys.argv[0]
213 if cmd.endswith('/commands/conary'):
214 cmd = cmd[:len('/commands/conary')] + '/bin/conary'
215 elif cmd.endswith('/commands/cvc'):
216 cmd = cmd[:len('/commands/cvc')] + '/bin/cvc'
217
218 origTb = tb
219 cmd = normpath(cmd)
220 sys.argv[0] = cmd
221 while tb.tb_next: tb = tb.tb_next
222 lineno = tb.tb_frame.f_lineno
223 filename = tb.tb_frame.f_code.co_filename
224 tmpfd, stackfile = tempfile.mkstemp('.txt', prefix)
225 os.write(tmpfd, tbString)
226 os.close(tmpfd)
227
228 sys.stderr.write(error % dict(command=' '.join(sys.argv),
229 filename=filename,
230 lineno=lineno,
231 errtype=type.__name__,
232 errmsg=value,
233 stackfile=stackfile))
234
235
236
237 return excepthook
238
239
240
242 if rc:
243 if not os.WIFEXITED(rc):
244 info = 'Shell command "%s" killed with signal %d' \
245 %(cmd, os.WTERMSIG(rc))
246 if os.WEXITSTATUS(rc):
247 info = 'Shell command "%s" exited with exit code %d' \
248 %(cmd, os.WEXITSTATUS(rc))
249 log.error(info)
250 raise RuntimeError, info
251
252 -def execute(cmd, destDir=None, verbose=True):
253 """
254 similar to os.system, but raises errors if exit code != 0 and closes stdin
255 so processes can never block on user input
256 """
257 if verbose:
258 log.info(cmd)
259 rc = subprocess.call(cmd, shell=True, cwd=destDir, stdin=open(os.devnull))
260
261 if rc < 0:
262
263 rc = rc * -1
264 else:
265
266 rc = rc << 8
267 _handle_rc(rc, cmd)
268
270 """
271 Version of popen() that throws errors on close(), unlike os.popen()
272 """
273
281