-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdriveupdater
executable file
·106 lines (73 loc) · 3.08 KB
/
gdriveupdater
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#! /usr/bin/env python
from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
from optparse import OptionParser
import sys
class GDriveCIUpdater:
def __init__(self, filename, project_name):
reload(sys)
sys.setdefaultencoding('utf8')
self.gauth = GoogleAuth()
self.gauth.CommandLineAuth() # get authenticated
self.drive = self._drive()
self.filename = filename
self.gdrive_filename = "%s.html" % project_name
self.update_file = False # a flag to decide if we need to retrieve an existing file or update a new one
self.file_id = None
self.file_list = self._get_file_list() # all of the users existing GDrive files
self._check_for_file_update()
def _get_file_list(self):
# this returns a list of tuples in 'title,id' format
# it can be used to compare filenames and retrieve the existing file if it's a match
try:
data = list()
raw = self.drive.ListFile({}).GetList()
for i in raw:
data.append((i['title'], i['id']))
return data
except Exception, e:
raise e
def _check_for_file_update(self):
# this will look to see if the desired filename already exists or not
for i in self.file_list:
if i[0] == self.gdrive_filename: # the file already exists
self.update_file = True
self.file_id = i[1]
def _drive(self):
try:
d = GoogleDrive(self.gauth) # Create GoogleDrive instance with authenticated GoogleAuth instance
return d
except Exception, e:
raise e
def _get_file(self):
# this will retrieve an existing file with the right file ID so we can update instead of uploading a copy
# it is called if self.update_file = True
try:
if self.update_file:
f = self.drive.CreateFile({'id': self.file_id})
else:
f = self.drive.CreateFile({'title': self.gdrive_filename})
return f
except Exception, e:
raise e
def upload_file(self):
gfile = self._get_file() # we decide if it's a new file or updating an existing one
gfile.SetContentFile(self.filename)
gfile.Upload() # Upload it
return gfile['id']
def main():
parser = OptionParser(usage="%prog ", version="%prog 0.1")
parser.add_option("-f", "--file", dest="filename", help="The filename you want added to GDrive", metavar="FILENAME")
parser.add_option("-g", "--gdrive_project", dest="project_name", help="The Google Drive name you want the file to have", metavar="PROJECT_NAME")
(options, args) = parser.parse_args()
if options.filename is None:
parser.print_help()
sys.exit(1)
if options.project_name is None:
parser.print_help()
sys.exit(1)
gdrive = GDriveCIUpdater(options.filename, options.project_name)
retval = gdrive.upload_file()
print retval
if __name__ == '__main__':
main()