buildframework/helium/tools/rombuild/dryrun_parser.py
changeset 179 d8ac696cc51f
parent 1 be27ed110b50
child 180 e02a83d4c571
child 592 3215c239276a
equal deleted inserted replaced
1:be27ed110b50 179:d8ac696cc51f
     1 #============================================================================ 
       
     2 #Name        : dryrun_parser.py 
       
     3 #Part of     : Helium 
       
     4 
       
     5 #Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     6 #All rights reserved.
       
     7 #This component and the accompanying materials are made available
       
     8 #under the terms of the License "Eclipse Public License v1.0"
       
     9 #which accompanies this distribution, and is available
       
    10 #at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    11 #
       
    12 #Initial Contributors:
       
    13 #Nokia Corporation - initial contribution.
       
    14 #
       
    15 #Contributors:
       
    16 #
       
    17 #Description:
       
    18 #===============================================================================
       
    19 
       
    20 """ parse a python file in a given environment, and get out some results """
       
    21 import re
       
    22 import os
       
    23 import sys
       
    24 
       
    25 if __name__ == '__main__':
       
    26     if len(sys.argv)<3:
       
    27         print("dryrun requires input txt file and output make file and the target to run")
       
    28         sys.exit(-1)
       
    29 
       
    30     #input arguments    
       
    31     input_file = sys.argv[1]
       
    32     output_file = sys.argv[2]
       
    33     make_target = sys.argv[3]
       
    34     
       
    35     
       
    36     variation_commands = {}
       
    37     regional_commands = []
       
    38 
       
    39     #parse imaker output
       
    40     fin = open(input_file, "r")
       
    41     imaker_pattern = '^imaker'
       
    42     foti_fota_pattern = ' fot[ia]'
       
    43     core_pattern = 'core|rofs3|flash$'
       
    44     workdir_pattern = 'WORKDIR='
       
    45     regional_pattern = '^unzip'
       
    46     regional_key = ""
       
    47     new_regional_key = ""
       
    48     regional_keys = []
       
    49     temporary_list = []
       
    50     unzip_commands = []
       
    51     workdir_string = ""
       
    52     
       
    53     for line in fin:
       
    54 #       """ First look for unzip command. """
       
    55         if(re.search(regional_pattern, line)):
       
    56             temporary_list = line.split(' ')
       
    57             unzip_commands.append(line)
       
    58             new_regional_key = temporary_list[-1]
       
    59             #print regional_key
       
    60             if(not len(regional_key) == 0):
       
    61 #                """ Will be processing next region, so store the commands for this region."""
       
    62                 if(variation_commands.has_key(regional_key)):
       
    63                     variation_commands[regional_key] = regional_commands
       
    64                 regional_key = new_regional_key
       
    65                 if(variation_commands.has_key(new_regional_key)):
       
    66                     regional_commands = variation_commands[new_regional_key]
       
    67                 else:
       
    68                     regional_commands = []
       
    69                     variation_commands[new_regional_key] = regional_commands
       
    70             else:
       
    71                 variation_commands[new_regional_key] = regional_commands
       
    72                 regional_key = new_regional_key
       
    73         if(re.search(imaker_pattern, line)):
       
    74             workdir_list = re.split(workdir_pattern, line)
       
    75             if(len(workdir_list) > 1):
       
    76                 #print workdir_list
       
    77                 workdir_string = re.split("\"", workdir_list[1])[0]
       
    78                 #print "workdir_string"
       
    79                 #print workdir_string
       
    80                 if(not os.path.exists(workdir_string)):
       
    81                     os.makedirs(workdir_string)
       
    82             regional_commands.append(line)
       
    83     fin.close()
       
    84     variation_commands[regional_key] = regional_commands
       
    85     
       
    86 #    """ Group the commands for the region and write the commands into a makefile."""
       
    87     fin = open(output_file, "w")
       
    88     all_string = "%s: foti_fota_all \\\n\tcore_rofs_image_all \\\n\trest_of_all" % make_target
       
    89     core_rofs_all_string = "core_rofs_image_all:"
       
    90     variation_image_all_string = "variation_all:"
       
    91     variation_image_string = ""
       
    92     core_rofs_string = ""
       
    93     core_image_id = 0
       
    94     e2flash_id = 0
       
    95     e2flash_all_string = "#pragma runlocal\ne2flash_target_all:\n"
       
    96     variation_image_id = 0
       
    97     variation_e2f_id = 0
       
    98     variation_id = 0
       
    99     
       
   100     foti_fota_all_string = "#pragma runlocal\nfoti_fota_all:"
       
   101     foti_fota_all_string += "\n\t"+unzip_commands[variation_id]
       
   102     rest_of_all_target_string = "rest_of_all:"
       
   103     rest_of_target_string = ""
       
   104     rest_of_target_id = 0
       
   105 
       
   106     count = 0
       
   107     dict_list = variation_commands.keys()
       
   108     for key in dict_list:
       
   109         print "before reverse key"
       
   110         print key
       
   111         print len(variation_commands.get(key))
       
   112     
       
   113     dict_list.reverse()
       
   114 
       
   115     for key in dict_list:
       
   116         print "key"
       
   117         print key
       
   118         print len(variation_commands.get(key))
       
   119     #    for command in commands:
       
   120     #        print command
       
   121 
       
   122     unzip_var_string = ""
       
   123     variation_image_all_string = ""
       
   124     variation_dependency = "variation0"
       
   125     for key in dict_list:
       
   126         commands = variation_commands.get(key)
       
   127         if(variation_id != 0):
       
   128             unzip_var_string += "unzip_var%d:%s" % (variation_id, variation_dependency)
       
   129             variation_dependency ="variation%d " % (variation_id)
       
   130             unzip_var_string += "\n\t"+unzip_commands[variation_id]
       
   131         all_string += "\\\n\tvariation%d " % variation_id
       
   132         variation_image_all_string +="variation%d: " % variation_id
       
   133         #print "no. of comands"
       
   134         #print len(commands)
       
   135         for command in commands:
       
   136             #print variation_id
       
   137             #print command
       
   138             if(re.search(foti_fota_pattern, command)):
       
   139                 #print "foti command"
       
   140                 #print command
       
   141                 foti_fota_all_string += "\t"+command
       
   142             elif(re.search('flash$',command)):
       
   143                 #print "flash command"
       
   144                 #print command
       
   145                 core_rofs_string += ("core_rofs_image%d:foti_fota_all" % (core_image_id))
       
   146                 temp_string = re.sub('flash$', 'core-image rofs2-image rofs3-image', command)
       
   147                 core_rofs_string += "\n\t"+temp_string+"\n"
       
   148                 temp_string = re.sub('flash$', 'core-e2flash rofs2-e2flash rofs3-e2flash', command)
       
   149                 e2flash_all_string += "\t"+temp_string
       
   150                 core_rofs_all_string += " \\\n\t core_rofs_image%d" % core_image_id
       
   151                 core_image_id += 1
       
   152             elif(re.search('core$',command)):
       
   153                 #print "core command"
       
   154                 #print command
       
   155                 core_rofs_string += ("core_rofs_image%d:foti_fota_all" % (core_image_id))
       
   156                 temp_string = re.sub('core$', 'core-image', command)
       
   157                 core_rofs_string += "\n\t"+temp_string + "\n"
       
   158                 temp_string = re.sub('core$', 'core-e2flash', command)
       
   159                 e2flash_all_string += "\t"+temp_string
       
   160                 core_rofs_all_string += " \\\n\t core_rofs_image%d" % core_image_id
       
   161                 core_image_id += 1
       
   162             elif(re.search('rofs3$', command)):
       
   163                 #print "rofs3 command"
       
   164                 #print command
       
   165                 core_rofs_string += ("core_rofs_image%d:foti_fota_all" % (core_image_id))
       
   166                 temp_string = re.sub('rofs3$', 'rofs3-image', command)
       
   167                 core_rofs_string += "\n\t"+temp_string + "\n"
       
   168                 temp_string = re.sub('rofs3$', 'rofs3-e2flash', command)
       
   169                 e2flash_all_string += "\t"+temp_string
       
   170                 core_rofs_all_string += " \\\n\t core_rofs_image%d" % core_image_id
       
   171                 core_image_id += 1
       
   172             elif(re.search('rofs2$',command)):
       
   173                 #print "rofs2 command"
       
   174                 #print variation_id
       
   175                 #print command
       
   176                 if(variation_id == 0):
       
   177                     variation_image_string += ("variation_image%d:rest_of_all")  %(variation_image_id)
       
   178                 else:
       
   179                     variation_image_string += ("variation_image%d:unzip_var%d")  %(variation_image_id, variation_id)
       
   180                 temp_string = re.sub('rofs2$', 'rofs2-image', command)
       
   181                 variation_image_string += "\n\t"+temp_string+"\n"
       
   182                 temp_string = re.sub('rofs2$', 'rofs2-e2flash', command)
       
   183                 e2flash_all_string += "\t"+temp_string
       
   184                 variation_image_all_string += " \\\n\tvariation_image%d" % variation_image_id
       
   185                 variation_image_id += 1
       
   186             else:
       
   187                 rest_of_target_string += ("\nrest_%d:core_rofs_image_all" % (rest_of_target_id))
       
   188                 rest_of_target_string += "\n\t"+command+"\n"
       
   189                 rest_of_all_target_string += " \\\n\t rest_%d" % rest_of_target_id
       
   190                 rest_of_target_id += 1
       
   191         variation_id += 1
       
   192         unzip_var_string += "\n\n"
       
   193         variation_image_all_string += "\n\n"
       
   194 
       
   195     all_string += "\\\n\te2flash_target_all\n\n"
       
   196     #print core_rofs_all_string
       
   197     core_rofs_all_string += "\n\n"
       
   198     rest_of_all_target_string += "\n\n"
       
   199     e2flash_all_string += "\n\n"
       
   200     foti_fota_all_string += "\n\n"
       
   201     core_rofs_string += "\n\n"
       
   202     rest_of_target_string += "\n\n"
       
   203     unzip_var_string += "\n\n"
       
   204     variation_image_all_string += "\n\n"
       
   205     e2flash_all_string += "\n\n"
       
   206     fin.write(all_string)
       
   207     fin.write(core_rofs_all_string)
       
   208     fin.write(rest_of_all_target_string)
       
   209     fin.write(unzip_var_string)
       
   210     fin.write(variation_image_all_string)
       
   211     fin.write(foti_fota_all_string)
       
   212     fin.write(core_rofs_string)
       
   213     fin.write(variation_image_string)
       
   214     fin.write(rest_of_target_string)
       
   215     fin.write(e2flash_all_string)
       
   216     fin.close()