728x90
나는 Mac OSX Leopard와 Windows XP를 멀티부팅으로 쓰고 있다.
예를들어 윈도우 업데이트를 하고 나면 리부팅이 필요한데
이때 다시 윈도우로 부팅이 되기를 기대한다.
그러나 디폴트 설정이 맥으로 되어있기 때문에
잠깐 한눈을 팔고나면 어느새 맥이 부팅되고 있다.

귀찮게.. 또 리부팅..

그래서 이 코드를 만들었다. 마지막으로 부팅한 것을 기본으로 부팅 순서를 변경하는 프로그램이다.
만약 윈도우로 부팅했다면 리부팅시 다음은 윈도우로 부팅된다.
물론 자신이 설정해둔 시간동안은 멀티부팅 선택화면이 나타난다.
반대로 맥으로 부팅한 후 리부팅하면 다시 맥으로 부팅된다.

단점은 다음날 아무 생각없이 놔뒀다가 엉뚱한 OS가 뜰 수도 있다.
예를들어 잠깐 윈도우를 사용했다가 껐는데
다음날 당연히 맥으로 부팅이 될거라 생각했지만
윈도우가 부팅될 수 있다는것.

사용법은 윈도우랑 맥 또는 리눅스에 적당히 카피하고
부팅시에 파이썬으로 실행할 수 있도록 시작프로그램 혹은 crontab 등에
등록해두면 된다. 뭐 py2exe나 py2app 등으로 실행파일을 만들어 쓸 수도 있다.

뭐... 필요한 사람만 쓰시라...ㅋㅋ
아... 참고로 이게 python으로 개발한 두 번째 프로그램이다.
난 대놓고 말하지만 python 잘 모른다. ㅋㅋㅋ


#!/usr/bin/python
import re
import platform
import sys
import os

#define function : replace boot sequence
def replace_bootseq( word ) :
        global data
	for os in os_list :
		if re.search(word, os[1], re.I | re.S) :
			data = re.sub(re.escape(default), 'default=' + os[0], data) 
			#CR/LF for Windows
			data = re.sub('\r','',data)
			return

def check_platform() :
        #check platform and modify
        if platform.system() == 'Windows' : 
                
                if platform.release() == 'XP' :
                        replace_bootseq( 'Windows XP' )
                                
                elif platform.release() == 'Vista' :
                        replace_bootseq( 'Windows Vista' )

        elif platform.system() == 'Mac' or 'Darwin' :
                replace_bootseq( 'Mac' )

        elif platform.system() == 'Linux' :
                replace_bootseq( 'Linux' )

def change_bootseq() :
	#We cannot modify system file without this
	if platform.system() == 'Windows' :
		os.system("attrib -H -S " + sys.argv[1])
	
	f = file(sys.argv[1], 'w+')
        f.write(data)
        f.close()
	
	#back to original property of system file
	if platform.system() == 'Windows' :
		os.system("attrib +H +S " + sys.argv[1])

if len(sys.argv) == 1 :
	print 'Windows : chSeq c:\\boot.ini'
	print 'Mac : ./chSeq /Volumes/Windows/boot.ini'
	print 'Linux : ./chSeq /mnt/sda1/boot.ini'
	exit(1)
	
#read boot infomation
if not os.path.isfile(sys.argv[1]) :
	print sys.argv[1] + ' file does not exist.'
	exit(1)

f = file(sys.argv[1], 'r')
data = f.read()
f.close()

#get a default value
default = re.search('default=(.*)', data).group()

#extract os lists
m = re.compile('\\n([^\\n]*?)=\"(.*?)\"', re.I | re.S)
os_list = m.findall(data)

check_platform()
change_bootseq()

728x90
복사했습니다!