Skip to content

Commit

Permalink
Merge pull request #1 from Dawn-India/sourcery/master
Browse files Browse the repository at this point in the history
Sourcery refactored master branch
  • Loading branch information
Dawn-India authored Jan 13, 2024
2 parents 1683ec6 + 634e52c commit 6a554cb
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 43 deletions.
2 changes: 1 addition & 1 deletion add_to_team_drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
drive = googleapiclient.discovery.build("drive", "v3", credentials=creds)
batch = drive.new_batch_http_request()

aa = glob.glob('%s/*.json' % acc_dir)
aa = glob.glob(f'{acc_dir}/*.json')
pbar = progress.bar.Bar("Readying accounts", max=len(aa))
for i in aa:
ce = json.loads(open(i, 'r').read())['client_email']
Expand Down
4 changes: 2 additions & 2 deletions bot/helper/ext_utils/bot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ def create_help_buttons():
buttons = ButtonMaker()
for name in list(MIRROR_HELP_DICT.keys())[1:]:
buttons.ibutton(name, f"help m {name}")
buttons.ibutton("Close", f"help close")
buttons.ibutton("Close", "help close")
COMMAND_USAGE["mirror"] = [MIRROR_HELP_DICT["main"], buttons.build_menu(3)]
buttons.reset()
for name in list(YT_HELP_DICT.keys())[1:]:
buttons.ibutton(name, f"help yt {name}")
buttons.ibutton("Close", f"help close")
buttons.ibutton("Close", "help close")
COMMAND_USAGE["yt"] = [YT_HELP_DICT["main"], buttons.build_menu(3)]


Expand Down
10 changes: 2 additions & 8 deletions bot/helper/listeners/jdownloader_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,10 @@ async def _jd_listener():
)
except:
continue
finished = []
for pack in packages:
if pack.get("finished", False):
finished.append(pack["uuid"])
finished = [pack["uuid"] for pack in packages if pack.get("finished", False)]
for gid in finished:
if gid in jd_downloads and jd_downloads[gid]["status"] != "done":
is_finished = True
for did in jd_downloads[gid]["ids"]:
if did not in finished:
is_finished = False
is_finished = all(did in finished for did in jd_downloads[gid]["ids"])
if is_finished:
jd_downloads[gid]["status"] = "done"
_onDownloadComplete(gid)
Expand Down
2 changes: 1 addition & 1 deletion bot/modules/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def argUsage(_, query):
await editMessage(message, MIRROR_HELP_DICT[data[2]], button)
elif data[1] == "yt":
buttons = ButtonMaker()
buttons.ibutton("Back", f"help back y")
buttons.ibutton("Back", "help back y")
button = buttons.build_menu()
await editMessage(message, YT_HELP_DICT[data[2]], button)

Expand Down
78 changes: 47 additions & 31 deletions gen_sa_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,23 @@ def _create_accounts(service, project, count):
batch = service.new_batch_http_request(callback=_def_batch_resp)
for _ in range(count):
aid = _generate_id('mfc-')
batch.add(service.projects().serviceAccounts().create(name='projects/' + project, body={'accountId': aid,
'serviceAccount': {
'displayName': aid}}))
batch.add(
service.projects()
.serviceAccounts()
.create(
name=f'projects/{project}',
body={
'accountId': aid,
'serviceAccount': {'displayName': aid},
},
)
)
batch.execute()


# Create accounts needed to fill project
def _create_remaining_accounts(iam, project):
print('Creating accounts in %s' % project)
print(f'Creating accounts in {project}')
sa_count = len(_list_sas(iam, project))
while sa_count != 100:
_create_accounts(iam, project, 100 - sa_count)
Expand All @@ -57,14 +65,14 @@ def _def_batch_resp(id, resp, exception):
if str(exception).startswith('<HttpError 429'):
sleep(sleep_time / 100)
else:
print(str(exception))
print(exception)


# Project Creation Batch Handler
def _pc_resp(id, resp, exception):
global project_create_ops
if exception is not None:
print(str(exception))
print(exception)
else:
for i in resp.values():
project_create_ops.append(i)
Expand Down Expand Up @@ -95,18 +103,19 @@ def _enable_services(service, projects, ste):
batch = service.new_batch_http_request(callback=_def_batch_resp)
for i in projects:
for j in ste:
batch.add(service.services().enable(
name='projects/%s/services/%s' % (i, j)))
batch.add(service.services().enable(name=f'projects/{i}/services/{j}'))
batch.execute()


# List SAs in project
def _list_sas(iam, project):
resp = iam.projects().serviceAccounts().list(
name='projects/' + project, pageSize=100).execute()
if 'accounts' in resp:
return resp['accounts']
return []
resp = (
iam.projects()
.serviceAccounts()
.list(name=f'projects/{project}', pageSize=100)
.execute()
)
return resp['accounts'] if 'accounts' in resp else []


# Create Keys Batch Handler
Expand All @@ -129,21 +138,26 @@ def _create_sa_keys(iam, projects, path):
global current_key_dump
for i in projects:
current_key_dump = []
print('Downloading keys from %s' % i)
print(f'Downloading keys from {i}')
while current_key_dump is None or len(current_key_dump) != 100:
batch = iam.new_batch_http_request(callback=_batch_keys_resp)
total_sas = _list_sas(iam, i)
for j in total_sas:
batch.add(iam.projects().serviceAccounts().keys().create(
name='projects/%s/serviceAccounts/%s' % (i, j['uniqueId']),
body={
'privateKeyType': 'TYPE_GOOGLE_CREDENTIALS_FILE',
'keyAlgorithm': 'KEY_ALG_RSA_2048'
}
))
batch.add(
iam.projects()
.serviceAccounts()
.keys()
.create(
name=f"projects/{i}/serviceAccounts/{j['uniqueId']}",
body={
'privateKeyType': 'TYPE_GOOGLE_CREDENTIALS_FILE',
'keyAlgorithm': 'KEY_ALG_RSA_2048',
},
)
)
batch.execute()
if current_key_dump is None:
print('Redownloading keys from %s' % i)
print(f'Redownloading keys from {i}')
current_key_dump = []
else:
for index, j in enumerate(current_key_dump):
Expand Down Expand Up @@ -204,7 +218,8 @@ def serviceaccountfactory(
if loads(e.content.decode('utf-8'))['error']['status'] == 'PERMISSION_DENIED':
try:
serviceusage.services().enable(
name='projects/%s/services/cloudresourcemanager.googleapis.com' % proj_id).execute()
name=f'projects/{proj_id}/services/cloudresourcemanager.googleapis.com'
).execute()
except HttpError as e:
print(e._get_reason())
input('Press Enter to retry.')
Expand All @@ -213,7 +228,7 @@ def serviceaccountfactory(
if list_sas:
return _list_sas(iam, list_sas)
if create_projects:
print("creat projects: {}".format(create_projects))
print(f"creat projects: {create_projects}")
if create_projects > 0:
current_count = len(_get_projects(cloud))
if current_count + create_projects <= max_projects:
Expand All @@ -237,7 +252,7 @@ def serviceaccountfactory(
ste = selected_projects
elif enable_services == '*':
ste = _get_projects(cloud)
services = [i + '.googleapis.com' for i in services]
services = [f'{i}.googleapis.com' for i in services]
print('Enabling services')
_enable_services(serviceusage, ste, services)
if create_sas:
Expand Down Expand Up @@ -268,7 +283,7 @@ def serviceaccountfactory(
elif delete_sas == '*':
std = _get_projects(cloud)
for i in std:
print('Deleting service accounts in %s' % i)
print(f'Deleting service accounts in {i}')
_delete_sas(iam, i)


Expand Down Expand Up @@ -310,7 +325,7 @@ def serviceaccountfactory(
print('No credentials found at %s. Please enable the Drive API in:\n'
'https://developers.google.com/drive/api/v3/quickstart/python\n'
'and save the json file as credentials.json' % args.credentials)
if len(options) < 1:
if not options:
exit(-1)
else:
print('Select a credentials file below.')
Expand All @@ -324,8 +339,9 @@ def serviceaccountfactory(
if inp in inp_options:
break
args.credentials = inp if inp in options else options[int(inp) - 1]
print('Use --credentials %s next time to use this credentials file.' %
args.credentials)
print(
f'Use --credentials {args.credentials} next time to use this credentials file.'
)
if args.quick_setup:
opt = '~' if args.new_only else '*'
args.services = ['iam', 'drive']
Expand All @@ -352,14 +368,14 @@ def serviceaccountfactory(
if resp:
print('Projects (%d):' % len(resp))
for i in resp:
print(' ' + i)
print(f' {i}')
else:
print('No projects.')
elif args.list_sas:
if resp:
print('Service accounts in %s (%d):' %
(args.list_sas, len(resp)))
for i in resp:
print(' %s (%s)' % (i['email'], i['uniqueId']))
print(f" {i['email']} ({i['uniqueId']})")
else:
print('No service accounts.')

0 comments on commit 6a554cb

Please sign in to comment.