gpt4 book ai didi

python 脚本执行 git 命令并在 html 页面上显示输出

转载 作者:行者123 更新时间:2023-12-01 05:23:58 24 4
gpt4 key购买 nike

我是 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

最佳答案

您可能有兴趣查看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 解开:)

关于python 脚本执行 git 命令并在 html 页面上显示输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21762573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com