Skip to content

Commit 443956a

Browse files
author
cody
committed
update
1 parent f55fe95 commit 443956a

File tree

3 files changed

+44
-40
lines changed

3 files changed

+44
-40
lines changed

add_to_google_group.py

-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
# group.members().insert(groupKey=gaddr, body=body).execute()
7070

7171
pbar.next()
72-
7372
pbar.finish()
7473
print('Adding...')
7574
batch.execute()

add_to_team_drive.py

+42-37
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,71 @@
77

88
stt = time.time()
99

10-
parse = argparse.ArgumentParser(description='A tool to add service accounts to a shared drive from a folder containing credential files.')
11-
parse.add_argument('--path','-p',default='accounts',help='Specify an alternative path to the service accounts folder.')
12-
parse.add_argument('--credentials','-c',default='./credentials.json',help='Specify the relative path for the credentials file.')
13-
parse.add_argument('--yes','-y',default=False,action='store_true',help='Skips the sanity prompt.')
10+
parse = argparse.ArgumentParser(
11+
description='A tool to add service accounts to a shared drive from a folder containing credential files.')
12+
parse.add_argument('--path', '-p', default='accounts',
13+
help='Specify an alternative path to the service accounts folder.')
14+
parse.add_argument('--credentials', '-c', default='./credentials.json',
15+
help='Specify the relative path for the credentials file.')
16+
parse.add_argument('--yes', '-y', default=False, action='store_true', help='Skips the sanity prompt.')
1417
parsereq = parse.add_argument_group('required arguments')
15-
parsereq.add_argument('--drive-id','-d',help='The ID of the Shared Drive.',required=True)
18+
parsereq.add_argument('--drive-id', '-d', help='The ID of the Shared Drive.', required=True)
1619

1720
args = parse.parse_args()
1821
acc_dir = args.path
1922
did = args.drive_id
2023
credentials = glob.glob(args.credentials)
2124

2225
try:
23-
open(credentials[0],'r')
24-
print('>> Found credentials.')
26+
open(credentials[0], 'r')
27+
print('>> Found credentials.')
2528
except IndexError:
26-
print('>> No credentials found.')
27-
sys.exit(0)
28-
29+
print('>> No credentials found.')
30+
sys.exit(0)
31+
2932
if not args.yes:
30-
# input('Make sure the following client id is added to the shared drive as Manager:\n' + json.loads((open(credentials[0],'r').read()))['installed']['client_id'])
31-
input('>> Make sure the **Google account** that has generated credentials.json\n is added into your Team Drive (shared drive) as Manager\n>> (Press any key to continue)')
33+
# input('Make sure the following client id is added to the shared drive as Manager:\n' + json.loads((open(
34+
# credentials[0],'r').read()))['installed']['client_id'])
35+
input('>> Make sure the **Google account** that has generated credentials.json\n is added into your Team Drive '
36+
'(shared drive) as Manager\n>> (Press any key to continue)')
3237

3338
creds = None
3439
if os.path.exists('token.pickle'):
35-
with open('token.pickle', 'rb') as token:
36-
creds = pickle.load(token)
40+
with open('token.pickle', 'rb') as token:
41+
creds = pickle.load(token)
3742
# If there are no (valid) credentials available, let the user log in.
3843
if not creds or not creds.valid:
39-
if creds and creds.expired and creds.refresh_token:
40-
creds.refresh(Request())
41-
else:
42-
flow = InstalledAppFlow.from_client_secrets_file(credentials[0], scopes=[
43-
'https://www.googleapis.com/auth/admin.directory.group',
44-
'https://www.googleapis.com/auth/admin.directory.group.member'
45-
])
46-
# creds = flow.run_local_server(port=0)
47-
creds = flow.run_console()
48-
# Save the credentials for the next run
49-
with open('token.pickle', 'wb') as token:
50-
pickle.dump(creds, token)
44+
if creds and creds.expired and creds.refresh_token:
45+
creds.refresh(Request())
46+
else:
47+
flow = InstalledAppFlow.from_client_secrets_file(credentials[0], scopes=[
48+
'https://www.googleapis.com/auth/admin.directory.group',
49+
'https://www.googleapis.com/auth/admin.directory.group.member'
50+
])
51+
# creds = flow.run_local_server(port=0)
52+
creds = flow.run_console()
53+
# Save the credentials for the next run
54+
with open('token.pickle', 'wb') as token:
55+
pickle.dump(creds, token)
5156

5257
drive = googleapiclient.discovery.build("drive", "v3", credentials=creds)
5358
batch = drive.new_batch_http_request()
5459

5560
aa = glob.glob('%s/*.json' % acc_dir)
56-
pbar = progress.bar.Bar("Readying accounts",max=len(aa))
61+
pbar = progress.bar.Bar("Readying accounts", max=len(aa))
5762
for i in aa:
58-
ce = json.loads(open(i,'r').read())['client_email']
59-
batch.add(drive.permissions().create(fileId=did, supportsAllDrives=True, body={
60-
"role": "fileOrganizer",
61-
"type": "user",
62-
"emailAddress": ce
63-
}))
64-
pbar.next()
63+
ce = json.loads(open(i, 'r').read())['client_email']
64+
batch.add(drive.permissions().create(fileId=did, supportsAllDrives=True, body={
65+
"role": "fileOrganizer",
66+
"type": "user",
67+
"emailAddress": ce
68+
}))
69+
pbar.next()
6570
pbar.finish()
6671
print('Adding...')
6772
batch.execute()
6873

6974
print('Complete.')
70-
hours, rem = divmod((time.time() - stt),3600)
71-
minutes, sec = divmod(rem,60)
72-
print("Elapsed Time:\n{:0>2}:{:0>2}:{:05.2f}".format(int(hours),int(minutes),sec))
75+
hours, rem = divmod((time.time() - stt), 3600)
76+
minutes, sec = divmod(rem, 60)
77+
print("Elapsed Time:\n{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), sec))

gen_sa_accounts.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ def serviceaccountfactory(
203203
selected_projects = nprjs
204204
else:
205205
sys.exit('No, you cannot create %d new project (s).\n'
206-
'Please reduce value of --quick-setup or delete existing project (%d already).\n'
207-
'Remember that you can totally create %d projects.\n' % (create_projects, current_count, max_projects))
206+
'Please reduce value of --quick-setup.\n'
207+
'Remember that you can totally create %d projects (%d already).\n' % (create_projects, max_projects, current_count))
208208
else:
209209
print('Please specify a number larger than 0.')
210210
if enable_services:

0 commit comments

Comments
 (0)