2015-06-05 01:29:17 +02:00
|
|
|
#!/usr/bin/env python2
|
2015-06-04 04:28:47 +02:00
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import sys
|
2015-11-29 14:35:13 +01:00
|
|
|
import jpgSorter
|
2015-06-05 01:29:17 +02:00
|
|
|
import time
|
2015-11-29 14:35:13 +01:00
|
|
|
import shutil
|
|
|
|
from time import gmtime, strftime
|
2015-09-17 20:29:05 +02:00
|
|
|
|
|
|
|
|
2015-11-29 14:35:13 +01:00
|
|
|
def getFolderSizeInGb(start_path = '.'):
|
|
|
|
total_size = 0
|
|
|
|
for dirpath, dirnames, filenames in os.walk(start_path):
|
|
|
|
for f in filenames:
|
|
|
|
fp = os.path.join(dirpath, f)
|
|
|
|
total_size += os.path.getsize(fp)
|
|
|
|
return int(total_size / 1024 / 1024 / 1024)
|
2015-09-17 20:29:05 +02:00
|
|
|
|
|
|
|
|
2015-11-29 14:35:13 +01:00
|
|
|
source = None
|
|
|
|
destination = None
|
2015-09-17 20:29:05 +02:00
|
|
|
|
2015-11-29 14:35:13 +01:00
|
|
|
if(len(sys.argv) < 3):
|
|
|
|
print("Enter source and destination: python sort.py source/path destination/path")
|
|
|
|
else:
|
|
|
|
source = sys.argv[1]
|
|
|
|
print("Source directory: " + source)
|
|
|
|
destination = sys.argv[2]
|
|
|
|
print("Destination directory: " + destination)
|
2015-09-18 20:03:07 +02:00
|
|
|
|
2015-11-29 14:35:13 +01:00
|
|
|
while ((source is None) or (not os.path.exists(source))):
|
|
|
|
source = input('Enter a valid source directory\n')
|
|
|
|
while ((destination is None) or (not os.path.exists(destination))):
|
|
|
|
destination = input('Enter a valid destination directory\n')
|
2015-09-17 20:29:05 +02:00
|
|
|
|
2015-11-29 14:35:13 +01:00
|
|
|
totalAmountToCopy = str(getFolderSizeInGb(source))
|
|
|
|
print("Files to copy: " + totalAmountToCopy + " GB.")
|
2015-09-17 20:29:05 +02:00
|
|
|
|
2015-09-20 11:33:54 +02:00
|
|
|
images = []
|
2015-06-04 04:28:47 +02:00
|
|
|
for root, dirs, files in os.walk(source, topdown=False):
|
2015-11-29 14:35:13 +01:00
|
|
|
print (strftime("%H:%M:%S", gmtime()) + ": " + str(getFolderSizeInGb(destination)) + " / " + totalAmountToCopy + " GB processed.")
|
|
|
|
for file in files:
|
|
|
|
extension = os.path.splitext(file)[1][1:].upper()
|
|
|
|
path = os.path.join(root,file)
|
|
|
|
|
|
|
|
destinationPath = os.path.join(destination, extension)
|
|
|
|
|
|
|
|
if not os.path.exists(destinationPath):
|
|
|
|
os.mkdir(destinationPath)
|
|
|
|
|
|
|
|
if extension == "JPG":
|
|
|
|
jpgSorter.postprocessImage(images, root, destinationPath, file)
|
|
|
|
else:
|
|
|
|
if os.path.exists(os.path.join(destinationPath, file)):
|
|
|
|
shutil.copy(os.path.join(root,file), os.path.join(destination, extension, str(time.time()) + file))
|
|
|
|
else:
|
|
|
|
shutil.copy(os.path.join(root,file), destinationPath)
|
|
|
|
|
|
|
|
jpgSorter.writeImages(images, os.path.join(destination, "JPG"))
|