用于执行 git 命令并在 HTML 页面上显示输出的 Python 脚本



我是python和git的新手。我试图编写一个 python 脚本来执行 git 命令,并在单击 html 页面上的按钮时在 html 页面上播放输出。我正在尝试使用子流程。波彭。但它什么也没显示。请帮帮我!!

#!/usr/bin/env python2.5
import cgi
import subprocess
import os
print "Content-type: text/htmln"
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 '%sn'
""" % 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 '%sn'
<h3> Git Repository successfully Initialized </h3>
</body>
</html>   
""" % out1

你可能有兴趣看看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 '%sn'
<h3> Git Repository successfully Initialized </h3>
</body>
</html>
""" % (REPO_DIR, status)

作为旁注:您可能对Jinja等模板引擎感兴趣,以使HTML和Python解开:)

相关内容

  • 没有找到相关文章