python: fix flake8 warnings and format with black
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Ic54f4a3ecf328b0d2c65af5cfd9eb2048d671ad5
diff --git a/scripts/get_unit_test_report.py b/scripts/get_unit_test_report.py
index 1c46b78..d4132c8 100755
--- a/scripts/get_unit_test_report.py
+++ b/scripts/get_unit_test_report.py
@@ -23,45 +23,67 @@
# https://github.com/openbmc/phosphor-sel-logger.git;protocol=git NO
# ***********************************OUTPUT***********************************
#
-# Other outputs and errors are redirected to output.log and debug.log in target_dir.
+# Other outputs and errors are redirected to output.log and debug.log in
+# target_dir.
import argparse
import logging
import os
import re
-import requests
import shutil
-import sys
import subprocess
+import requests
+
# Repo list not expected to contain UT. Will be moved to a file in future.
-skip_list = ["openbmc-tools", "inarp", "openbmc", "openbmc.github.io",
- "phosphor-ecc", "phosphor-pcie-presence", "phosphor-u-boot-env-mgr",
- "rrd-ipmi-blob", "librrdplus", "openpower-inventory-upload",
- "openpower-logging", "openpower-power-control", "docs",
- "openbmc-test-automation", "openbmc-build-scripts", "skeleton",
- "linux",
- # Not active, expected to be archived soon.
- "ibm-pldm-oem"]
+skip_list = [
+ "openbmc-tools",
+ "inarp",
+ "openbmc",
+ "openbmc.github.io",
+ "phosphor-ecc",
+ "phosphor-pcie-presence",
+ "phosphor-u-boot-env-mgr",
+ "rrd-ipmi-blob",
+ "librrdplus",
+ "openpower-inventory-upload",
+ "openpower-logging",
+ "openpower-power-control",
+ "docs",
+ "openbmc-test-automation",
+ "openbmc-build-scripts",
+ "skeleton",
+ "linux",
+ # Not active, expected to be archived soon.
+ "ibm-pldm-oem",
+]
# Create parser.
-text = '''%(prog)s target_dir [url_file]
+text = """%(prog)s target_dir [url_file]
Example usages:
get_unit_test_report.py target_dir
-get_unit_test_report.py target_dir repositories.txt'''
+get_unit_test_report.py target_dir repositories.txt"""
-parser = argparse.ArgumentParser(usage=text,
- description="Script generates the unit test coverage report")
-parser.add_argument("target_dir", type=str,
- help='''Name of a non-existing directory in pwd to store all
- cloned repos, logs and UT reports''')
-parser.add_argument("url_file", type=str, nargs='?',
- help='''Text file containing url of repositories.
- By using this argument, the user can get a report only for
- specific repositories given in the file.
- Refer ./scripts/repositories.txt''')
+parser = argparse.ArgumentParser(
+ usage=text, description="Script generates the unit test coverage report"
+)
+parser.add_argument(
+ "target_dir",
+ type=str,
+ help="""Name of a non-existing directory in pwd to store all
+ cloned repos, logs and UT reports""",
+)
+parser.add_argument(
+ "url_file",
+ type=str,
+ nargs="?",
+ help="""Text file containing url of repositories.
+ By using this argument, the user can get a report only for
+ specific repositories given in the file.
+ Refer ./scripts/repositories.txt""",
+)
args = parser.parse_args()
input_urls = []
@@ -70,13 +92,16 @@
# Get URLs from the file.
with open(args.url_file) as reader:
file_content = reader.read().splitlines()
- input_urls = list(filter(lambda x:x, file_content))
- if not(input_urls):
+ input_urls = list(filter(lambda x: x, file_content))
+ if not (input_urls):
print("Input file {} is empty. Quitting...".format(args.url_file))
quit()
except IOError as e:
- print("Issue in reading file '{}'. Reason: {}".format(args.url_file,
- str(e)))
+ print(
+ "Issue in reading file '{}'. Reason: {}".format(
+ args.url_file, str(e)
+ )
+ )
quit()
@@ -85,9 +110,13 @@
working_dir = os.path.join(pwd, args.target_dir)
try:
os.mkdir(working_dir)
-except OSError as e:
- answer = input("Target directory " + working_dir + " already exists. "
- + "Do you want to delete [Y/N]: ")
+except OSError:
+ answer = input(
+ "Target directory "
+ + working_dir
+ + " already exists. "
+ + "Do you want to delete [Y/N]: "
+ )
if answer == "Y":
try:
shutil.rmtree(working_dir)
@@ -112,8 +141,11 @@
# Log files
debug_file = os.path.join(log_dir, "debug.log")
output_file = os.path.join(log_dir, "output.log")
-logging.basicConfig(format='%(levelname)s - %(message)s', level=logging.DEBUG,
- filename=debug_file)
+logging.basicConfig(
+ format="%(levelname)s - %(message)s",
+ level=logging.DEBUG,
+ filename=debug_file,
+)
logger = logging.getLogger(__name__)
# Create handlers
@@ -123,7 +155,7 @@
file_handler.setLevel(logging.INFO)
# Create formatters and add it to handlers
-log_format = logging.Formatter('%(message)s')
+log_format = logging.Formatter("%(message)s")
console_handler.setFormatter(log_format)
file_handler.setFormatter(log_format)
@@ -143,8 +175,12 @@
# Clone OpenBmc build scripts.
try:
- output = subprocess.check_output("git clone https://github.com/openbmc/openbmc-build-scripts.git",
- shell=True, cwd=working_dir, stderr=subprocess.STDOUT)
+ output = subprocess.check_output(
+ "git clone https://github.com/openbmc/openbmc-build-scripts.git",
+ shell=True,
+ cwd=working_dir,
+ stderr=subprocess.STDOUT,
+ )
logger.debug(output)
except subprocess.CalledProcessError as e:
logger.error(e.output)
@@ -157,7 +193,7 @@
api_url = "https://api.github.com/repos/openbmc/"
for url in input_urls:
try:
- repo_name = url.strip().split('/')[-1].split(";")[0].split(".")[0]
+ repo_name = url.strip().split("/")[-1].split(";")[0].split(".")[0]
except IndexError as e:
logger.error("ERROR: Unable to get sandbox name for url " + url)
logger.error("Reason: " + str(e))
@@ -169,26 +205,27 @@
logger.info(api_url + repo_name + " ==> " + resp.reason)
continue
repo_data.extend([resp.json()])
- except ValueError as e:
+ except ValueError:
logger.error("ERROR: Failed to get response for " + repo_name)
logger.error(resp)
continue
else:
# Get number of pages.
- resp = requests.head('https://api.github.com/users/openbmc/repos')
+ resp = requests.head("https://api.github.com/users/openbmc/repos")
if resp.status_code != 200:
logger.error("Error! Unable to get repositories")
logger.error(resp.status_code)
logger.error(resp.reason)
quit()
- num_of_pages = int(resp.links['last']['url'].split('page=')[-1])
+ num_of_pages = int(resp.links["last"]["url"].split("page=")[-1])
logger.debug("No. of pages: " + str(num_of_pages))
# Fetch data from all pages.
- for page in range(1, num_of_pages+1):
- resp = requests.get('https://api.github.com/users/openbmc/repos?page='
- + str(page))
+ for page in range(1, num_of_pages + 1):
+ resp = requests.get(
+ "https://api.github.com/users/openbmc/repos?page=" + str(page)
+ )
data = resp.json()
repo_data.extend(data)
@@ -198,7 +235,7 @@
for repo in repo_data:
try:
url_info[repo["clone_url"]] = repo["archived"]
- except KeyError as e:
+ except KeyError:
logger.error("Failed to get archived status of {}".format(repo))
url_info[repo["clone_url"]] = False
continue
@@ -227,34 +264,47 @@
try:
# Eg: url = "https://github.com/openbmc/u-boot.git"
# sandbox_name = "u-boot"
- sandbox_name = url.strip().split('/')[-1].split(";")[0].split(".")[0]
+ sandbox_name = (
+ url.strip().split("/")[-1].split(";")[0].split(".")[0]
+ )
except IndexError as e:
logger.error("ERROR: Unable to get sandbox name for url " + url)
logger.error("Reason: " + str(e))
continue
- if (sandbox_name in skip_list or
- re.match(r'meta-', sandbox_name)):
+ if sandbox_name in skip_list or re.match(r"meta-", sandbox_name):
logger.debug("SKIPPING: " + sandbox_name)
skip = True
ut_status = "SKIPPED"
else:
checkout_cmd = "rm -rf " + sandbox_name + ";git clone " + url
try:
- subprocess.check_output(checkout_cmd, shell=True, cwd=working_dir,
- stderr=subprocess.STDOUT)
+ subprocess.check_output(
+ checkout_cmd,
+ shell=True,
+ cwd=working_dir,
+ stderr=subprocess.STDOUT,
+ )
except subprocess.CalledProcessError as e:
logger.debug(e.output)
logger.debug(e.cmd)
logger.debug("Failed to clone " + sandbox_name)
ut_status = "ERROR"
skip = True
- if not(skip):
- docker_cmd = "WORKSPACE=$(pwd) UNIT_TEST_PKG=" + sandbox_name + " " + \
- "./openbmc-build-scripts/run-unit-test-docker.sh"
+ if not (skip):
+ docker_cmd = (
+ "WORKSPACE=$(pwd) UNIT_TEST_PKG="
+ + sandbox_name
+ + " "
+ + "./openbmc-build-scripts/run-unit-test-docker.sh"
+ )
try:
- result = subprocess.check_output(docker_cmd, cwd=working_dir, shell=True,
- stderr=subprocess.STDOUT)
+ result = subprocess.check_output(
+ docker_cmd,
+ cwd=working_dir,
+ shell=True,
+ stderr=subprocess.STDOUT,
+ )
logger.debug(result)
logger.debug("UT BUILD COMPLETED FOR: " + sandbox_name)
@@ -268,8 +318,10 @@
repo_report_dir = os.path.join(report_dir, sandbox_name)
report_names = ("coveragereport", "test-suite.log", "LastTest.log")
- find_cmd = "".join("find " + folder_name + " -name " + report + ";"
- for report in report_names)
+ find_cmd = "".join(
+ "find " + folder_name + " -name " + report + ";"
+ for report in report_names
+ )
try:
result = subprocess.check_output(find_cmd, shell=True)
result = result.decode("utf-8")
@@ -290,15 +342,21 @@
elif "LastTest.log" in result:
file_names = result.splitlines()
for file in file_names:
- pattern_count_cmd = "sed -n '/Start testing/,/End testing/p;' " + \
- file + "|wc -l"
+ pattern_count_cmd = (
+ "sed -n '/Start testing/,/End testing/p;' "
+ + file
+ + "|wc -l"
+ )
try:
- num_of_lines = subprocess.check_output(pattern_count_cmd,
- shell=True)
+ num_of_lines = subprocess.check_output(
+ pattern_count_cmd, shell=True
+ )
except subprocess.CalledProcessError as e:
logger.debug(e.output)
logger.debug(e.cmd)
- logger.debug("CONTENT CHECK FAILED FOR: " + sandbox_name)
+ logger.debug(
+ "CONTENT CHECK FAILED FOR: " + sandbox_name
+ )
ut_status = "ERROR"
if int(num_of_lines.strip()) > 5:
@@ -309,11 +367,19 @@
tested_report_count += 1
result = result.splitlines()
for file_path in result:
- destination = os.path.dirname(os.path.join(report_dir,
- os.path.relpath(file_path,
- working_dir)))
- copy_cmd = "mkdir -p " + destination + ";cp -rf " + \
- file_path.strip() + " " + destination
+ destination = os.path.dirname(
+ os.path.join(
+ report_dir, os.path.relpath(file_path, working_dir)
+ )
+ )
+ copy_cmd = (
+ "mkdir -p "
+ + destination
+ + ";cp -rf "
+ + file_path.strip()
+ + " "
+ + destination
+ )
try:
subprocess.check_output(copy_cmd, shell=True)
except subprocess.CalledProcessError as e: