Ejemplo n.º 1
0
 def test_file_path_list_item_does_not_exist(self, capsys):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path="/foobar2783987")
     assert res is False
     assert re.search("file does not exist", instance.err)
     assert os.listdir(os.getcwd()) == []
Ejemplo n.º 2
0
 def test_archive_no_extension2(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename), archive_path="./foo", cls_name="ZipCompressor")
     assert res is True
     assert instance.err is None
     assert os.listdir(os.getcwd()) == ["foo.zip"]
Ejemplo n.º 3
0
 def test_specified_cls_name_zip(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename), archive_path="./", cls_name="ZipCompressor")
     assert res is True
     assert instance.err is None
     assert os.listdir(os.path.dirname(instance.output_archive_path)) == ['export.zip']
Ejemplo n.º 4
0
 def test_compress(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename), archive_path="./")
     assert res is True
     assert instance.err is None
     assert os.listdir(os.getcwd()) == ['export.zip']
Ejemplo n.º 5
0
 def test_specified_cls_name_pw_provided_but_not_supported(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename), archive_path="./test.pw.7z",
                             cls_name="ZipCompressor", password="******")
     assert res is False
     assert os.listdir(os.getcwd()) == []
Ejemplo n.º 6
0
 def test_compress(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename), archive_path="./")
     assert res is True
     assert instance.err is None
     assert os.listdir(os.getcwd()) == ['export.zip']
Ejemplo n.º 7
0
 def test_specified_cls_name_pw_provided_but_not_supported(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename), archive_path="./test.pw.7z",
                             cls_name="ZipCompressor", password="******")
     assert res is False
     assert os.listdir(os.getcwd()) == []
Ejemplo n.º 8
0
 def test_file_path_list_item_does_not_exist(self, capsys):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path="/foobar2783987")
     assert res is False
     assert re.search("file does not exist", instance.err)
     assert os.listdir(os.getcwd()) == []
Ejemplo n.º 9
0
 def test_archive_no_extension2(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename), archive_path="./foo", cls_name="ZipCompressor")
     assert res is True
     assert instance.err is None
     assert os.listdir(os.getcwd()) == ["foo.zip"]
Ejemplo n.º 10
0
 def test_specified_cls_name_zip(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename), archive_path="./", cls_name="ZipCompressor")
     assert res is True
     assert instance.err is None
     assert os.listdir(os.path.dirname(instance.output_archive_path)) == ['export.zip']
Ejemplo n.º 11
0
 def test_specified_cls_name_does_not_exist(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename), archive_path="./", cls_name="ThisDoesNotExistCompressor")
     assert res is False
     assert instance.err is not None
     assert re.search("invalid Compressor.*ThisDoesNotExistCompressor", instance.err)
     assert os.listdir(os.getcwd()) == []
Ejemplo n.º 12
0
 def test_file_without_extension(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename), archive_path="./test_compress")
     assert res is False
     assert instance.err is not None
     assert re.search("unable to discover extension", instance.err)
     assert os.listdir(os.getcwd()) == []
Ejemplo n.º 13
0
 def test_specified_cls_name_7z_pw_dot(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename), archive_path="./test.pw.7z",
                             cls_name="SevenZipSystemCompressor", password="******")
     assert res is True
     assert instance.err is None
     assert os.listdir(os.getcwd()) == ['test.pw.7z']
Ejemplo n.º 14
0
 def test_file_without_extension(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename), archive_path="./test_compress")
     assert res is False
     assert instance.err is not None
     assert re.search("unable to discover extension", instance.err)
     assert os.listdir(os.getcwd()) == []
Ejemplo n.º 15
0
 def test_specified_cls_name_7z_pw_dot(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename), archive_path="./test.pw.7z",
                             cls_name="SevenZipSystemCompressor", password="******")
     assert res is True
     assert instance.err is None
     assert os.listdir(os.getcwd()) == ['test.pw.7z']
Ejemplo n.º 16
0
 def test_specified_cls_name_does_not_exist(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename), archive_path="./", cls_name="ThisDoesNotExistCompressor")
     assert res is False
     assert instance.err is not None
     assert re.search("invalid Compressor.*ThisDoesNotExistCompressor", instance.err)
     assert os.listdir(os.getcwd()) == []
Ejemplo n.º 17
0
 def test_compress_new_dir(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename), archive_path="./barfoo/")
     assert res is True
     assert instance.err is None
     assert os.listdir(os.getcwd()) == ['barfoo']
     assert instance.output_archive_path == "./barfoo/export.zip"
     assert os.listdir(os.path.dirname(instance.output_archive_path)) == ['export.zip']
Ejemplo n.º 18
0
 def test_compress_new_dir(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename), archive_path="./barfoo/")
     assert res is True
     assert instance.err is None
     assert os.listdir(os.getcwd()) == ['barfoo']
     assert instance.output_archive_path == "./barfoo/export.zip"
     assert os.listdir(os.path.dirname(instance.output_archive_path)) == ['export.zip']
Ejemplo n.º 19
0
 def test_compress_to_tmp(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename))
     assert res is True
     assert instance.err is None
     assert os.listdir(os.getcwd()) == []
     out_dir, out_file = os.path.split(instance.output_archive_path)
     assert out_file == 'export.zip'
     assert out_dir.startswith("/tmp")
     shutil.rmtree(out_dir)
Ejemplo n.º 20
0
 def test_compress_to_tmp(self, capsys, filename):
     assert os.listdir(os.getcwd()) == []
     instance = Compressor()
     res = instance.compress(file_path=os.path.join(FIXTURE_DIR, filename))
     assert res is True
     assert instance.err is None
     assert os.listdir(os.getcwd()) == []
     out_dir, out_file = os.path.split(instance.output_archive_path)
     assert out_file == 'export.zip'
     assert out_dir.startswith("/tmp")
     shutil.rmtree(out_dir)
Ejemplo n.º 21
0
 def validate_compressor(self, value):
     compressors = list(Compressor().compressors)
     if value not in compressors:
         raise serializers.ValidationError(
             "{}: not in list of supported Compressors: {}".format(
                 value, compressors))
     return value
Ejemplo n.º 22
0
    def run(self, *args):
        try:
            args = self.parser.parse_args(args)
        except SystemExit:
            return

        # This command requires a session to be opened.
        if not __sessions__.is_set():
            self.log('error', "No open session. This command expects a file to be open.")
            self.parser.print_usage()
            return

        # Check for valid export path.
        if args.value is None:
            self.parser.print_usage()
            return

        if args.zip and args.sevenzip:
            self.log('error', "Please select either -z or -7 not both, abort")

        store_path = os.path.join(args.value, __sessions__.current.file.name)

        if not args.zip and not args.sevenzip:
            # Abort if the specified path already exists
            if os.path.isfile(store_path):
                self.log('error', "Unable to export file: File exists: '{0}'".format(store_path))
                return

            if not os.path.isdir(args.value):
                try:
                    os.makedirs(args.value)
                except OSError as err:
                    self.log('error', "Unable to export file: {0}".format(err))
                    return

            try:
                shutil.copyfile(__sessions__.current.file.path, store_path)
            except IOError as e:
                self.log('error', "Unable to export file: {0}".format(e))
            else:
                self.log('info', "File exported to {0}".format(store_path))

            return
        elif args.zip:
            cls = "ZipCompressor"

        elif args.sevenzip:
            cls = "SevenZipSystemCompressor"
        else:
            cls = ""
            self.log('error', "Not implemented".format())

        c = Compressor()

        if args.password:
            if c.compressors[cls].supports_password:
                _password = get_password_twice()
                if not _password:
                    self.log('error', "Passwords did not match, abort")
                    return
                res = c.compress(__sessions__.current.file.path, file_name=__sessions__.current.file.name,
                                 archive_path=store_path, cls_name=cls, password=_password)
            else:
                self.log('warning', "ignoring password (not supported): {}".format(cls))
                res = c.compress(__sessions__.current.file.path, file_name=__sessions__.current.file.name,
                                 archive_path=store_path, cls_name=cls)

        else:
            res = c.compress(__sessions__.current.file.path, file_name=__sessions__.current.file.name,
                             archive_path=store_path, cls_name=cls)

        if res:
            self.log('info', "File archived and exported to {0}".format(c.output_archive_path))
        else:
            self.log('error', "Unable to export file: {0}".format(c.err))
Ejemplo n.º 23
0
 def test_init(self):
     instance = Compressor()
     assert isinstance(instance, Compressor)
     assert isinstance(instance.compressors_by_extension, dict)
     assert isinstance(instance.extensions, (list, KeysView))
     assert "zip" in instance.extensions
Ejemplo n.º 24
0
    def run(self, *args):
        try:
            args = self.parser.parse_args(args)
        except SystemExit:
            return

        # This command requires a session to be opened.
        if not __sessions__.is_set():
            self.log('error', "No open session")
            self.parser.print_usage()
            return

        # Check for valid export path.
        if args.value is None:
            self.parser.print_usage()
            return

        if args.zip and args.sevenzip:
            self.log('error', "Please select either -z or -7 not both, abort")

        store_path = os.path.join(args.value, __sessions__.current.file.name)

        if not args.zip and not args.sevenzip:
            # Abort if the specified path already exists
            if os.path.isfile(store_path):
                self.log('error', "Unable to export file: File exists: '{0}'".format(store_path))
                return

            if not os.path.isdir(args.value):
                try:
                    os.makedirs(args.value)
                except OSError as err:
                    self.log('error', "Unable to export file: {0}".format(err))
                    return

            try:
                shutil.copyfile(__sessions__.current.file.path, store_path)
            except IOError as e:
                self.log('error', "Unable to export file: {0}".format(e))
            else:
                self.log('info', "File exported to {0}".format(store_path))

            return
        elif args.zip:
            cls = "ZipCompressor"

        elif args.sevenzip:
            cls = "SevenZipSystemCompressor"
        else:
            cls = ""
            self.log('error', "Not implemented".format())

        c = Compressor()

        if args.password:
            if c.compressors[cls].supports_password:
                _password = get_password_twice()
                if not _password:
                    self.log('error', "Passwords did not match, abort")
                    return
                res = c.compress(__sessions__.current.file.path, file_name=__sessions__.current.file.name,
                                 archive_path=store_path, cls_name=cls, password=_password)
            else:
                self.log('warning', "ignoring password (not supported): {}".format(cls))
                res = c.compress(__sessions__.current.file.path, file_name=__sessions__.current.file.name,
                                 archive_path=store_path, cls_name=cls)

        else:
            res = c.compress(__sessions__.current.file.path, file_name=__sessions__.current.file.name,
                             archive_path=store_path, cls_name=cls)

        if res:
            self.log('info', "File archived and exported to {0}".format(c.output_archive_path))
        else:
            self.log('error', "Unable to export file: {0}".format(c.err))
Ejemplo n.º 25
0
class CompressorAPIView(ListAPIView):
    """List available Archive Compressors"""
    queryset = [x.summary for (key, x) in Compressor().compressors.items()]
    serializer_class = PassSerializer
Ejemplo n.º 26
0
    def cmd_export(self, *args):
        parser = argparse.ArgumentParser(prog='export', description="Export the current session to file or zip")
        parser.add_argument('-z', '--zip', action='store_true', help="Export session in a zip archive (PW support: No)")
        parser.add_argument('-7', '--sevenzip', action='store_true', help="Export session in a 7z archive (PW support: Yes)")
        parser.add_argument('-p', '--password', action='store_true', help="Protect archive with a password (PW) if supported")
        parser.add_argument('value', help="path or archive name")

        try:
            args = parser.parse_args(args)
        except:
            return

        # This command requires a session to be opened.
        if not __sessions__.is_set():
            self.log('error', "No open session")
            parser.print_usage()
            return

        # Check for valid export path.
        if args.value is None:
            parser.print_usage()
            return

        if args.zip and args.sevenzip:
            self.log('error', "Please select either -z or -7 not both, abort")

        store_path = os.path.join(args.value, __sessions__.current.file.name)

        if not args.zip and not args.sevenzip:
            # Abort if the specified path already exists
            if os.path.isfile(store_path):
                self.log('error', "File at path \"{0}\" already exists, abort".format(args.value))
                return

            if os.path.isfile(args.value):
                self.log('error', "File at path \"{0}\" already exists, abort".format(args.value))
                return

            if not os.path.isdir(args.value):
                os.makedirs(args.value)

            try:
                shutil.copyfile(__sessions__.current.file.path, store_path)
            except IOError as e:
                self.log('error', "Unable to export file: {0}".format(e))
            else:
                self.log('info', "File exported to {0}".format(store_path))

            return
        elif args.zip:
            cls = "ZipCompressor"

        elif args.sevenzip:
            cls = "SevenZipSystemCompressor"
        else:
            cls = ""
            self.log('error', "Not implemented".format())

        c = Compressor()

        if args.password:
            if c.compressors[cls].supports_password:
                _password = get_password_twice()
                if not _password:
                    self.log('error', "Passwords did not match, abort")
                    return
                res = c.compress(__sessions__.current.file.path, file_name=__sessions__.current.file.name,
                                 archive_path=store_path, cls_name=cls, password=_password)
            else:
                self.log('warning', "ignoring password (not supported): {}".format(cls))
                res = c.compress(__sessions__.current.file.path, file_name=__sessions__.current.file.name,
                                 archive_path=store_path, cls_name=cls)

        else:
            res = c.compress(__sessions__.current.file.path, file_name=__sessions__.current.file.name,
                             archive_path=store_path, cls_name=cls)

        if res:
            self.log('info', "File archived and exported to {0}".format(c.output_archive_path))
        else:
            self.log('error', "Unable to export file: {0}".format(c.err))