Host 정보 변경 파이썬 코드

2009. 7. 21. 15:02내꺼

mac에서 불편하던 중 대략 만들어 봤따.
 
...
@ by ChangYeol,Kim
@ http://umakingtistory.com
'''
pt1 = '/uma/'
pt2 = '.txt'
hostList = ['1.Development', '2.Staging', '4.Production']

def chooseHostList():
   hosts = readFile('/etc/hosts')
   i = 0
   while(i < len(hostList)):
      src_buf = readFile(pt1 + hostList[i] + pt2)
      if hosts == src_buf:
        print '[*', (i+1), '] ', hostList[i]
      else:
        print '[ ', (i+1), '] ', hostList[i]
      i = i + 1

def loopMain():
   while True:
      print('=====================================')
      chooseHostList()
      print('-------------------------------------')
      inp = input('==>')
      if inp > 0 and inp <= len(hostList):
         buf = readFile(pt1 + hostList[inp-1] + pt2)
         writeFileToHosts(buf)
      else:
         print()

def readFile(filename):
   sf = open(filename)
   buf = sf.read()
   sf.close()
   return buf

def writeFileToHosts(buf):
   tf = open('/etc/hosts', 'w')
   tf.write(buf)
   tf.close()

loopMain()