Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import os, sys, argparse, datetime, re
- #input_path = "C:\\users\\" + os.getlogin()
- global input_path, output_path, verbose, force, time_start, sort_type, sort_by, exfilew, exfolderw
- input_path = ""
- output_path = ""
- verbose = 1
- force = False
- time_start = datetime.datetime.now()
- sort_type = ""
- sort_by = ""
- cluster = []
- parser = argparse.ArgumentParser(description = "An advanced copy script, see the readme file for more info.")
- parser.add_argument("i", help = "The input file/folder")
- parser.add_argument("o", help = "The output file/folder")
- verbosity_group = parser.add_mutually_exclusive_group()
- verbosity_group.add_argument("-v", "--verbose", action = "store_true", help = "Displays more output information.")
- verbosity_group.add_argument("-q", "--quiet", action = "store_true", help = "Doesn't display any output (not even errors).")
- parser.add_argument("-exfilew", "--excludefilewith", help = "Exclude files with particular name, extension or text in name. ex. starts_with:name:extension:optional_text_in_name")
- parser.add_argument("-exfolderw", "--excludefolderwith", help = "Exclude files with particular name, extension or text in name. ex. starts_with:name:optional_text_in_name or ::test")
- parser.add_argument("-s", "--sort", help = "Sort type = Size - 0, alphabetically - 1; sort by = from smallest/z to largest/a - 0, from largest/a to smallest/z - 1 (split sort options with colon ex. 1:0)")
- parser.add_argument("-f", "--force", action = "store_true" , help = "Forces the program to skip permission errors.")
- args = parser.parse_args()
- def copy():
- error("at line 29: Copy function is not finished.")
- def search_folder(from_path):
- global size
- v1_print("Searching for files...")
- cluster_indexes_files = []
- cluster_indexes_files.append([])
- cluster_indexes_files.append([])
- size = 0
- for dirname, dirnames, filenames in os.walk(from_path):
- for filename in filenames:
- path = os.path.join(dirname, filename)
- try:
- crr_size = get_size(path)
- size += crr_size
- cluster_indexes_files[0].append(crr_size)
- cluster_indexes_files[1].append(path)
- except:
- print("File '" + path + "': permission denied")
- v1_print("Searching finished.")
- return cluster_indexes_files
- def filter_files(files):
- global size, exfolderw, exfilew, ex_file_wT, ex_file_wN, ex_file_wE, ex_file_wS, ex_folder_wN, ex_folder_wT, ex_folder_wS, ex_folder, ex_file
- input_path_size = len(split_path(os.path.join(input_path)))
- for index, a in enumerate(files):
- file_path = split_path(a)
- for index, n in enumerate(range(input_path)):
- file_path.delete_at(index)
- extension = file_path.split(".")[-1]
- if len(file_path) - 1 == index and (not ex_file_wN == file_path[-1] or not extension == ex_file_wE or not ex_folder_wT in file_path[-1] or not file_path[-1].startswith(ex_file_wS)):
- size - get_size(a)
- files.delete_at(index)
- def split_path(path):
- dirname = path
- path_split = []
- while True:
- dirname, leaf = os.path.split(dirname)
- if(leaf):
- path_split = [leaf] + path_split
- else:
- path_split = [dirname] + path_split
- return path_split
- break
- return path_split
- def sort_cluster(cluster_indexes_files):
- message = ""
- if sort_type == 0:
- message += "Sorting files by size "
- if(sort_by == 0):
- message += "from smallest to largest."
- v1_print(message)#The print(message) is repeated because it indicates when it has initiated sorting cycle
- quickSort(cluster_indexes_files)
- else:
- message += "from largest to smallest."
- v1_print(message)
- quickSort(cluster_indexes_files)
- cluster_indexes_files.reverse()
- else: #TODO: Finish alphabeticall sorting.
- message += "Sorting files alphabetically "
- if(sort_by == 0):
- message += "from a to z."
- v1_print(message)
- sorted(cluster_indexes_files)
- else:
- message += "from z to a."
- v1_print(message)
- sorted(cluster_indexes_files[1], reverse = True)
- v1_print("File sorting finished.")
- return cluster_indexes_files
- def quickSort(cluster_indexes_files):
- quickSortHelper(cluster_indexes_files,0,len(cluster_indexes_files[0])-1)
- def quickSortHelper(cluster_indexes_files,first,last):
- if first<last:
- splitpoint = partition(cluster_indexes_files,first,last)
- quickSortHelper(cluster_indexes_files,first,splitpoint-1)
- quickSortHelper(cluster_indexes_files,splitpoint+1,last)
- def partition(cluster_indexes_files,first,last):
- pivotvalue = cluster_indexes_files[0][first]
- leftmark = first+1
- rightmark = last
- done = False
- while not done:
- while leftmark <= rightmark and cluster_indexes_files[0][leftmark] <= pivotvalue:
- leftmark = leftmark + 1
- while cluster_indexes_files[0][rightmark] >= pivotvalue and rightmark >= leftmark:
- rightmark = rightmark -1
- if rightmark < leftmark:
- done = True
- else:
- temp = cluster_indexes_files[0][leftmark]
- cluster_indexes_files[0][leftmark] = cluster_indexes_files[0][rightmark]
- cluster_indexes_files[0][rightmark] = temp
- temp = cluster_indexes_files[1][leftmark]
- cluster_indexes_files[1][leftmark] = cluster_indexes_files[1][rightmark]
- cluster_indexes_files[1][rightmark] = temp
- temp = cluster_indexes_files[0][first]
- cluster_indexes_files[0][first] = cluster_indexes_files[0][rightmark]
- cluster_indexes_files[0][rightmark] = temp
- temp = cluster_indexes_files[1][first]
- cluster_indexes_files[1][first] = cluster_indexes_files[1][rightmark]
- cluster_indexes_files[1][rightmark] = temp
- return rightmark
- def v2_print(message):
- if verbose == 2:
- print(message)
- def v1_print(message):
- if verbose > 0:
- print(message)
- def error(e):
- if verbose > 0: #Feel free to change this value to -1 if you want to have error messages in quiet mode
- print("----- ERROR " + e + " -----")
- exit_message()
- def time_diff(start, end):
- c = end - start
- time = divmod(c.days * 86400 + c.seconds, 60)
- return "{} minutes and {} seconds".format(str(time[0]), str(time[1]))
- def exit_message():
- if verbose == 2:
- print("Program ran for " + time_diff(time_start, datetime.datetime.now()))
- print("Exitting at " + str(datetime.datetime.now()))
- sys.exit()
- def get_size(filename):
- try:
- st = os.stat(filename)
- return st.st_size
- except Exception as e:
- print(e) #"File has not been found"
- input_path = args.i
- output_path = args.o
- force = args.force
- exfilew = args.excludefilewith
- exfolderw = args.excludefolderwith
- if args.quiet:
- verbose = 0
- elif args.verbose:
- verbose = 2
- #Tests if input_path specified as an argument "i" exists
- if os.path.exists(input_path):
- if os.path.isdir(input_path):
- v2_print("Input directory " + input_path + " exists.")
- input_type = "dir"
- v2_print("Input type set to: " + input_type)
- elif os.path.isfile(input_path):
- v2_print("Input file " + input_path + " exists.")
- input_type = "file"
- v2_print("Input type set to: " + input_type)
- else:
- error("at line 182: Input file/folder doesn't exist!")
- #Tests if output_path specified as an argument "o" exists
- if os.path.exists(output_path):
- if os.path.isdir(output_path):
- v2_print("Output directory " + output_path + " exists.")
- output_type = "dir"
- v2_print("Output type set to: " + output_type)
- elif os.path.isfile(output_path):
- v2_print("Output file " + output_path + " exists.")
- output_type = "file"
- v2_print("Output type set to: " + output_path)
- else:
- error("at line 195: Output file/folder doesn't exist!")
- #Test if sort is specified as an argument "s" and if it is specified in the right way
- if not args.sort is None:
- raw = args.sort.split(":")
- if (len(raw) == 2):
- try:
- sort_type = int(raw[0])
- except ValueError:
- error("at line 204: Sort_type is not a valid integer.")
- try:
- sort_by = int(raw[1])
- except ValueError:
- error("at line 208: Sort_by is not a valid integer.")
- if not (sort_type == 0 or sort_type == 1):
- error("at line 210: Invalid value of sort_type. It can be either 0 or 1")
- if not (sort_by == 0 or sort_by == 1):
- error("at line 212: Invalid value of sort_by. It can be either 0 or 1")
- sort_type = int(raw[0])
- sort_by = int(raw[1])
- v2_print("Sort_type set to " + str(sort_type) + " and sort_by set to " + str(sort_by))
- else:
- error("at line 217: Wrong sort argument value")
- else:
- v2_print("Sort argument has not been specified. Sorting will be skipped.")
- if not exfilew is None:
- global ex_file_wN, ex_file_wE, ex_file_wT, ex_file_wS
- ex_file_wN = ""
- ex_file_wE = ""
- ex_file_wS = ""
- ex_file_wT = ""
- raw = exfilew.split(":")
- if len(raw) == 4:
- if not raw[0] == "":
- ex_file_wS = raw[0]
- if not raw[1] == "":
- ex_file_wN = raw[1]
- if not raw[2] == "":
- ex_file_wE = raw[2]
- if not raw[3] == "":
- ex_file_wT = raw[3]
- v2_print("ex_file_wN set to: " + ex_file_wN + ", ex_file_wE set to: " + ex_file_wE + ", ex_file_wT set to: " + ex_file_wT + "ex_file_wS set to: " + ex_file_wS)
- else:
- error("at line 240: Wrong syntax of argument exfilew. Try -h for more info.")
- if not exfolderw is None:
- global ex_folder_wN, ex_folder_wT, ex_folder_wS
- ex_folder_wN = ""
- ex_folder_wS = ""
- ex_folder_wT = ""
- raw = exfolderw.split(":")
- if len(raw) == 3:
- if not raw[0] == "":
- ex_folder_wS = raw[0]
- if not raw[1] == "":
- ex_folder_wN = raw[1]
- if not raw[2] == "":
- ex_folder_wT = raw[2]
- v2_print("ex_folder_wN set to: " + ex_folder_wN + ", ex_folder_wT set to: " + ex_folder_wT + ", ex_folder_wS set to: " + ex_folder_wS )
- else:
- error("at line 254: Wrong syntax of argument exfolderw. Try -h for more info.")
- cluster_indexes_files = search_folder(input_path)
- if not args.sort is None:
- cluster_indexes_files = sort_cluster(cluster_indexes_files)
- v2_print("Moving clusters.")
- for a in cluster_indexes_files[1]:
- cluster.append(a)
- v2_print("Moving finished.")
- v1_print("Cluster length: " + str(len(cluster)))
- v1_print("File size in GB: " + str(float((((size)/1024)/1024)/1024)))
- v1_print("-------------------------------------")
- copy_start = datetime.datetime.now()
- v1_print("Copying files started at: " + str(copy_start))
- #copy()
- v1_print("Copying files took: " + time_diff(time_start, datetime.datetime.now()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement