def test_7z(self):
        self.assertImportFile('some file.1.txt.zip', 'test 7z.txt.zip', 'test 7z.txt')

        file_name = import_file(self._file('some folder.tar.gz'), 'some folder.tar.gz')
        assert file_name == 'some folder'
        assert os.path.isdir('some folder'), "directory not found"
        assert os.path.exists('some folder.tar.gz'), "file not found"
        shutil.rmtree('some folder')
        os.remove('some folder.tar.gz')

        file_name = import_file(
            self._file('some folder.tar.gz'),
            'some folder.tar.gz',
            ImportedFormat.COMPRESSED,
        )
        assert file_name == 'some folder.tar.gz'
        assert not os.path.isdir('some folder'), "directory should not exist"
        assert os.path.exists('some folder.tar.gz'), "file not found"

        file_name = import_file(self._file('some folder 1.zip'), 'some folder 1.zip')
        assert file_name == 'some folder 1'
        assert os.path.isdir('some folder 1'), "directory not found"
        assert os.path.exists('some folder 1.tar.gz'), "file not found"
        shutil.rmtree('some folder 1')
        os.remove('some folder 1.tar.gz')

        file_name = import_file(
            self._file('some folder 1.zip'),
            'some folder 1.zip',
            ImportedFormat.COMPRESSED,
        )
        assert file_name == 'some folder 1.tar.gz'
        assert not os.path.isdir('some folder 1'), "directory should not exist"
        assert os.path.exists('some folder 1.tar.gz'), "file not found"
    def test_uncompressed_url(self):
        responses.add(
            responses.GET, 'https://testurl/someslug', status=200, body='some text'
        )

        import_file('https://testurl/someslug', 'test uncompressed.txt')
        assert os.path.exists('test uncompressed.txt'), "file not found"
        assert os.path.exists('test uncompressed.txt.gz'), "file not found"
    def test_gz_corrupted(self):
        with self.assertRaises(
            ValueError, msg='invalid gzip file format: corrupted.gz'
        ):
            import_file(self._file('corrupted.gz'), 'corrupted.gz')

        with self.assertRaises(
            ValueError, msg='invalid gzip file format: corrupted.gz'
        ):
            import_file(
                self._file('corrupted.gz'), 'corrupted.gz', ImportedFormat.COMPRESSED
            )
    def test_gz_url(self):
        # Return gzipped file
        responses.add(
            responses.GET,
            'https://testurl/someslug',
            status=200,
            body=bytes.fromhex(
                '1f8b0808a6cea15c0003736f6d652066696c652e312e747874002bcecf4d'
                '552849ad28e10200bce62c190a000000'
            ),
        )

        import_file('https://testurl/someslug', 'test uncompressed.txt.gz')
        assert os.path.exists('test uncompressed.txt'), "file not found"
        assert os.path.exists('test uncompressed.txt.gz'), "file not found"
    def test_7z_url(self):
        # Return zipped file
        responses.add(
            responses.GET,
            'http://testurl/someslug',
            status=200,
            body=bytes.fromhex(
                '504b03041400080008000c5b814e0000000000000000000000000f001000'
                '736f6d652066696c652e312e74787455580c009fdaa15cc7d8a15cf50114'
                '002bcecf4d552849ad28e10200504b0708bce62c190c0000000a00000050'
                '4b010215031400080008000c5b814ebce62c190c0000000a0000000f000c'
                '000000000000000040a48100000000736f6d652066696c652e312e747874'
                '555808009fdaa15cc7d8a15c504b05060000000001000100490000005900'
                '00000000'
            ),
        )

        import_file('http://testurl/someslug', 'test uncompressed.txt.zip')
        assert os.path.exists('test uncompressed.txt'), "file not found"
        assert os.path.exists('test uncompressed.txt.gz'), "file not found"
    def assertImportFile(self, src, dst, returned_name):
        # Test both
        src = self._file(src)
        file_name = import_file(src, dst)
        assert file_name == returned_name
        assert os.path.exists(returned_name), "file not found"
        assert os.path.exists(returned_name + '.gz'), "file not found"
        os.remove(returned_name)
        os.remove(returned_name + '.gz')

        # Test extracted
        file_name = import_file(src, dst, ImportedFormat.EXTRACTED)
        assert file_name == returned_name
        assert os.path.exists(returned_name), "file not found"
        assert not os.path.exists(returned_name + '.gz'), "file should not exist"
        os.remove(returned_name)

        # Test compressed
        file_name = import_file(src, dst, ImportedFormat.COMPRESSED)
        assert file_name == returned_name + '.gz'
        assert os.path.exists(returned_name + '.gz'), "file not found"
        assert not os.path.exists(returned_name), "file should not exist"
        os.remove(returned_name + '.gz')
Beispiel #7
0
    def import_file(self, imported_format=None, progress_from=0.0, progress_to=None):
        """Import field source file to working directory.

        :param imported_format: Import file format (extracted, compressed or both)
        :param progress_from: Initial progress value
        :param progress_to: Final progress value
        :return: Destination file path (if extracted and compressed, extracted path given)
        """
        if not hasattr(resolwe_runtime_utils, "import_file"):
            raise RuntimeError("Requires resolwe-runtime-utils >= 2.0.0")

        if imported_format is None:
            imported_format = resolwe_runtime_utils.ImportedFormat.BOTH

        return resolwe_runtime_utils.import_file(
            src=self.file_temp,
            file_name=self.path,
            imported_format=imported_format,
            progress_from=progress_from,
            progress_to=progress_to,
        )
Beispiel #8
0
    def import_file(self, imported_format=None, progress_from=0.0, progress_to=None):
        """Import field source file to working directory.

        :param imported_format: Import file format (extracted, compressed or both)
        :param progress_from: Initial progress value
        :param progress_to: Final progress value
        :return: Destination file path (if extracted and compressed, extracted path given)
        """
        if not hasattr(resolwe_runtime_utils, 'import_file'):
            raise RuntimeError('Requires resolwe-runtime-utils >= 2.0.0')

        if imported_format is None:
            imported_format = resolwe_runtime_utils.ImportedFormat.BOTH

        return resolwe_runtime_utils.import_file(
            src=self.file_temp,
            file_name=self.path,
            imported_format=imported_format,
            progress_from=progress_from,
            progress_to=progress_to
        )
 def test_invalid_url(self):
     with self.assertRaises(requests.exceptions.ConnectionError):
         import_file('http://testurl/someslug', 'test uncompressed.txt.zip')
 def test_7z_corrupted(self):
     with self.assertRaises(ValueError, msg='failed to extract file: corrupted.zip'):
         import_file(self._file('corrupted.zip'), 'corrupted.zip')