Skip to content

Instantly share code, notes, and snippets.

@smartass08
Last active June 8, 2021 20:21
Show Gist options
  • Save smartass08/380b6ab0f22ff5f2c6247a9e06327b2a to your computer and use it in GitHub Desktop.
Save smartass08/380b6ab0f22ff5f2c6247a9e06327b2a to your computer and use it in GitHub Desktop.
A mount script which will fetch all your TeamDrives using service account and mount it using rclone mount
from __future__ import print_function
from google.oauth2 import service_account
from googleapiclient.discovery import build
import sys
from glob import glob
import os
SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
remote = 'docs:' # Always use --daemon or it may break stuff
folder_location = "/Volumes/MyShit/Mounts"
sa_folder = "accounts"
args = "--daemon"
unmount_method = "umount"
def getaccess():
SERVICE_ACCOUNT_FILE = sys.argv[1]
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('drive', 'v3', credentials=creds)
return service
def folder_check(res):
lol = f"{res}"
if not os.path.exists(lol):
os.makedirs(lol)
return(lol)
else:
return(lol)
def mount(id, name):
folder = folder_check(f"{folder_location}/{name}")
print(folder)
for current_sa in glob("{}/*.json".format(sa_folder)):
print(current_sa)
check_sa = sa_used(current_sa)
if check_sa is False:
f = open("checks.txt", "a")
f.write(f"{current_sa}")
f.close()
break
command = os.system('rclone mount {} "{}" --drive-root-folder-id "{}" --drive-service-account-file "{}" {}'.format(remote, folder, id, current_sa, args))
if command is not 0:
print(f"Error mounting {name} : error code {command}, Retrying")
os.system(f"{unmount_method} {folder}")
os.system('rclone mount {} "{}" --drive-root-folder-id "{}" --drive-service-account-file "{}" {}'.format(remote, folder, id, current_sa, args))
def sa_used(sa):
f = open("checks.txt", "r")
if f.mode == 'r':
contents = f.read()
if sa in contents:
f.close()
return True
else:
f.close()
return False
def loop(service, NPT):
if NPT is not "None":
results = service.drives().list(pageSize=100, pageToken=NPT).execute()
else:
results = service.drives().list(pageSize=100).execute()
count = 1
for elements in results["drives"]:
mount(elements["id"], elements["name"])
print(count)
count = count + 1
if 'nextPageToken' in results:
loop(service, results["nextPageToken"])
def main():
service = getaccess()
if os.path.exists("checks.txt"):
os.remove("checks.txt")
with open('checks.txt', 'a+'):
pass
loop(service, "None")
if __name__ == '__main__':
main()
@smartass08
Copy link
Author

Requirments

Needs rclone
service accounts (ofcoz)

Instructions to use

Edit the script and add any remote drive's name. (This script mounts your TD using different sa)
Change the path to where your accounts folder is and make sure there are enough sa in it. (Each TD will cost 1sa)
Change the path where you want to mount all your drives.
Add custom args if needed.
Run command python3 rclone_mount.py "sa.json" where "sa.json" will be the path to single json.

TO - DO

Add windows support since windhoes has weird file naming so feel free to contact me for testing since I don't have any windows device to test it

@comeonbaibay
Copy link

Thankyou so much

@smartass08
Copy link
Author

Thankyou so much

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment