Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only push objects that do not already exist in the bucket #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions git-fat
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import subprocess
import shlex
import shutil
import itertools
import json
import threading
import time
import collections
Expand Down Expand Up @@ -176,25 +177,54 @@ class GitFat(object):
raise RuntimeError('No rsync.remote in %s' % cfgpath)
return remote, ssh_port, ssh_user, options

def list_objects(self, s3_bucket):
bucket = s3_bucket[5:]
cmd = [
"aws",
"s3api",
"list-objects",
"--bucket",
bucket
]

process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()

if out:
# try to parse list from json
data = json.loads(out)
keys = [key.get('Key') for key in data.get('Contents')]
return keys
else:
print("Failed to list bucket with error: %s" % err)
sys.exit(1)

return []

def get_aws_cmd(self, push, s3_bucket, files):
if not which('aws'):
sys.stderr.write('Could not find aws cli install.\n')
sys.exit(1)

if not s3_bucket.startswith('s3://'):
s3_bucket = "s3://{}".format(s3_bucket)

cmds = []
if push:
self.verbose('Pushing to %s' % (s3_bucket))
remote_files = self.list_objects(s3_bucket)
for file in files:
cmd = [
"aws",
"s3",
"cp",
self.objdir + "/" + file,
s3_bucket + "/" + file
]
cmds.append(cmd)
# only push files that do not exist in the remote
if file not in remote_files:
self.verbose("%s does not exist in remote, will be pushed" % file)
cmd = [
"aws",
"s3",
"cp",
self.objdir + "/" + file,
s3_bucket + "/" + file
]
cmds.append(cmd)
else:
self.verbose('Pulling from %s' % (s3_bucket))
for file in files:
Expand All @@ -206,6 +236,7 @@ class GitFat(object):
self.objdir + "/" + file
]
cmds.append(cmd)

return cmds

def get_rsync_command(self,push,files):
Expand Down