示例#1
0
def update_request():
    """
    处理前端发来的请求更新请求(异步处理)
    :return: 更新信息(开始更新,更新状态,更新冷却,错误情况)等
    """
    return update.update_sync()
示例#2
0
"""
A program to synchronise the files between two directories given as arguments

ASSUMPTION: If a file has been deleted in one directory but exists in the other,
 and the modified times are the same then the file is deleted in both directories 

Author: Hayden Knowles
"""

dir1 = sys.argv[1] 
dir2 = sys.argv[2]

if dir1[-1] != '/':
	dir1 += '/'
if dir2[-1] != '/':
	dir2 += '/'
if not os.path.exists(dir1) and not os.path.exists(dir2):
	print("Error: arguments are not directories")
elif os.path.exists(dir1) or os.path.exists(dir2):
	# make directory and update sync files
	if not os.path.isdir(dir1):
		os.makedirs(dir1)
	elif not os.path.isdir(dir2):
		os.makedirs(dir2)

	# update directory sync files
	update_sync(dir1)
	update_sync(dir2)

	# compare and update directories and sync files
	compare(dir1, dir2)
示例#3
0
def compare(dir1, dir2):
	with open(dir1 + '.sync') as data:
		dict1 = json.load(data)
	with open(dir2 + '.sync') as data:
		dict2 = json.load(data)
	files1 = os.listdir(dir1)
	files2 = os.listdir(dir2)

	for item in files1:
		if item in files2 and item != '.sync':
			if os.path.isdir(dir1 + item):
				compare(dir1 + item + '/', dir2 + item + '/')
			else:		# a file
				if get_hash(dir1 + item) == get_hash(dir2 + item):		# file is the same
					if dict1[item][0][0] != dict2[item][0][0]:			# compare modified time
						date1 = datetime.fromtimestamp(time.mktime(time.strptime(dict1[item][0][0], '%Y-%m-%d %H:%M:%S %z')))
						date2 = datetime.fromtimestamp(time.mktime(time.strptime(dict2[item][0][0], '%Y-%m-%d %H:%M:%S %z')))
						if date1 > date2:
							os.system("touch -r {} {}".format(dir1 + item, dir2 + item))
						else:
							os.system("touch -r {} {}".format(dir2 + item, dir1 + item))
						recent_date = max(date1, date2)
						dict1[item][0][1] = recent_date.strftime('%Y-%m-%d %H:%M:%S %z')
						dict2[item][0][1] = recent_date.strftime('%Y-%m-%d %H:%M:%S %z')
				else:			# file is different
					digest1 = get_hash(dir1 + item)
					digest2 = get_hash(dir2 + item)
					for entry in dict2[item]:
						if digest1 in entry:				# file has been superseded
							shutil.copy(dir2 + item, dir1)	# copy more recent file to destination
							dict1[item].insert(0, dict2[item][0])		# copy changes to dated sync file		
					for entry in dict1[item]:
						if digest2 in entry:
							shutil.copy(dir1 + item, dir2)
							dict2[item].insert(0, dict1[item][0])

					# files are unique

					date1 = datetime.fromtimestamp(time.mktime(time.strptime(dict1[item][0][0], '%Y-%m-%d %H:%M:%S %z')))
					date2 = datetime.fromtimestamp(time.mktime(time.strptime(dict2[item][0][0], '%Y-%m-%d %H:%M:%S %z')))
					if date1 > date2:
						shutil.copy(dir1 + item, dir2)
						#print("copying {} to {}".format(dir1+item,dir2))
						dict2[item].insert(0, dict1[item][0])
					else:
						shutil.copy(dir2 + item, dir1)
						#print("copying this {} to {}".format(dir2+item,dir1))
						dict1[item].insert(0, dict2[item][0])
		else:		# file does not exist in other directory
			if item not in dict2 and item != '.sync':		# file has not existed in other directory
				if os.path.isfile(dir1 + item):
					shutil.copy(dir1 + item, dir2)	# copy file
					dict2[item] = []
					dict2[item].append(dict1[item][0])
				else:
					shutil.copytree(dir1 + item, dir2 + item)		# copy directory
					update_sync(dir2 + item + '/')
			elif item in dict2 and item != '.sync':						# file has been deleted in other directory
				date1 = datetime.fromtimestamp(time.mktime(time.strptime(dict1[item][0][0], '%Y-%m-%d %H:%M:%S %z')))
				date2 = datetime.fromtimestamp(time.mktime(time.strptime(dict2[item][0][0], '%Y-%m-%d %H:%M:%S %z')))
				if date1 == date2 and dict2[item][0][1] == "deleted":
					os.remove(dir1 + item)  # delete file
					dict1[item][0] = dict2[item][0]
				elif date1 > date2:
					shutil.copy(dir1 + item, dir2)
					dict2[item].insert(0, dict1[item][0])
				else:
					os.remove(dir1 + item)  # delete file
					dict1[item][0] = dict2[item][0]


	for item in files2:
		if item in files1 and item != '.sync':
			if os.path.isdir(dir2 + item):
				compare(dir1 + item + '/', dir2 + item + '/')
			else:		# a file
				if get_hash(dir1 + item) == get_hash(dir2 + item):		# file is the same
					if dict1[item][0][1] != dict2[item][0][1]:			# compare modified time
						date1 = datetime.fromtimestamp(time.mktime(time.strptime(dict1[item][0][0], '%Y-%m-%d %H:%M:%S %z')))
						date2 = datetime.fromtimestamp(time.mktime(time.strptime(dict2[item][0][0], '%Y-%m-%d %H:%M:%S %z')))
						if date1 > date2:
							os.system("touch -r {} {}".format(dir1 + item, dir2 + item))
						else:
							os.system("touch -r {} {}".format(dir2 + item, dir1 + item))
						recent_date = max(date1, date2)
						dict1[item][0][1] = recent_date.strftime('%Y-%m-%d %H:%M:%S %z')
						dict2[item][0][1] = recent_date.strftime('%Y-%m-%d %H:%M:%S %z')
				else:			# file is different
					digest1 = get_hash(dir1 + item)
					digest2 = get_hash(dir2 + item)
					for entry in dict2[item]:
						if digest1 in entry:				# file has been superseded
							shutil.copy(dir2 + item, dir1)	# copy more recent file to destination
							dict1[item].insert(0, dict2[item][0])		# copy changes to dated sync file		
					for entry in dict1[item]:
						if digest2 in entry:
							shutil.copy(dir1 + item, dir2)
							dict2[item].insert(0, dict1[item][0])

					# files are unique
					
					date1 = datetime.fromtimestamp(time.mktime(time.strptime(dict1[item][0][0], '%Y-%m-%d %H:%M:%S %z')))
					date2 = datetime.fromtimestamp(time.mktime(time.strptime(dict2[item][0][0], '%Y-%m-%d %H:%M:%S %z')))
					if date1 > date2:
						shutil.copy(dir1 + item, dir2)
						dict2[item].insert(0, dict1[item][0])
					else:
						shutil.copy(dir2 + item, dir1)
						dict1[item].insert(0, dict2[item][0])
		else:		# file does not exist in other directory
			if item not in dict1 and item != '.sync':		# file has not existed in other directory
				if os.path.isfile(dir2 + item):
					shutil.copy(dir2 + item, dir1)	# copy file
					dict1[item] = []
					dict1[item].append(dict2[item][0])
				else:
					shutil.copytree(dir2 + item, dir1 + item)		# copy directory
					update_sync(dir1 + item + '/')
			elif item in dict1 and item != '.sync':						# file has been deleted in other directory
				date1 = datetime.fromtimestamp(time.mktime(time.strptime(dict1[item][0][0], '%Y-%m-%d %H:%M:%S %z')))
				date2 = datetime.fromtimestamp(time.mktime(time.strptime(dict2[item][0][0], '%Y-%m-%d %H:%M:%S %z')))
				if date1 == date2 and dict1[item][0][1] == "deleted":
					os.remove(dir2 + item)  # delete file
					dict2[item][0] = dict1[item][0]
				elif date2 > date1:
					shutil.copy(dir2 + item, dir1)
					dict1[item].insert(0, dict2[item][0])
				else:
					os.remove(dir2 + item)  # delete file
					dict1[item][0] = dict2[item][0]

	#update .sync files
	sync1 = open(dir1 + '.sync', 'w')
	sync2 = open(dir2 + '.sync', 'w')
	json.dump(dict1, sync1, indent=4)
	json.dump(dict2, sync2, indent=4)