-
Notifications
You must be signed in to change notification settings - Fork 1
Sourcery refactored main branch #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,10 +104,8 @@ def merge(a, b, path=None): | |
merge(a[key], b[key], path + [str(key)]) | ||
elif isinstance(a[key], list) and isinstance(b[key], list): | ||
a[key] = sorted(set(a[key]).union(b[key])) | ||
elif a[key] == b[key]: | ||
pass # same leaf value | ||
else: | ||
raise Exception('Conflict at %s' % '.'.join(path + [str(key)])) | ||
elif a[key] != b[key]: | ||
raise Exception(f"Conflict at {'.'.join(path + [str(key)])}") | ||
else: | ||
a[key] = b[key] | ||
return a | ||
|
@@ -127,7 +125,10 @@ def check_decrypt_tool(): | |
except OSError as e: | ||
print(e, file=sys.stderr) | ||
|
||
print("WARNING: cannot find or use %s executable" % DECRYPT_TOOL, file=sys.stderr) | ||
print( | ||
f"WARNING: cannot find or use {DECRYPT_TOOL} executable", | ||
file=sys.stderr, | ||
) | ||
Comment on lines
-130
to
+131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return False | ||
|
||
|
||
|
@@ -139,10 +140,9 @@ def get_secrets_path(): | |
path = os.path.realpath(path) | ||
if os.path.isdir(path): | ||
return path | ||
else: | ||
print("WARNING: NODE_BUILD_SECRETS defined but not a directory", file=sys.stderr) | ||
print("It must be the path to a local checkout of https://github.com/nodejs-private/secrets", file=sys.stderr) | ||
return None | ||
print("WARNING: NODE_BUILD_SECRETS defined but not a directory", file=sys.stderr) | ||
print("It must be the path to a local checkout of https://github.com/nodejs-private/secrets", file=sys.stderr) | ||
return None | ||
Comment on lines
-142
to
+145
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
path = os.path.realpath('../secrets/build/') | ||
if os.path.isdir(path): | ||
|
@@ -197,7 +197,7 @@ def load_yaml_secrets(file_name): | |
p = subprocess.Popen([DECRYPT_TOOL, "-q", "--decrypt", file_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
(stdout, _) = p.communicate() | ||
if p.returncode != 0: | ||
print("WARNING: cannot load %s" % file_name, file=sys.stderr) | ||
print(f"WARNING: cannot load {file_name}", file=sys.stderr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return None | ||
|
||
return yaml.safe_load(stdout) | ||
|
@@ -210,10 +210,8 @@ def parse_yaml(hosts, config): | |
|
||
for host_types in hosts['hosts']: | ||
for host_type, providers in host_types.items(): | ||
export[host_type] = {} | ||
export[host_type]['hosts'] = [] | ||
|
||
key = '~/.ssh/nodejs_build_%s' % host_type | ||
export[host_type] = {'hosts': []} | ||
key = f'~/.ssh/nodejs_build_{host_type}' | ||
Comment on lines
-213
to
+214
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
export[host_type]['vars'] = { | ||
'ansible_ssh_private_key_file': key | ||
} | ||
|
@@ -225,8 +223,7 @@ def parse_yaml(hosts, config): | |
# some hosts have metadata appended to provider | ||
# which requires underscore | ||
delimiter = "_" if host.count('-') == 3 else "-" | ||
hostname = '{}-{}{}{}'.format(host_type, provider_name, | ||
delimiter, host) | ||
hostname = f'{host_type}-{provider_name}{delimiter}{host}' | ||
|
||
export[host_type]['hosts'].append(hostname) | ||
|
||
|
@@ -235,28 +232,28 @@ def parse_yaml(hosts, config): | |
try: | ||
parsed_host = parse_host(hostname) | ||
for k, v in parsed_host.items(): | ||
hostvars.update({k: v[0] if type(v) is dict else v}) | ||
hostvars[k] = v[0] if type(v) is dict else v | ||
except Exception as e: | ||
raise Exception('Failed to parse host: %s' % e) | ||
raise Exception(f'Failed to parse host: {e}') | ||
|
||
if 'ip' in metadata: | ||
hostvars.update({'ansible_host': metadata['ip']}) | ||
hostvars['ansible_host'] = metadata['ip'] | ||
del metadata['ip'] | ||
|
||
if 'port' in metadata: | ||
hostvars.update({'ansible_port': str(metadata['port'])}) | ||
hostvars['ansible_port'] = str(metadata['port']) | ||
del metadata['port'] | ||
|
||
if 'user' in metadata: | ||
hostvars.update({'ansible_user': metadata['user']}) | ||
hostvars.update({'ansible_become': True}) | ||
hostvars['ansible_user'] = metadata['user'] | ||
hostvars['ansible_become'] = True | ||
del metadata['user'] | ||
|
||
if 'password' in metadata: | ||
hostvars.update({'ansible_password': str(metadata['password'])}) | ||
hostvars['ansible_password'] = str(metadata['password']) | ||
del metadata['password'] | ||
|
||
hostvars.update(metadata) | ||
hostvars |= metadata | ||
|
||
# add specific options from config | ||
for option in filter(lambda s: s.startswith('hosts:'), | ||
|
@@ -265,7 +262,7 @@ def parse_yaml(hosts, config): | |
if option[6:] in hostname: | ||
for o in config.items(option): | ||
# configparser returns tuples of key, value | ||
hostvars.update({o[0]: o[1]}) | ||
hostvars[o[0]] = o[1] | ||
|
||
export['_meta']['hostvars'][hostname] = {} | ||
export['_meta']['hostvars'][hostname].update(hostvars) | ||
|
@@ -278,20 +275,17 @@ def parse_yaml(hosts, config): | |
def parse_host(host): | ||
"""Parses a host and validates it against our naming conventions""" | ||
|
||
hostinfo = dict() | ||
info = host.split('-') | ||
|
||
expected = ['type', 'provider', 'os', 'arch', 'uid'] | ||
|
||
if len(info) != 5: | ||
raise Exception('Host format is invalid: %s,' % host) | ||
|
||
for key, item in enumerate(expected): | ||
hostinfo[item] = has_metadata(info[key]) | ||
raise Exception(f'Host format is invalid: {host},') | ||
|
||
hostinfo = {item: has_metadata(info[key]) for key, item in enumerate(expected)} | ||
for item in ['type', 'provider', 'arch']: | ||
if hostinfo[item] not in valid[item]: | ||
raise Exception('Invalid %s: %s' % (item, hostinfo[item])) | ||
raise Exception(f'Invalid {item}: {hostinfo[item]}') | ||
Comment on lines
-281
to
+288
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
return hostinfo | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,7 +134,7 @@ def main(): | |
|
||
password = remmina_encrypt(remmina_secret, metadata['ansible_password']) | ||
rendered = render_template(hostname, metadata, password) | ||
filename = os.path.join(target_dir, '%s.remmina' % hostname) | ||
filename = os.path.join(target_dir, f'{hostname}.remmina') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
try: | ||
with open(filename, 'w+') as f: | ||
|
@@ -145,7 +145,7 @@ def main(): | |
|
||
os.chmod(filename, 0o600) | ||
|
||
module.exit_json(changed=True, meta=('Updated %s successfully' % target_dir)) | ||
module.exit_json(changed=True, meta=f'Updated {target_dir} successfully') | ||
|
||
|
||
if __name__ == '__main__': | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,10 +103,8 @@ def main(): | |
if not is_templatable(path, contents): | ||
module.fail_json(msg='Your ssh config lacks template stubs. Check README.md for instructions.') | ||
|
||
rendered = '{}{}{}'.format( | ||
pre_match, | ||
render_template(module.params['hostinfo']), | ||
post_match | ||
rendered = ( | ||
f"{pre_match}{render_template(module.params['hostinfo'])}{post_match}" | ||
Comment on lines
-106
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
) | ||
|
||
try: | ||
|
@@ -116,7 +114,7 @@ def main(): | |
except IOError: | ||
module.fail_json(msg='Couldn\'t write to ssh config. Check permissions') | ||
|
||
module.exit_json(changed=True, meta='Updated %s successfully' % path) | ||
module.exit_json(changed=True, meta=f'Updated {path} successfully') | ||
|
||
|
||
if __name__ == '__main__': | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ | |
import geoip2.database | ||
import os | ||
|
||
reader = geoip2.database.Reader(os.path.dirname(os.path.realpath(__file__)) + '/GeoLite2-City.mmdb') | ||
reader = geoip2.database.Reader( | ||
f'{os.path.dirname(os.path.realpath(__file__))}/GeoLite2-City.mmdb') | ||
Comment on lines
-8
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
|
||
logFileWriter = csv.writer(sys.stdout, quoting=csv.QUOTE_MINIMAL) | ||
logFileReader = csv.reader(sys.stdin) | ||
|
@@ -22,8 +23,7 @@ | |
region = "" | ||
|
||
try: | ||
georec = reader.city(row.pop(0)) | ||
if georec: | ||
if georec := reader.city(row.pop(0)): | ||
if georec.country.iso_code: | ||
country = georec.country.iso_code | ||
if georec.subdivisions.most_specific.iso_code: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
merge
refactored with the following changes:remove-pass-elif
)replace-interpolation-with-fstring
)This removes the following comments ( why? ):