Skip to content

Commit ef04885

Browse files
committed
Fix codefactor
1 parent 02f473a commit ef04885

3 files changed

Lines changed: 47 additions & 32 deletions

File tree

JenkinsHelper.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@
1313

1414
from typing import List, Any # noqa: F401 # pylint: disable=unused-import
1515
# We need to import 'List' and 'Any' for mypy to work
16-
from ZanataFunctions import UrlHelper, logging_init
16+
from .ZanataFunctions import UrlHelper, logging_init
1717

1818
ZANATA_JENKINS = {
1919
'server': os.environ.get('JENKINS_URL'),
2020
'user': os.environ.get('ZANATA_JENKINS_USER'),
2121
'token': os.environ.get('ZANATA_JENKINS_TOKEN'),
2222
}
23-
assert ZANATA_JENKINS['server'], "Missing environment 'JENKINS_URL'"
24-
assert ZANATA_JENKINS['user'], "Missing environment 'ZANATA_JENKINS_USER'"
25-
assert ZANATA_JENKINS['token'], "Missing environment 'ZANATA_JENKINS_TOKEN'"
23+
24+
if not ZANATA_JENKINS['server']:
25+
raise AssertionError("Missing environment 'JENKINS_URL'")
26+
if not ZANATA_JENKINS['user']:
27+
raise AssertionError("Missing environment 'ZANATA_JENKINS_USER'")
28+
if not ZANATA_JENKINS['token']:
29+
raise AssertionError("Missing environment 'ZANATA_JENKINS_TOKEN'")
2630

2731

2832
class JenkinsJobBuild(object):
@@ -76,7 +80,8 @@ def list_artifacts_related_paths(self, artifact_path_pattern='.*'):
7680
that matches the path pattern"""
7781
if not self.content:
7882
self.load()
79-
assert self.content, "Failed to load build from %s" % self.url
83+
if not self.content:
84+
raise AssertionError("Failed to load build from %s" % self.url)
8085
return map(
8186
lambda y: y['relativePath'],
8287
filter(
@@ -160,7 +165,8 @@ def get_last_successful_build(self):
160165
if not self.content:
161166
self.load()
162167

163-
assert self.content, "Failed to load job from %s" % self.url
168+
if not self.content:
169+
raise AssertionError("Failed to load job from %s" % self.url)
164170
return JenkinsJobBuild(
165171
self,
166172
int(self.get_elem('lastSuccessfulBuild/number')),
@@ -263,5 +269,5 @@ def parse():
263269
if __name__ == '__main__':
264270
logging_init()
265271

266-
args = parse()
272+
args = parse() # pylint: disable=invalid-name
267273
args.func()

ZanataFunctions.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
import os
88
import subprocess # nosec
99
import sys
10-
import urllib2 # noqa: F401 # pylint: disable=unused-import
11-
import urlparse # noqa: F401 # pylint: disable=unused-import
10+
import urllib2 # noqa: F401 # pylint: disable=import-error,unused-import
11+
import urlparse # noqa: F401 # pylint: disable=import-error,unused-import
1212

1313
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
1414
ZANATA_ENV_FILE = os.path.join(SCRIPT_DIR, 'zanata-env.sh')
15+
BASH_CMD = '/bin/bash'
1516

1617

1718
def logging_init(level=logging.INFO):
@@ -26,8 +27,8 @@ def logging_init(level=logging.INFO):
2627
def read_env(filename):
2728
# type (str) -> dict
2829
"""Read environment variables by sourcing a bash file"""
29-
proc = subprocess.Popen(
30-
['bash', '-c',
30+
proc = subprocess.Popen( # nosec
31+
[BASH_CMD, '-c',
3132
"source %s && set -o posix && set" % (filename)],
3233
stdout=subprocess.PIPE)
3334
return {kv[0]: kv[1] for kv in map(
@@ -40,17 +41,21 @@ def read_env(filename):
4041

4142
class HTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler):
4243
"""Handle Basic Authentication"""
43-
def http_error_401(self, req, error_body, code, msg, headers): # noqa: E501 # pylint: disable=unused-argument, too-many-arguments
44+
@staticmethod
45+
def http_error_401(req, error_body, code, msg, headers): # noqa: E501 # pylint: disable=unused-argument, too-many-arguments
4446
"""retry with basic auth when facing a 401"""
4547
host = req.get_host()
4648
realm = None
47-
return self.retry_http_basic_auth(host, req, realm)
49+
return urllib2.HTTPBasicAuthHandler.retry_http_basic_auth(
50+
host, req, realm)
4851

49-
def http_error_403(self, req, error_body, code, msg, headers): # noqa: E501 # pylint: disable=unused-argument,too-many-arguments
52+
@staticmethod
53+
def http_error_403(req, error_body, code, msg, headers): # noqa: E501 # pylint: disable=unused-argument,too-many-arguments
5054
"""retry with basic auth when facing a 403"""
5155
host = req.get_host()
5256
realm = None
53-
return self.retry_http_basic_auth(host, req, realm)
57+
return urllib2.HTTPBasicAuthHandler.retry_http_basic_auth(
58+
host, req, realm)
5459

5560

5661
class SshHost(object):
@@ -94,7 +99,7 @@ def run_command(self, command, sudo=False):
9499
('sudo ' if sudo else '') + command]
95100
logging.info(' '.join(cmd_list))
96101

97-
subprocess.check_call(cmd_list)
102+
subprocess.check_call(cmd_list) # nosec
98103

99104
def scp_to_remote(
100105
self, source_path, dest_path,
@@ -112,7 +117,7 @@ def scp_to_remote(
112117

113118
logging.info(' '.join(cmd_list))
114119

115-
subprocess.check_call(cmd_list)
120+
subprocess.check_call(cmd_list) # nosec
116121

117122

118123
class UrlHelper(object):
@@ -157,14 +162,14 @@ def download_file(url, dest_file='', dest_dir='.'):
157162
raise
158163

159164
logging.info("Downloading to %s from %s", target_path, url)
160-
response = urllib2.urlopen(url)
165+
response = urllib2.urlopen(url) # nosec
161166
chunk_count = 0
162167
with open(target_path, 'wb') as out_file:
163168
while True:
164-
buffer = response.read(chunk)
165-
if not buffer:
169+
buf = response.read(chunk)
170+
if not buf:
166171
break
167-
out_file.write(buffer)
172+
out_file.write(buf)
168173
chunk_count += 1
169174
if chunk_count % 100 == 0:
170175
sys.stderr.write('#')

ZanataWar.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
import argparse
88
import os
9-
import urlparse
10-
from ZanataFunctions import logging_init
11-
from ZanataFunctions import SshHost
12-
from ZanataFunctions import UrlHelper
13-
from JenkinsHelper import JenkinsJobBuild # noqa: E501,F401 # pylint: disable=unused-import
14-
from JenkinsHelper import JenkinsServer
9+
import urlparse # pylint: disable=import-error
10+
# python3-pylint does not do well on importing python2 module
11+
from .ZanataFunctions import logging_init
12+
from .ZanataFunctions import SshHost
13+
from .ZanataFunctions import UrlHelper
14+
from .JenkinsHelper import JenkinsJobBuild # noqa: E501,F401 # pylint: disable=unused-import
15+
from .JenkinsHelper import JenkinsServer
1516

1617

1718
class ZanataWar(object):
@@ -52,7 +53,8 @@ def download(
5253
dest_dir=None):
5354
# type (str, str) -> None
5455
"""Download WAR file from .download_url"""
55-
assert self.download_url, "war download_url is not set."
56+
if not self.download_url:
57+
raise AssertionError("war download_url is not set.")
5658
target_file = dest_file
5759
if not target_file:
5860
url_dict = urlparse.urlparse(self.download_url)
@@ -62,7 +64,7 @@ def download(
6264
UrlHelper.download_file(self.download_url, target_file, target_dir)
6365
self.local_path = os.path.join(target_dir, target_file)
6466

65-
def scp_to_server(
67+
def scp_to_server( # pylint: disable=too-many-arguments
6668
self, dest_host,
6769
dest_path=None,
6870
identity_file=None,
@@ -72,8 +74,10 @@ def scp_to_server(
7274
# type (str, str, str, bool, bool, str) -> None
7375
"""SCP to Zanata server"""
7476
local_path = source_path if source_path else self.local_path
75-
assert local_path, "source_path is missing"
76-
assert os.path.isfile(local_path), local_path + " does not exist"
77+
if not local_path:
78+
raise AssertionError("source_path is missing")
79+
if not os.path.isfile(local_path):
80+
raise AssertionError(local_path + " does not exist")
7781

7882
if dest_path:
7983
target_path = dest_path
@@ -190,5 +194,5 @@ def parse():
190194
# Set logging
191195
logging_init()
192196

193-
args = parse()
197+
args = parse() # pylint: disable=invalid-name
194198
args.func()

0 commit comments

Comments
 (0)