'''
Created on June 3, 2012
Modified on June 26, 2012
@author: Dilum Bandara
@version: 0.1
@license: Apache License v2.0

   Copyright 2012 H. M. N. Dilum Bandara and Anura P. Jayasumana, Colorado State
   University

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
'''

'''
Find physical machines which may appear as different machine due to
software of hardware changes. Use serial no to find them
'''

fdIn = open('machines.txt', 'r')
fdOut = open('sameMachine.txt', 'w')
machines = {}
reappear = [] #Machines that reappear with a different ID
firstLine = False
for line in fdIn:
    if firstLine == False: 
        firstLine = True
        continue
    tmp = line.split()
    if tmp[-1] in machines: 
        if machines[tmp[-1]] < int(tmp[0]):
            fdOut.write(str(machines[tmp[-1]]) + '\t' + tmp[0] + '\n')
        else:
            fdOut.write(tmp[0] + '\t' + str(machines[tmp[-1]]) + '\n')
        if machines[tmp[-1]] in reappear:
            print 'Appreaing for third time', machines[tmp[-1]] 
        reappear.append(machines[tmp[-1]])
    else:
        machines[tmp[-1]] = int(tmp[0])
fdIn.close()
fdOut.close()
print 'No of distinct machines', len(machines)
