1515
1616logger = logging .getLogger ('urlchecker' )
1717
18+
19+ def str2bool (str_bool ):
20+ """
21+ Convert str input to boolean
22+
23+ Args:
24+ - str_bool (str) : boolean variable as an str.
25+
26+ Returns
27+ bool
28+ """
29+ if isinstance (str_bool , bool ):
30+ return str_bool
31+ if str_bool .lower () in ('true' , 't' , 'y' , '1' ):
32+ return True
33+ elif str_bool .lower () in ('no' , 'false' , 'f' , 'n' , '0' ):
34+ return False
35+ else :
36+ raise ArgumentTypeError ('Boolean value expected.' )
37+
38+
1839def main (args , extra ):
1940 """
2041 main entrypoint for running a check. We expect an args object with
@@ -52,20 +73,25 @@ def main(args, extra):
5273 white_listed_patterns = remove_empty (args .white_listed_patterns .split ("," ))
5374 white_listed_files = remove_empty (args .white_listed_files .split ("," ))
5475
76+ # parse booleans
77+ cleanup = str2bool (args .cleanup )
78+ print_all = not str2bool (args .no_print )
79+ force_pass = str2bool (args .force_pass )
80+
5581 # Alert user about settings
5682 print (" original path: %s" % args .path )
5783 print (" final path: %s" % path )
5884 print (" subfolder: %s" % args .subfolder )
5985 print (" branch: %s" % args .branch )
60- print (" cleanup: %s" % args . cleanup )
86+ print (" cleanup: %s" % cleanup )
6187 print (" file types: %s" % file_types )
62- print (" print all: %s" % ( not args . no_print ) )
88+ print (" print all: %s" % print_all )
6389 print (" url whitetlist: %s" % white_listed_urls )
6490 print (" url patterns: %s" % white_listed_patterns )
6591 print (" file patterns: %s" % white_listed_files )
66- print (" force pass: %s" % args . force_pass )
92+ print (" force pass: %s" % force_pass )
6793 print (" retry count: %s" % args .retry_count )
68- print (" save: %s" % args .save )
94+ print (" save: %s" % args .save )
6995 print (" timeout: %s" % args .timeout )
7096
7197 # Run checks, get lookup of results and fails
@@ -74,7 +100,7 @@ def main(args, extra):
74100 white_listed_files = white_listed_files ,
75101 white_listed_urls = white_listed_urls ,
76102 white_listed_patterns = white_listed_patterns ,
77- print_all = not args . no_print ,
103+ print_all = print_all ,
78104 retry_count = args .retry_count ,
79105 timeout = args .timeout )
80106
@@ -83,22 +109,35 @@ def main(args, extra):
83109 save_results (check_results , args .save )
84110
85111 # delete repo when done, if requested
86- if args . cleanup :
112+ if cleanup :
87113 logger .info ("Cleaning up %s..." % path )
88114 delete_repo (path )
89115
90- # Case 1: We didn't find any urls to check
91- if not check_results ['failed' ] and not check_results ['passed' ]:
92- print ("\n \n Done. No urls were collected." )
93- sys .exit (0 )
116+ # force pass
117+ if force_pass :
118+ if check_results ['failed' ]:
119+ print ("\n \n Done. The following urls did not pass:" )
120+ for failed_url in check_results ['failed' ]:
121+ print_failure (failed_url )
122+ sys .exit (1 )
94123
95- # Case 2: We had errors, but force pass is True
96- elif args .force_pass and check_results ['failed' ]:
97- print ("\n \n Done. The following urls did not pass:" )
98- for failed_url in check_results ['failed' ]:
99- print_failure (failed_url )
100- sys .exit (1 )
124+ else :
125+ print ("\n \n Done. All URLS passed." )
126+ sys .exit (1 )
101127
128+ # no force pass
102129 else :
103- print ("\n \n Done. All URLS passed." )
104- sys .exit (0 )
130+ # Case 1: We didn't find any urls to check
131+ if not check_results ['failed' ] and not check_results ['passed' ]:
132+ print ("\n \n Done. No urls were collected." )
133+ sys .exit (0 )
134+
135+ # Case 2 : Only working urls found
136+ elif not check_results ['failed' ] and check_results ['passed' ]:
137+ print ("\n \n Done. All URLS passed." )
138+ sys .exit (0 )
139+
140+ # exit
141+ else :
142+ print ("\n \n Done." )
143+ sys .exit (0 )
0 commit comments