Skip to content
This repository has been archived by the owner on Jan 25, 2019. It is now read-only.

Allow repo update instead of reinit #4

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
71 changes: 45 additions & 26 deletions gitdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ def parse_args():

return p.parse_args()

def commit_revision(gd, opts, rev):
with open('content', 'w') as fd:
if 'exportLinks' in rev and not opts.raw:
# If the file provides an 'exportLinks' dictionary,
# download the requested MIME type.
r = gd.session.get(rev['exportLinks'][opts.mime_type])
elif 'downloadUrl' in rev:
# Otherwise, if there is a downloadUrl, use that.
r = gd.session.get(rev['downloadUrl'])
else:
raise KeyError('unable to download revision')

# Write file content into local file.
for chunk in r.iter_content():
fd.write(chunk)

# Commit changes to repository.
subprocess.call(['git', 'add', 'content'])
subprocess.call(['git', 'commit', '-m',
'revision from %s' % rev['modifiedDate']])


def main():
opts = parse_args()
if not opts.mime_type:
Expand All @@ -41,33 +63,30 @@ def main():
# an exception if the file does not exist.
md = gd.get_file_metadata(opts.docid)

# Initialize the git repository.
print 'Create repository "%(title)s"' % md
subprocess.call(['git','init',md['title']])
os.chdir(md['title'])

# Iterate over the revisions (from oldest to newest).
for rev in gd.revisions(opts.docid):
with open('content', 'w') as fd:
if 'exportLinks' in rev and not opts.raw:
# If the file provides an 'exportLinks' dictionary,
# download the requested MIME type.
r = gd.session.get(rev['exportLinks'][opts.mime_type])
elif 'downloadUrl' in rev:
# Otherwise, if there is a downloadUrl, use that.
r = gd.session.get(rev['downloadUrl'])
else:
raise KeyError('unable to download revision')

# Write file content into local file.
for chunk in r.iter_content():
fd.write(chunk)

# Commit changes to repository.
subprocess.call(['git', 'add', 'content'])
subprocess.call(['git', 'commit', '-m',
'revision from %s' % rev['modifiedDate']])
if os.path.isdir(md['title']):
# Find revision matching last commit and process only following revisions
os.chdir(md['title'])
print 'Update repository "%(title)s"' % md
last_commit_message = subprocess.check_output('git log -n 1 --format=%B', shell=True)
print 'Last commit: ' + last_commit_message + 'Iterating Google Drive revisions:'
revision_matched = False
for rev in gd.revisions(opts.docid):
if revision_matched:
print "New revision: " + rev['modifiedDate']
commit_revision(gd, opts, rev)
if rev['modifiedDate'] in last_commit_message:
print "Found matching revision: " + rev['modifiedDate']
revision_matched = True
print "Repository is up to date."
else:
# Initialize the git repository.
print 'Create repository "%(title)s"' % md
subprocess.call(['git','init',md['title']])
os.chdir(md['title'])

# Iterate over the revisions (from oldest to newest).
for rev in gd.revisions(opts.docid):
commit_revision(gd, opts, rev)
if __name__ == '__main__':
main()