forked from deltaray/cloudperm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistFiles
executable file
·84 lines (67 loc) · 3.3 KB
/
listFiles
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
#!/usr/bin/python
from __future__ import print_function
import re
# From https://wiki.python.org/moin/PrintFails
# Python tries to be too pedantic and ends up failing royally.
# So we have to do this so that we can do things like pipe output to other commands.
import sys, codecs, locale;
sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout);
from cloudperm import *
import pprint
try:
import argparse
parser = argparse.ArgumentParser(parents=[cloudperm_argparser])
parser.add_argument('--recursive', '-r', action='store_true', help='Recursively list files inside folders')
parser.add_argument('--max-depth', '-d', type=int, default=1000, help='The maximum depth that recursion can go.')
parser.add_argument('--exclude-folder', '-E', type=str, default=[], action='append', help='Do not decend into the folder specified by the DocID')
parser.add_argument('--show-mime-type', '-M', action='store_true', help='Show column for document MIME type')
parser.add_argument('--long-list', '-l', action='store_true', help='Show owner and date modified columns.')
parser.add_argument('documents', metavar='DocID', type=str, nargs='*', help='Google Document IDs')
args = parser.parse_args()
except ImportError:
args = None
def main():
"""List all files and folders recursively under the specified folder id.
"""
fieldsep = u' \u2588 ' # Unicode Full block
folderids = [];
# Get the arguments for the folders to check or from the config file.
if args.documents:
for folderid in args.documents:
folderids.append(folderid)
else:
folderids.append('root')
# for section in parser.sections(): # Fix this later.
# for name,value in parser.items(section):
# if name == "url":
# folderids.append(value)
pp = pprint.PrettyPrinter(indent=4)
credentials = get_credentials(args)
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v2', http=http)
numberoffiles = 0
depth = 0;
if args.recursive:
depth = args.max_depth # defaults to 1000, which probably means a loop.
# Get our list of file IDs.
for start_folder_id in folderids:
returnedfiles = walk_folders(service, start_folder_id, depth, args.exclude_folder)
if type(returnedfiles) == list:
for fileitem in returnedfiles:
#pp.pprint(fileitem)
path = build_first_path(service,fileitem['id'])
if args.show_mime_type:
outputline = u"{1:<45} {0} {2:<45} {0} {3}\n".format(fieldsep, fileitem['id'], fileitem['mimeType'], path)
if args.long_list:
ownerlist = ",".join(fileitem['ownerNames'])
lastmodified = fileitem['modifiedDate']
outputline = u"{1:<45} {0} {2:<25} {0} {3} {0} {4}\n".format(fieldsep, fileitem['id'], ownerlist, lastmodified, path)
else:
outputline = u"{1:<45} {0} {2}\n".format(fieldsep, fileitem['id'], path)
# Because it's unicode.
#sys.stdout.write(outputline.decode('utf-8'))
sys.stdout.write(outputline)
numberoffiles+=1
print("Total files: " + str(numberoffiles))
if __name__ == '__main__':
main()