我在Python 3中编写CLI-based web server control panel作为Plesk的替代。该应用程序的一个功能是在现有项目目录中创建和配置Git存储库。有趣的是,我可以使用os.system()
执行所有Git函数,但git push -u origin master
除外。以下是相关代码:
external_command = " cd %s ; " % (dir_name, )
external_command+= " git push -u origin master "
os.system(external_command)
结果如下:
fatal: 'origin' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
但是,在CLI上运行完全相同的命令会导致成功:
$ cd /var/www/foobar.com ; git push -u origin master
Counting objects: 190, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (185/185), done.
Writing objects: 100% (190/190), 1.36 MiB, done.
Total 190 (delta 131), reused 0 (delta 0)
To ssh://alpha/var/git/foobar.com.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
这个问题可能是什么原因?我该如何进行故障排除?请注意,有问题的命令是最后一个命令,因此问题不能是其他步骤尚未完成。整个相关功能在this file (Github link)中为add_dir_to_git()
,也在此处复制:
def add_dir_to_git():
print('\nAdd Git repo.\n')
dir_name = input('Directory path to add' + environment.prompt)
git_name = input('Git repo path' + environment.prompt)
# TODO: Validate input
if not dir_name.endswith('/'):
dir_name += '/'
if not os.path.isdir(dir_name):
print('The specified directory does not seem to exist: %s' % (dir_name, ))
print('Aborting operation.')
return False
external_command = "cd %s ; " % (dir_name, )
external_command+= " git status 2>&1 "
git_check = os.popen(external_command).read().strip()
if not 'Not a git repository' in git_check:
print('The specified directory is already in a Git repository: %s' % (dir_name, ))
print('Aborting operation.')
return False
external_command = " find %s -name '.git' -type d " % (dir_name, )
subdirectory_git = os.popen(external_command).read().strip().split('\n')
subdirectory_git = [x for x in subdirectory_git if x!='']
if len(subdirectory_git)>0:
print("Git directories were found in the following subdirectories:")
for dirname in subdirectory_git:
print(" * %s" % (dirname, ))
continue_install = input("Continue? [y/N]" + environment.prompt).lower()
if not continue_install=='y' and not continue_install=='yes':
print('Aborting operation.')
return False
git_version = os.popen("git --version | awk '{print $3}'").read().strip().split('.')
if git_version[0]=='1' and git_version[1]=='9':
push_default = " git config push.default simple ; "
else:
push_default = ''
external_command = "cd %s ; " % (dir_name, )
external_command+= " git init ; "
external_command+= " git config user.name 'Do not work on the server' ; "
external_command+= " git config user.email 'do_not_work@on.the.server' ; "
external_command+= push_default
external_command+= " git add . ; "
external_command+= " git commit -am 'Initial commit' 2>&1 "
create_repo = os.popen(external_command).read().strip()
#TODO: Check!
git_config_file = dir_name + '.git/config'
git_config = open(git_config_file, 'a')
git_append = """
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = %s
[branch "master"]
remote = origin
merge = refs/heads/master
""" % (git_name, )
git_config.write(git_append)
# TODO: Check that the steps completed properly.
print('Done! If connecting to A NEW PUBLIC REPO then you must do an initial branch-configuration push. Would you like to do that now?')
initial_push = input('[y/N]' + environment.prompt).lower()
if initial_push=='y' or initial_push=='yes':
external_command = " cd %s ; " % (dir_name, )
external_command+= " git push -u origin master "
os.system(external_command)
return True
答案 0 :(得分:1)
使用python sh模块..它允许您将命令导入为函数。这比使用os.system更好,更加万无一失。 https://pypi.python.org/pypi/sh
然后你就可以做到:
from sh import git, cd
cd(dir_name)
git("push -u origin master")