Enabled UT report generation for given repositories

Change-Id: I2d6b9a86e69f85a6c61ec0434b9b09922cad73a6
Signed-off-by: Anusha Dathatri <adathatr@in.ibm.com>
diff --git a/scripts/get_unit_test_report.py b/scripts/get_unit_test_report.py
index 5767d0a..b9c8bb9 100755
--- a/scripts/get_unit_test_report.py
+++ b/scripts/get_unit_test_report.py
@@ -3,12 +3,18 @@
 # This script generates the unit test coverage report for openbmc project.
 #
 # Usage:
-# get_unit_test_report.py target_dir
+# get_unit_test_report.py target_dir [url_file]
 #
-# Description of arguments:
+# Positional arguments:
 # target_dir  Target directory in pwd to place all cloned repos and logs.
+# url_file    Text file containing url of repositories. Optional.
+#             By using this argument, the user can get a report only for
+#             specific repositories given in the file.
+#             Refer ./scripts/repositories.txt
 #
-# Eg: get_unit_test_report.py target_dir
+# Examples:
+#     get_unit_test_report.py target_dir
+#     get_unit_test_report.py target_dir repositories.txt
 #
 # Output format:
 #
@@ -28,15 +34,51 @@
 import sys
 import subprocess
 
+# 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"]
+
 
 # Create parser.
-parser = argparse.ArgumentParser(usage='%(prog)s file target_dir',
+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'''
+
+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 = []
+if args.url_file:
+    try:
+        # 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):
+            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)))
+        quit()
+
 
 # Create target working directory.
 pwd = os.getcwd()
@@ -67,16 +109,6 @@
     quit()
 
 
-# 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"]
-
 # Log files
 debug_file = os.path.join(log_dir, "debug.log")
 output_file = os.path.join(log_dir, "output.log")
@@ -115,32 +147,61 @@
                                      shell=True, cwd=working_dir, stderr=subprocess.STDOUT)
     logger.debug(output)
 except subprocess.CalledProcessError as e:
-    logger.debug(e.output)
-    logger.debug(e.cmd)
-    logger.debug("Unable to clone openbmc-build-scripts")
+    logger.error(e.output)
+    logger.error(e.cmd)
+    logger.error("Unable to clone openbmc-build-scripts")
     quit()
 
-# Get number of pages.
-resp = requests.head('https://api.github.com/users/openbmc/repos')
-if resp.status_code != 200:
-    logger.error("Error! Unable to get repositories")
-    logger.debug(resp.status_code)
-    logger.debug(resp.text)
-    quit()
-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.
 repo_data = []
-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)
+if input_urls:
+    api_url = "https://api.github.com/repos/openbmc/"
+    for url in input_urls:
+        try:
+            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))
+            continue
+
+        try:
+            resp = requests.get(api_url + repo_name)
+            if resp.status_code != 200:
+                logger.info(api_url + repo_name + " ==> " + resp.reason)
+                continue
+            repo_data.extend([resp.json()])
+        except ValueError as e:
+            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')
+    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])
+    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))
+        data = resp.json()
+        repo_data.extend(data)
+
 
 # Get URLs and their archive status from response.
 url_info = {}
 for repo in repo_data:
-    url_info[repo["clone_url"]] = repo["archived"]
+    try:
+        url_info[repo["clone_url"]] = repo["archived"]
+    except KeyError as e:
+        logger.error("Failed to get archived status of {}".format(repo))
+        url_info[repo["clone_url"]] = False
+        continue
 logger.debug(url_info)
 repo_count = len(url_info)
 logger.info("Number of repositories (Including archived): " + str(repo_count))
@@ -168,8 +229,9 @@
             #     sandbox_name = "u-boot"
             sandbox_name = url.strip().split('/')[-1].split(";")[0].split(".")[0]
         except IndexError as e:
-            logger.debug("ERROR: Unable to get sandbox name for url " + url)
-            logger.debug("Reason: " + str(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)):
diff --git a/scripts/repositories.txt b/scripts/repositories.txt
new file mode 100644
index 0000000..5d6b5df
--- /dev/null
+++ b/scripts/repositories.txt
@@ -0,0 +1,3 @@
+https://github.com/openbmc/entity-manager.git
+https://github.com/openbmc/boost-dbus.git
+