From ebad2af1ae5524e75caa62ec616d82cfda13c15e Mon Sep 17 00:00:00 2001 From: Terrtia Date: Wed, 19 Sep 2018 13:38:31 +0200 Subject: [PATCH] fix: [import_dir] fix #251, fix paste import if the date directory tree don't exist, he will be generated --- bin/Mixer.py | 2 ++ bin/import_dir.py | 63 +++++++++++++++++++++++++---------------------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/bin/Mixer.py b/bin/Mixer.py index 96f20815..e1656b8e 100755 --- a/bin/Mixer.py +++ b/bin/Mixer.py @@ -101,6 +101,8 @@ if __name__ == '__main__': #feeder_name = ( complete_paste.replace("archive/","") ).split("/")[0] feeder_name, paste_name = complete_paste.split('>>') feeder_name.replace(" ","") + if 'import_dir' in feeder_name: + feeder_name = feeder_name.split('/')[1] paste_name = complete_paste except ValueError as e: diff --git a/bin/import_dir.py b/bin/import_dir.py index d8360631..6d6e4a94 100755 --- a/bin/import_dir.py +++ b/bin/import_dir.py @@ -4,6 +4,7 @@ import zmq import base64 from io import StringIO +import datetime import gzip import argparse import os @@ -13,10 +14,10 @@ import mimetypes ''' ' ' Import content/pastes into redis. -' If content is not compressed yet, compress it. +' If content is not compressed yet, compress it (only text). ' ' /!\ WARNING /!\ - Content to be imported must be placed in a directory tree of the form + Content to be imported can be placed in a directory tree of the form root/ | +-- Year/ @@ -28,6 +29,10 @@ import mimetypes +-- Content e.g.: ~/to_import/2017/08/22/paste1.gz + + or this directory tree will be created with the current date + e.g.: + ~/to_import/paste1.gz ' ''' @@ -66,36 +71,34 @@ if __name__ == "__main__": for filename in filenames: complete_path = os.path.join(dirname, filename) - #take wanted path of the file - wanted_path = os.path.realpath(complete_path) - wanted_path = wanted_path.split('/') - wanted_path = '/'.join(wanted_path[-(4+args.hierarchy):]) - - with gzip.open(complete_path, 'rb') as f: + with open(complete_path, 'rb') as f: messagedata = f.read() - #print(type(complete_path)) - #file = open(complete_path) - #messagedata = file.read() - - #if paste do not have a 'date hierarchy' ignore it - if not is_hierachy_valid(complete_path): - print('/!\ hierarchy not valid, should have the format yyyy/mm/dd/paste.gz /!\ ') - print(complete_path) - break - #verify that the data is gzipEncoded. if not compress it if 'text' in str(mimetypes.guess_type(complete_path)[0]): - out = StringIO.StringIO() - with gzip.GzipFile(fileobj=out, mode="w") as f: - f.write(messagedata) - messagedata = out.getvalue() - wanted_path += '.gz' + messagedata = gzip.compress(messagedata) + complete_path += '.gz' - print(args.name+'>'+wanted_path) - path_to_send = args.name + '>' + wanted_path - #s = b'{} {} {}'.format(args.channel, path_to_send, base64.b64encode(messagedata)) - # use bytes object - s = b' '.join( [ args.channel.encode(), path_to_send.encode(), base64.b64encode(messagedata) ] ) - socket.send(s) - time.sleep(args.seconds) + + if complete_path[-4:] != '.gz': + + #if paste do not have a 'date hierarchy', create it + if not is_hierachy_valid(complete_path): + now = datetime.datetime.now() + paste_name = complete_path.split('/')[-1] + directory = complete_path.split('/')[-2] + wanted_path = os.path.join(directory, now.strftime("%Y"), now.strftime("%m"), now.strftime("%d"), paste_name) + else: + #take wanted path of the file + wanted_path = os.path.realpath(complete_path) + wanted_path = wanted_path.split('/') + wanted_path = '/'.join(wanted_path[-(4+args.hierarchy):]) + + path_to_send = 'import_dir/' + args.name + '>>' + wanted_path + s = b' '.join( [ args.channel.encode(), path_to_send.encode(), base64.b64encode(messagedata) ] ) + socket.send(s) + print('import_dir/' + args.name+'>>'+wanted_path) + time.sleep(args.seconds) + + else: + print('{} : incorrect type'.format(complete_path))