linux - permanently change directory python scripting/what environment do python scripts run in? -
i have small git_cloner script clones companies projects correctly. in scripts, use func hasn't given me problems yet:
def call_sp( command, **arg_list): p = subprocess.popen(command, shell=true, **arg_list) p.communicate()
at end of individual script, use:
call_sp('cd {}'.format(branch_path))
this line not change terminal ran script in directory branch_path, in fact, worse, annoyingly asks me password! when removing cd yadayada
line above, script no longer demands password before completing. wonder:
how these python scripts running? since
cd
command had no permanent effect. assume script splits own private subprocess separate terminal doing, kills when script finishes?based on how #1 works, how force scripts change terminal directory permanently save me time,
why merely running change directory ask me password?
the full script below, thank you,
cody
#!/usr/bin/env python import subprocess import sys import time os.path import expanduser home_path = expanduser('~') project_path = home_path + '/projects' d = {'cwd': ''} #calling script: # ./git_cloner.py projectname branchname # make new branch ./git_cloner.py project branchname #interactive: # 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 project name (i.e., mainworkproject):\n') if not project: continue break while true: branch = raw_input('enter 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 you're doing maybe need 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 make symlinks, 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))
you cannot use popen
change current directory of running script. popen
create new process own environment. if cd
within that, change directory running process, exit.
if want change directory script use os.chdir(path)
, subsequent commands in script run new path.
child processes cannot alter environment of parents though, can't have process create change environment of caller.
Comments
Post a Comment