本文整理汇总了Python中portage.os.unlink函数的典型用法代码示例。如果您正苦于以下问题:Python unlink函数的具体用法?Python unlink怎么用?Python unlink使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了unlink函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write
def write(self, sign=False, force=False):
""" Write Manifest instance to disk, optionally signing it. Returns
True if the Manifest is actually written, and False if the write
is skipped due to existing Manifest being identical."""
rval = False
if not self.allow_create:
return rval
self.checkIntegrity()
try:
myentries = list(self._createManifestEntries())
update_manifest = True
existing_st = None
if myentries and not force:
try:
f = io.open(_unicode_encode(self.getFullname(),
encoding=_encodings['fs'], errors='strict'),
mode='r', encoding=_encodings['repo.content'],
errors='replace')
oldentries = list(self._parseManifestLines(f))
existing_st = os.fstat(f.fileno())
f.close()
if len(oldentries) == len(myentries):
update_manifest = False
for i in range(len(oldentries)):
if oldentries[i] != myentries[i]:
update_manifest = True
break
except (IOError, OSError) as e:
if e.errno == errno.ENOENT:
pass
else:
raise
if update_manifest:
if myentries or not (self.thin or self.allow_missing):
# If myentries is empty, don't write an empty manifest
# when thin or allow_missing is enabled. Except for
# thin manifests with no DIST entries, myentries is
# non-empty for all currently known use cases.
write_atomic(self.getFullname(), "".join("%s\n" %
_unicode(myentry) for myentry in myentries))
self._apply_max_mtime(existing_st, myentries)
rval = True
else:
# With thin manifest, there's no need to have
# a Manifest file if there are no DIST entries.
try:
os.unlink(self.getFullname())
except OSError as e:
if e.errno != errno.ENOENT:
raise
rval = True
if sign:
self.sign()
except (IOError, OSError) as e:
if e.errno == errno.EACCES:
raise PermissionDenied(str(e))
raise
return rval
示例2: _start
def _start(self):
if self.background:
# Automatically prevent color codes from showing up in logs,
# since we're not displaying to a terminal anyway.
self.settings['NOCOLOR'] = 'true'
if self._enable_ipc_daemon:
self.settings.pop('PORTAGE_EBUILD_EXIT_FILE', None)
if self.phase not in self._phases_without_builddir:
self.settings['PORTAGE_IPC_DAEMON'] = "1"
self._start_ipc_daemon()
else:
self.settings.pop('PORTAGE_IPC_DAEMON', None)
else:
# Since the IPC daemon is disabled, use a simple tempfile based
# approach to detect unexpected exit like in bug #190128.
self.settings.pop('PORTAGE_IPC_DAEMON', None)
if self.phase not in self._phases_without_builddir:
exit_file = os.path.join(
self.settings['PORTAGE_BUILDDIR'],
'.exit_status')
self.settings['PORTAGE_EBUILD_EXIT_FILE'] = exit_file
try:
os.unlink(exit_file)
except OSError:
if os.path.exists(exit_file):
# make sure it doesn't exist
raise
else:
self.settings.pop('PORTAGE_EBUILD_EXIT_FILE', None)
SpawnProcess._start(self)
示例3: _start
def _start(self):
"""
Note: Unlike a normal AsynchronousTask.start() method,
this one does all work is synchronously. The returncode
attribute will be set before it returns.
"""
pkg = self.pkg
root_config = pkg.root_config
bintree = root_config.trees["bintree"]
rval = os.EX_OK
stdout_orig = sys.stdout
stderr_orig = sys.stderr
out = portage.StringIO()
try:
sys.stdout = out
sys.stderr = out
try:
bintree.digestCheck(pkg)
except portage.exception.FileNotFound:
writemsg("!!! Fetching Binary failed " + \
"for '%s'\n" % pkg.cpv, noiselevel=-1)
rval = 1
except portage.exception.DigestException as e:
writemsg("\n!!! Digest verification failed:\n",
noiselevel=-1)
writemsg("!!! %s\n" % e.value[0],
noiselevel=-1)
writemsg("!!! Reason: %s\n" % e.value[1],
noiselevel=-1)
writemsg("!!! Got: %s\n" % e.value[2],
noiselevel=-1)
writemsg("!!! Expected: %s\n" % e.value[3],
noiselevel=-1)
rval = 1
if rval == os.EX_OK:
# If this was successful, discard the log here since otherwise
# we'll get multiple logs for the same package.
if self.logfile is not None:
try:
os.unlink(self.logfile)
except OSError:
pass
else:
pkg_path = bintree.getname(pkg.cpv)
head, tail = os.path.split(pkg_path)
temp_filename = _checksum_failure_temp_file(head, tail)
writemsg("File renamed to '%s'\n" % (temp_filename,),
noiselevel=-1)
finally:
sys.stdout = stdout_orig
sys.stderr = stderr_orig
msg = _unicode_decode(out.getvalue(),
encoding=_encodings['content'], errors='replace')
if msg:
self.scheduler.output(msg, log_path=self.logfile)
self.returncode = rval
self.wait()
示例4: priming_commit
def priming_commit(self, myupdates, myremoved, commitmessage):
myfiles = myupdates + myremoved
fd, commitmessagefile = tempfile.mkstemp(".repoman.msg")
mymsg = os.fdopen(fd, "wb")
mymsg.write(_unicode_encode(commitmessage))
mymsg.close()
separator = '-' * 78
print()
print(green("Using commit message:"))
print(green(separator))
print(commitmessage)
print(green(separator))
print()
# Having a leading ./ prefix on file paths can trigger a bug in
# the cvs server when committing files to multiple directories,
# so strip the prefix.
myfiles = [f.lstrip("./") for f in myfiles]
retval = self.vcs_settings.changes.commit(myfiles, commitmessagefile)
# cleanup the commit message before possibly exiting
try:
os.unlink(commitmessagefile)
except OSError:
pass
if retval != os.EX_OK:
writemsg_level(
"!!! Exiting on %s (shell) "
"error code: %s\n" % (self.vcs_settings.vcs, retval),
level=logging.ERROR, noiselevel=-1)
sys.exit(retval)
示例5: _init_ipc_fifos
def _init_ipc_fifos(self):
input_fifo = os.path.join(
self.settings['PORTAGE_BUILDDIR'], '.ipc_in')
output_fifo = os.path.join(
self.settings['PORTAGE_BUILDDIR'], '.ipc_out')
for p in (input_fifo, output_fifo):
st = None
try:
st = os.lstat(p)
except OSError:
os.mkfifo(p)
else:
if not stat.S_ISFIFO(st.st_mode):
st = None
try:
os.unlink(p)
except OSError:
pass
os.mkfifo(p)
apply_secpass_permissions(p,
uid=os.getuid(),
gid=portage.data.portage_gid,
mode=0o770, stat_cached=st)
return (input_fifo, output_fifo)
示例6: close
def close(self):
"""Closes the temporary file, copies permissions (if possible),
and performs the atomic replacement via os.rename(). If the abort()
method has been called, then the temp file is closed and removed."""
f = object.__getattribute__(self, '_file')
real_name = object.__getattribute__(self, '_real_name')
if not f.closed:
try:
f.close()
if not object.__getattribute__(self, '_aborted'):
try:
apply_stat_permissions(f.name, os.stat(real_name))
except OperationNotPermitted:
pass
except FileNotFound:
pass
except OSError as oe: # from the above os.stat call
if oe.errno in (errno.ENOENT, errno.EPERM):
pass
else:
raise
os.rename(f.name, real_name)
finally:
# Make sure we cleanup the temp file
# even if an exception is raised.
try:
os.unlink(f.name)
except OSError as oe:
pass
示例7: perform_checksum
def perform_checksum(filename, hashname="MD5", calc_prelink=0):
"""
Run a specific checksum against a file. The filename can
be either unicode or an encoded byte string. If filename
is unicode then a UnicodeDecodeError will be raised if
necessary.
@param filename: File to run the checksum against
@type filename: String
@param hashname: The type of hash function to run
@type hashname: String
@param calc_prelink: Whether or not to reverse prelink before running the checksum
@type calc_prelink: Integer
@rtype: Tuple
@return: The hash and size of the data
"""
global prelink_capable
# Make sure filename is encoded with the correct encoding before
# it is passed to spawn (for prelink) and/or the hash function.
filename = _unicode_encode(filename,
encoding=_encodings['fs'], errors='strict')
myfilename = filename
prelink_tmpfile = None
try:
if (calc_prelink and prelink_capable and
is_prelinkable_elf(filename)):
# Create non-prelinked temporary file to checksum.
# Files rejected by prelink are summed in place.
try:
tmpfile_fd, prelink_tmpfile = tempfile.mkstemp()
try:
retval = portage.process.spawn([PRELINK_BINARY,
"--verify", filename], fd_pipes={1:tmpfile_fd})
finally:
os.close(tmpfile_fd)
if retval == os.EX_OK:
myfilename = prelink_tmpfile
except portage.exception.CommandNotFound:
# This happens during uninstallation of prelink.
prelink_capable = False
try:
if hashname not in hashfunc_map:
raise portage.exception.DigestException(hashname + \
" hash function not available (needs dev-python/pycrypto)")
myhash, mysize = hashfunc_map[hashname](myfilename)
except (OSError, IOError) as e:
if e.errno in (errno.ENOENT, errno.ESTALE):
raise portage.exception.FileNotFound(myfilename)
elif e.errno == portage.exception.PermissionDenied.errno:
raise portage.exception.PermissionDenied(myfilename)
raise
return myhash, mysize
finally:
if prelink_tmpfile:
try:
os.unlink(prelink_tmpfile)
except OSError as e:
if e.errno != errno.ENOENT:
raise
del e
示例8: _testPipeLogger
def _testPipeLogger(self, test_string):
producer = PopenProcess(proc=subprocess.Popen(
["bash", "-c", self._echo_cmd % test_string],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT),
scheduler=global_event_loop())
fd, log_file_path = tempfile.mkstemp()
try:
consumer = PipeLogger(background=True,
input_fd=producer.proc.stdout,
log_file_path=log_file_path)
producer.pipe_reader = consumer
producer.start()
producer.wait()
self.assertEqual(producer.returncode, os.EX_OK)
self.assertEqual(consumer.returncode, os.EX_OK)
with open(log_file_path, 'rb') as f:
content = f.read()
finally:
os.close(fd)
os.unlink(log_file_path)
return content.decode('ascii', 'replace')
示例9: _fetch_copier_exit
def _fetch_copier_exit(self, copier):
self._assert_current(copier)
try:
os.unlink(self._fetch_tmp_file)
except OSError:
pass
if self._was_cancelled():
self.wait()
return
if copier.returncode == os.EX_OK:
self._success()
self.returncode = os.EX_OK
self.wait()
else:
# out of space?
msg = "%s %s copy failed unexpectedly" % \
(self.distfile, self._fetch_tmp_dir_info)
self.scheduler.output(msg + '\n', background=True,
log_path=self._log_path)
logging.error(msg)
self.config.log_failure("%s\t%s\t%s" %
(self.cpv, self.distfile, msg))
self.config.file_failures[self.distfile] = self.cpv
self.returncode = 1
self.wait()
示例10: _hardlink_atomic
def _hardlink_atomic(self, src, dest, dir_info):
head, tail = os.path.split(dest)
hardlink_tmp = os.path.join(head, ".%s._mirrordist_hardlink_.%s" % \
(tail, os.getpid()))
try:
try:
os.link(src, hardlink_tmp)
except OSError as e:
if e.errno != errno.EXDEV:
msg = "hardlink %s from %s failed: %s" % \
(self.distfile, dir_info, e)
self.scheduler.output(msg + '\n', background=True,
log_path=self._log_path)
logging.error(msg)
return False
try:
os.rename(hardlink_tmp, dest)
except OSError as e:
msg = "hardlink rename '%s' from %s failed: %s" % \
(self.distfile, dir_info, e)
self.scheduler.output(msg + '\n', background=True,
log_path=self._log_path)
logging.error(msg)
return False
finally:
try:
os.unlink(hardlink_tmp)
except OSError:
pass
return True
示例11: _testPipeLogger
def _testPipeLogger(self, test_string):
producer = PopenProcess(proc=subprocess.Popen(
["bash", "-c", self._echo_cmd % test_string],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT),
scheduler=global_event_loop())
fd, log_file_path = tempfile.mkstemp()
try:
consumer = PipeLogger(background=True,
input_fd=os.dup(producer.proc.stdout.fileno()),
log_file_path=log_file_path)
# Close the stdout pipe, since we duplicated it, and it
# must be closed in order to avoid a ResourceWarning.
producer.proc.stdout.close()
producer.pipe_reader = consumer
producer.start()
producer.wait()
self.assertEqual(producer.returncode, os.EX_OK)
self.assertEqual(consumer.returncode, os.EX_OK)
with open(log_file_path, 'rb') as f:
content = f.read()
finally:
os.close(fd)
os.unlink(log_file_path)
return content.decode('ascii', 'replace')
示例12: collect_ebuild_messages
def collect_ebuild_messages(path):
""" Collect elog messages generated by the bash logging function stored
at 'path'.
"""
mylogfiles = None
try:
mylogfiles = os.listdir(path)
except OSError:
pass
# shortcut for packages without any messages
if not mylogfiles:
return {}
# exploit listdir() file order so we process log entries in chronological order
mylogfiles.reverse()
logentries = {}
for msgfunction in mylogfiles:
filename = os.path.join(path, msgfunction)
if msgfunction not in EBUILD_PHASES:
writemsg(_("!!! can't process invalid log file: %s\n") % filename,
noiselevel=-1)
continue
if not msgfunction in logentries:
logentries[msgfunction] = []
lastmsgtype = None
msgcontent = []
f = io.open(_unicode_encode(filename,
encoding=_encodings['fs'], errors='strict'),
mode='r', encoding=_encodings['repo.content'], errors='replace')
for l in f:
l = l.rstrip('\n')
if not l:
continue
try:
msgtype, msg = l.split(" ", 1)
except ValueError:
writemsg(_("!!! malformed entry in "
"log file: '%s'\n") % filename, noiselevel=-1)
continue
if lastmsgtype is None:
lastmsgtype = msgtype
if msgtype == lastmsgtype:
msgcontent.append(msg)
else:
if msgcontent:
logentries[msgfunction].append((lastmsgtype, msgcontent))
msgcontent = [msg]
lastmsgtype = msgtype
f.close()
if msgcontent:
logentries[msgfunction].append((lastmsgtype, msgcontent))
# clean logfiles to avoid repetitions
for f in mylogfiles:
try:
os.unlink(os.path.join(path, f))
except OSError:
pass
return logentries
示例13: _archive_copy
def _archive_copy(src_st, src_path, dest_path):
"""
Copy file from src_path to dest_path. Regular files and symlinks
are supported. If an EnvironmentError occurs, then it is logged
to stderr.
@param src_st: source file lstat result
@type src_st: posix.stat_result
@param src_path: source file path
@type src_path: str
@param dest_path: destination file path
@type dest_path: str
"""
# Remove destination file in order to ensure that the following
# symlink or copy2 call won't fail (see bug #535850).
try:
os.unlink(dest_path)
except OSError:
pass
try:
if stat.S_ISLNK(src_st.st_mode):
os.symlink(os.readlink(src_path), dest_path)
else:
shutil.copy2(src_path, dest_path)
except EnvironmentError as e:
portage.util.writemsg(
_('dispatch-conf: Error copying %(src_path)s to '
'%(dest_path)s: %(reason)s\n') % {
"src_path": src_path,
"dest_path": dest_path,
"reason": e
}, noiselevel=-1)
示例14: _success
def _success(self):
if not self._previously_added:
size = self.digests["size"]
self.config.added_byte_count += size
self.config.added_file_count += 1
self.config.log_success("%s\t%s\tadded %i bytes" %
(self.cpv, self.distfile, size))
if self._log_path is not None:
if not self.config.options.dry_run:
try:
os.unlink(self._log_path)
except OSError:
pass
if self.config.options.recycle_dir is not None:
recycle_file = os.path.join(
self.config.options.recycle_dir, self.distfile)
if self.config.options.dry_run:
if os.path.exists(recycle_file):
logging.info("dry-run: delete '%s' from recycle" %
(self.distfile,))
else:
try:
os.unlink(recycle_file)
except OSError:
pass
else:
logging.debug("delete '%s' from recycle" %
(self.distfile,))
示例15: add_manifest
def add_manifest(self, mymanifests, myheaders, myupdates, myremoved,
commitmessage):
myfiles = mymanifests[:]
# If there are no header (SVN/CVS keywords) changes in
# the files, this Manifest commit must include the
# other (yet uncommitted) files.
if not myheaders:
myfiles += myupdates
myfiles += myremoved
myfiles.sort()
fd, commitmessagefile = tempfile.mkstemp(".repoman.msg")
mymsg = os.fdopen(fd, "wb")
mymsg.write(_unicode_encode(commitmessage))
mymsg.close()
retval = self.vcs_settings.changes.commit(myfiles, commitmessagefile)
# cleanup the commit message before possibly exiting
try:
os.unlink(commitmessagefile)
except OSError:
pass
if retval != os.EX_OK:
writemsg_level(
"!!! Exiting on %s (shell) "
"error code: %s\n" % (self.vcs_settings.vcs, retval),
level=logging.ERROR, noiselevel=-1)
sys.exit(retval)