views:

344

answers:

3

I'm trying to make a web app that will manage my Mercurial repositories for me. I want it so that when I tell it to load repository X:

  • Connect to a MySQL server and make sure X exists.
  • Check if the user is allowed to access the repository.
  • If above is true, get the location of X from a mysql server.
  • Run a hgweb cgi script (python) containing the path of the repository.

Here is the problem, I want to: take the hgweb script, modify it, and run it. But I do not want to: take the hgweb script, modify it, write it to a file and redirect there. I am using Apache to run the httpd process.

+2  A: 

You can run shell scripts from within PHP. There are various ways to do it, and complications with some hosts not providing the proper permissions, all of which are well-documented on php.net. That said, the simplest way is to simply enclose your command in backticks. So, to unzip a file, I could say:

`unzip /path/to/file`

SO, if your python script is such that it can be run from a command-line environment (or you could modify it so to run), this would seem to be the preferred method.

Ryan Ballantyne
This doesn't answer his question at all.
Paul McMillan
@Paul McMillan: Pretty sure it does answer the question. The question is "How can I execute CGI files from PHP?", and the backtick operator is the way to go.
zombat
A: 

As far as you question, no, you're not likely to get php to execute a modified script without writing it somewhere, whether that's a file on the disk, a virtual file mapped to ram, or something similar.

It sounds like you might be trying to pound a railroad spike with a twig. If you're to the point where you're filtering access based on user permissions stored in MySQL, have you looked at existing HG solutions to make sure there isn't something more applicable than hgweb? It's really built for doing exactly one thing well, and this is a fair bit beyond it's normal realm.

I might suggest looking into apache's native authentication as a more convenient method for controlling access to repositories, then just serve the repo without modifying the script.

Paul McMillan
+2  A: 

Ryan Ballantyne has the right answer posted (I upvoted it). The backtick operator is the way to execute a shell script.

The simplest solution is probably to modify the hgweb script so that it doesn't "contain" the path to the repository, per se. Instead, pass it as a command-line argument. This means you don't have to worry about modifying and writing the hgweb script anywhere. All you'd have to do is:

//do stuff to get location of repository from MySQL into variable $x
//run shell script
$res = `python hgweb.py $x`;
zombat
presumably the poster already knew about executing a script with php, since he specifically says he wants to do something more complex than that. Your answer does solve the problem though.
Paul McMillan
Yeah, tough to interpret I guess, the title question and the posted question contradict each other a bit.
zombat