我是python和git的新手。我试图编写一个python脚本来执行git命令,并在html页面上单击一个按钮时在html页面上显示输出。我正在尝试使用subprocess.Popen。但它什么都没显示。请帮帮我!!
#!/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
答案 0 :(得分:2)
您可能有兴趣查看GitPython。通过以下方式安装:
$ sudo pip install GitPython
然后用
替换你的代码#!/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)
作为旁注:您可能对Jinja之类的模板引擎感兴趣,以便将HTML和Python解开:)