Tool: Update some new features and refinements

 - Support name the output database file.
 - Support input specific name database file.
 - Fix the defect that delete all database file in the path.
 - UI option and terminal options are conflicting now.
 - Terminal messages are reversed, summary info shows at last.
 - Support sort messages in terminal mode.
 - Update help message and document.
 - Other code and structure refinements.

Signed-off-by: Jianliang Shen <jianliang.shen@arm.com>
Change-Id: I597f05bebc4f20d316f400ed3d099bd9dbbd972a
diff --git a/code-size-analyze-tool/src/sq.py b/code-size-analyze-tool/src/sq.py
index 11f620a..039563a 100644
--- a/code-size-analyze-tool/src/sq.py
+++ b/code-size-analyze-tool/src/sq.py
@@ -6,8 +6,6 @@
 # ------------------------------------------------------------------------------
 
 import sqlite3
-import os
-import glob
 from xlsxwriter.workbook import Workbook
 
 class SQ(object):
@@ -22,7 +20,8 @@
     - Variables:
       - SQ().armcc     - ARMCLANG option.
       - SQ().gnuarm    - GNUARM option.
-      - SQ().file_path - The map file path which detail information comes from.
+      - SQ().map_file  - The map file path which detail information comes from.
+      - SQ().db_name   - The database file name to be saved.
     """
     def __init__(self):
         """
@@ -30,27 +29,20 @@
         """
         self.gnuarm = False
         self.armcc = False
-        self.file_path = ""
+        self.map_file = ""
+        self.db_name = ""
 
         self.__gnuarm_info = []
         self.__sec_dict = {}
-        self.__delete()
-        self.__con = sqlite3.connect("data.db")
-        self.__cur = self.__con.cursor()
-
-    def __delete(self):
-        """
-        Search and delete the previous database file and excel file.
-        """
-        for infile in glob.glob(os.path.join(os.getcwd(), '*.db')):
-            os.remove(infile)
-        for infile in glob.glob(os.path.join(os.getcwd(), '*.xlsx')):
-            os.remove(infile)
+        self.__excel_name = ""
 
     def __new(self):
         """
         Create tables in a new empty database.
         """
+        self.__con = sqlite3.connect(self.db_name)
+        self.__cur = self.__con.cursor()
+        self.__excel_name = self.db_name +'.xlsx'
         self.__cur.execute('''create table Summary
                              (Code               INT     NOT NULL,
                               RO_data            INT     NOT NULL,
@@ -82,7 +74,7 @@
                               lib_file           TEXT    NOT_NULL);''')
         self.__cur.execute('''create table Library
                              (name               TEXT    NOT NULL,
-                              flashsize          INT     NOT NULL,
+                              size               INT     NOT NULL,
                               ramsize            INT     NOT NULL,
                               code               INT     NOT NULL,
                               rodata             INT     NOT NULL,
@@ -92,8 +84,8 @@
                               Debug              INT     NOT NULL);''')
         self.__cur.execute('''create table Object
                              (name               TEXT    NOT NULL,
-                              library            TEXT    NOT NULL,
-                              flashsize          INT     NOT NULL,
+                              lib_file           TEXT    NOT NULL,
+                              size               INT     NOT NULL,
                               ramsize            INT     NOT NULL,
                               code               INT     NOT NULL,
                               rodata             INT     NOT NULL,
@@ -101,6 +93,9 @@
                               zidata             INT     NOT NULL,
                               incdata            INT     NOT_NULL,
                               Debug              INT     NOT NULL);''')
+        self.__cur.execute('''create table Compiler
+                             (Compiler           TEXT    NOT NULL,
+                              flag               INT     NOT NULL);''')
         if self.gnuarm:
             self.__cur.execute('''create table Unknown
                              (name               TEXT    NOT NULL,
@@ -111,6 +106,12 @@
                               obj_file           TEXT    NOT_NULL,
                               lib_file           TEXT    NOT_NULL);''')
 
+    def __collect_compiler(self):
+        if self.gnuarm:
+            self.__cur.execute("insert into Compiler values (?, ?)", ("gnuarm", 1))
+        if self.armcc:
+            self.__cur.execute("insert into Compiler values (?, ?)", ("armcc", 0))
+
     def __collect_summary(self):
         code_size = ro_data = rw_data = zi_data = flash_size = ram_size = extra_ram = extra_flash = 0
         if self.gnuarm:
@@ -150,7 +151,7 @@
             """
             For Secure image, the TFM_DATA part is loaded from Flash.
             """
-            for line in open(self.file_path, "r"):
+            for line in open(self.map_file, "r"):
                 if line.find("load address") >= 0:
                     extra_flash_addr = int(line.split()[-1] ,16)
                     extra_flash_data = int(line.split()[-4], 16)
@@ -158,7 +159,7 @@
                         flash_size += extra_flash_data
 
         elif self.armcc:
-            for line in open(self.file_path, "r"):
+            for line in open(self.map_file, "r"):
                 if line.find("gram Size: Code=") > 0:
                     content = line.split()
                     code_size = int(content[2].split('=')[1])
@@ -208,7 +209,7 @@
             section_addr = ""
             section_size = 0
             section_pad_size = 0
-            for line in open(self.file_path, "r"):
+            for line in open(self.map_file, "r"):
                 line_idx += 1
 
                 if line.find("Execution Region") > 0:
@@ -284,7 +285,7 @@
 
         elif self.armcc:
             line_idx, line_start = 0, 0
-            for line in open(self.file_path, "r"):
+            for line in open(self.map_file, "r"):
                 line_idx += 1
                 if line.find("Code (inc. data)   RO Data    RW Data    ZI Data      Debug   Library Name") > 0:
                     line_start = line_idx + 1
@@ -325,7 +326,7 @@
                                      0, 0))
         elif self.armcc:
             line_idx, line_start = 0, 0
-            for line in open(self.file_path, "r"):
+            for line in open(self.map_file, "r"):
                 line_idx += 1
                 if line.find("Code (inc. data)   RO Data    RW Data    ZI Data      Debug   Object Name") > 0:
                     line_start = line_idx + 1
@@ -346,7 +347,7 @@
                     else:
                         break
             line_idx, line_start = 0, 0
-            for line in open(self.file_path, "r"):
+            for line in open(self.map_file, "r"):
                 line_idx += 1
                 if line.find("Code (inc. data)   RO Data    RW Data    ZI Data      Debug   Library Member Name") > 0:
                     line_start = line_idx + 1
@@ -355,7 +356,7 @@
                     if len(content) == 7:
                         obj_name = content[6]
                         library_file = ""
-                        for line in open(self.file_path, "r"):
+                        for line in open(self.map_file, "r"):
                             if line.find(obj_name) > 0:
                                 ch_r = line[line.find(obj_name) + len(obj_name)]
                                 ch_l = line[line.find(obj_name) - 1]
@@ -379,7 +380,7 @@
 
     def __get_ram_and_flash_start_addr(self):
         start = False
-        for line in open(self.file_path, "r"):
+        for line in open(self.map_file, "r"):
             if line.find('Memory Configuration') >= 0:
                 start = True
             if line.find('Linker script and memory map') == 0:
@@ -397,7 +398,7 @@
         def get_key_content():
                 start, end, real_start = False, False, False
                 content = ""
-                for line in open(self.file_path, "r"):
+                for line in open(self.map_file, "r"):
                     if line.find('Linker script and memory map') >= 0:
                         start = True
                     if line.find('OUTPUT(') == 0:
@@ -558,7 +559,7 @@
         table_list = ["Summary", "Section", "Library", "Object", "Function", "Data"]
         if self.gnuarm:
             table_list.append("Unknown")
-        workbook = Workbook('data.xlsx')
+        workbook = Workbook(self.__excel_name)
         title_m = workbook.add_format({'bold': True,
                                        'align': 'left',
                                        'font_size': 12})
@@ -603,6 +604,7 @@
         self.__new()
         if self.gnuarm:
             self.__get_info_from_gnuarm_map()
+        self.__collect_compiler()
         self.__collect_summary()
         self.__collect_section()
         self.__collect_library()
diff --git a/code-size-analyze-tool/src/ui.py b/code-size-analyze-tool/src/ui.py
index e555e59..6b5a704 100644
--- a/code-size-analyze-tool/src/ui.py
+++ b/code-size-analyze-tool/src/ui.py
@@ -20,6 +20,7 @@
     - Methods:
       - UI().run()              - Run the UI in terminal.
       - UI.draw_<symbol>_page() - Get the information of specific symbol
+      - UI.open_db()            - Open database.
 
     - Variables:
       - UI().items              - The result searched from database.
@@ -31,6 +32,9 @@
       - UI().section_name       - Specific section name.
       - UI().library_name       - Specific library name.
       - UI().obj_file           - Specific object file name.
+      - UI().db_file            - Database file.
+      - UI().sort               - Database list order: size, name and so on.
+      - UI().order              - Database sort order: DESC and ASC.
     """
 
     def __init__(self):
@@ -45,9 +49,10 @@
         self.gnuarm = False
         self.armcc = False
         self.items = []
-        self.con = sqlite3.connect("data.db")
+        self.db_file = ""
+        self.sort = "size"
+        self.order = "DESC"
 
-        self.__cur = self.con.cursor()
         self.__window = None
         self.__width = 0
         self.__height = 0
@@ -62,6 +67,17 @@
         self.__cmd_func="Enter the function name:"
         self.__cmd_data="Enter the data name:"
 
+    def open_db(self):
+        self.con = sqlite3.connect(self.db_file)
+        self.__cur = self.con.cursor()
+        cursor = self.__cur.execute("select * from Compiler")
+        for row in cursor:
+            compiler = row[1]
+            if compiler == 1:
+                self.gnuarm = True
+            elif compiler == 0:
+                self.armcc = True
+
     def __init_curses(self):
         """
         Setup the curses
@@ -413,7 +429,7 @@
         self.items = ["{:<50}{:<16}{:<16}{:<16}".
                       format("Name", "Size", "Address", "PAD size")]
 
-        cursor = self.__cur.execute("select * from Section ORDER BY size DESC")
+        cursor = self.__cur.execute("select * from Section ORDER BY {} {}".format(self.sort, self.order))
         for row in cursor:
             self.items.append("{:<50}{:<16}{:<16}{:<16}".
                               format(row[0], row[1], row[2], row[3]))
@@ -559,7 +575,7 @@
         Dump library information.
         """
         self.items = [self.__line1]
-        insert_column_line("\t\t\t\t\t\t\tSection libraries")
+        insert_column_line(" " * ((128 - len("Section libraries"))//2) + "Section libraries")
         for s in lib_list:
             if s['Name'].find(".o") > 0:
                 exsit_no_lib_obj = True
@@ -571,7 +587,7 @@
         """
         Dump object file information.
         """
-        insert_column_line("\t\t\t\t\t\t\tSection object files")
+        insert_column_line(" " * ((128 - len("Section object files"))//2) + "Section object files")
         for s in obj_list:
             quick_insert_data_line(s)
         ret = sum_data(obj_list)
@@ -581,8 +597,8 @@
         Dump NOT-IN-LIBRARY object file information.
         """
         if exsit_no_lib_obj:
-            insert_column_line(
-                "\t\t\t\t\t\t\tSection NOT-IN-LIBRARY object files")
+            insert_column_line(" " * ((128 - len("Section NOT-IN-LIBRARY object files"))//2) +
+                "Section NOT-IN-LIBRARY object files")
             tmp_list = []
             for s in lib_list:
                 if s['Name'].find(".o") > 0:
@@ -600,14 +616,14 @@
                               format(self.section_name, row[1], total_flash_size, total_ram_size, row[3]))
             break
         self.items.insert(0, self.__line2)
-        self.items.insert(0, "\t\t\t\t\t\t\tSection information")
+        self.items.insert(0, " " * ((128 - len("Section information"))//2) + "Section information")
         self.items.insert(0, self.__line1)
 
         """
         Dump detail information of the section.
         """
         index = 4 * ' '
-        self.items.append("\t\t\t\t\t\t\tDetail information")
+        self.items.append(" " * ((128 - len("Detail information"))//2) + "Detail information")
         self.items.append(self.__line2)
         for s in lib_list:
             self.items.append("{} Code Size = {} RO Data = {} RW Data = {} ZI Data = {}".
@@ -617,10 +633,12 @@
                     self.items.append(index + "{} Code Size = {} RO Data = {} RW Data = {} ZI Data = {}".format(
                         t['Name'], t['Code'], t['RO'], t['RW'], t['ZI']))
                     count = 0
-                    cursor = self.__cur.execute("select * from Function WHERE section = '{}' and lib_file = '{}' and obj_file = '{}' ORDER BY size DESC".
+                    cursor = self.__cur.execute("select * from Function WHERE section = '{}' and lib_file = '{}' and obj_file = '{}' ORDER BY {} {}".
                                                 format(self.section_name,
                                                        s['Name'],
-                                                       t['Name']))
+                                                       t['Name'],
+                                                       self.sort,
+                                                       self.order))
                     for row in cursor:
                         if row and count == 0:
                             self.items.append(index * 2 + "Code size = {}".
@@ -631,11 +649,13 @@
 
                     def get_certain_data(type_name, s, t):
                         count = 0
-                        cursor = self.__cur.execute("select * from Data WHERE section = '{}' and lib_file = '{}' and obj_file = '{}'  and type = '{}' ORDER BY size DESC".
+                        cursor = self.__cur.execute("select * from Data WHERE section = '{}' and lib_file = '{}' and obj_file = '{}'  and type = '{}' ORDER BY {} {}".
                                                     format(self.section_name,
                                                            s['Name'],
                                                            t['Name'],
-                                                           type_name))
+                                                           type_name,
+                                                           self.sort,
+                                                           self.order))
                         for row in cursor:
                             if row and count == 0:
                                 self.items.append(index * 2 + "{} Data = {}".
@@ -648,6 +668,7 @@
                     get_certain_data('RW', s, t)
                     get_certain_data('ZI', s, t)
             self.items.append(self.__line2)
+        self.items[-1] = self.__line1
 
     def draw_section_func(self):
         self.items = ["{:<50}{:<32}{:<10}{:<16}{:<40}{:<40}".
@@ -657,8 +678,8 @@
                              "Address",
                              "Object File",
                              "Library")]
-        cursor = self.__cur.execute("select * from Function WHERE section = '{}' ORDER BY size DESC".
-                                  format(self.section_name))
+        cursor = self.__cur.execute("select * from Function WHERE section = '{}' ORDER BY {} {}".
+                                  format(self.section_name, self.sort, self.order))
         for row in cursor:
             self.items.append("{:<50}{:<32}{:<10}{:<16}{:<40}{:<40}".
                               format(row[0], row[1], row[2], row[3], row[4], row[5]))
@@ -673,8 +694,8 @@
                              "Object File",
                              "Library")]
 
-        cursor = self.__cur.execute("select * from Data WHERE section = '{}' ORDER BY size DESC".
-                                  format(self.section_name))
+        cursor = self.__cur.execute("select * from Data WHERE section = '{}' ORDER BY {} {}".
+                                  format(self.section_name, self.sort, self.order))
         for row in cursor:
             data_name = row[0]
             if len(data_name) >= 50:
@@ -718,7 +739,7 @@
                              "Debug")]
 
         cursor = self.__cur.execute(
-            "select * from Library ORDER BY flashsize DESC")
+            "select * from Library ORDER BY {} {}".format(self.sort, self.order))
         for row in cursor:
             self.items.append("{:<50}{:<12}{:<12}{:<12}{:<12}{:<12}{:<12}{:<12}{:<12}".
                               format(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]))
@@ -761,8 +782,8 @@
                                  "Inc. data"))
         self.items.append(self.__line2)
 
-        cursor = self.__cur.execute("select * from Object WHERE library = '{}' ORDER BY flashsize DESC".
-                                  format(self.library_name))
+        cursor = self.__cur.execute("select * from Object WHERE lib_file = '{}' ORDER BY {} {}".
+                                  format(self.library_name, self.sort, self.order))
         for row in cursor:
             self.items.append("{:<50}{:<12}{:<12}{:<12}{:<12}{:<12}{:<12}{:<12}".
                               format(row[0], row[2], row[3], row[4], row[5], row[6], row[7], row[8]))
@@ -770,8 +791,8 @@
         """
         Draw functions.
         """
-        cursor = self.__cur.execute("select * from Function WHERE lib_file = '{}' ORDER BY size DESC".
-                                  format(self.library_name))
+        cursor = self.__cur.execute("select * from Function WHERE lib_file = '{}' ORDER BY {} {}".
+                                  format(self.library_name, self.sort, self.order))
         for row in cursor:
             if not flag:
                 self.__quick_append()
@@ -782,22 +803,22 @@
         """
         Draw RO data.
         """
-        cursor = self.__cur.execute("select * from Data WHERE type = 'RO' and lib_file = '{}' ORDER BY size DESC".
-                                  format(self.library_name))
+        cursor = self.__cur.execute("select * from Data WHERE type = 'RO' and lib_file = '{}' ORDER BY {} {}".
+                                  format(self.library_name, self.sort, self.order))
         self.__quick_append_data(cursor)
 
         """
         Draw RW data.
         """
-        cursor = self.__cur.execute("select * from Data WHERE type = 'RW' and lib_file = '{}' ORDER BY size DESC".
-                                  format(self.library_name))
+        cursor = self.__cur.execute("select * from Data WHERE type = 'RW' and lib_file = '{}' ORDER BY {} {}".
+                                  format(self.library_name, self.sort, self.order))
         self.__quick_append_data(cursor)
 
         """
         Draw ZI data.
         """
-        cursor = self.__cur.execute("select * from Data WHERE type = 'ZI' and lib_file = '{}' ORDER BY size DESC".
-                                  format(self.library_name))
+        cursor = self.__cur.execute("select * from Data WHERE type = 'ZI' and lib_file = '{}' ORDER BY {} {}".
+                                  format(self.library_name, self.sort, self.order))
         self.__quick_append_data(cursor)
         self.items.append(self.__line1)
 
@@ -813,13 +834,12 @@
                              "ZI data",
                              "Inc. data",
                              "Debug")]
-        cursor = self.__cur.execute(
-            "select * from Object WHERE library = 'no library' ORDER BY flashsize DESC")
+        cursor = self.__cur.execute("select * from Object WHERE lib_file = 'no library' ORDER BY {} {}".format(self.sort, self.order))
         for row in cursor:
             self.items.append("{:<50}{:<50}{:<12}{:<12}{:<12}{:<12}{:<12}{:<12}{:<12}{:<12}".
                               format(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9]))
         cursor = self.__cur.execute(
-            "select * from Object WHERE library != 'no library' ORDER BY flashsize DESC")
+            "select * from Object WHERE lib_file != 'no library' ORDER BY {} {}".format(self.sort, self.order))
         for row in cursor:
             self.items.append("{:<50}{:<50}{:<12}{:<12}{:<12}{:<12}{:<12}{:<12}{:<12}{:<12}".
                               format(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9]))
@@ -845,8 +865,8 @@
                               format(row[0], row[2], row[3], row[4], row[5], row[6], row[7], row[8]))
             break
 
-        cursor = self.__cur.execute("select * from Function WHERE obj_file = '{}' ORDER BY size DESC".
-                                  format(self.obj_file))
+        cursor = self.__cur.execute("select * from Function WHERE obj_file = '{}' ORDER BY {} {}".
+                                  format(self.obj_file, self.sort, self.order))
         for row in cursor:
             if not flag:
                 self.__quick_append()
@@ -854,16 +874,16 @@
                               format(row[0], row[1], row[2], "Code", row[4]))
             flag = True
 
-        cursor = self.__cur.execute("select * from Data WHERE type = 'RO' and obj_file = '{}' ORDER BY size DESC".
-                                  format(self.obj_file))
+        cursor = self.__cur.execute("select * from Data WHERE type = 'RO' and obj_file = '{}' ORDER BY {} {}".
+                                  format(self.obj_file, self.sort, self.order))
         self.__quick_append_data(cursor)
 
-        cursor = self.__cur.execute("select * from Data WHERE type = 'RW' and obj_file = '{}' ORDER BY size DESC".
-                                  format(self.obj_file))
+        cursor = self.__cur.execute("select * from Data WHERE type = 'RW' and obj_file = '{}' ORDER BY {} {}".
+                                  format(self.obj_file, self.sort, self.order))
         self.__quick_append_data(cursor)
 
-        cursor = self.__cur.execute("select * from Data WHERE type = 'ZI' and obj_file = '{}' ORDER BY size DESC".
-                                  format(self.obj_file))
+        cursor = self.__cur.execute("select * from Data WHERE type = 'ZI' and obj_file = '{}' ORDER BY {} {}".
+                                  format(self.obj_file, self.sort, self.order))
         self.__quick_append_data(cursor)
         self.items.append(self.__line1)
 
@@ -880,8 +900,7 @@
             cursor = self.__cur.execute("select * from Function WHERE name LIKE '%{}%'".
                                       format(search_func))
         else:
-            cursor = self.__cur.execute(
-                "select * from Function ORDER BY size DESC")
+            cursor = self.__cur.execute("select * from Function ORDER BY {} {}".format(self.sort, self.order))
         for row in cursor:
             self.items.append("{:<50}{:<50}{:<10}{:<16}{:<40}{:<40}".
                               format(row[0], row[1], row[2], row[3], row[4], row[5]))
@@ -917,7 +936,7 @@
             cursor = self.__cur.execute("select * from Data WHERE name LIKE '%{}%'".
                                       format(search_data))
         else:
-            cursor = self.__cur.execute("select * from Data ORDER BY size DESC")
+            cursor = self.__cur.execute("select * from Data ORDER BY {} {}".format(self.sort, self.order))
         for row in cursor:
             data_name = row[0]
             if len(data_name) >= 50:
@@ -948,6 +967,7 @@
         """
         Continue running the TUI until get interrupted
         """
+        self.open_db()
         self.__init_curses()
         try:
             self.__draw_page()
@@ -955,4 +975,5 @@
         except KeyboardInterrupt:
             pass
         finally:
+            self.con.close()
             curses.endwin()