1

I have a small git_cloner script that clones my companies projects correctly. In all my scripts, I use a func that hasn't given me problems yet:

def call_sp(
command, **arg_list):
        p = subprocess.Popen(command, shell=True, **arg_list)
        p.communicate()

At the end of this individual script, I use:

call_sp('cd {}'.format(branch_path))

This line does not change the terminal I ran my script in to the directory branch_path, in fact, even worse, it annoyingly asks me for my password! When removing the cd yadayada line above, my script no longer demands a password before completing. I wonder:

  1. How are these python scripts actually running? Since the cd command had no permanent effect. I assume the script splits its own private subprocess separate from what the terminal is doing, then kills itself when the script finishes?

  2. Based on how #1 works, how do I force my scripts to change the terminal directory permanently to save me time,

  3. Why would merely running a change directory ask me for my password?

The full script is below, thank you,

Cody

#!/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))

1 Answer 1

1

You cannot use Popen to change the current directory of the running script. Popen will create a new process with its own environment. If you do a cd within that, it will change directory for that running process, which will then immediately exit.

If you want to change the directory for the script you could use os.chdir(path), then all subsequent commands in the script will be run from that new path.

Child processes cannot alter the environment of their parents though, so you can't have a process you create change the environment of the caller.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.