_current_job = t
return t
+def getjobparam(*args):
+ return current_job()['script_parameters'].get(*args)
+
def api():
return service
exit(0)
class util:
+ @staticmethod
+ def clear_tmpdir(path=None):
+ """
+ Ensure the given directory (or TASK_TMPDIR if none given)
+ exists and is empty.
+ """
+ if path == None:
+ path = current_task().tmpdir
+ if os.path.exists(path):
+ p = subprocess.Popen(['rm', '-rf', path])
+ stdout, stderr = p.communicate(None)
+ if p.returncode != 0:
+ raise Exception('rm -rf %s: %s' % (path, stderr))
+ os.mkdir(path)
+
@staticmethod
def run_command(execargs, **kwargs):
- if 'stdin' not in kwargs:
- kwargs['stdin'] = subprocess.PIPE
- if 'stdout' not in kwargs:
- kwargs['stdout'] = subprocess.PIPE
- if 'stderr' not in kwargs:
- kwargs['stderr'] = subprocess.PIPE
- p = subprocess.Popen(execargs, close_fds=True, shell=False,
- **kwargs)
+ kwargs.setdefault('stdin', subprocess.PIPE)
+ kwargs.setdefault('stdout', subprocess.PIPE)
+ kwargs.setdefault('stderr', sys.stderr)
+ kwargs.setdefault('close_fds', True)
+ kwargs.setdefault('shell', False)
+ p = subprocess.Popen(execargs, **kwargs)
stdoutdata, stderrdata = p.communicate(None)
if p.returncode != 0:
raise Exception("run_command %s exit %d:\n%s" %
collection -- collection locator
path -- where to extract: absolute, or relative to job tmp
"""
+ matches = re.search(r'^([0-9a-f]+)(\+[\w@]+)*$', collection)
+ if matches:
+ collection_hash = matches.group(1)
+ else:
+ collection_hash = hashlib.md5(collection).hexdigest()
if not re.search('^/', path):
path = os.path.join(current_job().tmpdir, path)
lockfile = open(path + '.lock', 'w')
os.mkdir(path)
already_have_it = False
try:
- if os.readlink(os.path.join(path, '.locator')) == collection:
+ if os.readlink(os.path.join(path, '.locator')) == collection_hash:
already_have_it = True
except OSError:
pass
outfile.close()
if len(files_got) < len(files):
raise Exception("Wanted files %s but only got %s from %s" % (files, files_got, map(lambda z: z.name(), list(CollectionReader(collection).all_files()))))
- os.symlink(collection, os.path.join(path, '.locator'))
+ os.symlink(collection_hash, os.path.join(path, '.locator'))
lockfile.close()
return path
self.start_new_stream(stream_name)
todo = []
if max_manifest_depth == 0:
- dirents = util.listdir_recursive(path)
+ dirents = sorted(util.listdir_recursive(path))
else:
dirents = sorted(os.listdir(path))
for dirent in dirents:
map(lambda x: self.write_directory_tree(*x), todo)
def write(self, newdata):
+ if hasattr(newdata, '__iter__'):
+ for s in newdata:
+ self.write(s)
+ return
self._data_buffer += [newdata]
self._data_buffer_len += len(newdata)
self._current_stream_length += len(newdata)
def set_current_stream_name(self, newstreamname):
if re.search(r'[ \t\n]', newstreamname):
raise AssertionError("Manifest stream names cannot contain whitespace")
- self._current_stream_name = newstreamname
+ self._current_stream_name = '.' if newstreamname=='' else newstreamname
def current_stream_name(self):
return self._current_stream_name
def finish_current_stream(self):