diff --git a/debian/changelog b/debian/changelog index 66afd8e..2cf3267 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,10 @@ +ubports-qa-scripts (0.3) xenial; urgency=medium + + * Log using a logger rather than print statements + * Fix installation of PR repos + + -- Dalton Durst Mon, 29 Oct 2018 15:38:32 -0500 + ubports-qa-scripts (0.2) xenial; urgency=medium * Update code style diff --git a/ubports-qa b/ubports-qa index d259c8b..a228d67 100755 --- a/ubports-qa +++ b/ubports-qa @@ -10,10 +10,14 @@ import os import argparse from enum import Enum import requests +import logging + +NO_PR = -1 GITHUB_API_PULLREQUEST = "https://api.github.com/repos/ubports/{repo}/pulls/{num}" JENKINS_API_BUILD = "https://ci.ubports.com/blue/rest/organizations/jenkins/pipelines/{repo}/branches/{ref}" +LOG = logging.getLogger(name="ubports-qa") IS_ROOT = os.geteuid() == 0 @@ -43,19 +47,21 @@ class WritableRootFS: @classmethod def attempt_writable_mount(cls): """Tries to mount the rootfs read-write""" + LOG.debug("Attempting to mount rootfs read-write.") ensure_root() subprocess.run(["mount", "-o", "rw,remount", "/"]) @classmethod def attempt_unmount(cls): + LOG.debug("Attempting to mount rootfs read-only.") if not os.path.exists("/userdata/.writable_image"): ensure_root() os.sync() try: subprocess.run(["mount", "-o", "ro,remount", "/"], check=True) except subprocess.CalledProcessError: - print_error("Failed to remount root filesystem read-only.") - print_error("Please consider rebooting your device.") + LOG.error("Failed to remount root filesystem read-only.") + LOG.error("Please consider rebooting your device.") def ensure_root(): @@ -64,25 +70,31 @@ def ensure_root(): def apt_update(): + LOG.debug("Running 'apt update'.") try: subprocess.run(["apt", "update"], check=True) except subprocess.CalledProcessError: - print_error("Failed to run 'apt update'. See the output above for details.") + LOG.error("Failed to run 'apt update'. See the output above for details.") def apt_upgrade(): + LOG.debug("Running 'apt upgrade'.") try: subprocess.run(["apt", "upgrade"], check=True) except subprocess.CalledProcessError: - print_error("Failed to run 'apt upgrade'. See the output above for details.") + LOG.error("Failed to run 'apt upgrade'. See the output above for details.") def get_list_file(branch): - return "/etc/apt/sources.list.d/ubports-{}.list".format(branch) + list_file = "/etc/apt/sources.list.d/ubports-{}.list".format(branch) + LOG.debug("List file is " + list_file) + return list_file def get_pref_file(branch): - return "/etc/apt/preferences.d/ubports-{}.pref".format(branch) + pref_file = "/etc/apt/preferences.d/ubports-{}.pref".format(branch) + LOG.debug("Pref file is " + "/etc/apt/preferences.d/ubports-{}.pref".format(branch)) + return pref_file def list_exists(branch): @@ -114,13 +126,17 @@ def remove_list(branch): def get_github_pr(repo, num): - ret = requests.get(GITHUB_API_PULLREQUEST.format(repo=repo, num=num)) + pull_request_url = GITHUB_API_PULLREQUEST.format(repo=repo, num=num) + LOG.debug("Getting pull request information from " + pull_request_url) + ret = requests.get(pull_request_url) if ret.status_code != 200: die("Pull-Request not found") return ret.json() def get_jenkins_build(repo, ref): + LOG.debug("Checking PR for valid build on UBports CI.") + LOG.debug(JENKINS_API_BUILD.format(repo=repo, ref=ref)) ret = requests.get(JENKINS_API_BUILD.format(repo=repo, ref=ref)) if ret.status_code != 200: die("Jenkins build not found") @@ -129,7 +145,7 @@ def get_jenkins_build(repo, ref): def get_issue_status(repo, ref): build = get_jenkins_build(repo, ref)["latestRun"] - print(build) + LOG.debug(build) if build["result"] == "SUCCESS": return Status.SUCCESS elif build["result"] == "BULDING": @@ -138,39 +154,50 @@ def get_issue_status(repo, ref): return Status.FAILED -def print_error(error_message): - """Prints error_message in red""" - print("\033[91m" + error_message + "\033[0m") - - def die(error_message): - """Prints error_message in red and exits with status 3""" - print_error(error_message) + """Logs error_message and exits with status 3""" + LOG.critical(error_message) exit(3) def install_command(args): """Install a PPA or Pull Request""" - if args.pr != -1: + if args.pr is not NO_PR: args.repo.replace("ubports/", "") - ref = 'PR-' + str(args.pr) + ref = "PR-" + str(args.pr) + LOG.debug( + "Checking repository ubports/" + args.repo + " for PR #" + str(args.pr) + ) status = get_issue_status(args.repo, ref) if status == Status.FAILED: die("Issue failed to build") if status == Status.BUILDING: die("Issue is currently building") + repository_name = ref + else: + repository_name = args.repo + LOG.debug("No PR set, installing " + repository_name) + with WritableRootFS(): - add_list(args.repo) + add_list(repository_name) apt_update() apt_upgrade() + LOG.info( + "You can remove this repository by running 'sudo ubports-qa remove {}'".format( + repository_name + ) + ) + def remove_command(args): """Remove and uninstall a PPA""" - if not list_exists(args.repo): - die("Repo {} is not installed".format(args.repo)) + repository = args.repo + LOG.info("Removing repo " + repository) + if not list_exists(repository): + die("Repo {} is not installed".format(repository)) with WritableRootFS(): - remove_list(args.repo) + remove_list(repository) apt_update() apt_upgrade() @@ -190,6 +217,9 @@ def update_command(args): parser = argparse.ArgumentParser( description="The UBports QA scripts allow you to efficiently manage PPAs from repo.ubports.com for testing deb components. See http://docs.ubports.com/en/latest/about/process/ppa.html." ) +parser.add_argument( + "-v", "--verbose", help="Print verbose logging information", action="store_true" +) subparsers = parser.add_subparsers(help="") parser_install = subparsers.add_parser( @@ -207,7 +237,7 @@ parser_install.add_argument( type=int, help="Numeric ID of a pull-request on the git repository specified in the 'repo' argument. If 'repo' is supposed to be the name of a ppa, the 'pr' argument should not be specified.", nargs="?", - default=-1, + default=NO_PR, ) parser_install.set_defaults(func=install_command) @@ -232,6 +262,11 @@ parser_update.set_defaults(func=update_command) try: ARGS = parser.parse_args() + logging.basicConfig() + if ARGS.verbose: + LOG.setLevel(logging.DEBUG) + else: + LOG.setLevel(logging.INFO) ARGS.func(ARGS) except IOError as e: # We weren't allowed to do something with a file.