简体   繁体   中英

git and python: Clone a repository using subprocess module in python

I have a simple html file which accepts the repository path as argument and passes it over to the python cgi script, which then clones the repository using the below command..

pr = subprocess.Popen(['/usr/bin/git clone ' + str(repoPath)],
   cwd=os.path.dirname('/clone/here/'),
   stdout=subprocess.PIPE,
   stderr=subprocess.PIPE,
   shell=True)
(out, error) = pr.communicate()
print out
print error

wherein, repoPath is the variable whose value i read from a html form. When i execute the above script, i get the below error

fatal: could not create work tree dir 'repository'.: Permission denied

But when i create a python script and directly run the above command, it works fine. Am i missing something here?

The directory already exists, and is owned by a different user (most likely your shell login if you tested manually).

What you're doing is really unsafe. Instead you should create a unique directory and clone in there. I found this example tempdir function. (Note: there's a risk of endless loop if you can't write to $dir )

That way you will avoid any name collisions.

Should be doing something like:

subprocess.Popen(['git', 'clone', str(repoPath), '/clone/here'])

I don't really understand what is the deal with the cwd statement you have there, but you need to close into the right location, otherwise the cwd could be anything, like '/'.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM