Skip to content

Commit f7bc1a7

Browse files
committed
python: make code compatible with Python3.7
* add Pipfile * de-lint with `flake8`
1 parent 3891a43 commit f7bc1a7

File tree

10 files changed

+81
-54
lines changed

10 files changed

+81
-54
lines changed
File renamed without changes.

.flake8

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

.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

Pipfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
ansible = "*"
8+
"flake8" = "*"
9+
"flake8-import-order" = "*"
10+
11+
[dev-packages]
12+
13+
[requires]
14+
python_version = "3.7"

ansible/plugins/inventory/nodejs_yaml.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,33 @@
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', 'ppc64', 'x32', 'x64', 'x86', 's390', 's390x'),
42+
43+
# valid roles - add as necessary
44+
'type': ('infra', 'release', 'test'),
45+
46+
# providers - validated for consistency
47+
'provider': (
48+
'azure', 'digitalocean', 'joyent', 'ibm', 'linuxonecc',
49+
'macstadium', 'marist', 'mininodes', 'msft', 'osuosl',
50+
'rackspace', 'requireio', 'scaleway', 'softlayer', 'voxer',
51+
'packetnet', 'nearform'
52+
)
5553
}
5654
DECRYPT_TOOL = "gpg"
5755
INVENTORY_FILENAME = "inventory.yml"
@@ -97,15 +95,16 @@ def main():
9795
# https://stackoverflow.com/a/7205107
9896
def merge(a, b, path=None):
9997
"merges b into a"
100-
if path is None: path = []
98+
if path is None:
99+
path = []
101100
for key in b:
102101
if key in a:
103102
if isinstance(a[key], dict) and isinstance(b[key], dict):
104103
merge(a[key], b[key], path + [str(key)])
105104
elif isinstance(a[key], list) and isinstance(b[key], list):
106105
a[key] = sorted(set(a[key]).union(b[key]))
107106
elif a[key] == b[key]:
108-
pass # same leaf value
107+
pass # same leaf value
109108
else:
110109
raise Exception('Conflict at %s' % '.'.join(path + [str(key)]))
111110
else:
@@ -167,7 +166,7 @@ def load_yaml_file(file_name):
167166
# get inventory
168167
with open(file_name, 'r') as stream:
169168
try:
170-
hosts = yaml.load(stream)
169+
hosts = yaml.safe_load(stream)
171170

172171
except yaml.YAMLError as exc:
173172
print(exc)
@@ -186,11 +185,11 @@ def load_yaml_secrets(file_name):
186185
print("WARNING: cannot load %s" % file_name, file=sys.stderr)
187186
return None
188187

189-
return yaml.load(stdout)
188+
return yaml.safe_load(stdout)
190189

191190

192191
def parse_yaml(hosts, config):
193-
"""Parses host information from the output of yaml.load"""
192+
"""Parses host information from the output of yaml.safe_load"""
194193

195194
export = {'_meta': {'hostvars': {}}}
196195

@@ -210,7 +209,7 @@ def parse_yaml(hosts, config):
210209

211210
# some hosts have metadata appended to provider
212211
# which requires underscore
213-
delimiter = "_" if host.count('-') is 3 else "-"
212+
delimiter = "_" if host.count('-') == 3 else "-"
214213
hostname = '{}-{}{}{}'.format(host_type, provider_name,
215214
delimiter, host)
216215

@@ -265,7 +264,7 @@ def parse_host(host):
265264

266265
expected = ['type', 'provider', 'os', 'arch', 'uid']
267266

268-
if len(info) is not 5:
267+
if len(info) != 5:
269268
raise Exception('Host format is invalid: %s,' % host)
270269

271270
for key, item in enumerate(expected):
@@ -279,9 +278,11 @@ def parse_host(host):
279278

280279

281280
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"""
281+
"""
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
285+
"""
285286

286287
metadata = info.split('_', 1)
287288

@@ -296,9 +297,8 @@ def has_metadata(info):
296297

297298

298299
if __name__ == "__main__":
299-
parser = argparse.ArgumentParser()
300-
parser.add_argument('--list', action='store_true')
301-
parser.add_argument('--host', action='store')
302-
args = parser.parse_args()
303-
300+
# parser = argparse.ArgumentParser()
301+
# parser.add_argument('--list', action='store_true')
302+
# parser.add_argument('--host', action='store')
303+
# args = parser.parse_args()
304304
main()

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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
with open('out/index.csv') as index:
77
index_csv = filter(lambda line: line, index.read().split('\n'))
88

9+
# noqa
910
with open('out/index.html', 'w') as out:
10-
out.write(
11-
'''
11+
out.write('''
1212
<!DOCTYPE html>
1313
<html>
1414
<head>
@@ -96,6 +96,7 @@
9696
<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>
9797
<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>
9898
</div>'''.format(date, sha, float(jscov), float(cxxcov)))
99+
99100
out.write('''
100101
</div>
101102
</div>

setup/www/host_vars/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
infra-*
1+
infra-*
2+
!node-www.tmpl

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)