我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用shutil.copystat()。
def unpack_directory(filename, extract_dir, progress_filter=default_filter): """"Unpack" a directory, using the same interface as for archives Raises ``UnrecognizedFormat`` if `filename` is not a directory """ if not os.path.isdir(filename): raise UnrecognizedFormat("%s is not a directory" % filename) paths = { filename: ('', extract_dir), } for base, dirs, files in os.walk(filename): src, dst = paths[base] for d in dirs: paths[os.path.join(base, d)] = src + d + '/', os.path.join(dst, d) for f in files: target = os.path.join(dst, f) target = progress_filter(src + f, target) if not target: # skip non-files continue ensure_directory(target) f = os.path.join(base, f) shutil.copyfile(f, target) shutil.copystat(f, target)
def copytree(src, dst, symlinks = False, ignore = None): 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)
def unpack_directory(filename, extract_dir, progress_filter=default_filter): """"Unpack" a directory, using the same interface as for archives Raises ``UnrecognizedFormat`` if `filename` is not a directory """ if not os.path.isdir(filename): raise UnrecognizedFormat("%s is not a directory" % (filename,)) paths = {filename:('',extract_dir)} for base, dirs, files in os.walk(filename): src,dst = paths[base] for d in dirs: paths[os.path.join(base,d)] = src+d+'/', os.path.join(dst,d) for f in files: name = src+f target = os.path.join(dst,f) target = progress_filter(src+f, target) if not target: continue # skip non-files ensure_directory(target) f = os.path.join(base,f) shutil.copyfile(f, target) shutil.copystat(f, target)
def _ExtractClassFiles(jar_path, dest_dir, java_files): """Extracts all .class files not corresponding to |java_files|.""" # Two challenges exist here: # 1. |java_files| have prefixes that are not represented in the the jar paths. # 2. A single .java file results in multiple .class files when it contains # nested classes. # Here's an example: # source path: ../../base/android/java/src/org/chromium/Foo.java # jar paths: org/chromium/Foo.class, org/chromium/Foo$Inner.class # To extract only .class files not related to the given .java files, we strip # off ".class" and "$*.class" and use a substring match against java_files. def extract_predicate(path): if not path.endswith('.class'): return False path_without_suffix = re.sub(r'(?:\$|\.)[^/]*class$', '', path) partial_java_path = path_without_suffix + '.java' return not any(p.endswith(partial_java_path) for p in java_files) build_utils.ExtractAll(jar_path, path=dest_dir, predicate=extract_predicate) for path in build_utils.FindInDirectory(dest_dir, '*.class'): shutil.copystat(jar_path, path)
def copystat(src, dst): """ Copy the permission bits, last access time, last modification time, and flags from src to dst. The file contents, owner, and group are unaffected. src and dst are path names given as strings. :Arguments: src - permission bits of the file to be copied dst - file on which permission bits has to be copied :Return: True/False - based on the success/failure of the operation """ status = False try: shutil.copystat(src, dst) print_info("metadata of src {} copied to dst {} successfully". format(src, dst)) status = True except Exception as e: print_error("copying file stat from {} to file {} raised exception {}". format(src, dst, str(e))) return status
def copy2(src, dst): """ Similar to shutil.copy(), but metadata (permissions etc., as mentioned in copy_stat above) is copied as well in fact, this is just shutil.copy() followed by copystat(). This is similar to the Unix command cp -p. :Arguments: src - file and metadata to be copied dst - file/dir on which to be copied :Return: True/False - based on the success/failure of the operation """ status = False try: shutil.copy2(src, dst) print_info("src {} copied to dst {} successfully along with metadata". format(src, dst)) status = True except Exception as e: print_error("copying file {} with metadata to file {} raised exception" " {}".format(src, dst, str(e))) return status
def copystats(from_file, to_file): """ Copy stat info from ``from_file`` to ``to_file`` using `shutil.copystat`. If possible, also copy the user and/or group ownership information. """ shutil.copystat(from_file, to_file) if hasattr(os, 'chown'): st = os.stat(from_file) # Based on GNU sed's behavior: try: os.chown(to_file, st.st_uid, st.st_gid) except EnvironmentError: try: os.chown(to_file, -1, st.st_gid) except EnvironmentError: pass
def writeData(database, filename, user, option_box=None): # Rename file, if it exists already, with <filename>.bak # as it it for normal XML export. if os.path.isfile(filename): try: shutil.copyfile(filename, filename + ".bak") shutil.copystat(filename, filename + ".bak") except: pass if option_box: option_box.parse_options() database = option_box.get_filtered_database(database) writer = PackageWriter(database, filename, user) return writer.export() #------------------------------------------------------------------------- # # PackageWriter # #-------------------------------------------------------------------------
def rmsection(filename, pattern): pattern_compiled = re.compile(pattern) with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_file: with open(filename) as src_file: for line in src_file: (sline, nsub) = pattern_compiled.subn('', line) tmp_file.write(sline) if nsub > 0: next(src_file) shutil.copystat(filename, tmp_file.name) shutil.move(tmp_file.name, filename) # See https://github.com/rtfd/readthedocs.org/issues/1139