src/tools/code_size.py
changeset 0 ca70ae20a155
equal deleted inserted replaced
-1:000000000000 0:ca70ae20a155
       
     1 # Copyright (c) 2008 Nokia Corporation
       
     2 #
       
     3 # Licensed under the Apache License, Version 2.0 (the "License");
       
     4 # you may not use this file except in compliance with the License.
       
     5 # You may obtain a copy of the License at
       
     6 #
       
     7 #     http://www.apache.org/licenses/LICENSE-2.0
       
     8 #
       
     9 # Unless required by applicable law or agreed to in writing, software
       
    10 # distributed under the License is distributed on an "AS IS" BASIS,
       
    11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    12 # See the License for the specific language governing permissions and
       
    13 # limitations under the License.
       
    14 
       
    15 # This script calculates the code size of the dll, pyd's and the python runtime
       
    16 # sis, which is displayed graphically in project metrics tab of release build.
       
    17 
       
    18 import sys
       
    19 import os
       
    20 import time
       
    21 
       
    22 # `metrics_data`: map format -> {binary_type: (path, extension)}
       
    23 # 'binary_type' are the metrics for which code size is calculated.The value
       
    24 # for these keys comprise of the path along with the extension(if specified),
       
    25 # to the respective 'binary_type'.
       
    26 metrics_data = {'Python25.dll':
       
    27                     ('\\epoc32\\release\\armv5\\urel\\python25.dll', ''),
       
    28                 'Other PYDs': ('\\epoc32\\release\\armv5\\urel\\', 'pyd')}
       
    29 binary_size = {}
       
    30 
       
    31 
       
    32 def file_size(file_path):
       
    33     file_stat = os.stat(file_path)
       
    34     return (file_stat.st_size) / 1024.00   # Get size of file in kilobytes
       
    35 
       
    36 
       
    37 def print_metrics(log_file, release):
       
    38     log = open(log_file, 'a+')
       
    39     now = time.strftime("%b %d %Y %H:%M:%S")  # Get date, time when writing log
       
    40     log_string = "Time=%s,Release=%s," % (now, release)
       
    41     for code in binary_size:
       
    42         log_string += "%s=%f," % (code, binary_size[code])
       
    43     log.write(log_string + "\n")
       
    44     log.close()
       
    45 
       
    46 
       
    47 def updatelog(release, python_runtime, log_file):
       
    48 # This function is called to log the code size, something like this
       
    49 # code_size.log(platform, python_runtime, "C:\\logs\\code_size.log")
       
    50     global binary_size
       
    51     metrics_data['Python25_runtime.sis'] = (python_runtime, '')
       
    52     for binary_type in metrics_data:
       
    53         binary_path, file_ext = metrics_data[binary_type]
       
    54         if "pyd" in file_ext:
       
    55             all_files = os.listdir(binary_path)
       
    56             total_size = 0
       
    57             for afile in all_files:
       
    58                 if afile.endswith(".pyd"):
       
    59                     total_size += file_size(os.path.join(binary_path, afile))
       
    60             binary_size[binary_type] = total_size
       
    61         elif not file_ext:  # Either sis or dll
       
    62             code_size = file_size(binary_path)
       
    63             binary_size[binary_type] = code_size
       
    64 
       
    65     print_metrics(log_file, release)