Skip to content

Commit 2920347

Browse files
cclaussTrott
authored andcommitted
ansible: improve Python 3 compatibility (#1929)
* ansible: improve Python 3 compatibility * fixup: review comments
1 parent 388e797 commit 2920347

File tree

7 files changed

+61
-46
lines changed

7 files changed

+61
-46
lines changed

.flake8

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[flake8]
2+
exclude=.venv
3+
max-line-length=250
4+
import-order-style=google
5+
ignore=E111

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ setup/*/host_vars/release-*
1212
ansible/host_vars/*
1313
!ansible/host_vars/README.md
1414
!ansible/host_vars/*-template
15+
.venv
16+
Pipfile.lock

ansible/plugins/inventory/nodejs_yaml.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,32 @@
2323
#
2424

2525
from __future__ import print_function
26+
2627
import argparse
27-
try:
28-
import configparser
29-
except ImportError:
30-
import ConfigParser as configparser
31-
try:
32-
from future_builtins import filter # Python 2
33-
except ImportError:
34-
pass # Python 3
3528
import json
36-
import yaml
3729
import os
38-
import sys
3930
import subprocess
31+
import sys
4032

33+
import yaml
34+
try:
35+
import configparser
36+
except ImportError:
37+
import ConfigParser as configparser
4138

4239
valid = {
43-
# taken from nodejs/node.git: ./configure
44-
'arch': ('armv6l', 'armv7l', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc',
45-
'ppc64', 'x32', 'x64', 'x86', 's390', 's390x'),
46-
47-
# valid roles - add as necessary
48-
'type': ('infra', 'release', 'test'),
49-
50-
# providers - validated for consistency
51-
'provider': ('azure', 'digitalocean', 'joyent', 'ibm', 'linuxonecc',
52-
'macstadium', 'marist', 'mininodes', 'msft', 'osuosl',
53-
'rackspace', 'requireio', 'scaleway', 'softlayer', 'voxer',
54-
'packetnet', 'nearform')
40+
# taken from nodejs/node.git: ./configure
41+
'arch': ('armv6l', 'armv7l', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc',
42+
'ppc64', 'x32', 'x64', 'x86', 's390', 's390x'),
43+
44+
# valid roles - add as necessary
45+
'type': ('infra', 'release', 'test'),
46+
47+
# providers - validated for consistency
48+
'provider': ('azure', 'digitalocean', 'joyent', 'ibm', 'linuxonecc',
49+
'macstadium', 'marist', 'mininodes', 'msft', 'osuosl',
50+
'rackspace', 'requireio', 'scaleway', 'softlayer', 'voxer',
51+
'packetnet', 'nearform')
5552
}
5653
DECRYPT_TOOL = "gpg"
5754
INVENTORY_FILENAME = "inventory.yml"
@@ -97,15 +94,15 @@ def main():
9794
# https://stackoverflow.com/a/7205107
9895
def merge(a, b, path=None):
9996
"merges b into a"
100-
if path is None: path = []
97+
path = path or []
10198
for key in b:
10299
if key in a:
103100
if isinstance(a[key], dict) and isinstance(b[key], dict):
104101
merge(a[key], b[key], path + [str(key)])
105102
elif isinstance(a[key], list) and isinstance(b[key], list):
106103
a[key] = sorted(set(a[key]).union(b[key]))
107104
elif a[key] == b[key]:
108-
pass # same leaf value
105+
pass # same leaf value
109106
else:
110107
raise Exception('Conflict at %s' % '.'.join(path + [str(key)]))
111108
else:
@@ -167,7 +164,7 @@ def load_yaml_file(file_name):
167164
# get inventory
168165
with open(file_name, 'r') as stream:
169166
try:
170-
hosts = yaml.load(stream)
167+
hosts = yaml.safe_load(stream)
171168

172169
except yaml.YAMLError as exc:
173170
print(exc)
@@ -186,11 +183,11 @@ def load_yaml_secrets(file_name):
186183
print("WARNING: cannot load %s" % file_name, file=sys.stderr)
187184
return None
188185

189-
return yaml.load(stdout)
186+
return yaml.safe_load(stdout)
190187

191188

192189
def parse_yaml(hosts, config):
193-
"""Parses host information from the output of yaml.load"""
190+
"""Parses host information from the output of yaml.safe_load"""
194191

195192
export = {'_meta': {'hostvars': {}}}
196193

@@ -279,9 +276,11 @@ def parse_host(host):
279276

280277

281278
def has_metadata(info):
282-
"""Checks for metadata in variables. These are separated from the "key"
283-
metadata by underscore. Not used anywhere at the moment for anything
284-
other than descriptiveness"""
279+
"""
280+
Checks for metadata in variables. These are separated from the "key"
281+
metadata by underscore. Not used anywhere at the moment for anything
282+
other than descriptiveness
283+
"""
285284

286285
metadata = info.split('_', 1)
287286

ansible/plugins/library/remmina_config.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@
2323
#
2424

2525
from __future__ import print_function
26-
from ansible.module_utils.basic import *
27-
from jinja2 import Environment
26+
27+
import base64
2828
import os
29+
30+
from ansible.module_utils.basic import AnsibleModule
31+
from Crypto.Cipher import DES3
32+
from jinja2 import Environment
2933
try:
30-
import configparser # Python 3
34+
import configparser # Python 3
3135
except ImportError:
32-
import ConfigParser as configparser # Python 2
33-
import base64
34-
from Crypto.Cipher import DES3
36+
import ConfigParser as configparser # Python 2
3537

3638

3739
host_template = \

ansible/plugins/library/ssh_config.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
# IN THE SOFTWARE.
2323
#
2424

25-
from ansible.module_utils.basic import *
26-
from jinja2 import Environment
2725
import os
2826
import re
2927

28+
from ansible.module_utils.basic import AnsibleModule
29+
from jinja2 import Environment
30+
31+
3032
pre_match = '# begin: node.js template'
3133
post_match = '# end: node.js template'
3234
match = re.compile(r'^' + re.escape(pre_match) + '(.*)' + re.escape(post_match),
@@ -99,8 +101,7 @@ def main():
99101
path)
100102

101103
if not is_templatable(path, contents):
102-
module.fail_json(msg='Your ssh config lacks template stubs. ' +
103-
'Check README.md for instructions.')
104+
module.fail_json(msg='Your ssh config lacks template stubs. Check README.md for instructions.')
104105

105106
rendered = '{}{}{}'.format(
106107
pre_match,

jenkins/scripts/coverage/generate-index-html.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
index_csv = filter(lambda line: line, index.read().split('\n'))
88

99
with open('out/index.html', 'w') as out:
10-
out.write(
11-
'''
10+
out.write('''
1211
<!DOCTYPE html>
1312
<html>
1413
<head>
@@ -28,6 +27,7 @@
2827
<link rel="stylesheet" href="https://nodejs.org/layouts/css/styles.css" media="all">
2928
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600">
3029
<style>
30+
#logo { margin-bottom: 1rem; }
3131
main { margin-bottom: 2rem; }
3232
.table-header,
3333
.table-row {
@@ -64,6 +64,11 @@
6464
</style>
6565
</head>
6666
<body>
67+
<header>
68+
<div class="container" id="logo">
69+
<img src="https://nodejs.org/static/images/logos/nodejs-new-white-pantone.png" alt="node.js">
70+
</div>
71+
</header>
6772
<div id="main">
6873
<div class="container">
6974
<h1>Node.js Nightly Code Coverage</h1>
@@ -90,6 +95,7 @@
9095
<div><div class="cell-header">JS Coverage</div><div class="cell-value"><a href="coverage-{1}/index.html">{2:05.2f}&nbsp;%</a></div></div>
9196
<div><div class="cell-header">C++ Coverage</div><div class="cell-value"><a href="coverage-{1}/cxxcoverage.html">{3:05.2f}&nbsp;%</a></div></div>
9297
</div>'''.format(date, sha, float(jscov), float(cxxcov)))
98+
9399
out.write('''
94100
</div>
95101
</div>

setup/www/tools/metrics/country-lookup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#!/usr/bin/env python
22

3-
import sys
43
import csv
5-
import geoip2.database
64
import os
5+
import sys
6+
7+
import geoip2.database
78

89
reader = geoip2.database.Reader(os.path.dirname(os.path.realpath(__file__)) + '/GeoLite2-City.mmdb')
910

@@ -28,11 +29,10 @@
2829
country = georec.country.iso_code
2930
if georec.subdivisions.most_specific.iso_code:
3031
region = georec.subdivisions.most_specific.iso_code
31-
except:
32+
except Exception:
3233
pass
3334

3435
row.insert(1, country.encode('utf-8'))
3536
row.insert(2, region.encode('utf-8'))
3637

3738
logFileWriter.writerow(row)
38-

0 commit comments

Comments
 (0)