Exemple #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()) == []
 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"]
 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']
Exemple #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']
 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()) == []
 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']
Exemple #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()) == []
 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()) == []
Exemple #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"]
Exemple #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']
 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()) == []
 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()) == []
 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']
Exemple #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()) == []
Exemple #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']
Exemple #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()) == []
Exemple #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']
 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']
Exemple #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)
 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)
Exemple #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
Exemple #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))
Exemple #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
Exemple #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))
Exemple #25
0
class CompressorAPIView(ListAPIView):
    """List available Archive Compressors"""
    queryset = [x.summary for (key, x) in Compressor().compressors.items()]
    serializer_class = PassSerializer
Exemple #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))