Blame TestTools/line_equivalent.py

Packit 857059
import sys
Packit 857059
import os
Packit 857059
import tempfile
Packit 857059
dict = {0:0}
Packit 857059
Packit 857059
# Help Text
Packit 857059
def usage():
Packit 857059
  print "Usage: python line_equivalent.py <file_name> <line_number> <buildFeatureDefs>\n"
Packit 857059
  print "The tool is used to find the equivalent line number for stripped file"
Packit 857059
  print "Input: file name, line number of stripped file (customer copy) and buildFeatureDefs file"
Packit 857059
  print "Output: line number in the code used by developers"
Packit 857059
  print "Note: make sure the buildFeatureDefs is the same that was used to create the customer release"
Packit 857059
Packit 857059
# Create a hash table line_num_of_stripped_file:line_num_of_orig_file
Packit 857059
def create_table(file_name, buildFeatureDefs):
Packit 857059
  # Create a temporary file, to store unifdef output
Packit 857059
  temp_file = tempfile.NamedTemporaryFile()
Packit 857059
  # Use unifdef to strip the file based on buildFeatureDefs,
Packit 857059
  # This temporary file should look same as the one in RPM 
Packit 857059
  cmd="unifdef -f %s %s > %s"%(buildFeatureDefs, file_name, temp_file.name)
Packit 857059
  os.system(cmd)
Packit 857059
  # Create table
Packit 857059
  line_num_temp_file=0
Packit 857059
  line_num_orig_file=0
Packit 857059
  with open(file_name) as orig_file, temp_file:
Packit 857059
    for orig_line in iter(orig_file.readline, ''):
Packit 857059
      line_num_orig_file += 1
Packit 857059
      last_pos = temp_file.tell()
Packit 857059
      if orig_line != temp_file.readline():
Packit 857059
        temp_file.seek(last_pos)
Packit 857059
        continue
Packit 857059
      line_num_temp_file += 1
Packit 857059
      dict[line_num_temp_file] = line_num_orig_file
Packit 857059
Packit 857059
# Query hash_table
Packit 857059
def query_table(line_number):
Packit 857059
  if int(line_number) in dict:
Packit 857059
    print dict[int(line_number)]
Packit 857059
  else:
Packit 857059
    print "Invalid query: Line number do not exist in the stripped file"
Packit 857059
Packit 857059
# main
Packit 857059
if len(sys.argv) != 4:
Packit 857059
  usage()
Packit 857059
  exit()
Packit 857059
Packit 857059
# Collect Input
Packit 857059
file_name = sys.argv[1]
Packit 857059
line_number = sys.argv[2]
Packit 857059
buildFeatureDefs=sys.argv[3]
Packit 857059
Packit 857059
# Check if file exists
Packit 857059
if not os.path.isfile(file_name):
Packit 857059
  print "Invalid file path - %s\nprovide complete path to the file"%file_name
Packit 857059
  exit(1)
Packit 857059
if not os.path.isfile(buildFeatureDefs):
Packit 857059
  print "Invalid file path - %s\nprovide complete path to the file"%buildFeatureDefs
Packit 857059
  exit(1)
Packit 857059
Packit 857059
create_table(file_name, buildFeatureDefs)
Packit 857059
query_table(line_number)