1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
13 from config import config, init_conf
21 retries_times = [5 * 60, 5 * 60, 10 * 60, 10 * 60, 30 * 60, 60 * 60]
23 def read_name_val(file):
25 r = {'_file': file[:-5], '_desc': file}
26 rx = re.compile(r"^([^:]+)\s*:(.*)$")
27 for l in f.xreadlines():
33 r[m.group(1)] = string.strip(m.group(2))
39 def scp_file(src, target):
41 f = os.popen("scp -v -B %s %s 2>&1 < /dev/null" % (src, target))
48 def copy_file(src, target):
50 shutil.copyfile(src, target)
54 exctype, value = sys.exc_info()[:2]
55 problem[src] = "cannot copy file: %s" % traceback.format_exception_only(exctype, value)
58 def rsync_file(src, target, host):
60 p = open(path.rsync_password_file, "r")
62 for l in p.xreadlines():
64 if len(l) >= 2 and l[0] == host:
67 rsync = "rsync --verbose --archive"
69 p = open(".rsync.pass", "w")
70 os.chmod(".rsync.pass", 0600)
71 p.write("%s\n" % password)
73 rsync += " --password-file .rsync.pass"
74 f = os.popen("%s %s %s 2>&1 < /dev/null" % (rsync, src, target))
77 os.unlink(".rsync.pass")
83 def rsync_ssh_file(src, target):
85 rsync = "rsync --verbose --archive -e ssh"
86 f = os.popen("%s %s %s 2>&1 < /dev/null" % (rsync, src, target))
93 def post_file(src, url):
99 req = urllib2.Request(url, data)
100 req.add_header('X-Filename', os.path.basename(src))
101 f = urllib2.urlopen(req)
108 def send_file(src, target):
111 log.notice("sending %s to %s (size %d bytes)" % (src, target, os.stat(src).st_size))
112 m = re.match('rsync://([^/]+)/.*', target)
114 return not rsync_file(src, target, host = m.group(1))
115 if target != "" and target[0] == '/':
116 return not copy_file(src, target)
117 m = re.match('scp://([^@:]+@[^/:]+)(:|)(.*)', target)
119 return not scp_file(src, m.group(1) + ":" + m.group(3))
120 m = re.match('ssh\+rsync://([^@:]+@[^/:]+)(:|)(.*)', target)
122 return not rsync_ssh_file(src, m.group(1) + ":" + m.group(3))
123 m = re.match('http://.*', target)
125 return not post_file(src, target)
126 log.alert("unsupported protocol: %s" % target)
129 log.error("send_file(%s, %s): %s" % (src, target, e))
133 def maybe_flush_queue(dir):
136 f = open(dir + "/retry-at")
137 last_retry = int(string.strip(f.readline()))
138 retry_delay = int(string.strip(f.readline()))
140 if last_retry + retry_delay > time.time():
142 os.unlink(dir + "/retry-at")
146 status.push("flushing %s" % dir)
149 f = open(dir + "/retry-at", "w")
150 if retry_delay in retries_times:
151 idx = retries_times.index(retry_delay)
152 if idx < len(retries_times) - 1: idx += 1
155 f.write("%d\n%d\n" % (time.time(), retries_times[idx]))
160 def flush_queue(dir):
163 for f in glob.glob(dir + "/*.desc"):
165 if d != None: q.append(d)
167 rc = cmp(x['Time'], y['Time'])
168 if rc == 0 and x.has_key('Type') and y.has_key('Type'):
169 return cmp(x['Type'], y['Type'])
178 if not send_file(d['_file'], d['Target']):
181 if os.access(d['_file'] + ".info", os.F_OK):
182 if not send_file(d['_file'] + ".info", d['Target'] + ".info"):
185 os.unlink(d['_file'] + ".info")
186 os.unlink(d['_file'])
187 os.unlink(d['_desc'])
192 emails[config.admin_email] = 1
194 for src, msg in problems.iteritems():
195 pr = pr + "[src: %s]\n\n%s\n" % (src, msg)
197 if d.has_key('Requester'):
198 emails[d['Requester']] = 1
201 m.set_headers(to = string.join(e, ", "),
202 subject = "[%s] builder queue problem" % config.builder)
203 m.write("there were problems sending files from queue %s:\n" % dir)
204 m.write("problems:\n")
207 log.error("error sending files from %s:\n%s\n" % (dir, pr))
215 if lock.lock("sending-files", non_block = 1) == None:
218 maybe_flush_queue(path.notify_queue_dir)
219 maybe_flush_queue(path.buildlogs_queue_dir)
220 maybe_flush_queue(path.ftp_queue_dir)
222 if __name__ == '__main__':