From 68274206e156f872e9a34d086168d3a499ff28ef Mon Sep 17 00:00:00 2001 From: ronan Date: Thu, 20 Sep 2018 15:32:18 +0200 Subject: [PATCH] =?UTF-8?q?modification=20incr=C3=A9ment=20ZIP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logrotate.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/logrotate.py b/logrotate.py index 3b94ec9..c3f7d1d 100644 --- a/logrotate.py +++ b/logrotate.py @@ -86,8 +86,6 @@ def removeUnusedFiles(FLOG): def incrementZipFile(LIST, FLOG, LISTERR = []): """ Incrémente les archives ZIP jusqu'à ``NBARCHIVE`` - Suppression de l'archive ZIP dont son index est supérieur - ou égal à ``NBARCHIVE`` :return: Une liste de fichiers en erreur :rtype: list @@ -100,20 +98,28 @@ def incrementZipFile(LIST, FLOG, LISTERR = []): for FILE in WLIST: TF = FILE.split('.') IDX = int(TF[2]) - OLDFILE = os.path.join(F, FILE) - if IDX >= NBARCHIVE: - delFile(OLDFILE, FLOG) - else: + if IDX < NBARCHIVE: + OLDFILE = os.path.join(F, FILE) TF[2] = str(IDX + 1) NEWFILE = os.path.join(F, '.'.join(TF)) - try: os.rename(OLDFILE, NEWFILE) - except: - ERR = True - LISTERR.append(f) - log(FLOG, TXT, OLDFILE, '{NEWFILE} ***KO***'.format(NEWFILE=NEWFILE)) - else: log(FLOG, TXT, OLDFILE, '{NEWFILE}'.format(NEWFILE=NEWFILE)) - finally: - if ERR: break + try: + with zipfile.ZipFile(OLDFILE, mode='r', compression=zipfile.ZIP_DEFLATED) as f1: + try: + CT = f1.read(os.path.splitext(FILE)[0]) + except: ERR = True + else: + BASE = os.path.splitext(os.path.basename(NEWFILE))[0] + try: + with zipfile.ZipFile(NEWFILE, mode='w', compression=zipfile.ZIP_DEFLATED) as f2: + f2.writestr(BASE, CT) + except: ERR = True + else: log(FLOG, TXT, OLDFILE, '{NEWFILE}'.format(NEWFILE=NEWFILE)) + except: ERR = True + finally: + if ERR: + LISTERR.append(f) + log(FLOG, TXT, OLDFILE, '{NEWFILE} ***KO***'.format(NEWFILE=NEWFILE)) + break return LISTERR def log_0ToZip(LIST, FLOG, LISTERR = []): -- 1.7.9.5