本文整理汇总了Python中shutil.copystat函数的典型用法代码示例。如果您正苦于以下问题:Python copystat函数的具体用法?Python copystat怎么用?Python copystat使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了copystat函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: copytree
def copytree(src, dst, symlinks=False, ignore=None):
"""
copytree that works even if folder already exists
"""
# http://stackoverflow.com/questions/1868714/how-do-i-copy-an-entire-directory-of-files-into-an-existing-directory-using-pyth
if not os.path.exists(dst):
os.makedirs(dst)
shutil.copystat(src, dst)
lst = os.listdir(src)
if ignore:
excl = ignore(src, lst)
lst = [x for x in lst if x not in excl]
for item in lst:
s = os.path.join(src, item)
d = os.path.join(dst, item)
if symlinks and os.path.islink(s):
if os.path.lexists(d):
os.remove(d)
os.symlink(os.readlink(s), d)
try:
st = os.lstat(s)
mode = stat.S_IMODE(st.st_mode)
os.lchmod(d, mode)
except:
pass # lchmod not available
elif os.path.isdir(s):
copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
示例2: backup
def backup(self, datasetType, dataId):
"""Rename any existing object with the given type and dataId.
The CameraMapper implementation saves objects in a sequence of e.g.:
foo.fits
foo.fits~1
foo.fits~2
All of the backups will be placed in the output repo, however, and will
not be removed if they are found elsewhere in the _parent chain. This
means that the same file will be stored twice if the previous version was
found in an input repo.
"""
n = 0
suffix = ""
newLocation = self.map(datasetType, dataId, write=True)
newPath = newLocation.getLocations()[0]
path = self._parentSearch(newPath)
oldPaths = []
while path is not None:
n += 1
oldPaths.append((n, path))
path = self._parentSearch("%s~%d" % (newPath, n))
for n, oldPath in reversed(oldPaths):
newDir, newFile = os.path.split(newPath)
if not os.path.exists(newDir):
os.makedirs(newDir)
shutil.copy(oldPath, "%s~%d" % (newPath, n))
shutil.copystat(oldPath, "%s~%d" % (newPath, n))
示例3: overwriteCopy
def overwriteCopy(src, dest, symlinks=False, ignore=None):
if not os.path.exists(dest):
os.makedirs(dest)
shutil.copystat(src, dest)
sub_list = os.listdir(src)
if ignore:
excl = ignore(src, sub_list)
sub_list = [x for x in sub_list if x not in excl]
for i_sub in sub_list:
s_path = os.path.join(src, i_sub)
d_path = os.path.join(dest, i_sub)
if symlinks and os.path.islink(s_path):
if os.path.lexists(d_path):
os.remove(d_path)
os.symlink(os.readlink(s_path), d_path)
try:
s_path_s = os.lstat(s_path)
s_path_mode = stat.S_IMODE(s_path_s.st_mode)
os.lchmod(d_path, s_path_mode)
except Exception:
pass
elif os.path.isdir(s_path):
overwriteCopy(s_path, d_path, symlinks, ignore)
else:
shutil.copy2(s_path, d_path)
示例4: sed_inplace
def sed_inplace(filename, pattern, repl):
"""Perform the pure-Python equivalent of in-place `sed` substitution: e.g.,
`sed -i -e 's/'${pattern}'/'${repl}' "${filename}"`.
Examples
--------
sed_inplace('/etc/apt/sources.list', r'^\# deb', 'deb')
"""
# For efficiency, precompile the passed regular expression.
pattern_compiled = re.compile(pattern)
# For portability, NamedTemporaryFile() defaults to mode "w+b" (i.e., binary
# writing with updating). This is usually a good thing. In this case,
# however, binary writing imposes non-trivial encoding constraints trivially
# resolved by switching to text writing. Let's do that.
with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_file:
with open(filename) as src_file:
for line in src_file:
tmp_file.write(pattern_compiled.sub(repl, line))
# Overwrite the original file with the munged temporary file in a
# manner preserving file attributes (e.g., permissions).
shutil.copystat(filename, tmp_file.name)
shutil.move(tmp_file.name, filename)
示例5: assemble
def assemble(self):
if _check_path_overlap(self.name) and os.path.isdir(self.name):
_rmtree(self.name)
logger.info("Building COLLECT %s", self.tocbasename)
os.makedirs(self.name)
toc = add_suffix_to_extensions(self.toc)
for inm, fnm, typ in toc:
if not os.path.exists(fnm) or not os.path.isfile(fnm) and is_path_to_egg(fnm):
# file is contained within python egg, it is added with the egg
continue
if os.pardir in os.path.normpath(inm) or os.path.isabs(inm):
raise SystemExit('Security-Alert: try to store file outside '
'of dist-directory. Aborting. %r' % inm)
tofnm = os.path.join(self.name, inm)
todir = os.path.dirname(tofnm)
if not os.path.exists(todir):
os.makedirs(todir)
if typ in ('EXTENSION', 'BINARY'):
fnm = checkCache(fnm, strip=self.strip_binaries,
upx=(self.upx_binaries and (is_win or is_cygwin)),
dist_nm=inm)
if typ != 'DEPENDENCY':
shutil.copy(fnm, tofnm)
try:
shutil.copystat(fnm, tofnm)
except OSError:
logger.warn("failed to copy flags of %s", fnm)
if typ in ('EXTENSION', 'BINARY'):
os.chmod(tofnm, 0o755)
示例6: _createType
def _createType(self, meta_name, root, movie_info, group, file_type, i): # Get file path
camelcase_method = underscoreToCamel(file_type.capitalize())
name = getattr(self, 'get' + camelcase_method + 'Name')(meta_name, root, i)
if name and (self.conf('meta_' + file_type) or self.conf('meta_' + file_type) is None):
# Get file content
content = getattr(self, 'get' + camelcase_method)(movie_info = movie_info, data = group, i = i)
if content:
log.debug('Creating %s file: %s', (file_type, name))
if os.path.isfile(content):
content = sp(content)
name = sp(name)
if not os.path.exists(os.path.dirname(name)):
os.makedirs(os.path.dirname(name))
shutil.copy2(content, name)
shutil.copyfile(content, name)
# Try and copy stats seperately
try: shutil.copystat(content, name)
except: pass
else:
self.createFile(name, content)
group['renamed_files'].append(name)
try:
os.chmod(sp(name), Env.getPermission('file'))
except:
log.debug('Failed setting permissions for %s: %s', (name, traceback.format_exc()))
示例7: copyDigestedFile
def copyDigestedFile(src, dst, copystat=1):
""" Copy data from `src` to `dst`, adding a fingerprint to `dst`.
If `copystat` is true, the file status is copied, too
(like shutil.copy2).
"""
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))
dummy, ext = os.path.splitext(src)
if ext not in digested_file_types:
if copystat:
return shutil.copy2(src, dst)
else:
return shutil.copyfile(src, dst)
fsrc = None
fdst = None
try:
fsrc = open(src, 'r')
fdst = DigestFile(dst)
shutil.copyfileobj(fsrc, fdst)
finally:
if fdst: fdst.close()
if fsrc: fsrc.close()
if copystat: shutil.copystat(src, dst)
示例8: copystatRecursive
def copystatRecursive(src, dst):
if S_ISDIR(os.stat(src).st_mode):
srcEntries = os.listdir(src)
dstEntries = os.listdir(dst)
for i in xrange(0, len(srcEntries)):
copystatRecursive(os.path.join(src, srcEntries[i]), os.path.join(dst, dstEntries[i]))
shutil.copystat(src, dst)
示例9: safe_copyfile
def safe_copyfile(src, dest):
"""safely copy src to dest using a temporary intermediate and then renaming
to dest"""
fd, tmpname = tempfile.mkstemp(dir=os.path.dirname(dest))
shutil.copyfileobj(open(src, 'rb'), os.fdopen(fd, 'wb'))
shutil.copystat(src, tmpname)
os.rename(tmpname, dest)
示例10: destaging_harness
def destaging_harness(backup, func):
path = backup[0:-len('.release')]
trace(path)
def relocate_for_release(token):
newtoken = token.replace(self.staged_prefix, self.prefix)
if newtoken != token:
trace('%s:\n\t%s\t->\t%s' %
(os.path.basename(path), token, newtoken))
return newtoken
try:
trace('Destaging %s' % path)
func(path, relocate_for_release)
if os.path.exists(path + '.stage'):
os.remove(path)
shutil.move(path + '.stage', path)
shutil.copystat(backup, path)
os.remove(backup)
except Exception as e:
warn ('Critical: Destaging failed for ''%s''' % path)
raise
示例11: copy2
def copy2(src, dst):
"""
shutil.copy2 does not copy the file attributes on windows, so we
hack into the shutil module to fix the problem
"""
old(src, dst)
shutil.copystat(src, dst)
示例12: staging_harness
def staging_harness(path, func, failure_count=failure_count):
def relocate_to_profile(token):
if token.find(package.staged_prefix) == -1 and token.find(package.staged_profile) == -1:
newtoken = token.replace(
package.package_prefix, package.staged_profile)
else:
newtoken = token.replace(
package.staged_prefix, package.staged_profile)
if newtoken != token:
package.trace('%s:\n\t%s\t->\t%s' %
(os.path.basename(path), token, newtoken))
return newtoken
if (path.endswith('.release')):
error('Staging backup exists in dir we''re trying to stage: %s' % path)
backup = path + '.release'
shutil.copy2(path, backup)
try:
trace('Staging %s' % path)
func(path, relocate_to_profile)
if os.path.exists(path + '.stage'):
os.remove(path)
shutil.move(path + '.stage', path)
shutil.copystat(backup, path)
except CommandException as e:
package.rm_if_exists(path)
shutil.copy2(backup, path)
package.rm(backup)
warn('Staging failed for %s' % os.path.basename(path))
error(str(e))
failure_count = failure_count + 1
if failure_count > 10:
error('Possible staging issue, >10 staging failures')
示例13: make_thumbnails
def make_thumbnails(files, thumbfunc):
"""
For any file without a matching image file, invokes function to generate
a thumbnail image.
"""
dirs_changed = {} # {path name: os.stat_result}
imageexts = [x.replace("*", "") for x in TYPEGROUPS["image"]]
for video in files:
path, tail = os.path.split(video)
base = os.path.splitext(tail)[0]
if any(os.path.isfile(os.path.join(path, base + x)) for x in imageexts):
continue # for video
pathstat = dirs_changed.get(path) or os.stat(path)
image = os.path.join(path, base + ".jpg")
tempimage = os.path.join(TEMP_DIR, uuid.uuid4().hex[:8] + ".jpg")
if os.path.exists(tempimage): os.remove(tempimage)
print("Creating thumb for video \"%s\"." % video)
attempts = 3
while attempts:
try:
thumbfunc(video, tempimage), shutil.move(tempimage, image)
break # while attempts
except Exception:
attempts -= 1
if os.path.exists(image):
shutil.copystat(video, image) # Set video file timestamps to image
dirs_changed[path] = pathstat
else:
print("Failed to produce \"%s\"." % image)
for path, stat in dirs_changed.items(): # Restore directory timestamps
os.utime(path, (stat.st_atime, stat.st_mtime))
示例14: copytree
def copytree(src, dst, symlinks=False):
names = os.listdir(src)
os.makedirs(dst)
errors = []
for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and islink(srcname):
linkto = readlink(srcname)
symlink(linkto, dstname)
elif os.path.isdir(srcname):
copytree(srcname, dstname, symlinks)
else:
copy2(srcname, dstname)
except OSError as why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except Error as err:
errors.extend(err.args[0])
try:
copystat(src, dst)
except OSError as why:
# can't copy file access times on Windows
if why.winerror is None:
errors.extend((src, dst, str(why)))
if errors:
raise Error(errors)
示例15: do_copyfile
def do_copyfile(self, from_file, to_file):
outdir = os.path.split(to_file)[0]
if not os.path.isfile(from_file) and not os.path.islink(from_file):
raise RuntimeError('Tried to install something that isn\'t a file:'
'{!r}'.format(from_file))
# copyfile fails if the target file already exists, so remove it to
# allow overwriting a previous install. If the target is not a file, we
# want to give a readable error.
if os.path.exists(to_file):
if not os.path.isfile(to_file):
raise RuntimeError('Destination {!r} already exists and is not '
'a file'.format(to_file))
if self.should_preserve_existing_file(from_file, to_file):
append_to_log(self.lf, '# Preserving old file %s\n' % to_file)
print('Preserving existing file %s.' % to_file)
return False
os.remove(to_file)
print('Installing %s to %s' % (from_file, outdir))
if os.path.islink(from_file):
if not os.path.exists(from_file):
# Dangling symlink. Replicate as is.
shutil.copy(from_file, outdir, follow_symlinks=False)
else:
# Remove this entire branch when changing the behaviour to duplicate
# symlinks rather than copying what they point to.
print(symlink_warning)
shutil.copyfile(from_file, to_file)
shutil.copystat(from_file, to_file)
else:
shutil.copyfile(from_file, to_file)
shutil.copystat(from_file, to_file)
selinux_updates.append(to_file)
append_to_log(self.lf, to_file)
return True