永久更改目录 python scripting/what 环境做 python 脚本 运行 中?
permanently change directory python scripting/what environment do python scripts run in?
我有一个 git_cloner 小脚本可以正确克隆我公司的项目。在我所有的脚本中,我使用了一个还没有给我带来问题的函数:
def call_sp(
command, **arg_list):
p = subprocess.Popen(command, shell=True, **arg_list)
p.communicate()
在这个单独的脚本的末尾,我使用:
call_sp('cd {}'.format(branch_path))
这一行并没有改变我 运行 我的脚本进入目录 branch_path 的终端,事实上,更糟糕的是,它烦人地询问我的密码!删除上面的 cd yadayada
行时,我的脚本在完成前不再需要密码。我想知道:
这些 python 脚本实际上 运行 怎么样?由于 cd
命令没有永久效果。我假设脚本将其自己的私有子进程与终端正在执行的操作分开,然后在脚本完成时自行终止?
根据#1 的工作原理,我如何强制我的脚本永久更改终端目录以节省时间,
为什么只是 运行 更改目录就要求我输入密码?
完整的脚本如下,谢谢,
科迪
#!/usr/bin/env python
import subprocess
import sys
import time
from os.path import expanduser
home_path = expanduser('~')
project_path = home_path + '/projects'
d = {'cwd': ''}
#calling from script:
# ./git_cloner.py projectname branchname
# to make a new branch say ./git_cloner.py project branchname
#interactive:
# just run ./git_cloner.py
if len(sys.argv) == 3:
project = sys.argv[1]
branch = sys.argv[2]
if len(sys.argv) < 3:
while True:
project = raw_input('Enter a project name (i.e., mainworkproject):\n')
if not project:
continue
break
while True:
branch = raw_input('Enter a branch name (i.e., dev):\n')
if not branch:
continue
break
def call_sp(command, **arg_list):
p = subprocess.Popen(command, shell=True, **arg_list)
p.communicate()
print "making new branch \"%s\" in project \"%s\"" % (branch, project)
this_project_path = '%s/%s' % (project_path, project)
branch_path = '%s/%s' % (this_project_path, branch)
d['cwd'] = project_path
call_sp('mkdir %s' % branch, **d)
d['cwd'] = branch_path
git_string = 'git clone ssh://git@git/home/git/repos/{}.git {}'.format(project, d['cwd'])
#see what you're doing to maybe need to cancel
print '\n'
print "{}\n\n".format(git_string)
call_sp(git_string)
time.sleep(30)
call_sp('git checkout dev', **d)
time.sleep(2)
call_sp('git checkout -b {}'.format(branch), **d)
time.sleep(5)
#...then I make some symlinks, which work
call_sp('cp {}/dev/settings.py {}/settings.py'.format(project_path, branch_path))
print 'dont forget "git push -u origin {}"'.format(branch)
call_sp('cd {}'.format(branch_path))
您不能使用 Popen
更改 运行ning 脚本的当前目录。 Popen
将创建一个具有自己环境的新进程。如果您在其中执行 cd
,它将更改该 运行ning 进程的目录,然后该进程将立即退出。
如果您想要更改脚本的目录,您可以使用 os.chdir(path)
,那么脚本中的所有后续命令都将来自该新路径 运行。
虽然子进程无法改变其父进程的环境,因此您不能让您创建的进程改变调用者的环境。
我有一个 git_cloner 小脚本可以正确克隆我公司的项目。在我所有的脚本中,我使用了一个还没有给我带来问题的函数:
def call_sp(
command, **arg_list):
p = subprocess.Popen(command, shell=True, **arg_list)
p.communicate()
在这个单独的脚本的末尾,我使用:
call_sp('cd {}'.format(branch_path))
这一行并没有改变我 运行 我的脚本进入目录 branch_path 的终端,事实上,更糟糕的是,它烦人地询问我的密码!删除上面的 cd yadayada
行时,我的脚本在完成前不再需要密码。我想知道:
这些 python 脚本实际上 运行 怎么样?由于
cd
命令没有永久效果。我假设脚本将其自己的私有子进程与终端正在执行的操作分开,然后在脚本完成时自行终止?根据#1 的工作原理,我如何强制我的脚本永久更改终端目录以节省时间,
为什么只是 运行 更改目录就要求我输入密码?
完整的脚本如下,谢谢,
科迪
#!/usr/bin/env python
import subprocess
import sys
import time
from os.path import expanduser
home_path = expanduser('~')
project_path = home_path + '/projects'
d = {'cwd': ''}
#calling from script:
# ./git_cloner.py projectname branchname
# to make a new branch say ./git_cloner.py project branchname
#interactive:
# just run ./git_cloner.py
if len(sys.argv) == 3:
project = sys.argv[1]
branch = sys.argv[2]
if len(sys.argv) < 3:
while True:
project = raw_input('Enter a project name (i.e., mainworkproject):\n')
if not project:
continue
break
while True:
branch = raw_input('Enter a branch name (i.e., dev):\n')
if not branch:
continue
break
def call_sp(command, **arg_list):
p = subprocess.Popen(command, shell=True, **arg_list)
p.communicate()
print "making new branch \"%s\" in project \"%s\"" % (branch, project)
this_project_path = '%s/%s' % (project_path, project)
branch_path = '%s/%s' % (this_project_path, branch)
d['cwd'] = project_path
call_sp('mkdir %s' % branch, **d)
d['cwd'] = branch_path
git_string = 'git clone ssh://git@git/home/git/repos/{}.git {}'.format(project, d['cwd'])
#see what you're doing to maybe need to cancel
print '\n'
print "{}\n\n".format(git_string)
call_sp(git_string)
time.sleep(30)
call_sp('git checkout dev', **d)
time.sleep(2)
call_sp('git checkout -b {}'.format(branch), **d)
time.sleep(5)
#...then I make some symlinks, which work
call_sp('cp {}/dev/settings.py {}/settings.py'.format(project_path, branch_path))
print 'dont forget "git push -u origin {}"'.format(branch)
call_sp('cd {}'.format(branch_path))
您不能使用 Popen
更改 运行ning 脚本的当前目录。 Popen
将创建一个具有自己环境的新进程。如果您在其中执行 cd
,它将更改该 运行ning 进程的目录,然后该进程将立即退出。
如果您想要更改脚本的目录,您可以使用 os.chdir(path)
,那么脚本中的所有后续命令都将来自该新路径 运行。
虽然子进程无法改变其父进程的环境,因此您不能让您创建的进程改变调用者的环境。