This repository has been archived by the owner on Jan 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
gitdriver.py
73 lines (60 loc) · 2.33 KB
/
gitdriver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python
import os
import sys
import argparse
import subprocess
import yaml
from drive import GoogleDrive, DRIVE_RW_SCOPE
def parse_args():
p = argparse.ArgumentParser()
p.add_argument('--config', '-f', default='gd.conf')
p.add_argument('--text', '-T', action='store_const', const='text/plain',
dest='mime_type')
p.add_argument('--html', '-H', action='store_const', const='text/html',
dest='mime_type')
p.add_argument('--mime-type', dest='mime_type')
p.add_argument('--raw', '-R', action='store_true',
help='Download original document if possible.')
p.add_argument('docid')
return p.parse_args()
def main():
opts = parse_args()
if not opts.mime_type:
print "Exactly one mime-type must be given!"
exit(1)
cfg = yaml.load(open(opts.config))
gd = GoogleDrive(
client_id=cfg['googledrive']['client id'],
client_secret=cfg['googledrive']['client secret'],
scopes=[DRIVE_RW_SCOPE],
)
# Establish our credentials.
gd.authenticate()
# Get information about the specified file. This will throw
# 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 __name__ == '__main__':
main()