-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrm.py
255 lines (196 loc) · 7.67 KB
/
grm.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env python
"""
grm
GDataCopier, http://gdatacopier.googlecode.com/
Copyright 2010 Eternity Technologies.
Distributed under the terms and conditions of the GNU/GPL v3
GDataCopier is free software and comes with absolutely NO WARRANTY. Use
of this software is completely at YOUR OWN RISK.
Version 2.1.2
"""
__version__ = "2.1.2"
__author__ = "Devraj Mukherjee"
"""
Imports the required modules
"""
try:
from optparse import OptionParser
import datetime
import sys
import os
import re
import signal
import getpass
except:
print "grm failed to find some basic python modules, please validate the environment"
exit(1)
try:
import gdata.docs
import gdata.docs.service
except:
print "grm %s requires gdata-python-client v2.0+, fetch from Google at" % __version__
print "<http://code.google.com/p/gdata-python-client/>"
exit(1)
def signal_handler(signal, frame):
print "\n[Interrupted] Bye Bye!"
sys.exit(0)
"""
Validate email address function courtsey using regular expressions
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65215
"""
def is_email(email):
if len(email) > 7:
if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) != None:
return True
return False
"""
Adds a category of documents to the filter
"""
def add_category_filter(document_query, docs_type):
# If the user provided a doctype then add a filter
if docs_type == "docs" or docs_type == "documents":
document_query.categories.append('document')
elif docs_type == "sheets" or docs_type == "spreadsheets":
document_query.categories.append('spreadsheet')
elif docs_type == "slides" or docs_type == "presentation":
document_query.categories.append('presentation')
elif docs_type == "folders":
document_query.categories.append('folder')
elif docs_type == "pdf":
document_query.categories.append('pdf')
"""
If there's a filter for a title then this adds it on
"""
def add_title_match_filter(document_query, name_filter):
# Add title match
if not name_filter == None:
if name_filter[len(name_filter) - 1: len(name_filter)] == "*":
document_query['title-exact'] = 'false'
document_query['title'] = name_filter[:len(name_filter) - 1]
else:
document_query['title-exact'] = 'true'
document_query['title'] = name_filter
"""
Makes a list of all the objects on the server that match the cr
"""
def remove_doc_objects(server_string, options):
username, document_path = server_string.split(':', 1)
# Counters for the uploads
docs_counter = 0
sheets_counter = 0
slides_counter = 0
pdf_counter = 0
if not is_email(username):
print "Usernames must be provided as your full Gmail address, hosted domains included."
sys.exit(2)
docs_type = None
folder_name = None
name_filter = None
doc_param_parts = document_path.split('/')
if len(doc_param_parts) > 1 and not (doc_param_parts[1] == '' or doc_param_parts[1] == '*'):
docs_type = doc_param_parts[1]
if len(doc_param_parts) > 2 and not (doc_param_parts[2] == '' or doc_param_parts[2] == '*'):
folder_name = doc_param_parts[2]
if len(doc_param_parts) > 3 and not (doc_param_parts[3] == '' or doc_param_parts[3] == '*'):
name_filter = doc_param_parts[3]
# Get a handle to the document list service
sys.stdout.write("Logging into Google server as %s ... " % (username))
gd_client = gdata.docs.service.DocsService(source="etk-gdatacopier-v2")
document_query = gdata.docs.service.DocumentQuery()
add_category_filter(document_query, docs_type)
# If the user provided a folder type then add this here
if not folder_name == None and not folder_name == "all":
document_query.AddNamedFolder(username, folder_name)
add_title_match_filter(document_query, name_filter)
try:
# Authenticate to the document service'
gd_client.ClientLogin(username, options.password)
print "done."
sys.stdout.write("Fetching document list feeds from Google servers for %s ... " % (username))
feed = gd_client.Query(document_query.ToUri())
print "done.\n"
for entry in feed.entry:
document_type = entry.GetDocumentType()
# Thanks to http://stackoverflow.com/questions/127803/how-to-parse-iso-formatted-date-in-python
# we are use regular expression to parse RFC3389
updated_time = datetime.datetime(*map(int, re.split('[^\d]', entry.updated.text)[:-1]))
date_string = updated_time.strftime('%b %d %Y %H:%M')
print '%-60s' % (entry.title.text[0:45])
if not options.force:
user_answer = ""
while not user_answer == "NO" and not user_answer.upper() == "YES":
user_answer = raw_input("delete (yes/NO): ")
if user_answer == "": user_answer = "NO"
if user_answer == "NO": continue
try:
gd_client.Delete(entry.GetEditLink().href)
print "DELETED"
except gdata.service.Error:
print "SERVICE ERROR"
except:
print "FAILED"
# Icrease counters
if document_type == "document":
docs_counter = docs_counter + 1
elif document_type == "spreadsheet":
sheets_counter = sheets_counter + 1
elif document_type == "presentation":
slides_counter = slides_counter + 1
elif document_type == "pdf":
pdf_counter = pdf_counter + 1
except gdata.service.BadAuthentication:
print "Failed, Bad Password!"
sys.exit(2)
except gdata.service.Error:
print "Failed!"
sys.exit(2)
except gdata.service.CaptchaRequired:
print "Captcha required, please login using the web interface and try again."
sys.exit(2)
except:
print "Failed."
sys.exit(2)
print "\n%i document(s), %i spreadsheet(s), %i presentation(s), %i pdf(s)" % (docs_counter, sheets_counter, slides_counter, pdf_counter)
"""
Is able to match a remote server directive
"""
def is_remote_server_string(remote_address):
re_remote_address = re.compile(r'[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}:/')
matched_strings = re_remote_address.findall(remote_address)
return len(matched_strings) > 0
def parse_user_input():
usage = "usage: %prog [options] username@domain.com:/[doctype]/[folder]/Title*"
parser = OptionParser(usage)
parser.add_option('-s', '--silent', action = 'store_true', dest = 'silent', default = False,
help = 'decreases verbosity, supresses all messages but summaries and critical errors')
parser.add_option('-p', '--password', dest = 'password',
help = 'password for the user account, use with extreme caution. Could be stored in logs/history')
parser.add_option('-f', '--force', action = 'store_true', dest = 'force', default = False,
help = 'forces delete of document objects, user is not prompted for confirmation')
(options, args) = parser.parse_args()
greet(options)
# arg1 must be a remote server string to fetch document lists
if not len(args) == 1 or (not is_remote_server_string(args[0])):
print "you most provide a remote server address as username@gmail.com:/[doctype]/[folder]/Title*"
exit(1)
# If password not provided as part of the command line arguments, prompt the user
# to enter the password on the command line
if options.password == None:
options.password = getpass.getpass()
remove_doc_objects(args[0], options)
# Prints Greeting
def greet(options):
if not options.silent:
print "grm %s, folder creation utility. Copyright 2010 Eternity Technologies" % __version__
print "Released under the GNU/GPL v3 at <http://gdatacopier.googlecode.com>\n"
# main() is where things come together, this joins all the messages defined above
# these messages must be executed in the defined order
def main():
signal.signal(signal.SIGINT, signal_handler)
parse_user_input() # Check to see we have the right options or exit
# Begin execution of the main method since we are at the bottom of the script
if __name__ == "__main__":
main()
"""
End of Python file
"""