Skip to content

Commit

Permalink
Improve support for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelAquilina committed Nov 22, 2018
1 parent e7b3e86 commit 4b9d1d6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
22 changes: 14 additions & 8 deletions s4/clients/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ def is_ignored_key(key, ignore_files):
return False


# Use this instead of os.path.join for generating s3 paths
# This is because on Windows the path separator used (\\) does
# not generate correct paths on s3
def s3_path(*args):
return os.path.normpath("/".join(args))


class S3SyncClient(SyncClient):
DEFAULT_IGNORE_FILES = [".index", ".s4lock"]

Expand All @@ -82,10 +89,10 @@ def __repr__(self):
return "S3SyncClient<{}, {}>".format(self.bucket, self.prefix)

def get_uri(self, key=""):
return "s3://{}/{}".format(self.bucket, os.path.join(self.prefix, key))
return "s3://{}/{}".format(self.bucket, s3_path(self.prefix, key))

def index_path(self):
return os.path.join(self.prefix, ".index")
return s3_path(self.prefix, ".index")

@property
def index(self):
Expand All @@ -100,7 +107,7 @@ def index(self, value):
def put(self, key, sync_object, callback=None):
self.boto.upload_fileobj(
Bucket=self.bucket,
Key=os.path.join(self.prefix, key),
Key=s3_path(self.prefix, key),
Fileobj=sync_object.fp,
Callback=callback,
)
Expand All @@ -109,7 +116,7 @@ def put(self, key, sync_object, callback=None):
def get(self, key):
try:
resp = self.boto.get_object(
Bucket=self.bucket, Key=os.path.join(self.prefix, key)
Bucket=self.bucket, Key=s3_path(self.prefix, key)
)
return SyncObject(
resp["Body"],
Expand All @@ -121,8 +128,7 @@ def get(self, key):

def delete(self, key):
resp = self.boto.delete_objects(
Bucket=self.bucket,
Delete={"Objects": [{"Key": os.path.join(self.prefix, key)}]},
Bucket=self.bucket, Delete={"Objects": [{"Key": s3_path(self.prefix, key)}]}
)
return "Deleted" in resp

Expand Down Expand Up @@ -190,7 +196,7 @@ def get_local_keys(self):
def get_real_local_timestamp(self, key):
try:
response = self.boto.head_object(
Bucket=self.bucket, Key=os.path.join(self.prefix, key)
Bucket=self.bucket, Key=s3_path(self.prefix, key)
)
return utils.to_timestamp(response["LastModified"])
except ClientError:
Expand Down Expand Up @@ -243,7 +249,7 @@ def reload_ignore_files(self):
self._ignore_files = copy.copy(self.DEFAULT_IGNORE_FILES)
try:
response = self.boto.get_object(
Bucket=self.bucket, Key=os.path.join(self.prefix, ".syncignore")
Bucket=self.bucket, Key=s3_path(self.prefix, ".syncignore")
)
data = response["Body"].read()
data = data.decode("utf8")
Expand Down
2 changes: 1 addition & 1 deletion tests/clients/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_get_uri(self):
)

client = s3.S3SyncClient(s3_client, "testbucket", "foo/bar")
assert client.get_uri() == "s3://testbucket/foo/bar/"
assert client.get_uri() == "s3://testbucket/foo/bar"
assert client.get_uri("apples.txt") == "s3://testbucket/foo/bar/apples.txt"

@mock_s3
Expand Down
4 changes: 2 additions & 2 deletions tests/commands/test_sync_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def test_all_targets(self, SyncWorker, capsys):
out, err = capsys.readouterr()
assert out == ""
assert err == (
"Syncing bar [/home/mike/barmil/ <=> s3://foobar/barrel/]\n"
"Syncing foo [/home/mike/docs/ <=> s3://foobar/docs/]\n"
"Syncing bar [/home/mike/barmil/ <=> s3://foobar/barrel]\n"
"Syncing foo [/home/mike/docs/ <=> s3://foobar/docs]\n"
)
assert SyncWorker.call_count == 2
2 changes: 1 addition & 1 deletion tests/test_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_repr(self):
)
expected_repr = (
"Resolution<action=CREATE, "
"to=s3://mortybucket/dimensional/portals/, "
"to=s3://mortybucket/dimensional/portals, "
"from=/home/picklerick/, "
"key=foo, timestamp=20023>"
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def test_repr(self):
s3_client = s3.S3SyncClient(None, "burgerbucket", "foozie")
worker = sync.SyncWorker(local_client, s3_client)
assert (
repr(worker) == "SyncWorker</home/bobs/burgers/, s3://burgerbucket/foozie/>"
repr(worker) == "SyncWorker</home/bobs/burgers/, s3://burgerbucket/foozie>"
)


Expand Down

0 comments on commit 4b9d1d6

Please sign in to comment.