python script to execute git command and display output on a html page

I’m a newbie to python and git. Im trying to write a python script to execute the git commands and diplay the output on a html page when a button is clicked on the html page. I’m trying to make use of subprocess.Popen. But it displays nothing. Please help me out!!

#!/usr/bin/env python2.5
import cgi
import subprocess
import os
print "Content-type: text/html\n"
print
print """\
<html>
<head>
<title> Initialization of Repository </title>
</head>
<body>
<h2><pre>              Initialization of Git Repo </pre></h2>
"""
pr=subprocess.Popen(['/usr/bin/git','init'],
    cwd=os.path.dirname('/home/manju/Desktop/manju/'),
    stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=False)
(out,error)=pr.communicate()
print """\
print '%s\n'
""" % out

pr=subprocess.Popen(['/usr/bin/git','status'],
    cwd=os.path.dirname('/home/manju/Desktop/manju/'),
    stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=False)
(out1,error)=pr.communicate()
 print """\
print '%s\n'
<h3> Git Repository successfully Initialized </h3>
</body>
</html>   
""" % out1

  • What is the best practice for dealing with passwords in github?
  • How can I configure git to open the relevant COMMIT_EDITMSG in sublime text 3?
  • Indenting Bash Script Output
  • GitHub - Pushed a change to master, but appears as different user than me?
  • $PATH extended at .bashrc not available within git hook script
  • Shell script - trying to validate if a git tag exists in a git repository in an if/else statement
  • Why doesn't gitignore work in this case?
  • GIT: change commit date to author date
  • How to delete “remote” branch that still shows locally?
  • Add line break to git commit -m from command line
  • How do medium to large development teams constantly push changes to a DVCS?
  • Cygwin, Git, win32 error when checking out tag
  • One Solution collect form web for “python script to execute git command and display output on a html page”

    You may be interested in having a look at GitPython. Install it via:

    $ sudo pip install GitPython
    

    And then replace your code with

    #!/usr/bin/env python2.5
    
    import os
    import os.path
    
    from git import *
    
    REPO_DIR = os.path.join(os.getcwd(), 'repository')
    repo = Repo.init(REPO_DIR)
    status = repo.git.status()
    
    print """\
    Content-type: text/html
    
    
    <html>
    <head>
    <title> Initialization of Repository </title>
    </head>
    <body>
    <h2><pre>              Initialization of Git Repo </pre></h2>
    Initialized empty Git repository at %s
    print '%s\n'
    <h3> Git Repository successfully Initialized </h3>
    </body>
    </html>
    """ % (REPO_DIR, status)
    

    As a side note: you may be interested in template engines such as Jinja to get HTML and Python disentangled 🙂

    Git Baby is a git and github fan, let's start git clone.