Tools: Silence Generating... messages

The build log is cluttered with messages that are usually
inconsequential. These messages start with 'Generating' and tend to
hide warnings and errors that appear.

This commit adds a quiet flag to the script in question and allows
enabling the quiet mode by with a flag. If you would not like to see
these messages enable PARSE_MANIFEST_QUIET. This goes a long way to
having a shorter, less noisy build log.

Change-Id: I085ac4f4909e86c3ccecf1bdf5482bafaecd3dcf
Signed-off-by: Jimmy Brisson <jimmy.brisson@linaro.org>
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index 745ea24..5dfcaee 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -123,11 +123,18 @@
     SOURCES ${OUTPUT_FILES}
 )
 
+if (PARSE_MANIFEST_QUIET)
+    set(PARSE_MANIFEST_QUIET_FLAG "-q")
+else()
+    set(PARSE_MANIFEST_QUIET_FLAG "")
+endif()
+
 add_custom_command(OUTPUT ${OUTPUT_FILES}
     COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tfm_parse_manifest_list.py
                                   -m ${COMBINED_LIST}
                                   -f ${GENERATED_FILE_LISTS}
                                   -o ${CMAKE_BINARY_DIR}/generated
+                                  ${PARSE_MANIFEST_QUIET_FLAG}
     DEPENDS ${TEMPLATE_FILES} ${MANIFEST_FILES}
     DEPENDS ${TEMP_MANIFEST_LISTS} ${CONFIGURED_MANIFEST_LISTS} ${GENERATED_FILE_LISTS}
 )
@@ -140,6 +147,7 @@
                                   -m ${COMBINED_LIST}
                                   -f ${GENERATED_FILE_LISTS}
                                   -o ${CMAKE_BINARY_DIR}/generated
+                                  ${PARSE_MANIFEST_QUIET_FLAG}
     RESULT_VARIABLE RET
 )
 
diff --git a/tools/tfm_parse_manifest_list.py b/tools/tfm_parse_manifest_list.py
index b359f15..b6a687e 100644
--- a/tools/tfm_parse_manifest_list.py
+++ b/tools/tfm_parse_manifest_list.py
@@ -10,13 +10,14 @@
 import re
 import sys
 import argparse
+import logging
 from jinja2 import Environment, BaseLoader, select_autoescape, TemplateNotFound
 
 try:
     import yaml
 except ImportError as e:
-    print (str(e) + ' To install it, type:')
-    print ('pip install PyYAML')
+    logging.error (str(e) + " To install it, type:")
+    logging.error ("pip install PyYAML")
     exit(1)
 
 donotedit_warning = \
@@ -150,12 +151,12 @@
     # Get all the manifests information as a dictionary
     for i, item in enumerate(manifest_lists):
         if i % 2 == 0 and not os.path.isfile(item):
-            print('Manifest list item [{}] must be a file'.format(i))
+            logging.error('Manifest list item [{}] must be a file'.format(i))
             exit(1)
 
         if i % 2 == 1:
             if not os.path.isdir(item):
-                print('Manifest list item [{}] must be a directory'.format(i))
+                logging.error('Manifest list item [{}] must be a directory'.format(i))
                 exit(1)
 
             # Skip original manifest paths
@@ -278,14 +279,14 @@
     memorytemplate = ENV.get_template(os.path.join(sys.path[0], 'templates/partition_intermedia.template'))
     infotemplate = ENV.get_template(os.path.join(sys.path[0], 'templates/partition_load_info.template'))
 
-    print ('Start to generate partition files:')
+    logging.info ("Start to generate partition files:")
 
     for one_partition in context['partitions']:
         partition_context['manifest'] = one_partition['manifest']
         partition_context['attr'] = one_partition['attr']
         partition_context['manifest_out_basename'] = one_partition['manifest_out_basename']
 
-        print ('Generating Header: ' + one_partition['header_file'])
+        logging.info ("Generating Header: " + one_partition['header_file'])
         outfile_path = os.path.dirname(one_partition['header_file'])
         if not os.path.exists(outfile_path):
             os.makedirs(outfile_path)
@@ -294,7 +295,7 @@
         headerfile.write(manifesttemplate.render(partition_context))
         headerfile.close()
 
-        print ('Generating Intermedia: ' + one_partition['intermedia_file'])
+        logging.info ("Generating Intermedia: " + one_partition['intermedia_file'])
         intermediafile_path = os.path.dirname(one_partition['intermedia_file'])
         if not os.path.exists(intermediafile_path):
             os.makedirs(intermediafile_path)
@@ -302,7 +303,7 @@
         intermediafile.write(memorytemplate.render(partition_context))
         intermediafile.close()
 
-        print ('Generating Loadinfo: ' + one_partition['loadinfo_file'])
+        logging.info ("Generating Loadinfo: " + one_partition['loadinfo_file'])
         infofile_path = os.path.dirname(one_partition['loadinfo_file'])
         if not os.path.exists(infofile_path):
             os.makedirs(infofile_path)
@@ -310,7 +311,7 @@
         infooutfile.write(infotemplate.render(partition_context))
         infooutfile.close()
 
-    print ('Per-partition files done:')
+    logging.info ("Per-partition files done:")
 
 def gen_summary_files(context, gen_file_lists):
     """
@@ -328,7 +329,7 @@
             file_list_yaml = yaml.safe_load(file_list_yaml_file)
             file_list.extend(file_list_yaml['file_list'])
 
-    print('Start to generate file from the generated list:')
+    logging.info("Start to generate file from the generated list:")
     for file in file_list:
         # Replace environment variables in the output filepath
         manifest_out_file = os.path.expandvars(file['output'])
@@ -337,7 +338,8 @@
 
         manifest_out_file = os.path.join(OUT_DIR, manifest_out_file)
 
-        print ('Generating ' + manifest_out_file)
+        logging.info ("Generating " + manifest_out_file)
+
         outfile_path = os.path.dirname(manifest_out_file)
         if not os.path.exists(outfile_path):
             os.makedirs(outfile_path)
@@ -348,7 +350,7 @@
         outfile.write(template.render(context))
         outfile.close()
 
-    print ('Generation of files done')
+    logging.info ("Generation of files done")
 
 def process_stateless_services(partitions):
     """
@@ -483,11 +485,17 @@
                         , required=True
                         , metavar='file-list'
                         , help='These files descripe the file list to generate')
+    parser.add_argument('-q', '--quiet'
+                        , dest='quiet'
+                        , required=False
+                        , default=False
+                        , action='store_true'
+                        , help='Reduce log messages')
 
     args = parser.parse_args()
 
     if len(args.manifest_lists) % 2 != 0:
-        print('Invalid structure in manifest lists.\n'
+        logging.error('Invalid structure in manifest lists.\n'
               'Each element shall consist of a manifest list and its original path')
         exit(1)
 
@@ -512,6 +520,9 @@
 
     args = parse_args()
 
+    logging.basicConfig(format='%(message)s'
+                        , level=logging.WARNING if args.quiet else logging.INFO)
+
     OUT_DIR = os.path.abspath(args.outdir)
 
     manifest_lists = [os.path.abspath(x) for x in args.manifest_lists]