本文整理汇总了Python中twitter.common.dirutil.safe_delete函数的典型用法代码示例。如果您正苦于以下问题:Python safe_delete函数的具体用法?Python safe_delete怎么用?Python safe_delete使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了safe_delete函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: erase_logs
def erase_logs(self, task_id):
for fn in self.get_logs(task_id, with_size=False):
safe_delete(fn)
state = self.state(task_id)
if state and state.header:
safe_rmtree(TaskPath(root=self._root, task_id=task_id, log_dir=state.header.log_dir)
.getpath('process_logbase'))
示例2: select_binary
def select_binary(base_path, version, name, config=None):
"""Selects a binary matching the current os and architecture.
:raises: :class:`pants.binary_util.BinaryUtil.BinaryNotFound` if no binary of the given version
and name could be found.
"""
# TODO(John Sirois): finish doc of the path structure expexcted under base_path
config = config or Config.load()
bootstrap_dir = config.getdefault('pants_bootstrapdir')
binary_path = select_binary_base_path(base_path, version, name)
bootstrapped_binary_path = os.path.join(bootstrap_dir, binary_path)
if not os.path.exists(bootstrapped_binary_path):
downloadpath = bootstrapped_binary_path + '~'
try:
with select_binary_stream(base_path, version, name, config) as stream:
with safe_open(downloadpath, 'wb') as bootstrapped_binary:
bootstrapped_binary.write(stream())
os.rename(downloadpath, bootstrapped_binary_path)
chmod_plus_x(bootstrapped_binary_path)
finally:
safe_delete(downloadpath)
log.debug('Selected {binary} binary bootstrapped to: {path}'
.format(binary=name, path=bootstrapped_binary_path))
return bootstrapped_binary_path
示例3: swap_files
def swap_files(self, src, tgt):
if os.path.exists(tgt):
safe_delete(tgt)
try:
os.rename(src, tgt)
except OSError as e:
if e.errno != errno.ENOENT:
raise
示例4: use_cached_files
def use_cached_files(self, cache_key):
artifact = self._cache.use_cached_files(cache_key)
if artifact and self._post_read_func:
paths = artifact.get_paths()
new_paths = self._post_read_func(paths) # Can return None to signal failure.
if new_paths is None: # Failure. Delete artifact and pretend it was never found.
for path in paths:
safe_delete(path)
self.delete(cache_key)
artifact = None
else:
artifact.override_paths(new_paths)
return artifact
示例5: temporary_file
def temporary_file(root_dir=None, cleanup=True):
"""
A with-context that creates a temporary file and returns a writeable file descriptor to it.
You may specify the following keyword args:
root_dir [path]: The parent directory to create the temporary file.
cleanup [True/False]: Whether or not to clean up the temporary file.
"""
with tempfile.NamedTemporaryFile(dir=root_dir, delete=False) as fd:
try:
yield fd
finally:
if cleanup:
safe_delete(fd.name)
示例6: instrument
def instrument(self, targets, tests, junit_classpath):
self._cobertura_classpath = self._task_exports.tool_classpath(self._cobertura_bootstrap_key)
safe_delete(self._coverage_datafile)
classes_by_target = self._context.products.get_data('classes_by_target')
for target in targets:
if self.is_coverage_target(target):
classes_by_rootdir = classes_by_target.get(target)
if classes_by_rootdir:
for root, products in classes_by_rootdir.rel_paths():
self._rootdirs[root].update(products)
# Cobertura uses regular expressions for filters, and even then there are still problems
# with filtering. It turned out to be easier to just select which classes to instrument
# by filtering them here.
# TODO(ji): Investigate again how we can use cobertura's own filtering mechanisms.
if self._coverage_filters:
for basedir, classes in self._rootdirs.items():
updated_classes = []
for cls in classes:
does_match = False
for positive_filter in self._include_filters:
if fnmatch.fnmatchcase(_classfile_to_classname(cls), positive_filter):
does_match = True
for negative_filter in self._exclude_filters:
if fnmatch.fnmatchcase(_classfile_to_classname(cls), negative_filter):
does_match = False
if does_match:
updated_classes.append(cls)
self._rootdirs[basedir] = updated_classes
for basedir, classes in self._rootdirs.items():
if not classes:
continue # No point in running instrumentation if there is nothing to instrument!
args = [
'--basedir',
basedir,
'--datafile',
self._coverage_datafile,
]
with temporary_file() as fd:
fd.write('\n'.join(classes) + '\n')
args.append('--listOfFilesToInstrument')
args.append(fd.name)
main = 'net.sourceforge.cobertura.instrument.InstrumentMain'
result = execute_java(classpath=self._cobertura_classpath + junit_classpath,
main=main,
args=args,
workunit_factory=self._context.new_workunit,
workunit_name='cobertura-instrument')
if result != 0:
raise TaskError("java %s ... exited non-zero (%i)"
" 'failed to instrument'" % (main, result))
示例7: symlink_cachepath
def symlink_cachepath(inpath, symlink_dir, outpath):
"""Symlinks all paths listed in inpath into symlink_dir.
Writes the resulting paths to outpath.
Returns a map of path -> symlink to that path.
"""
safe_mkdir(symlink_dir)
with safe_open(inpath, 'r') as infile:
paths = filter(None, infile.read().strip().split(os.pathsep))
symlinks = []
for path in paths:
symlink = os.path.join(symlink_dir, os.path.basename(path))
safe_delete(symlink)
os.symlink(path, symlink)
symlinks.append(symlink)
with safe_open(outpath, 'w') as outfile:
outfile.write(':'.join(symlinks))
symlink_map = dict(zip(paths, symlinks))
return symlink_map
示例8: execute
def execute(self):
dist_dir = self._config.getdefault('pants_distdir')
target_base = '%s-%s' % (
self.target.provides.name, self.target.provides.version)
setup_dir = os.path.join(dist_dir, target_base)
expected_tgz = '%s.tar.gz' % target_base
expected_target = os.path.join(setup_dir, 'dist', expected_tgz)
dist_tgz = os.path.join(dist_dir, expected_tgz)
chroot = Chroot(dist_dir, name=self.target.provides.name)
self.write_contents(chroot)
self.write_setup(chroot)
safe_rmtree(setup_dir)
os.rename(chroot.path(), setup_dir)
with pushd(setup_dir):
cmd = '%s setup.py %s' % (sys.executable, self.options.run or 'sdist')
print('Running "%s" in %s' % (cmd, setup_dir))
extra_args = {} if self.options.run else dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE)
po = subprocess.Popen(cmd, shell=True, **extra_args)
stdout, stderr = po.communicate()
if self.options.run:
print('Ran %s' % cmd)
print('Output in %s' % setup_dir)
return po.returncode
elif po.returncode != 0:
print('Failed to run %s!' % cmd)
for line in ''.join(stdout).splitlines():
print('stdout: %s' % line)
for line in ''.join(stderr).splitlines():
print('stderr: %s' % line)
return po.returncode
else:
if not os.path.exists(expected_target):
print('Could not find expected target %s!' % expected_target)
sys.exit(1)
safe_delete(dist_tgz)
os.rename(expected_target, dist_tgz)
safe_rmtree(setup_dir)
print('Wrote %s' % dist_tgz)
示例9: execute
def execute(self):
config = Config.load()
distdir = config.getdefault('pants_distdir')
setup_dir = os.path.join(distdir, '%s-%s' % (
self.target.provides._name, self.target.provides._version))
chroot = Chroot(distdir, name=self.target.provides._name)
self.write_sources(chroot)
self.write_setup(chroot)
if os.path.exists(setup_dir):
import shutil
shutil.rmtree(setup_dir)
os.rename(chroot.path(), setup_dir)
with pushd(setup_dir):
cmd = '%s setup.py %s' % (sys.executable, self.options.run or 'sdist')
print('Running "%s" in %s' % (cmd, setup_dir))
extra_args = {} if self.options.run else dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE)
po = subprocess.Popen(cmd, shell=True, **extra_args)
po.wait()
if self.options.run:
print('Ran %s' % cmd)
print('Output in %s' % setup_dir)
return po.returncode
elif po.returncode != 0:
print('Failed to run %s!' % cmd)
for line in po.stdout.read().splitlines():
print('stdout: %s' % line)
for line in po.stderr.read().splitlines():
print('stderr: %s' % line)
return po.returncode
expected_tgz = '%s-%s.tar.gz' % (self.target.provides._name, self.target.provides._version)
expected_target = os.path.join(setup_dir, 'dist', expected_tgz)
dist_tgz = os.path.join(distdir, expected_tgz)
if not os.path.exists(expected_target):
print('Could not find expected target %s!' % expected_target)
sys.exit(1)
safe_delete(dist_tgz)
os.rename(expected_target, dist_tgz)
print('Wrote %s' % dist_tgz)
safe_rmtree(setup_dir)
示例10: safe_file
def safe_file(path, suffix=None, cleanup=True):
"""A with-context that copies a file, and copies the copy back to the original file on success.
This is useful for doing work on a file but only changing its state on success.
- suffix: Use this suffix to create the copy. Otherwise use a random string.
- cleanup: Whether or not to clean up the copy.
"""
safe_path = path + '.%s' % suffix or uuid.uuid4()
if os.path.exists(path):
shutil.copy(path, safe_path)
try:
yield safe_path
if cleanup:
shutil.move(safe_path, path)
else:
shutil.copy(safe_path, path)
finally:
if cleanup:
safe_delete(safe_path)
示例11: temporary_file
def temporary_file(root_dir=None, cleanup=True):
"""
A with-context that creates a temporary file and returns a writeable file descriptor to it.
You may specify the following keyword args:
:param str root_dir: The parent directory to create the temporary file.
:param bool cleanup: Whether or not to clean up the temporary file.
>>> with temporary_file() as fp:
... fp.write('woot')
... fp.sync()
... # pass fp on to something else
"""
with tempfile.NamedTemporaryFile(dir=root_dir, delete=False) as fd:
try:
yield fd
finally:
if cleanup:
safe_delete(fd.name)
示例12: _bootstrap_ivy_classpath
def _bootstrap_ivy_classpath(self, executor, workunit_factory, retry=True):
# TODO(John Sirois): Extract a ToolCache class to control the path structure:
# https://jira.twitter.biz/browse/DPB-283
ivy_bootstrap_dir = \
os.path.join(self._config.getdefault('pants_bootstrapdir'), 'tools', 'jvm', 'ivy')
digest = hashlib.sha1()
if os.path.isfile(self._version_or_ivyxml):
with open(self._version_or_ivyxml) as fp:
digest.update(fp.read())
else:
digest.update(self._version_or_ivyxml)
classpath = os.path.join(ivy_bootstrap_dir, '%s.classpath' % digest.hexdigest())
if not os.path.exists(classpath):
ivy = self._bootstrap_ivy(os.path.join(ivy_bootstrap_dir, 'bootstrap.jar'))
args = ['-confs', 'default', '-cachepath', classpath]
if os.path.isfile(self._version_or_ivyxml):
args.extend(['-ivy', self._version_or_ivyxml])
else:
args.extend(['-dependency', 'org.apache.ivy', 'ivy', self._version_or_ivyxml])
try:
ivy.execute(args=args, executor=executor,
workunit_factory=workunit_factory, workunit_name='ivy-bootstrap')
except ivy.Error as e:
safe_delete(classpath)
raise self.Error('Failed to bootstrap an ivy classpath! %s' % e)
with open(classpath) as fp:
cp = fp.read().strip().split(os.pathsep)
if not all(map(os.path.exists, cp)):
safe_delete(classpath)
if retry:
return self._bootstrap_ivy_classpath(executor, workunit_factory, retry=False)
raise self.Error('Ivy bootstrapping failed - invalid classpath: %s' % ':'.join(cp))
return cp
示例13: select_binary
def select_binary(base_path, version, name, config=None):
"""Selects a binary matching the current os and architecture.
Raises TaskError if no binary of the given version and name could be found.
"""
# TODO(John Sirois): finish doc of the path structure expexcted under base_path
config = config or Config.load()
cachedir = config.getdefault('pants_cachedir', default=os.path.expanduser('~/.pants.d'))
baseurl = config.getdefault('pants_support_baseurl')
timeout_secs = config.getdefault('pants_support_fetch_timeout_secs', type=int, default=30)
sysname, _, release, _, machine = os.uname()
os_id = _ID_BY_OS[sysname.lower()]
if os_id:
middle_path = _PATH_BY_ID[os_id(release, machine)]
if middle_path:
binary_path = os.path.join(base_path, *(middle_path + [version, name]))
cached_binary_path = os.path.join(cachedir, binary_path)
if not os.path.exists(cached_binary_path):
url = posixpath.join(baseurl, binary_path)
log.info('Fetching %s binary from: %s' % (name, url))
downloadpath = cached_binary_path + '~'
try:
with closing(urllib_request.urlopen(url, timeout=timeout_secs)) as binary:
with safe_open(downloadpath, 'wb') as cached_binary:
cached_binary.write(binary.read())
os.rename(downloadpath, cached_binary_path)
chmod_plus_x(cached_binary_path)
except (IOError, urllib_error.HTTPError, urllib_error.URLError) as e:
raise TaskError('Failed to fetch binary from %s: %s' % (url, e))
finally:
safe_delete(downloadpath)
log.debug('Selected %s binary cached at: %s' % (name, cached_binary_path))
return cached_binary_path
raise TaskError('No %s binary found for: %s' % (name, (sysname, release, machine)))
示例14: cleanup
def cleanup(self):
for fetched in self._fetched:
safe_delete(fetched)
示例15: delete
def delete(self, cache_key):
safe_delete(self._cache_file_for_key(cache_key))