2016-07-23 159 views
0

我想創建一個腳本來自動編譯Apache。可悲的是,在我的工作中,我需要編譯每個安裝的apache。
所以,我想出了這個小代碼來運行一個命令:python上運行命令

print("Source location %s" % source_location) 
print("Configure command %s" % configure_command) 
config = subprocess.Popen(configure_command, stdout=subprocess.PIPE, shell=True) 
(output, err) = config.communicate() 
config_status = config.wait() 
print("Return configure status = %s" % config_status) 

目前我卡上的配置部分。
基本上configure行是這樣的:

/Volumes/nirvash/script/workarea/httpd-2.2.31/configure前綴=/TMP /阿帕奇-2.2.31-INSTANCE1 --enable- mods-shared = all --enable-proxy --enable-proxy-connect --enable-proxy-ftp --enable-proxy-http --enable-deflate --enable-cache --enable-disk-cache - enable-mem-cache --enable-file-cache --with-included-apr --with-mpm = worker

問題是,當apache編譯時,它會創建(mkdir)「include 「目錄裏面的httpd-2.2.31。但在這種情況下,該目錄是在我的腳本的bin目錄中創建的。
因此,腳本正在運行時創建目錄。

可以解決這個問題嗎?有沒有辦法在編譯的目錄中運行configure?

回答

1

您可以使用os.chdir將腳本的當前目錄更改爲包含源代碼的目錄。

os.chdir(source_location) 

另外,還可以使用cd運行configure之前更改configure_command先更改目錄。

configure_command = 'cd "%s" && %s' % (source_location, configure_command)