0
votes

interesting enough, when I tried to clone object using bash shell

ssh -t -i ~/.ssh/security.pem [email protected] 'rm -rf myproject && git clon -b mybranch https://github.com/myproject.git'

everything works beautifully.

But when I tried to do it from python subprocess call, like

subprocess.check_call("ssh -t -i ~/.ssh/security.pem [email protected] 'rm -rf myproject && git clone -b mybranch https://github.com/myproject.git'", shell=True)

then I will get the following error:

fatal: destination path 'myproject' already exists and is not an empty directory.
Traceback (most recent call last):
File "", line 1, in
File "C:\Python27\lib\subprocess.py", line 540, in check_call
raise CalledProcessError(retcode, cmd)

2
run check_call("ssh remote-host 'hostname && hostname'", shell=True) or (better) check_call(["ssh", "remote-host", "hostname && hostname"]) to see that both commands are executed on the remote host. Python is not the issue, the issue is in your actual command (not the one that you've shown) -- check for stray single quotes.jfs

2 Answers

-1
votes

If you are doing a long bash command, you have to split up its arguments into a list. Like if I wanted to run "ls -a" using the subprocess library I would have to do:

subprocess.call(["ls","-a"])

Check out the docs for reference: https://docs.python.org/2/library/subprocess.html

But, if you still have trouble removing the folder, shutil.rmtree() will work, using the shutil library.

-1
votes

Here is what I find out: shell=True. For some reason, the python will execute the first shell command 'rm -rf myproject' in the remote machine and then close the SSH connection, finally to execute the second command 'git clone -b branch https://github.com/myproject.git' in the local machine. In my case, I have the same git repository 'myproject' in my local directory, so git tries to clone in my local directory and complains it. After changing to shell=False, or leave it out, then everything works. Not sure about why the shell value can cause that?!