Python将文件推送到没有本地工作目录的Github远程仓库 [英] Python push files to Github remote repo without local working directory

查看:228
本文介绍了Python将文件推送到没有本地工作目录的Github远程仓库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基于Python的Web应用程序,用于协作XML /文档编辑,并且客户端的一个要求是用户应该能够将他们创建的文件(并保存在服务器上)直接推送到Github远程回购站,而不需要在服务器上创建本地克隆(即没有本地工作目录或任何类型的跟踪)。在图形用户界面中,这将对应于转到Github网站,并通过单击上传文件或创建新文件按钮手动将文件添加到远程回购站,或者简单地编辑Github网站上远程回购站上的现有文件然后在Web浏览器中提交更改。我不知道这个功能甚至有可能使用一些Python Github模块来实现,或者使用Github API或从头开始编写一些代码? 解决方案



让我们使用github3.py作为示例如何做到这一点:

  import github3 

gh = github3.login(username =' foo',password ='bar')
repository = gh.repository('organization-name','repository-name')
for file_info in files_to_upload:
with open(file_info,' rb')作为fd:
contents = fd.read()
repository.create_file(
path = file_info,
message ='开始跟踪{!r}'。format file_info),
content = contents,

您需要检查它返回一个你期望验证文件w的对象成功上传。您还可以指定提交者作者字典,以便您可以将提交归因于您的服务,以便人员不在假设该人在本地git设置中创作它。

I am working on a Python based web application for collaborative xml/document editing, and one requirement from the client is that users should be able to push the files they created (and saved on the server) directly to a Github remote repo, without the need of ever creating a local clone on the server (i.e., no local working directory or tracking of any sort). In GUI terms, this would correspond to going to Github website and manually add the file to the remote repo by clicking the "Upload files" or "create new file" button, or simply edit the existing file on the remote repo on the Github website and then commit the change inside the web browser. I wonder is this functionality even possible to achieve either using some Python Github modules or writing some code from scratch using the Github API or something?

解决方案

So you can create files via the API and if the user has their own GitHub account, you can upload it as them.

Let's use github3.py as an example of how to do this:

import github3

gh = github3.login(username='foo', password='bar')
repository = gh.repository('organization-name', 'repository-name')
for file_info in files_to_upload:
    with open(file_info, 'rb') as fd:
        contents = fd.read()
    repository.create_file(
        path=file_info,
        message='Start tracking {!r}'.format(file_info),
        content=contents,
    )

You will want to check that it returns an object you'd expect to verify the file was successfully uploaded. You can also specify committer and author dictionaries so you could attribute the commit to your service so people aren't under the assumption that the person authored it on a local git set-up.

这篇关于Python将文件推送到没有本地工作目录的Github远程仓库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆