22"""Script to automate creating builds of projects."""
33
44import argparse
5- import io
65import logging
76import os
87import platform
@@ -79,18 +78,15 @@ def _BuildProject(self, build_helper_object, source_helper_object, distribution)
7978 log_filename = "_" .join (
8079 [source_helper_object .project_name , build_helper_object .LOG_FILENAME ]
8180 )
82-
8381 # Remove older logfiles if they exists otherwise the rename
8482 # fails on Windows.
8583 if os .path .exists (log_filename ):
8684 os .remove (log_filename )
8785
8886 os .rename (build_helper_object .LOG_FILENAME , log_filename )
8987 logging .warning (
90- (
91- f"Build of: { source_helper_object .project_name :s} failed, for more "
92- f"information check { log_filename :s} "
93- )
88+ f"Build of: { source_helper_object .project_name :s} failed, for more "
89+ f"information check { log_filename :s} "
9490 )
9591
9692 return False
@@ -252,14 +248,12 @@ def Download(self, project_definition):
252248 download_helper_object = (
253249 download_helper .DownloadHelperFactory .NewDownloadHelper (project_definition )
254250 )
255-
256251 source_helper_object = source_helper .SourcePackageHelper (
257252 project_definition .name ,
258253 project_definition ,
259254 self ._downloads_directory ,
260255 download_helper_object ,
261256 )
262-
263257 source_helper_object .Clean ()
264258
265259 # TODO: add a step to make sure build environment is sane
@@ -286,7 +280,7 @@ def ReadProjectDefinitions(self, path):
286280 Args:
287281 path (str): path of the project definitions file.
288282 """
289- with io . open (path , "r" , encoding = "utf-8" ) as file_object :
283+ with open (path , encoding = "utf-8" ) as file_object :
290284 project_definition_reader = projects .ProjectDefinitionReader ()
291285 self .project_definitions = {
292286 definition .name : definition
@@ -306,7 +300,7 @@ def ReadProjectsPreset(self, path, preset_name):
306300 """
307301 preset_definitions = {}
308302
309- with io . open (path , "r" , encoding = "utf-8" ) as file_object :
303+ with open (path , encoding = "utf-8" ) as file_object :
310304 definition_reader = presets .PresetDefinitionReader ()
311305 preset_definitions = {
312306 preset_definition .name : preset_definition
@@ -317,19 +311,17 @@ def ReadProjectsPreset(self, path, preset_name):
317311
318312
319313def Main ():
320- """The main program function .
314+ """Entry point of console script .
321315
322316 Returns:
323- bool: True if successful or False if not .
317+ int: exit code that is provided to sys.exit() .
324318 """
325319 build_targets = frozenset (
326320 ["download" , "dpkg" , "dpkg-source" , "rpm" , "source" , "srpm" , "wheel" ]
327321 )
328-
329322 argument_parser = argparse .ArgumentParser (
330323 description = ("Downloads and builds the latest versions of projects." )
331324 )
332-
333325 argument_parser .add_argument (
334326 "build_target" ,
335327 choices = sorted (build_targets ),
@@ -338,7 +330,6 @@ def Main():
338330 default = None ,
339331 help = "The build target." ,
340332 )
341-
342333 default_builds_directory = os .path .join (".." , "l2tbuilds" )
343334 argument_parser .add_argument (
344335 "--build-directory" ,
@@ -352,7 +343,6 @@ def Main():
352343 default = default_builds_directory ,
353344 help = "The location of the build directory." ,
354345 )
355-
356346 argument_parser .add_argument (
357347 "-c" ,
358348 "--config" ,
@@ -365,7 +355,6 @@ def Main():
365355 "files e.g. projects.ini."
366356 ),
367357 )
368-
369358 argument_parser .add_argument (
370359 "--distributions" ,
371360 dest = "distributions" ,
@@ -374,7 +363,6 @@ def Main():
374363 default = "" ,
375364 help = ("comma separated list of specific distribution names to build." ),
376365 )
377-
378366 argument_parser .add_argument (
379367 "--download-directory" ,
380368 "--downloads-directory" ,
@@ -387,7 +375,6 @@ def Main():
387375 default = None ,
388376 help = "The location of the downloads directory." ,
389377 )
390-
391378 argument_parser .add_argument (
392379 "--preset" ,
393380 dest = "preset" ,
@@ -400,7 +387,6 @@ def Main():
400387 "The presets are defined in the preset.ini configuration file."
401388 ),
402389 )
403-
404390 argument_parser .add_argument (
405391 "--projects" ,
406392 dest = "projects" ,
@@ -413,22 +399,21 @@ def Main():
413399 "configuration file."
414400 ),
415401 )
416-
417402 options = argument_parser .parse_args ()
418403
419404 if not options .build_target :
420405 print ("Build target missing." )
421406 print ("" )
422407 argument_parser .print_help ()
423408 print ("" )
424- return False
409+ return 1
425410
426411 if options .build_target not in build_targets :
427412 print (f"Unsupported build target: { options .build_target :s} " )
428413 print ("" )
429414 argument_parser .print_help ()
430415 print ("" )
431- return False
416+ return 1
432417
433418 config_path = options .config_path
434419 if not config_path :
@@ -439,27 +424,27 @@ def Main():
439424 if not options .preset and not options .projects :
440425 print ("Please define a preset or projects to build." )
441426 print ("" )
442- return False
427+ return 1
443428
444429 presets_file = os .path .join (config_path , "presets.ini" )
445430 if options .preset and not os .path .exists (presets_file ):
446431 print (f"No such config file: { presets_file :s} " )
447432 print ("" )
448- return False
433+ return 1
449434
450435 projects_file = os .path .join (config_path , "projects.ini" )
451436 if not os .path .exists (projects_file ):
452437 print (f"No such config file: { projects_file :s} " )
453438 print ("" )
454- return False
439+ return 1
455440
456441 if os .path .abspath (options .builds_directory ).startswith (l2tdevtools_path ):
457442 print (
458443 "Builds directory cannot be within l2tdevtools directory due to "
459444 "usage of pbr"
460445 )
461446 print ("" )
462- return False
447+ return 1
463448
464449 logging .basicConfig (level = logging .INFO , format = "[%(levelname)s] %(message)s" )
465450
@@ -471,14 +456,13 @@ def Main():
471456 project_builder = ProjectBuilder (
472457 options .build_target , l2tdevtools_path , options .downloads_directory
473458 )
474-
475459 project_names = []
476460 if options .preset :
477461 project_names = project_builder .ReadProjectsPreset (presets_file , options .preset )
478462 if not project_names :
479463 print (f"Undefined preset: { options .preset :s} " )
480464 print ("" )
481- return False
465+ return 1
482466
483467 elif options .projects :
484468 project_names = options .projects .split ("," )
@@ -608,11 +592,11 @@ def Main():
608592 for name in sorted (failed_builds ):
609593 print (f"\t { name :s} " )
610594
611- return not failed_downloads and not missing_build_dependencies and not failed_builds
595+ if failed_downloads or missing_build_dependencies or failed_builds :
596+ return 1
597+
598+ return 0
612599
613600
614601if __name__ == "__main__" :
615- if not Main ():
616- sys .exit (1 )
617- else :
618- sys .exit (0 )
602+ sys .exit (Main ())
0 commit comments