From 81ac5264019b179275afa6bdb5c983af9d776866 Mon Sep 17 00:00:00 2001 From: Alessandro ZANNI Date: Tue, 4 Oct 2016 23:56:55 +0200 Subject: [PATCH 1/2] zip / unzip functions --- pupy/packages/all/pupyutils/zip.py | 62 ++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 pupy/packages/all/pupyutils/zip.py diff --git a/pupy/packages/all/pupyutils/zip.py b/pupy/packages/all/pupyutils/zip.py new file mode 100644 index 00000000..11291446 --- /dev/null +++ b/pupy/packages/all/pupyutils/zip.py @@ -0,0 +1,62 @@ +import os +import zipfile + +def zip(src, dst): + + if not os.path.exists(src): + print "[-] The file \"%s\" does not exists" % src + return + + isDir = False + if os.path.isdir(src): + isDir = True + + if not dst: + if isDir: + d = src.split(os.sep) + dst = d[len(d)-1] + '.zip' + else: + dst = src + '.zip' + + # To not overwrite an existing file + if os.path.exists(dst): + print "[-] The destination file \"%s\" already exists" % dst + return + + # Zip process + zf = zipfile.ZipFile(dst, "w", zipfile.ZIP_DEFLATED) + if isDir: + abs_src = os.path.abspath(src) + for dirname, subdirs, files in os.walk(src): + for filename in files: + absname = os.path.abspath(os.path.join(dirname, filename)) + arcname = absname[len(abs_src) + 1:] + zf.write(absname, arcname) + else: + zf.write(src) + + print "[+] File zipped correctly: \"%s\"" % dst + zf.close() + + +def unzip(src, dst): + + if not os.path.exists(src): + print "[-] The file \"%s\" does not exists" % src + return + + if not dst: + d = src.split(os.sep) + dst = d[len(d)-1].replace('.zip', '') + + # To not overwrite an existing file + if os.path.exists(dst): + print "[-] The destination file \"%s\" already exists" % dst + return + + if zipfile.is_zipfile(src): + with zipfile.ZipFile(src, "r") as z: + z.extractall(dst) + print "[+] File unzipped correctly: \"%s\"" % dst + else: + print '[-] The zipped file does not have a valid zip format: \"%s\"' \ No newline at end of file From 3b3f9e8b7e0cb35026eec805f501d0720000c57f Mon Sep 17 00:00:00 2001 From: Alessandro ZANNI Date: Tue, 4 Oct 2016 23:57:07 +0200 Subject: [PATCH 2/2] zip / unzip main module --- pupy/modules/zip.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 pupy/modules/zip.py diff --git a/pupy/modules/zip.py b/pupy/modules/zip.py new file mode 100644 index 00000000..4a359caf --- /dev/null +++ b/pupy/modules/zip.py @@ -0,0 +1,26 @@ +# -*- coding: UTF8 -*- +from pupylib.PupyModule import * +from pupylib.utils.rpyc_utils import redirected_stdio + +__class_name__="Zip" + +@config(cat="admin") +class Zip(PupyModule): + """ zip / unzip file or directory """ + + def init_argparse(self): + self.arg_parser = PupyArgumentParser(prog="zip", description=self.__doc__) + self.arg_parser.add_argument('source', type=str, help='path of the source file or directory to zip') + + self.arg_parser.add_argument('-u', action='store_true', help='unzip file (default: zip file)') + self.arg_parser.add_argument('-d', dest='destination', help='path of the destination file (default: current directory)') + + def run(self, args): + self.client.load_package("pupyutils.zip") + with redirected_stdio(self.client.conn): + # zip + if not args.u: + self.client.conn.modules["pupyutils.zip"].zip(args.source, args.destination) + # unzip + else: + self.client.conn.modules["pupyutils.zip"].unzip(args.source, args.destination) \ No newline at end of file