def Rsync_file(passfile, rsync_user, rsync_dir, cdn_server, local_file_path, rsync_sub_dir, exclue_file=None, rsync_opts='-Ratpv', sshconnect=None, verbose=False, count=False): #local_dir 进入目录 global stdout, stderr stdout = "" stderr = "" local_dir = os.path.split(local_file_path)[0] #传送的文件 send_file = os.path.split(local_file_path)[1] exclude_opt = "" ########################错误收集######################## nofile = re.compile("failed: No such file or directory") permission = re.compile("by root when running as root") permission2 = re.compile( "ERROR: password file must not be other-accessible") ########################错误收集######################## if exclue_file != None: if type(exclue_file) is list: for f in exclue_file: exclude_opt += "--exclude %s " % f else: warn("排除文件必须是一个list") exit(1) #远程调用 try: if sshconnect: cmd = '''"cd %s && rsync --password-file=%s %s %s %s %s@%s::%s/%s"''' % ( local_dir, passfile, rsync_opts, send_file, exclude_opt, rsync_user, cdn_server, rsync_dir, rsync_sub_dir) print '\033[1;33mcmd: %s\033[0m' % cmd confirm() print "\033[1;32m正在远程执行操作....\033[0m" print "正在同步中.请等待....." stdout, stderr, returncode = sshconnect.run(cmd, drop_socket=False) else: if not os.listdir(local_dir): warn("%s 目录为空.." % local_dir) confirm(show="确认无误请按yes,取消输入no\t") returncode = 0 verbose = False else: cmd = "cd %s && rsync --password-file=%s %s %s %s %s@%s::%s/%s" % ( local_dir, passfile, rsync_opts, send_file, exclude_opt, rsync_user, cdn_server, rsync_dir, rsync_sub_dir) print '\033[1;33mcmd: %s\033[0m' % cmd confirm() print "正在同步中.请等待....." run = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) if count: while run.poll() == None: line = run.stdout.readline() print line.strip("\n") else: while run.poll() == None: Progress() stdout, stderr = run.communicate() returncode = run.returncode if returncode != 0: message = stdout.strip("\n") if nofile.findall(message) or message == "": warn("没有文件可传!!! 3s后继续...") time.sleep(3) elif permission.findall(message) or permission2.findall(message): warn("使用的用户不对或密码文件权限不对.请检查...") exit(1) else: #抛出异常 raise OtherException(message) else: if verbose: if count == False: print( "\033[1;32m#########################信息如下#########################\033[0m" ) print stdout.strip("\n") print stderr.strip("\n") print( "\033[1;32m##########################################################\033[0m" ) success("rsync 传送成功...") except OtherException, err: error(err)
if returncode != 0: message = stdout.strip("\n") if nofile.findall(message) or message == "": warn("没有文件可传!!! 3s后继续...") time.sleep(3) elif permission.findall(message) or permission2.findall(message): warn("使用的用户不对或密码文件权限不对.请检查...") exit(1) else: #抛出异常 raise OtherException(message) else: if verbose: if count == False: print( "\033[1;32m#########################信息如下#########################\033[0m" ) print stdout.strip("\n") print stderr.strip("\n") print( "\033[1;32m##########################################################\033[0m" ) success("rsync 传送成功...") except OtherException, err: error(err) except Exception, err: message = '\033[1;31m"fail",rsync 失败,信息:%s,%s,%s\033[0m' % ( err, stdout, stderr) error(message)
def Rsync_file(passfile,rsync_user,rsync_dir,cdn_server,local_file_path,rsync_sub_dir,exclue_file=None,rsync_opts='-Ratpv',sshconnect=None,verbose=False,count=False): #local_dir 进入目录 global stdout,stderr stdout = "" stderr = "" local_dir=os.path.split(local_file_path)[0] #传送的文件 send_file=os.path.split(local_file_path)[1] exclude_opt = "" ########################错误收集######################## nofile=re.compile("failed: No such file or directory") permission = re.compile("by root when running as root") permission2 = re.compile("ERROR: password file must not be other-accessible") ########################错误收集######################## if exclue_file != None: if type(exclue_file) is list: for f in exclue_file: exclude_opt += "--exclude %s " % f else: warn("排除文件必须是一个list") exit(1) #远程调用 try: if sshconnect: cmd = '''"cd %s && rsync --password-file=%s %s %s %s %s@%s::%s/%s"'''%(local_dir,passfile,rsync_opts,send_file,exclude_opt,rsync_user,cdn_server,rsync_dir,rsync_sub_dir) print '\033[1;33mcmd: %s\033[0m'%cmd confirm() print "\033[1;32m正在远程执行操作....\033[0m" print "正在同步中.请等待....." stdout,stderr,returncode = sshconnect.run(cmd,drop_socket=False) else: if not os.listdir(local_dir): warn("%s 目录为空.."%local_dir) confirm(show="确认无误请按yes,取消输入no\t") returncode = 0 verbose=False else: cmd = "cd %s && rsync --password-file=%s %s %s %s %s@%s::%s/%s"%(local_dir,passfile,rsync_opts,send_file,exclude_opt,rsync_user,cdn_server,rsync_dir,rsync_sub_dir) print '\033[1;33mcmd: %s\033[0m'%cmd confirm() print "正在同步中.请等待....." run = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True) if count: while run.poll() == None: line = run.stdout.readline() print line.strip("\n") else: while run.poll() == None: Progress() stdout,stderr = run.communicate() returncode = run.returncode if returncode != 0: message=stdout.strip("\n") if nofile.findall(message) or message == "": warn("没有文件可传!!! 3s后继续...") time.sleep(3) elif permission.findall(message) or permission2.findall(message): warn("使用的用户不对或密码文件权限不对.请检查...") exit(1) else: #抛出异常 raise OtherException(message) else: if verbose: if count == False: print("\033[1;32m#########################信息如下#########################\033[0m") print stdout.strip("\n") print stderr.strip("\n") print("\033[1;32m##########################################################\033[0m") success("rsync 传送成功...") except OtherException,err: error(err)
def ensure(path): if not os.path.isfile(path) and not os.path.isdir(path): error("%s,不存在!!请检查.."%path) exit(1)
else: while run.poll() == None: Progress() stdout,stderr = run.communicate() returncode = run.returncode if returncode != 0: message=stdout.strip("\n") if nofile.findall(message) or message == "": warn("没有文件可传!!! 3s后继续...") time.sleep(3) elif permission.findall(message) or permission2.findall(message): warn("使用的用户不对或密码文件权限不对.请检查...") exit(1) else: #抛出异常 raise OtherException(message) else: if verbose: if count == False: print("\033[1;32m#########################信息如下#########################\033[0m") print stdout.strip("\n") print stderr.strip("\n") print("\033[1;32m##########################################################\033[0m") success("rsync 传送成功...") except OtherException,err: error(err) except Exception,err: message = '\033[1;31m"fail",rsync 失败,信息:%s,%s,%s\033[0m'%(err,stdout,stderr) error(message)