From 9f87f38347f8376204f22d80b12b7e1b69e0abfc Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Wed, 28 Jun 2023 23:03:27 +0200 Subject: [PATCH] fix file tmp for directory --- import_export_canalblog.py | 31 ++++++++++++++++++------------- lib/WPImport.py | 10 +++++----- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/import_export_canalblog.py b/import_export_canalblog.py index 86ffde7..3b14d59 100644 --- a/import_export_canalblog.py +++ b/import_export_canalblog.py @@ -11,6 +11,18 @@ from lib.WPExport import WPExport from lib.WPRemove import WPRemove from lib.WPChange import WPChange +def errorRevert(logger, revert, tmp): + if revert is True: + files_tmp = glob.glob("{0}/*.json".format(tmp)) + if len(files_tmp) == 0: + logger.error("Error revert, because files not found") + exit(1) + if len(files_tmp) != int(args.parallel): + for file_r in files_tmp: + os.remove(file_r) + logger.error("Error revert, because number files tmp is incompatible with parallel number") + exit(1) + def change(index, number, args, logger): changeWp = WPChange(logger=logger, index_name=index, number_thread=number) changeWp.fromDirectory(args.directory) @@ -106,7 +118,7 @@ def importUrl(name_thread, max_thread, canalblog, logger, parser, wordpress, bas del importWp -def importDirectory(name_thread, max_thread, directory, logger, parser, wordpress, basic, serial, ssl_wordpress, create, update, image): +def importDirectory(name_thread, max_thread, directory, logger, parser, wordpress, basic, serial, ssl_wordpress, create, update, image, revert): name = "Thread-{0}".format(int(name_thread) + 1) directory = directory.split(",") wordpress = wordpress.split(",") @@ -114,7 +126,7 @@ def importDirectory(name_thread, max_thread, directory, logger, parser, wordpres for i in wordpress: importWp = WPimport(name=name, basic=basic, wordpress=i, logger=logger, parser=parser, ssl_wordpress=ssl_wordpress, no_create=create, no_update=update, no_image=image) for j in directory: - importWp.fromDirectory(j, name_thread, max_thread) + importWp.fromDirectory(j, name_thread, max_thread, revert) del importWp else: @@ -123,7 +135,7 @@ def importDirectory(name_thread, max_thread, directory, logger, parser, wordpres exit(1) for i in range(0, len(wordpress)-1): importWp = WPimport(name=name, basic=basic, wordpress=wordpress[i], logger=logger, parser=parser, ssl_wordpress=ssl_wordpress, no_create=create, no_update=update, no_image=image) - importWp.fromDirectory(directory[i]) + importWp.fromDirectory(directory[i], name_thread, max_thread, revert) del importWp @@ -249,8 +261,9 @@ if __name__ == '__main__': with futures.ThreadPoolExecutor(max_workers=int(args.parallel)) as ex: wait_for = [ ex.submit(remove, i, args.parallel, args, basic, logger, ssl_wordpress) for i in range(0, int(args.parallel)) ] wait(wait_for, return_when=ALL_COMPLETED) + errorRevert(logger, args.revert, args.tmp) wait_for = [ - ex.submit(importDirectory, i, int(args.parallel), args.directory, logger, args.parser, args.wordpress, basic, args.serial, ssl_wordpress, args.create, args.update, args.image) + ex.submit(importDirectory, i, int(args.parallel), args.directory, logger, args.parser, args.wordpress, basic, args.serial, ssl_wordpress, args.create, args.update, args.image, args.revert) for i in range(0, int(args.parallel)) ] except Exception as err: @@ -260,15 +273,7 @@ if __name__ == '__main__': with futures.ThreadPoolExecutor(max_workers=int(args.parallel)) as ex: wait_for = [ ex.submit(remove, i, args.parallel, args, basic, logger, ssl_wordpress) for i in range(0, int(args.parallel)) ] wait(wait_for, return_when=ALL_COMPLETED) - if args.revert is True: - files_tmp = glob.glob("{0}/*.json".format(args.tmp)) - if len(files_tmp) == 0: - logger.error("Error revert, because files not found") - exit(1) - if len(files_tmp) != int(args.parallel): - for file_r in files_tmp: - os.remove(file_r) - + errorRevert(logger, args.revert, args.tmp) wait_for = [ ex.submit(importUrl, i, int(args.parallel), args.canalblog, logger, args.parser, args.wordpress, basic, args.serial, ssl_wordpress, ssl_canalblog, args.create, args.update, args.image, args.revert, args.tmp) for i in range(0, int(args.parallel)) diff --git a/lib/WPImport.py b/lib/WPImport.py index dde5f05..9f0767a 100644 --- a/lib/WPImport.py +++ b/lib/WPImport.py @@ -80,7 +80,7 @@ class WPimport: directories = self._getDirectories([], "{0}".format(directory)) if len(directories) > 0: files = self._getFiles(directories) - if args.revert is False: + if revert is False: self._tmpFiles(files=files, number_thread=number_thread, max_thread=max_thread) self._fromFileTmp() else: @@ -90,7 +90,7 @@ class WPimport: def fromFile(self, files=[]): for i in range(0, len(files)): if os.path.exists(files[i]): - self._logger.info("{0} : ({1}/{2}) File is being processed : {3}".format(self._name, i+1, currentRangeFiles + 1, files[i])) + self._logger.info("{0} : ({1}/{2}) File is being processed : {3}".format(self._name, i+1, len(files), files[i])) with open(files[i], 'r') as f: content = f.read() self._logger.debug("{0} : Size of article : {1}".format(self._name, len(content))) @@ -110,10 +110,10 @@ class WPimport: try: with open("{0}/{1}.json".format(self._tmp, self._name)) as file: files = json.loads(file.read()) - self._logger.debug("{0} : size of webpage : {1}".format(self._name, len(webpage))) + self._logger.debug("{0} : size of webpage : {1}".format(self._name, len(files))) for i in range(0, len(files)): if os.path.exists(files[i]): - self._logger.info("{0} : ({1}/{2}) File is being processed : {3}".format(self._name, i+1, currentRangeFiles + 1, files[i])) + self._logger.info("{0} : ({1}/{2}) File is being processed : {3}".format(self._name, i+1, len(files), files[i])) with open(files[i], 'r') as f: content = f.read() self._logger.debug("{0} : Size of article : {1}".format(self._name, len(content))) @@ -128,7 +128,7 @@ class WPimport: self._logger.error("{0} : Read file json from tmp : {1}".format(self._name, ex)) - def _tmpFiles(self, number_thread=1, max_thread=1): + def _tmpFiles(self, files=[], number_thread=1, max_thread=1): divFiles = int(len(files) / max_thread) currentRangeFiles = int(divFiles * (number_thread+1)) firstRange = int(currentRangeFiles - divFiles)