Beispiel #1
0
    def test_temp_url(self):
        basic_file = 'test.txt'
        complex_file = 'my test?file=special_chars.txt'
        with NamedTemporaryDirectory(change_dir=True) as tmp_d:
            nested_tmp_dir = stor.join(tmp_d, 'tmp')
            os.mkdir(nested_tmp_dir)
            basic_file_p = stor.join(nested_tmp_dir, basic_file)
            complex_file_p = stor.join(nested_tmp_dir, 'my test?file=special_chars.txt')

            with stor.open(basic_file_p, 'w') as f:
                f.write('basic test')
            with stor.open(complex_file_p, 'w') as f:
                f.write('complex test')

            self.test_container.upload(['.'])

        with NamedTemporaryDirectory(change_dir=True) as tmp_d:
            basic_obj = stor.Path(
                stor.join(self.test_container, 'tmp', basic_file))
            basic_temp_url = basic_obj.temp_url(inline=False, filename=basic_file)
            r = requests.get(basic_temp_url)
            self.assertEquals(r.content, 'basic test')
            self.assertEquals(r.headers['Content-Disposition'],
                              'attachment; filename="test.txt"; filename*=UTF-8\'\'test.txt')

            complex_obj = stor.Path(
                stor.join(self.test_container, 'tmp', complex_file))
            complex_temp_url = complex_obj.temp_url(inline=False, filename=complex_file)
            r = requests.get(complex_temp_url)
            self.assertEquals(r.content, 'complex test')
            self.assertEquals(r.headers['Content-Disposition'],
                              'attachment; filename="my test%3Ffile%3Dspecial_chars.txt"; filename*=UTF-8\'\'my%20test%3Ffile%3Dspecial_chars.txt')  # noqa
Beispiel #2
0
    def test_open_works(self):
        with tempfile.NamedTemporaryFile() as f:
            p = stor.Path(f.name).open()
            p.close()

        with tempfile.NamedTemporaryFile() as f:
            p = stor.open(f.name)
            p.close()
Beispiel #3
0
def s3_to_swift(s3_path):
    """S3 Cloud-Sync style path to SwiftStack Path

    Args:
        s3_path (str|Path): path to convert
    Returns:
        SwiftPath: the converted path
    """
    return stor.join('swift://', *stor.Path(s3_path).resource.split('/')[1:])
Beispiel #4
0
    def test_metadata_pulling(self):
        file_in_folder = stor.join(self.test_container, 'somefile.svg')
        with stor.open(file_in_folder, 'w') as fp:
            fp.write('12345\n')

        self.assertEqual(stor.getsize(file_in_folder), 6)
        stat_data = stor.Path(file_in_folder).stat()
        self.assertIn('Content-Type', stat_data)
        self.assertEqual(stat_data['Content-Type'], 'image/svg+xml')
Beispiel #5
0
def _convert_swiftstack(path, bucket=None):
    path = stor.Path(path)
    if utils.is_swift_path(path):
        if not bucket:
            # TODO (jtratner): print help here
            raise ValueError('--bucket is required for swift paths')
        return swiftstack.swift_to_s3(path, bucket=bucket)
    elif utils.is_s3_path(path):
        return swiftstack.s3_to_swift(path)
    else:
        raise ValueError("invalid path for conversion: '%s'" % path)
Beispiel #6
0
    def test_invalid_open(self):
        pth = stor.join(self.drive, 'B/C/D')
        with self.assertRaisesRegexp(ValueError, 'mode'):
            # keep reference here
            f = stor.open(pth, 'invalid')  # noqa
            assert False, 'should error before this'  # pragma: no cover

        with self.assertRaisesRegexp(ValueError, 'mode'):
            with stor.open(pth, 'invalid'):
                assert False, 'should error before this'  # pragma: no cover

        with self.assertRaisesRegexp(ValueError, 'mode'):
            with stor.Path(pth).open('invalid'):
                assert False, 'should error before this'  # pragma: no cover
Beispiel #7
0
    def test_joinpath_w_resource(self):
        f = DXPath('dx://project:/dir')
        assert f.joinpath("file") == DXPath('dx://project:/dir/file')
        assert f.joinpath("/dir") == stor.Path('/dir')

        f = DXPath('dx://project-123456789012345678901234:dir/dir2')
        assert f.joinpath("file") == DXPath(
            'dx://project-123456789012345678901234:/dir/dir2/file')

        f = DXPath(
            'dx://project-123456789012345678901234:/file-123456789012345678901234'
        )
        with pytest.raises(ValueError, match='ambiguous'):
            f.joinpath('asd')
Beispiel #8
0
def swift_to_s3(swift_path, bucket):
    """SwiftStack Swift Path to S3 Cloud-Shunt Path

    Args:
        swift_path (str|Path): path to convert
        bucket (str): name of S3 bucket
    Returns:
        S3Path: the converted path

    See https://www.swiftstack.com/docs/admin/cluster_management/cloud_sync.html#swift-object-representation-in-s3 for details
    """  # noqa
    if not bucket:
        raise TypeError('bucket is required')
    swift_path = stor.Path(swift_path)
    h = hashlib.md5((u'%s/%s' % (swift_path.tenant, swift_path.container)
                     ).encode("utf8")).hexdigest()
    prefix = hex(int(h, 16) % 16**6).lstrip('0x').rstrip('L')
    pth = stor.join('s3://%s' % bucket, prefix, swift_path.tenant, swift_path.container)
    if swift_path.resource:
        pth = stor.join(pth, swift_path.resource)
    return pth
 def test_encoding_typeerror_py2(self):
     test_file = self.test_dir / 'test_file.txt'
     with pytest.raises(TypeError, regex='encoding'):
         stor.open(test_file, mode='r', encoding='utf-8')
     with pytest.raises(TypeError, regex='encoding'):
         stor.Path(test_file).open(mode='r', encoding='utf-8')
Beispiel #10
0
 def test_s3_returned(self):
     p = stor.Path('s3://my/s3/path')
     self.assertTrue(isinstance(p, S3Path))
Beispiel #11
0
def _to_url(path):
    if stor.is_filesystem_path(path):
        raise ValueError('must be swift or s3 path')
    return stor.Path(path).to_url()
Beispiel #12
0
def _completions(**kwargs):
    with (stor.Path(__file__).parent / 'stor-completion.bash').open('r') as fp:
        return fp.read()