Skip to content

Commit 9ca4436

Browse files
committed
Fix codefactor
1 parent 02f473a commit 9ca4436

3 files changed

Lines changed: 39 additions & 28 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: 11 additions & 10 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(
@@ -94,7 +95,7 @@ def run_command(self, command, sudo=False):
9495
('sudo ' if sudo else '') + command]
9596
logging.info(' '.join(cmd_list))
9697

97-
subprocess.check_call(cmd_list)
98+
subprocess.check_call(cmd_list) # nosec
9899

99100
def scp_to_remote(
100101
self, source_path, dest_path,
@@ -112,7 +113,7 @@ def scp_to_remote(
112113

113114
logging.info(' '.join(cmd_list))
114115

115-
subprocess.check_call(cmd_list)
116+
subprocess.check_call(cmd_list) # nosec
116117

117118

118119
class UrlHelper(object):
@@ -157,14 +158,14 @@ def download_file(url, dest_file='', dest_dir='.'):
157158
raise
158159

159160
logging.info("Downloading to %s from %s", target_path, url)
160-
response = urllib2.urlopen(url)
161+
response = urllib2.urlopen(url) # nosec
161162
chunk_count = 0
162163
with open(target_path, 'wb') as out_file:
163164
while True:
164-
buffer = response.read(chunk)
165-
if not buffer:
165+
buf = response.read(chunk)
166+
if not buf:
166167
break
167-
out_file.write(buffer)
168+
out_file.write(buf)
168169
chunk_count += 1
169170
if chunk_count % 100 == 0:
170171
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)