2018-05-11 22:10:03 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import threading
|
|
|
|
|
2018-05-13 00:56:09 +02:00
|
|
|
import mover
|
2018-05-11 22:10:03 +02:00
|
|
|
import config
|
|
|
|
import rotator
|
2018-06-13 18:36:55 +02:00
|
|
|
import fileprop
|
2018-05-11 22:10:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Importer(threading.Thread):
|
2018-06-13 18:36:55 +02:00
|
|
|
def __init__(self, config, input_path, output_path, dryrun):
|
2018-05-11 22:10:03 +02:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.__config = config
|
|
|
|
self.__input_path = input_path
|
|
|
|
self.__output_path = output_path
|
2018-06-13 18:36:55 +02:00
|
|
|
self.__dryrun = dryrun
|
2018-05-20 14:50:02 +02:00
|
|
|
self.__mov = None
|
2018-05-11 22:10:03 +02:00
|
|
|
self.__rot = None
|
2018-05-13 00:56:09 +02:00
|
|
|
self.__stat = {'stage': ''}
|
2018-05-11 22:10:03 +02:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
logging.info(
|
2018-06-13 18:36:55 +02:00
|
|
|
'Start: %s -> %s (dryrun: %s)' %
|
|
|
|
(self.__input_path, self.__output_path, self.__dryrun))
|
2018-05-11 22:10:03 +02:00
|
|
|
|
2018-05-21 21:37:08 +02:00
|
|
|
filenames, dirs = self.__scan_files(self.__input_path)
|
2018-05-11 22:10:03 +02:00
|
|
|
|
2018-06-13 18:36:55 +02:00
|
|
|
new_filenames = self.__image_filenames(self.__move_files(filenames))
|
2018-05-11 22:10:03 +02:00
|
|
|
|
2018-05-21 21:37:08 +02:00
|
|
|
if self.__config['main']['remove_empty_dirs']:
|
|
|
|
self.__remove_empty_dirs(dirs)
|
|
|
|
|
2018-05-11 22:10:03 +02:00
|
|
|
self.__rotate_files(new_filenames)
|
2018-05-13 00:56:09 +02:00
|
|
|
|
|
|
|
self.__stat['stage'] = 'done'
|
2018-05-11 22:10:03 +02:00
|
|
|
logging.info('Done')
|
|
|
|
|
|
|
|
def __scan_files(self, input_path):
|
2018-05-21 21:37:08 +02:00
|
|
|
self.__stat['stage'] = 'scan'
|
|
|
|
res_dir = []
|
2018-05-11 22:10:03 +02:00
|
|
|
res = []
|
2018-05-12 22:40:23 +02:00
|
|
|
for root, dirs, files in os.walk(
|
|
|
|
input_path, onerror=self.__on_walk_error):
|
|
|
|
|
2018-05-11 22:10:03 +02:00
|
|
|
for fname in files:
|
|
|
|
res.append(os.path.join(root, fname))
|
|
|
|
|
2018-05-21 21:37:08 +02:00
|
|
|
for dname in dirs:
|
|
|
|
res_dir.append(os.path.join(root, dname))
|
|
|
|
|
2018-05-11 22:10:03 +02:00
|
|
|
self.__stat['total'] = len(res)
|
2018-06-13 18:36:55 +02:00
|
|
|
res.sort()
|
|
|
|
res_dir.sort()
|
2018-05-21 21:37:08 +02:00
|
|
|
logging.info('Found %i files and %i dirs' % (len(res), len(res_dir)))
|
|
|
|
return res, res_dir
|
2018-05-11 22:10:03 +02:00
|
|
|
|
2018-05-12 22:40:23 +02:00
|
|
|
def __on_walk_error(self, err):
|
|
|
|
logging.error('Scan files error: %s' % err)
|
|
|
|
|
2018-05-11 22:10:03 +02:00
|
|
|
def __move_files(self, filenames):
|
2018-05-21 21:37:08 +02:00
|
|
|
logging.info('Moving')
|
2018-05-13 00:56:09 +02:00
|
|
|
self.__mov = mover.Mover(
|
|
|
|
self.__config,
|
|
|
|
self.__input_path,
|
|
|
|
self.__output_path,
|
2018-06-13 18:36:55 +02:00
|
|
|
filenames,
|
|
|
|
self.__dryrun)
|
2018-05-21 21:37:08 +02:00
|
|
|
self.__stat['stage'] = 'move'
|
2018-05-11 22:10:03 +02:00
|
|
|
|
2018-05-21 21:37:08 +02:00
|
|
|
res = self.__mov.run()
|
|
|
|
logging.info('Processed %s files' % len(res))
|
|
|
|
return res
|
2018-05-11 22:10:03 +02:00
|
|
|
|
2018-06-13 18:36:55 +02:00
|
|
|
def __image_filenames(self, move_result):
|
|
|
|
res = []
|
|
|
|
for old, new, prop in move_result:
|
|
|
|
if prop.type() == fileprop.IMAGE:
|
|
|
|
res.append(new)
|
|
|
|
return res
|
|
|
|
|
2018-05-11 22:10:03 +02:00
|
|
|
def __rotate_files(self, filenames):
|
2018-05-21 21:37:08 +02:00
|
|
|
logging.info('Rotating')
|
2018-05-13 00:56:09 +02:00
|
|
|
self.__rot = rotator.Rotator(
|
|
|
|
self.__config,
|
2018-06-13 18:36:55 +02:00
|
|
|
filenames,
|
|
|
|
self.__dryrun)
|
2018-05-21 21:37:08 +02:00
|
|
|
self.__stat['stage'] = 'rotate'
|
2018-05-13 00:56:09 +02:00
|
|
|
|
2018-05-11 22:10:03 +02:00
|
|
|
self.__rot.run()
|
|
|
|
|
2018-05-21 21:37:08 +02:00
|
|
|
def __remove_empty_dirs(self, dirs):
|
|
|
|
logging.info('Removing empty dirs')
|
|
|
|
len_dirs = reversed(sorted([(len(d), d) for d in dirs]))
|
|
|
|
for l, d in len_dirs:
|
|
|
|
try:
|
|
|
|
os.rmdir(d)
|
|
|
|
logging.info('Removed: %s', d)
|
|
|
|
except OSError:
|
|
|
|
logging.info('Skipped: %s', d)
|
|
|
|
|
2018-05-11 22:10:03 +02:00
|
|
|
def status(self):
|
2018-05-13 00:56:09 +02:00
|
|
|
if self.__mov:
|
|
|
|
self.__stat['move'] = self.__mov.status()
|
|
|
|
|
2018-05-11 22:10:03 +02:00
|
|
|
if self.__rot:
|
2018-05-13 00:56:09 +02:00
|
|
|
self.__stat['rotate'] = self.__rot.status()
|
|
|
|
|
2018-05-11 22:10:03 +02:00
|
|
|
return self.__stat
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
import log
|
|
|
|
|
|
|
|
log.initLogger()
|
|
|
|
|
2018-05-13 00:56:09 +02:00
|
|
|
imp = Importer(config.Config(), sys.argv[1], sys.argv[2])
|
2018-05-11 22:10:03 +02:00
|
|
|
imp.start()
|
|
|
|
imp.join()
|
|
|
|
|
|
|
|
print(imp.status())
|