Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.59 KB | None | 0 0
  1. #!/usr/bin/python
  2. import os, sys, argparse, datetime, re
  3.  
  4. #input_path = "C:\\users\\" + os.getlogin()
  5. global input_path, output_path, verbose, force, time_start, sort_type, sort_by, exfilew, exfolderw
  6. input_path = ""
  7. output_path = ""
  8. verbose = 1
  9. force = False
  10. time_start = datetime.datetime.now()
  11. sort_type = ""
  12. sort_by = ""
  13. cluster = []
  14.  
  15. parser = argparse.ArgumentParser(description = "An advanced copy script, see the readme file for more info.")
  16. parser.add_argument("i", help = "The input file/folder")
  17. parser.add_argument("o", help = "The output file/folder")
  18. verbosity_group = parser.add_mutually_exclusive_group()
  19. verbosity_group.add_argument("-v", "--verbose", action = "store_true", help = "Displays more output information.")
  20. verbosity_group.add_argument("-q", "--quiet", action = "store_true", help = "Doesn't display any output (not even errors).")
  21. 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")
  22. 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")
  23. 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)")
  24. parser.add_argument("-f", "--force", action = "store_true" , help = "Forces the program to skip permission errors.")
  25.  
  26. args = parser.parse_args()
  27.  
  28. def copy():
  29. error("at line 29: Copy function is not finished.")
  30.  
  31. def search_folder(from_path):
  32. global size
  33. v1_print("Searching for files...")
  34. cluster_indexes_files = []
  35. cluster_indexes_files.append([])
  36. cluster_indexes_files.append([])
  37. size = 0
  38. for dirname, dirnames, filenames in os.walk(from_path):
  39. for filename in filenames:
  40. path = os.path.join(dirname, filename)
  41. try:
  42. crr_size = get_size(path)
  43. size += crr_size
  44. cluster_indexes_files[0].append(crr_size)
  45. cluster_indexes_files[1].append(path)
  46. except:
  47. print("File '" + path + "': permission denied")
  48. v1_print("Searching finished.")
  49. return cluster_indexes_files
  50.  
  51.  
  52. def filter_files(files):
  53. 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
  54. input_path_size = len(split_path(os.path.join(input_path)))
  55.  
  56. for index, a in enumerate(files):
  57. file_path = split_path(a)
  58. for index, n in enumerate(range(input_path)):
  59. file_path.delete_at(index)
  60. extension = file_path.split(".")[-1]
  61. 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)):
  62. size - get_size(a)
  63. files.delete_at(index)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. def split_path(path):
  72. dirname = path
  73. path_split = []
  74. while True:
  75. dirname, leaf = os.path.split(dirname)
  76. if(leaf):
  77. path_split = [leaf] + path_split
  78. else:
  79. path_split = [dirname] + path_split
  80. return path_split
  81. break
  82. return path_split
  83.  
  84.  
  85.  
  86.  
  87. def sort_cluster(cluster_indexes_files):
  88. message = ""
  89. if sort_type == 0:
  90. message += "Sorting files by size "
  91. if(sort_by == 0):
  92. message += "from smallest to largest."
  93. v1_print(message)#The print(message) is repeated because it indicates when it has initiated sorting cycle
  94. quickSort(cluster_indexes_files)
  95. else:
  96. message += "from largest to smallest."
  97. v1_print(message)
  98. quickSort(cluster_indexes_files)
  99. cluster_indexes_files.reverse()
  100. else: #TODO: Finish alphabeticall sorting.
  101. message += "Sorting files alphabetically "
  102. if(sort_by == 0):
  103. message += "from a to z."
  104. v1_print(message)
  105. sorted(cluster_indexes_files)
  106. else:
  107. message += "from z to a."
  108. v1_print(message)
  109. sorted(cluster_indexes_files[1], reverse = True)
  110. v1_print("File sorting finished.")
  111. return cluster_indexes_files
  112.  
  113.  
  114.  
  115.  
  116. def quickSort(cluster_indexes_files):
  117. quickSortHelper(cluster_indexes_files,0,len(cluster_indexes_files[0])-1)
  118.  
  119. def quickSortHelper(cluster_indexes_files,first,last):
  120. if first<last:
  121.  
  122. splitpoint = partition(cluster_indexes_files,first,last)
  123.  
  124. quickSortHelper(cluster_indexes_files,first,splitpoint-1)
  125. quickSortHelper(cluster_indexes_files,splitpoint+1,last)
  126.  
  127.  
  128. def partition(cluster_indexes_files,first,last):
  129. pivotvalue = cluster_indexes_files[0][first]
  130.  
  131. leftmark = first+1
  132. rightmark = last
  133.  
  134. done = False
  135. while not done:
  136.  
  137. while leftmark <= rightmark and cluster_indexes_files[0][leftmark] <= pivotvalue:
  138. leftmark = leftmark + 1
  139.  
  140. while cluster_indexes_files[0][rightmark] >= pivotvalue and rightmark >= leftmark:
  141. rightmark = rightmark -1
  142.  
  143. if rightmark < leftmark:
  144. done = True
  145. else:
  146. temp = cluster_indexes_files[0][leftmark]
  147. cluster_indexes_files[0][leftmark] = cluster_indexes_files[0][rightmark]
  148. cluster_indexes_files[0][rightmark] = temp
  149. temp = cluster_indexes_files[1][leftmark]
  150. cluster_indexes_files[1][leftmark] = cluster_indexes_files[1][rightmark]
  151. cluster_indexes_files[1][rightmark] = temp
  152.  
  153. temp = cluster_indexes_files[0][first]
  154. cluster_indexes_files[0][first] = cluster_indexes_files[0][rightmark]
  155. cluster_indexes_files[0][rightmark] = temp
  156. temp = cluster_indexes_files[1][first]
  157. cluster_indexes_files[1][first] = cluster_indexes_files[1][rightmark]
  158. cluster_indexes_files[1][rightmark] = temp
  159.  
  160.  
  161. return rightmark
  162.  
  163. def v2_print(message):
  164. if verbose == 2:
  165. print(message)
  166. def v1_print(message):
  167. if verbose > 0:
  168. print(message)
  169.  
  170. def error(e):
  171. if verbose > 0: #Feel free to change this value to -1 if you want to have error messages in quiet mode
  172. print("----- ERROR " + e + " -----")
  173. exit_message()
  174.  
  175. def time_diff(start, end):
  176. c = end - start
  177. time = divmod(c.days * 86400 + c.seconds, 60)
  178. return "{} minutes and {} seconds".format(str(time[0]), str(time[1]))
  179.  
  180. def exit_message():
  181. if verbose == 2:
  182. print("Program ran for " + time_diff(time_start, datetime.datetime.now()))
  183. print("Exitting at " + str(datetime.datetime.now()))
  184. sys.exit()
  185.  
  186. def get_size(filename):
  187. try:
  188. st = os.stat(filename)
  189. return st.st_size
  190. except Exception as e:
  191. print(e) #"File has not been found"
  192.  
  193.  
  194. input_path = args.i
  195. output_path = args.o
  196. force = args.force
  197. exfilew = args.excludefilewith
  198. exfolderw = args.excludefolderwith
  199.  
  200. if args.quiet:
  201. verbose = 0
  202. elif args.verbose:
  203. verbose = 2
  204.  
  205. #Tests if input_path specified as an argument "i" exists
  206. if os.path.exists(input_path):
  207. if os.path.isdir(input_path):
  208. v2_print("Input directory " + input_path + " exists.")
  209. input_type = "dir"
  210. v2_print("Input type set to: " + input_type)
  211. elif os.path.isfile(input_path):
  212. v2_print("Input file " + input_path + " exists.")
  213. input_type = "file"
  214. v2_print("Input type set to: " + input_type)
  215. else:
  216. error("at line 182: Input file/folder doesn't exist!")
  217.  
  218. #Tests if output_path specified as an argument "o" exists
  219. if os.path.exists(output_path):
  220. if os.path.isdir(output_path):
  221. v2_print("Output directory " + output_path + " exists.")
  222. output_type = "dir"
  223. v2_print("Output type set to: " + output_type)
  224. elif os.path.isfile(output_path):
  225. v2_print("Output file " + output_path + " exists.")
  226. output_type = "file"
  227. v2_print("Output type set to: " + output_path)
  228. else:
  229. error("at line 195: Output file/folder doesn't exist!")
  230.  
  231. #Test if sort is specified as an argument "s" and if it is specified in the right way
  232. if not args.sort is None:
  233. raw = args.sort.split(":")
  234. if (len(raw) == 2):
  235. try:
  236. sort_type = int(raw[0])
  237. except ValueError:
  238. error("at line 204: Sort_type is not a valid integer.")
  239. try:
  240. sort_by = int(raw[1])
  241. except ValueError:
  242. error("at line 208: Sort_by is not a valid integer.")
  243. if not (sort_type == 0 or sort_type == 1):
  244. error("at line 210: Invalid value of sort_type. It can be either 0 or 1")
  245. if not (sort_by == 0 or sort_by == 1):
  246. error("at line 212: Invalid value of sort_by. It can be either 0 or 1")
  247. sort_type = int(raw[0])
  248. sort_by = int(raw[1])
  249. v2_print("Sort_type set to " + str(sort_type) + " and sort_by set to " + str(sort_by))
  250. else:
  251. error("at line 217: Wrong sort argument value")
  252. else:
  253. v2_print("Sort argument has not been specified. Sorting will be skipped.")
  254.  
  255. if not exfilew is None:
  256. global ex_file_wN, ex_file_wE, ex_file_wT, ex_file_wS
  257. ex_file_wN = ""
  258. ex_file_wE = ""
  259. ex_file_wS = ""
  260. ex_file_wT = ""
  261. raw = exfilew.split(":")
  262. if len(raw) == 4:
  263. if not raw[0] == "":
  264. ex_file_wS = raw[0]
  265. if not raw[1] == "":
  266. ex_file_wN = raw[1]
  267. if not raw[2] == "":
  268. ex_file_wE = raw[2]
  269. if not raw[3] == "":
  270. ex_file_wT = raw[3]
  271. 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)
  272. else:
  273. error("at line 240: Wrong syntax of argument exfilew. Try -h for more info.")
  274.  
  275. if not exfolderw is None:
  276. global ex_folder_wN, ex_folder_wT, ex_folder_wS
  277. ex_folder_wN = ""
  278. ex_folder_wS = ""
  279. ex_folder_wT = ""
  280. raw = exfolderw.split(":")
  281. if len(raw) == 3:
  282. if not raw[0] == "":
  283. ex_folder_wS = raw[0]
  284. if not raw[1] == "":
  285. ex_folder_wN = raw[1]
  286. if not raw[2] == "":
  287. ex_folder_wT = raw[2]
  288. 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 )
  289. else:
  290. error("at line 254: Wrong syntax of argument exfolderw. Try -h for more info.")
  291.  
  292. cluster_indexes_files = search_folder(input_path)
  293. if not args.sort is None:
  294. cluster_indexes_files = sort_cluster(cluster_indexes_files)
  295. v2_print("Moving clusters.")
  296. for a in cluster_indexes_files[1]:
  297. cluster.append(a)
  298. v2_print("Moving finished.")
  299. v1_print("Cluster length: " + str(len(cluster)))
  300. v1_print("File size in GB: " + str(float((((size)/1024)/1024)/1024)))
  301. v1_print("-------------------------------------")
  302. copy_start = datetime.datetime.now()
  303. v1_print("Copying files started at: " + str(copy_start))
  304. #copy()
  305. v1_print("Copying files took: " + time_diff(time_start, datetime.datetime.now()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement