python - 如何使用python将本地文件推送到github? (或通过 Python 发布提交)

标签 python git subprocess popen

有哪些选项可以将文件从 python 提交和推送到 github?

以下是我认为应该可行的三种方法,因此依次尝试:

  1. 使用 pygithub : (Github 的 python API)将推送请求发送到我的存储库。失败,因为我在 API 中找不到推送功能。我可以看到编辑文件,但是当我计划经常替换文件时这无济于事。

  2. 在 python 子进程 (HTTPS) 的命令行中使用 git push: 这几乎可行,但我不知道如何填写用户和需要密码字段。尝试:

    import subprocess
    from pexpect import popen_spawn
    
    
    user = 'GithubUsername'
    password = '***********'
    
    cmd = "cd C:\\Users\Dropbox\git-test"
    returned_value = subprocess.call(cmd, shell=True)  # returns the exit code in unix
    
    cmd = "git add ." 
    subprocess.call(cmd, shell=True)
    
    cmd = 'git commit -m "python project update"'
    subprocess.call(cmd, shell=True)
    
    cmd = "git remote set-url origin https://github.com/Tehsurfer/git-test.git"
    subprocess.call(cmd, shell=True)
    
    cmd = "git push "
    child_process = popen_spawn.PopenSpawn(cmd)
    child_process.expect('User')
    child_process.sendline(user)
    child_process.expect('Password')
    child_process.sendline(password)
    print('returned value:', returned_value)
    
    print('end of commands')`
    
  3. 在 python 子进程 (SSH) 的命令行中使用 git push: 我在这里遇到的问题是我找不到创建 ssh 的方法Windows 命令提示符中的代理程序。我已经能够通过 this tutorial 在 MINGW64 终端中轻松创建一个,但无法通过 Python 与之交互。

最佳答案

How do I push new files to GitHub?

一个非常相似的问题,我能够修改谁的代码以通过 python 将多个文件推送到 github:

import base64
from github import Github
from github import InputGitTreeElement

user = "GithubUsername"
password = "*********"
g = Github(user,password)
repo = g.get_user().get_repo('git-test') # repo name
file_list = [
    'C:\\Users\jesse\Dropbox\Swell-Forecast\git-test\index.html',
    'C:\\Users\jesse\Dropbox\Swell-Forecast\git-test\margin_table.html'
]
file_names = [
    'index.html',
    'margin_table.html'
]
commit_message = 'python commit'
master_ref = repo.get_git_ref('heads/master')
master_sha = master_ref.object.sha
base_tree = repo.get_git_tree(master_sha)

element_list = list()
for i, entry in enumerate(file_list):
    with open(entry) as input_file:
        data = input_file.read()
    if entry.endswith('.png'): # images must be encoded
        data = base64.b64encode(data)
    element = InputGitTreeElement(file_names[i], '100644', 'blob', data)
    element_list.append(element)

tree = repo.create_git_tree(element_list, base_tree)
parent = repo.get_git_commit(master_sha)
commit = repo.create_git_commit(commit_message, tree, [parent])
master_ref.edit(commit.sha)

关于python - 如何使用python将本地文件推送到github? (或通过 Python 发布提交),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50071841/

相关文章:

python - 属性错误 : 'map' obejct has no attribute 'index' (python 3)

python - Scrapy只爬取一页

python - 通过 Python 获取 Git 存储库文件的最后一次提交时间?

python - 当 grep 找不到匹配项时,使用 grep 命令的 subprocess.check_output 失败

python - subprocess ssh 命令对某些命令失败但对其他命令失败(命令在终端中运行)

python - Matlab 立即返回退出代码

python - 矩阵和向量的每一列之间的numpy协方差

python - 抓取文章分享计数

git - 如何在 Git 扩展中配置 KDiff3?

git - 将分支推送到私有(private)仓库