@@ -10,10 +10,14 @@ import os
1010import argparse
1111from enum import Enum
1212import requests
13+ import logging
1314
15+
16+ NO_PR = - 1
1417GITHUB_API_PULLREQUEST = "https://api.github.com/repos/ubports/{repo}/pulls/{num}"
1518JENKINS_API_BUILD = "https://ci.ubports.com/blue/rest/organizations/jenkins/pipelines/{repo}/branches/{ref}"
1619
20+ LOG = logging .getLogger (name = "ubports-qa" )
1721IS_ROOT = os .geteuid () == 0
1822
1923
@@ -43,19 +47,21 @@ class WritableRootFS:
4347 @classmethod
4448 def attempt_writable_mount (cls ):
4549 """Tries to mount the rootfs read-write"""
50+ LOG .debug ("Attempting to mount rootfs read-write." )
4651 ensure_root ()
4752 subprocess .run (["mount" , "-o" , "rw,remount" , "/" ])
4853
4954 @classmethod
5055 def attempt_unmount (cls ):
56+ LOG .debug ("Attempting to mount rootfs read-only." )
5157 if not os .path .exists ("/userdata/.writable_image" ):
5258 ensure_root ()
5359 os .sync ()
5460 try :
5561 subprocess .run (["mount" , "-o" , "ro,remount" , "/" ], check = True )
5662 except subprocess .CalledProcessError :
57- print_error ("Failed to remount root filesystem read-only." )
58- print_error ("Please consider rebooting your device." )
63+ LOG . error ("Failed to remount root filesystem read-only." )
64+ LOG . error ("Please consider rebooting your device." )
5965
6066
6167def ensure_root ():
@@ -64,25 +70,31 @@ def ensure_root():
6470
6571
6672def apt_update ():
73+ LOG .debug ("Running 'apt update'." )
6774 try :
6875 subprocess .run (["apt" , "update" ], check = True )
6976 except subprocess .CalledProcessError :
70- print_error ("Failed to run 'apt update'. See the output above for details." )
77+ LOG . error ("Failed to run 'apt update'. See the output above for details." )
7178
7279
7380def apt_upgrade ():
81+ LOG .debug ("Running 'apt upgrade'." )
7482 try :
7583 subprocess .run (["apt" , "upgrade" ], check = True )
7684 except subprocess .CalledProcessError :
77- print_error ("Failed to run 'apt upgrade'. See the output above for details." )
85+ LOG . error ("Failed to run 'apt upgrade'. See the output above for details." )
7886
7987
8088def get_list_file (branch ):
81- return "/etc/apt/sources.list.d/ubports-{}.list" .format (branch )
89+ list_file = "/etc/apt/sources.list.d/ubports-{}.list" .format (branch )
90+ LOG .debug ("List file is " + list_file )
91+ return list_file
8292
8393
8494def get_pref_file (branch ):
85- return "/etc/apt/preferences.d/ubports-{}.pref" .format (branch )
95+ pref_file = "/etc/apt/preferences.d/ubports-{}.pref" .format (branch )
96+ LOG .debug ("Pref file is " + "/etc/apt/preferences.d/ubports-{}.pref" .format (branch ))
97+ return pref_file
8698
8799
88100def list_exists (branch ):
@@ -114,13 +126,17 @@ def remove_list(branch):
114126
115127
116128def get_github_pr (repo , num ):
117- ret = requests .get (GITHUB_API_PULLREQUEST .format (repo = repo , num = num ))
129+ pull_request_url = GITHUB_API_PULLREQUEST .format (repo = repo , num = num )
130+ LOG .debug ("Getting pull request information from " + pull_request_url )
131+ ret = requests .get (pull_request_url )
118132 if ret .status_code != 200 :
119133 die ("Pull-Request not found" )
120134 return ret .json ()
121135
122136
123137def get_jenkins_build (repo , ref ):
138+ LOG .debug ("Checking PR for valid build on UBports CI." )
139+ LOG .debug (JENKINS_API_BUILD .format (repo = repo , ref = ref ))
124140 ret = requests .get (JENKINS_API_BUILD .format (repo = repo , ref = ref ))
125141 if ret .status_code != 200 :
126142 die ("Jenkins build not found" )
@@ -129,7 +145,7 @@ def get_jenkins_build(repo, ref):
129145
130146def get_issue_status (repo , ref ):
131147 build = get_jenkins_build (repo , ref )["latestRun" ]
132- print (build )
148+ LOG . debug (build )
133149 if build ["result" ] == "SUCCESS" :
134150 return Status .SUCCESS
135151 elif build ["result" ] == "BULDING" :
@@ -138,39 +154,50 @@ def get_issue_status(repo, ref):
138154 return Status .FAILED
139155
140156
141- def print_error (error_message ):
142- """Prints error_message in red"""
143- print ("\033 [91m" + error_message + "\033 [0m" )
144-
145-
146157def die (error_message ):
147- """Prints error_message in red and exits with status 3"""
148- print_error (error_message )
158+ """Logs error_message and exits with status 3"""
159+ LOG . critical (error_message )
149160 exit (3 )
150161
151162
152163def install_command (args ):
153164 """Install a PPA or Pull Request"""
154- if args .pr != - 1 :
165+ if args .pr is not NO_PR :
155166 args .repo .replace ("ubports/" , "" )
156- ref = 'PR-' + str (args .pr )
167+ ref = "PR-" + str (args .pr )
168+ LOG .debug (
169+ "Checking repository ubports/" + args .repo + " for PR #" + str (args .pr )
170+ )
157171 status = get_issue_status (args .repo , ref )
158172 if status == Status .FAILED :
159173 die ("Issue failed to build" )
160174 if status == Status .BUILDING :
161175 die ("Issue is currently building" )
176+ repository_name = ref
177+ else :
178+ repository_name = args .repo
179+ LOG .debug ("No PR set, installing " + repository_name )
180+
162181 with WritableRootFS ():
163- add_list (args . repo )
182+ add_list (repository_name )
164183 apt_update ()
165184 apt_upgrade ()
166185
186+ LOG .info (
187+ "You can remove this repository by running 'sudo ubports-qa remove {}'" .format (
188+ repository_name
189+ )
190+ )
191+
167192
168193def remove_command (args ):
169194 """Remove and uninstall a PPA"""
170- if not list_exists (args .repo ):
171- die ("Repo {} is not installed" .format (args .repo ))
195+ repository = args .repo
196+ LOG .info ("Removing repo " + repository )
197+ if not list_exists (repository ):
198+ die ("Repo {} is not installed" .format (repository ))
172199 with WritableRootFS ():
173- remove_list (args . repo )
200+ remove_list (repository )
174201 apt_update ()
175202 apt_upgrade ()
176203
@@ -190,6 +217,9 @@ def update_command(args):
190217parser = argparse .ArgumentParser (
191218 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."
192219)
220+ parser .add_argument (
221+ "-v" , "--verbose" , help = "Print verbose logging information" , action = "store_true"
222+ )
193223subparsers = parser .add_subparsers (help = "" )
194224
195225parser_install = subparsers .add_parser (
@@ -207,7 +237,7 @@ parser_install.add_argument(
207237 type = int ,
208238 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." ,
209239 nargs = "?" ,
210- default = - 1 ,
240+ default = NO_PR ,
211241)
212242parser_install .set_defaults (func = install_command )
213243
@@ -232,6 +262,11 @@ parser_update.set_defaults(func=update_command)
232262
233263try :
234264 ARGS = parser .parse_args ()
265+ logging .basicConfig ()
266+ if ARGS .verbose :
267+ LOG .setLevel (logging .DEBUG )
268+ else :
269+ LOG .setLevel (logging .INFO )
235270 ARGS .func (ARGS )
236271except IOError as e :
237272 # We weren't allowed to do something with a file.
0 commit comments