interesting enough, when I tried to clone object using bash shell
ssh -t -i ~/.ssh/security.pem root@xx.xxx.xx.xx '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 root@xx.xxx.xx.xx '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)
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.
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?!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.