本文整理汇总了Python中shutil.copy2函数的典型用法代码示例。如果您正苦于以下问题:Python copy2函数的具体用法?Python copy2怎么用?Python copy2使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了copy2函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main():
# Parameters.
bookmarkdir = os.environ["HOME"] + "/Dropbox/Apps/pinboard/"
pinboard_credentials = bookmarkdir + "pinboard_credentials.txt"
current = bookmarkdir + "most_current_bookmarks.xml"
pinboard_api = "https://api.pinboard.in/v1/"
yearfmt = "%Y"
datefmt = "%m-%d"
homeTZ = pytz.timezone("GMT")
y = datetime.now(pytz.utc).strftime(yearfmt)
t = datetime.now(pytz.utc).strftime(datefmt)
daily_file = bookmarkdir + y + "/pinboard-backup." + t + ".xml"
# Get the user's authentication token
with open(pinboard_credentials) as credentials:
for line in credentials:
me, token = line.split(":")
if not os.path.exists(bookmarkdir + y):
os.makedirs(bookmarkdir + y)
# Set up a new bookmarks file
bookmarkfile = open(daily_file, "w")
# Get all the posts from Pinboard
u = urllib2.urlopen(pinboard_api + "posts/all?auth_token=" + me + ":" + token)
bookmarkfile.write(u.read())
bookmarkfile.close()
shutil.copy2(daily_file, current)
示例2: main
def main():
os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.system("pip install certifi")
print "Copying certifi's cacert.pem"
import certifi
shutil.copy2(certifi.where(), 'agkyra/resources/cacert.pem')
示例3: buildApp
def buildApp():
# ----------------------
# Under Windows, we're just going to grab the xulrunner-stub.exe and run Resource Hacker
# to rename it to "Kylo", set the icon, and set some version info, etc.
reshack_temp_dir = os.path.join(Settings.prefs.build_dir, "stub")
if not os.path.exists(reshack_temp_dir):
os.makedirs(reshack_temp_dir)
build_stub.main(Settings.config.get('App','Version'),
Settings.config.get('App','BuildID'),
temp_dir = reshack_temp_dir,
stub_dir = os.path.join(Settings.prefs.build_dir, "application"))
# ----------------------
# We also need mozilla DLLS
for lib in ["mozcrt19.dll", "mozutils.dll", "gkmedias.dll"]:
f = os.path.join(Settings.prefs.xul_dir, lib)
if (os.path.isfile(f)):
shutil.copy2(f, os.path.join(Settings.prefs.build_dir, "application"))
# ----------------------
# Now let's grab the XULRunner directory and drop it in to our working application directory
xulrunner_dir = os.path.join(Settings.prefs.build_dir, "application", "xulrunner")
if not os.path.exists(xulrunner_dir):
os.makedirs(xulrunner_dir)
build_util.syncDirs(Settings.prefs.xul_dir, xulrunner_dir, exclude=["xulrunner-stub.exe"])
示例4: copyfile
def copyfile(src, dst, logger=None, force=True, vars=None, subst_content=False):
if vars is not None:
src = subst_vars(src, **vars)
dst = subst_vars(dst, **vars)
if not os.path.exists(src) and not force:
if logger is not None:
logger.info("**Skiping copy file %s to %s. Source does not exists." % (src, dst))
return
if logger is not None:
logger.info("Copying file %s to %s." % (src, dst))
if vars is None or not subst_content:
shutil.copy2(src, dst)
return
print ("copyfile " + src)
f = open(src, "rt")
content = f.read()
f.close()
content = subst_vars(content, **vars)
f = open(dst, "wt")
f.write(content)
f.close()
示例5: put_files_cache
def put_files_cache(self):
if getattr(self, "cached", None):
return None
sig = self.signature()
ssig = Utils.to_hex(self.uid()) + Utils.to_hex(sig)
dname = os.path.join(self.generator.bld.cache_global, ssig)
tmpdir = tempfile.mkdtemp(prefix=self.generator.bld.cache_global + os.sep + "waf")
try:
shutil.rmtree(dname)
except:
pass
try:
for node in self.outputs:
dest = os.path.join(tmpdir, node.name)
shutil.copy2(node.abspath(), dest)
except (OSError, IOError):
try:
shutil.rmtree(tmpdir)
except:
pass
else:
try:
os.rename(tmpdir, dname)
except OSError:
try:
shutil.rmtree(tmpdir)
except:
pass
else:
try:
os.chmod(dname, Utils.O755)
except:
pass
示例6: addFile
def addFile(self, opath):
"""Copy PATH to MEDIADIR, and return new filename.
If the same name exists, compare checksums."""
mdir = self.dir()
# remove any dangerous characters
base = re.sub(r"[][<>:/\\&]", "", os.path.basename(opath))
dst = os.path.join(mdir, base)
# if it doesn't exist, copy it directly
if not os.path.exists(dst):
shutil.copy2(opath, dst)
return base
# if it's identical, reuse
if self.filesIdentical(opath, dst):
return base
# otherwise, find a unique name
(root, ext) = os.path.splitext(base)
def repl(match):
n = int(match.group(1))
return " (%d)" % (n+1)
while True:
path = os.path.join(mdir, root + ext)
if not os.path.exists(path):
break
reg = " \((\d+)\)$"
if not re.search(reg, root):
root = root + " (1)"
else:
root = re.sub(reg, repl, root)
# copy and return
shutil.copy2(opath, path)
return os.path.basename(os.path.basename(path))
示例7: copy_python
def copy_python(src, dst, symlinks=False):
" Copies just Python source files "
import shutil
names = os.listdir(src)
try:
os.makedirs(dst)
except:
pass
errors = []
for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
if not name.startswith("."):
copy_python(srcname, dstname, symlinks)
else:
if name.endswith(".py"):
print "create", dstname
shutil.copy2(srcname, dstname)
# XXX What about devices, sockets etc.?
except (IOError, os.error), why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except Error, err:
errors.extend(err.args[0])
示例8: pull
def pull(self):
if common.isurl(self.source):
self.download()
else:
shutil.copy2(self.source, self.source_dir)
self.provision(self.source_dir)
示例9: walk
def walk(src, dest):
print '****************************************************************'
print dest
print '****************************************************************'
dirCmp = filecmp.dircmp(src, dest, ignore=['Thumbs.db'])
for destFile in dirCmp.right_only:
destFilePath = dest+'/'+destFile
if os.path.isfile(destFilePath):
print u'删除文件\n',destFilePath
os.remove(destFilePath)
else:
print u'删除文件夹\n',destFilePath
# os.rmdir(destFilePath)
shutil.rmtree(destFilePath)
for srcFile in dirCmp.left_only:
srcFilePath = src+'/'+srcFile
destFilePath = dest+'/'+srcFile
if os.path.isfile(srcFilePath):
print u'复制文件\n',destFilePath
shutil.copy2(srcFilePath, dest)
else:
print u'复制文件夹\n',destFilePath
shutil.copytree(srcFilePath, destFilePath)
for srcFile in dirCmp.diff_files:
srcFilePath = src+'/'+srcFile
destFilePath = dest+'/'+srcFile
print u'同步文件\n',destFilePath
shutil.copy2(srcFilePath, dest)
subDirs = set(os.listdir(src))-set(dirCmp.left_only)
targetDirs = [subDir for subDir in subDirs if os.path.isdir(src+'/'+subDir)]
for targetDir in targetDirs:
walk(src+'/'+targetDir, dest+'/'+targetDir)
示例10: install
def install(host, src, dstdir):
if isLocal(host):
if not exists(host, src):
util.output("file does not exist: %s" % src)
return False
dst = os.path.join(dstdir, os.path.basename(src))
if exists(host, dst):
# Do not clobber existing files/dirs (this is not an error)
return True
util.debug(1, "cp %s %s" % (src, dstdir))
try:
if os.path.isfile(src):
shutil.copy2(src, dstdir)
elif os.path.isdir(src):
shutil.copytree(src, dst)
except OSError:
# Python 2.6 has a bug where this may fail on NFS. So we just
# ignore errors.
pass
else:
util.error("install() not yet supported for remote hosts")
return True
示例11: browse_curve_file
def browse_curve_file(self, preferredExt="crv"):
"""
Gets curve file from a file browser. If the returned string from the
browser is a valid file, the curve input box is set to that new item
and "Default Curve" and "Estimate Curve Only" are disabled
Args:
preferredExt (str): The extension that the browser will look for
whenever listing directories for a curve file
"""
# make sure to filter out all unwanted files and leave a * (All Files)
#
filepaths = QtGui.QFileDialog.getOpenFileName(self, "Browse Curve Profile")
filepaths = [x for x in filepaths if x.strip() != ""]
for f in filepaths:
if os.path.isfile(f) and f.lower().endswith(preferredExt):
fname = paths.path_leaf(f)
shutil.copy2(f, os.path.join(self.curveInputDir, fname))
self.curveInput_cb.addItem(fname)
index = self.curveInput_cb.findText(fname,
QtCore.Qt.MatchFixedString)
if index >= 0:
self.curveInput_cb.setCurrentIndex(index)
self.defaultCurve_cb.setEnabled(False)
self.estimateCurve_cb.setEnabled(False)
示例12: configure_step
def configure_step(self):
"""Configure SCOTCH build: locate the template makefile, copy it to a general Makefile.inc and patch it."""
# pick template makefile
comp_fam = self.toolchain.comp_family()
if comp_fam == toolchain.INTELCOMP: #@UndefinedVariable
makefilename = 'Makefile.inc.x86-64_pc_linux2.icc'
elif comp_fam == toolchain.GCC: #@UndefinedVariable
makefilename = 'Makefile.inc.x86-64_pc_linux2'
else:
self.log.error("Unknown compiler family used: %s" % comp_fam)
# create Makefile.inc
try:
srcdir = os.path.join(self.cfg['start_dir'], 'src')
src = os.path.join(srcdir, 'Make.inc', makefilename)
dst = os.path.join(srcdir, 'Makefile.inc')
shutil.copy2(src, dst)
self.log.debug("Successfully copied Makefile.inc to src dir.")
except OSError:
self.log.error("Copying Makefile.inc to src dir failed.")
# the default behaviour of these makefiles is still wrong
# e.g., compiler settings, and we need -lpthread
try:
for line in fileinput.input(dst, inplace=1, backup='.orig.easybuild'):
# use $CC and the likes since we're at it.
line = re.sub(r"^CCS\s*=.*$", "CCS\t= $(CC)", line)
line = re.sub(r"^CCP\s*=.*$", "CCP\t= $(MPICC)", line)
line = re.sub(r"^CCD\s*=.*$", "CCD\t= $(MPICC)", line)
# append -lpthread to LDFLAGS
line = re.sub(r"^LDFLAGS\s*=(?P<ldflags>.*$)", "LDFLAGS\t=\g<ldflags> -lpthread", line)
sys.stdout.write(line)
except IOError, err:
self.log.error("Can't modify/write Makefile in 'Makefile.inc': %s" % (err))
示例13: savedir
def savedir(self, savedir):
if savedir is None:
self._savedir = None
elif hasattr(self, '_savedir') and savedir == self.savedir:
return
else:
if hasattr(self, '_savedir'):
orig_dir = self.savedir
else:
orig_dir = False
savedir = abspath(savedir)
if not savedir.endswith('.sima'):
savedir += '.sima'
os.makedirs(savedir)
self._savedir = savedir
if orig_dir:
from shutil import copy2
for f in os.listdir(orig_dir):
if f.endswith('.pkl'):
try:
copy2(os.path.join(orig_dir, f), self.savedir)
except IOError:
pass
if self._read_only:
self._read_only = False
示例14: _stage_files
def _stage_files(self, bin_name=None):
dirs = self.query_abs_dirs()
abs_app_dir = self.query_abs_app_dir()
# For mac these directories are in Contents/Resources, on other
# platforms abs_res_dir will point to abs_app_dir.
abs_res_dir = self.query_abs_res_dir()
abs_res_components_dir = os.path.join(abs_res_dir, 'components')
abs_res_plugins_dir = os.path.join(abs_res_dir, 'plugins')
abs_res_extensions_dir = os.path.join(abs_res_dir, 'extensions')
if bin_name:
self.info('copying %s to %s' % (os.path.join(dirs['abs_test_bin_dir'],
bin_name), os.path.join(abs_app_dir, bin_name)))
shutil.copy2(os.path.join(dirs['abs_test_bin_dir'], bin_name),
os.path.join(abs_app_dir, bin_name))
self.copytree(dirs['abs_test_bin_components_dir'],
abs_res_components_dir,
overwrite='overwrite_if_exists')
self.mkdir_p(abs_res_plugins_dir)
self.copytree(dirs['abs_test_bin_plugins_dir'],
abs_res_plugins_dir,
overwrite='overwrite_if_exists')
if os.path.isdir(dirs['abs_test_extensions_dir']):
self.mkdir_p(abs_res_extensions_dir)
self.copytree(dirs['abs_test_extensions_dir'],
abs_res_extensions_dir,
overwrite='overwrite_if_exists')
示例15: _persist_file
def _persist_file(self, abspath):
"""Persist file and bind mount it back to its current location
"""
persisted_path = self._config_path(abspath)
if os.path.exists(persisted_path):
current_checksum = self.checksum(abspath)
stored_checksum = self.checksum(persisted_path)
if stored_checksum == current_checksum:
self._logger.warn('File "%s" had already been persisted',
abspath)
return
else:
# If this happens, somehow the bind mount was undone, so we try
# and clean up before re-persisting
try:
if mount.ismount(abspath):
mount.umount(abspath)
os.unlink(persisted_path)
except OSError as ose:
self._logger.error('Failed to clean up persisted file '
'"%s": %s', abspath, ose.message)
self._prepare_dir(abspath, persisted_path)
shutil.copy2(abspath, persisted_path)
self.copy_attributes(abspath, persisted_path)
mount.mount(persisted_path, abspath, flags=mount.MS_BIND)
self._logger.info('File "%s" successfully persisted', abspath)
self._add_path_entry(abspath)