Skip to content

Instantly share code, notes, and snippets.

@ak9999
Last active August 23, 2019 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ak9999/9fc504d6f46abf7d8c77ecf6aaa0e1df to your computer and use it in GitHub Desktop.
Save ak9999/9fc504d6f46abf7d8c77ecf6aaa0e1df to your computer and use it in GitHub Desktop.
from csv import reader
from collections import namedtuple
import logging
import subprocess
# TODO: In the future this script should also possibly generate the data so that no other steps are required.
# E.g.:
# gam all users show filelist id alternateLink query "'user@example.com' in readers" > file.csv
# gam all users show filelist id alternateLink query "'user@example.com' in writers" > file.csv
logging.basicConfig(filename='results.log', filemode='w', level=logging.DEBUG)
class DriveFile(namedtuple('FileBase', 'owner file_id alternateLink')):
@classmethod
# def from_csv(cls, fields):
def from_csv(cls, owner, acl, alternatelink):
# The folllowing line, these three items should be arguments instead
# owner, file_id, alternateLink = fields
return cls(owner, acl, alternatelink)
if __name__ == '__main__':
with open('/path/to/file.csv') as f:
next(f) # skip header
# Get all documents that the user has read access to.
all_items = (DriveFile.from_csv(*fields) for fields in reader(f))
for i in all_items:
# Next print function call is just to make sure we are grabbing the right info
# print(f'Owner: {i.owner}, ID: {i.file_id}')
# The next call to subprocess.run is equivalent to the command below:
# gam user i.owner delete drivefileacl i.file_id user@example.com
# stdout and stderr outputs are piped into the log.
output = subprocess.run(['gam', 'user', f'{i.owner}', 'delete', 'drivefileacl', f'{i.file_id}', 'user@example.com'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
logging.info(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment