参考了网上的例子,例如python脚本实现文件夹增量复制、python实现文件夹增量同步,自己写了一个文件复制的程序
'''
同步文件
'''
import os
import sys
import fnmatch
import time
import shutil
from file_sync import sync_list, svn_update
from file_sync.svn_update import svn_sync
from file_sync.sync_list import get_src_dir, get_target_dir, get_svns, get_sync_list
IGNORE_LIST = ['*.svn*','*target/*','*node_modules*','*.idea*']
def genDir(path):
'''
根据目录结构创建文件夹
:param path:
:return:
'''
if not os.path.exists(path):
os.makedirs(path)
def sys_node_project(src_dir,project_dir ,target_dir,start_time,prefix):
src = os.path.join(src_dir, project_dir,prefix)
if os.path.exists(src):
os.chdir(src)
for dir in os.listdir(src):
print('当前扫描的目录或文件为:%s' % (dir))
if any(fnmatch.fnmatch(dir, pattern) for pattern in IGNORE_LIST):
# 针对node项目,直接遍历第一层找到node_modules,就不再往下找了
continue
else:
if os.path.isfile(dir):
# 如果是文件,那么直接将文件复制过去
target_file = os.path.join(target_dir,src[len(src_dir):],dir)
src_file = os.path.join(src,dir)
cp_file(start_time,src_file, target_file)
pass
else:
# 目录
for root, dirs, files in os.walk(dir):
for file in files:
src_file = os.path.join(src,root, file)
if any(fnmatch.fnmatch(src_file, pattern) for pattern in IGNORE_LIST):
continue
target_file = os.path.join(target_dir, src_file[len(src_dir):])
cp_file(start_time,src_file,target_file)
pass
def syc_java_project(src_dir,project_dir ,target_dir,start_time):
'''
同步单个工程
:param src_dir:
:param project_dir:
:param target:
:return:
'''
src = os.path.join(src_dir, project_dir)
if os.path.exists(src):
for dir in os.listdir(src):
('当前扫描的目录或文件为:%s'%(dir))
if any(fnmatch.fnmatch(dir, pattern) for pattern in IGNORE_LIST):
continue
else:
if os.path.isfile(dir):
# 如果是文件,那么直接将文件复制过去
target_file = os.path.join(target_dir,src[len(src_dir):],dir)
src_file = os.path.join(src,dir)
cp_file(start_time,src_file, target_file)
pass
else:
# 目录
for root, dirs, files in os.walk(dir):
for file in files:
src_file = os.path.join(src,root, file)
if any(fnmatch.fnmatch(src_file, pattern) for pattern in IGNORE_LIST):
continue
target_file = os.path.join(target_dir, src_file[len(src_dir):])
cp_file(start_time,src_file,target_file)
def cp_file(start_time,src_file,target_file):
'''
增量复制文件
:param start_time:
:param src_file:
:param target_file:
:return:
'''
# 获取文件的修改时间
mtime = time.ctime(os.path.getmtime(src_file))
mtime = time.mktime(time.strptime(mtime, "%a %b %d %H:%M:%S %Y"))
if mtime >= start_time:
genDir(os.path.dirname(target_file))
print(target_file)
shutil.copyfile(src_file, target_file)
def start_cp(src_dir ,target_dir,):
sync_list = get_sync_list()
for l in sync_list:
project = l['project']
svn = l['svn']
first = l['first']
# 注意每次同步完毕后,需要更新一下时间,作为下次更新的起点
start_time = time.mktime(time.strptime(l['start'], '%Y-%m-%d %H:%M:%S'))
# svn下载
print('正在下载 %s' % (str(svn)))
svn_sync(src_dir, project,svn,first)
print('svn下载完成')
# 复制工程到备份目录
print('正在同步 %s' % (str(project)))
if 'node' in l:
sys_node_project(src_dir, project, target_dir, start_time, l['prefix'])
else:
syc_java_project(src_dir, project, target_dir, start_time)
print('同步项目代码%s完成' % (str(project)))
print('同步完成')
if __name__ == '__main__':
start_cp(get_src_dir(),get_target_dir())
'''
同步svn文件
'''
import os
import subprocess
from file_sync.sync_list import svn_auth
def svn_sync(src_dir, project, svn_dir, first):
project_dir = os.path.join(src_dir, project)
os.chdir(project_dir)
if first:
cmd = 'svn checkout --force %s ' % svn_dir + svn_auth()
else:
cmd = 'svn export --force %s '% svn_dir + svn_auth()
result = execute(cmd)
print(result)
def execute(commandLine):
'''
执行命令行
:param commandLine:
:return:
'''
output = []
p = subprocess.Popen(commandLine, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
output.append(line)
retval = p.wait()
return output