-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlistFiles
executable file
·132 lines (105 loc) · 5.05 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
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
#!/usr/bin/env python3
from __future__ import print_function
import re
from cloudperm import *
#export processes
import csv
import json
from googleapiclient import discovery
from httplib2 import *
# 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);
import pprint
#command line stuff
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('--show-parents', '-P', action='store_true', help='Show the parent folder(s) of a document')
parser.add_argument('--long-list', '-l', action='store_true', help='Show owner and date modified columns.')
parser.add_argument('--dump-objects', '-J', action='store_true', help='Dump all the data objects in JSON format.')
parser.add_argument('documents', metavar='DocID', type=str, nargs='*', help='Google Document IDs')
args = parser.parse_args()
except ImportError:
args = None
def listFile():
"""List all files and folders recursively under the specified folder id.
"""
#make database
makeDB()
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')
pp = pprint.PrettyPrinter(indent=4)
credentials = get_credentials(args)
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v2', http=http)
tot_files = 0
depth = 0;
if args.recursive:
depth = args.max_depth # defaults to 1000, which probably means a loop.
###########################################################################
#create a dictionary
listFileDict = {}
# # Get our list of file IDs.
key = 0
for start_folder_id in folderids:
#calling walk_folders from cloudperm.py-> executes query through API
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'])
#need this for writing to files, otherwise weird chatacters are created and will make it more work to use data in .csv/.tsv/.json
path = path.replace("▶", ">")
#not sure what this is doing exactly (think managing/formatting data)
if args.show_mime_type:
outputline = "{1:<45} {0} {2:<45} {0} {3}\n".format(fieldsep, fileitem['id'], fileitem['mimeType'], path)
listFileDict[tot_files]= (fileitem['id'], fileitem['mimeType'], path)
if args.long_list:
ownerlist = ",".join(fileitem['ownerNames'])
lastmodified = fileitem['modifiedDate']
outputline = "{1:<45} {0} {2:<25} {0} {3} {0} {4}\n".format(fieldsep, fileitem['id'], ownerlist, lastmodified, path)
listFileDict[tot_files]= (fileitem['id'], ownerlist, lastmodified, path)
else:
outputline = "{1:<45} {0} {2}\n".format(fieldsep, fileitem['id'], path)
listFileDict[tot_files]=(fileitem['id'], path)
sys.stdout.write(outputline)
tot_files+=1
print("Total files: " + str(tot_files))
#write .json file
with open('listFiles.json', 'w') as json_file:
json.dump(listFileDict, json_file)
#csv file create and write
with open('listFiles.csv', 'w') as csv_file:
csvwriter = csv.writer(csv_file)
#create header row???
#write dictionary to .csv
for i in range(len(listFileDict)):
csvwriter.writerow(listFileDict[i])
i+=1
#create and write TSV files
with open('listFiles.tsv', 'w') as tsv_file:
tsvWriter = csv.writer(tsv_file, delimiter='\t')
#create header row
#write dictionary to .tsv
for j in range(len(listFileDict)):
tsvWriter.writerow(listFileDict[j])
j+=1
#things to think about adding:
#create headers
#write out listFileDict data to the csv
#write out total files
if __name__ == '__main__':
main()