簡體   English   中英

如何使用Python以編程方式歸檔遠程git存儲庫?

[英]How to archive a remote git repository programmatically using Python?

我正在嘗試使用Python代碼歸檔遠程git repo。 我使用Git命令行和以下命令成功完成了此操作。

> git archive --format=zip --remote=ssh://path/to/my/repo -o archived_file.zip     
HEAD:path/to/directory filename

此命令從存儲庫中提取所需的文件,並將zip文件存儲在我當前的工作目錄中。 請注意,沒有克隆遠程倉庫。

現在,我必須使用Python代碼來完成此操作。 我正在使用GitPython 1.0.1。 我想如果使用命令行是可行的,那么應該使用GitPython庫實現。 根據文檔

repo.archive(open(join(rw_dir, 'archived_file.zip'), 'wb'))

上面的代碼行將存檔存儲庫。 這里repo是Repo類的實例。 可以使用初始化

repo = Repo('path to the directory which has .git folder')

如果我在上一行中給出了我的遠程倉庫(例如ssh:// path / to / my / repo)的路徑,它將在包含該代碼的.py文件所在的目錄中找到它(例如,Path \\ to \\ python \\ file \\ ssh:\\ path \\ to \\ my \\ repo),這不是我想要的。 因此,總而言之,我可以使用GitPython來存檔本地倉庫,而不是遠程倉庫。 如果我能夠創建指向遠程倉庫的倉庫實例,則可以存檔遠程倉庫。 我對Git和Python很陌生。

有什么方法可以使用Python代碼歸檔遠程倉庫,而無需在本地克隆它?

順便說一句,這是一個糟糕的主意,因為您已經開始使用gitpython,而我從未嘗試過使用它,但是我只想讓您知道,無需使用gitpython就可以在不將其克隆到本地的情況下進行操作。

只需使用子進程在外殼中運行git命令即可。 在python中運行bash命令


編輯:添加了一些演示代碼,讀取stdout和編寫stdin。

其中一些是從這里偷來的: http : //eyalarubas.com/python-subproc-nonblock.html

剩下的是一個小演示。.前兩個先決條件

shell.py

import sys
while True:
    s = raw_input("Enter command: ")
    print "You entered: {}".format(s)
    sys.stdout.flush()

nbstreamreader.py:

from threading import Thread
from Queue import Queue, Empty

class NonBlockingStreamReader:

    def __init__(self, stream):
        '''
        stream: the stream to read from.
                Usually a process' stdout or stderr.
        '''

        self._s = stream
        self._q = Queue()

        def _populateQueue(stream, queue):
            '''
            Collect lines from 'stream' and put them in 'quque'.
            '''

            while True:
                line = stream.readline()
                if line:
                    queue.put(line)
                else:
                    raise UnexpectedEndOfStream

        self._t = Thread(target = _populateQueue,
                args = (self._s, self._q))
        self._t.daemon = True
        self._t.start() #start collecting lines from the stream

    def readline(self, timeout = None):
        try:
            return self._q.get(block = timeout is not None,
                    timeout = timeout)
        except Empty:
            return None

class UnexpectedEndOfStream(Exception): pass

然后是實際代碼:

from subprocess import Popen, PIPE
from time import sleep
from nbstreamreader import NonBlockingStreamReader as NBSR

# run the shell as a subprocess:
p = Popen(['python', 'shell.py'],
        stdin = PIPE, stdout = PIPE, stderr = PIPE, shell = False)
# wrap p.stdout with a NonBlockingStreamReader object:
nbsr = NBSR(p.stdout)
# issue command:
p.stdin.write('command\n')
# get the output
i = 0
while True:
    output = nbsr.readline(0.1)
    # 0.1 secs to let the shell output the result
    if not output:
        print "time out the response took to long..."
        #do nothing, retry reading..
        continue
    if "Enter command:" in output:
        p.stdin.write('try it again' + str(i) + '\n')
        i += 1
    print output

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM