updated
[mlfp.git] / traces / convert-traces.py
blob1fbf84e348cf94490b6e1ddd5829965d3dc96e18
1 #!/usr/bin/python
3 import commands
4 import sys
6 class FunctionRepository:
7 def __init__(self):
8 self.function_to_index = {}
9 self.next_index = 1
10 self.functions = []
11 def GetIndex(self, function_name):
12 if not self.function_to_index.has_key(function_name):
13 self.function_to_index[function_name] = self.next_index
14 self.next_index += 1
15 self.functions.append(function_name)
16 return self.function_to_index[function_name]
17 def ToString(self):
18 # Print out a description of the form function_name:function_index
19 ret_str = ''
20 for i in range(len(self.functions)):
21 if ret_str:
22 ret_str += ' '
23 ret_str += self.functions[i] + ':' + str(i + 1)
24 return ret_str
26 class Sequence:
27 def __init__(self, name):
28 self.name = name
29 self.observations = []
30 def AddIndex(self, index):
31 self.observations.append(index)
32 def ToString(self):
33 ret = ' '.join([str(obs) for obs in self.observations])
34 ret += '\n' + self.name
35 return ret
37 class Sequences:
38 def __init__(self):
39 self.sequences = []
40 self.function_repos = FunctionRepository()
41 def AddFile(self, filename):
42 infile = open(filename, 'r')
43 sequence = Sequence(filename)
44 for line in infile:
45 function = line.strip()
46 index = self.function_repos.GetIndex(function)
47 sequence.AddIndex(index)
48 self.sequences.append(sequence)
49 def Print(self):
50 # Print the function repository
51 print self.function_repos.ToString()
52 # Print out each sequence
53 for seq in self.sequences:
54 print seq.ToString()
56 def GetFiles(pattern):
57 (status, output) = commands.getstatusoutput('ls %s' % pattern)
58 if not status:
59 return output.split('\n')
60 return None
62 def main(argv):
63 # argv[1] gives a comma separated list of patterns to search for files.
64 patterns = argv[1]
65 files = []
66 for pattern in patterns.split(','):
67 matched_files = GetFiles(pattern)
68 if matched_files and matched_files != []:
69 files.extend(matched_files)
70 sequences = Sequences()
71 for filename in files:
72 sequences.AddFile(filename)
73 sequences.Print()
75 if __name__ == '__main__':
76 main(sys.argv)