简体   繁体   English

在python上运行命令

[英]Run commands on python

I'm trying to create a script to automatically compile apache. 我正在尝试创建一个脚本来自动编译apache。 Sadly on my work I need to compile each an every apache I install. 可悲的是,在我的工作中,我需要编译我安装的每个Apache。
So, I came up with this little code to run a command: 因此,我想到了以下代码来运行命令:

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)

At the moment I'm stuck on the configure part. 目前,我还停留在配置部分。
Basically the configure line is like this: 基本上,配置行是这样的:

/Volumes/nirvash/script/workarea/httpd-2.2.31/configure --prefix=/tmp/apache-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 /Volumes/nirvash/script/workarea/httpd-2.2.31/configure --prefix = / tmp / apache-2.2.31-instance1 --enable-mods-shared = all --enable-proxy --enable-proxy-连接--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

The problem is that when the apache is compiling, it creates (mkdir) an "include" directory inside the httpd-2.2.31. 问题在于,在编译apache时,它将在httpd-2.2.31内创建(mkdir)一个“ include”目录。 But in this case the directory is created on the bin directory of my script. 但是在这种情况下,该目录是在脚本的bin目录中创建的。
So the directory is created were the script is running. 因此,目录是在脚本运行时创建的。

Is it possible to fix this? 有可能解决这个问题吗? Is there any way to run the configure in the directory that is compiling? 有什么方法可以在正在编译的目录中运行配置?

You can use os.chdir to change the current directory of your script to be the same as the directory which contains the source code. 您可以使用os.chdir将脚本的当前目录更改为与包含源代码的目录相同的目录。

os.chdir(source_location)

Alternately, you could change configure_command to first change directories using cd prior to running configure . 或者,您可以将configure_command更改为先使用cd更改目录,然后再运行configure

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM