【问题标题】:Python update files on Github remote repo without local working directory没有本地工作目录的 Github 远程 repo 上的 Python 更新文件
【发布时间】:2017-03-11 12:25:16
【问题描述】:

这是关于在没有本地工作目录的情况下推送到远程仓库的问题 (Python push files to Github remote repo without local working directory) 的后续问题。我想知道如果远程存储库中已经存在该文件并且我只想使用修改后的同名文件来更新它怎么办? (例如,相当于在Github网站上,上传远程已经存在的文件的修改版本)

编辑:我们想出了一个解决方案:

contents_object = repository.contents(file_path)
push_status = contents_object.update("test_message",contents)

然而,虽然这在一台机器上成功运行,但它在另一台机器上抛出了一个错误(具体来说,第一行将得到一个 AttributeError)。这是因为 github3 的版本可能不同吗?

【问题讨论】:

  • 您是否使用某些包与他们的 API 进行通信?您上一个问题的答案使用 github3。你看过他们的文档吗?
  • 对不起,我正在使用 github3,正如原始帖子的答案所建议的那样。我确实查看了文档,但不清楚哪种方法需要本地工作目录

标签: python github github-api


【解决方案1】:

似乎很清楚,在 github3 版本 0.9.6 下,到目前为止,您将使用 pip install github3.py(https://github3py.readthedocs.io/en/master/#installation) 获得它,这将起作用(在没有任何本地工作目录的情况下更新远程 repo ):

def update_to_git(username,password,path,account,repo,message):
    files_to_upload = [path]
    gh = github3.login(username=username, password=password)
    repository = gh.repository(account, repo)
    for file_info in files_to_upload:
        with open(file_info, 'rb') as fd:
            contents = fd.read()
        contents_object = repository.contents(file_info)
        contents_object.update(message,contents)

但是,如果你有 github3 版本 1.0.0a4,这将不起作用。具体来说,您将在contents_object = repository.contents(file_info) 行中获得AttributeError,这可能是由于github3 中的实现发生了变化。

【讨论】:

  • 在 v1.0.0a4 中,Repository.contents() 已拆分为 Repository.directory_contents()Repository.file_contents()
猜你喜欢
  • 2017-02-05
  • 2013-01-09
  • 1970-01-01
  • 2013-08-22
  • 1970-01-01
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
相关资源
最近更新 更多