Python 一次性从Gitolite克隆repos

Python 一次性从Gitolite克隆repos,python,git,ssh,gitolite,Python,Git,Ssh,Gitolite,以下是我试图简化准备工作环境的过程。假设您的团队有许多Gitolite托管的存储库,并且您希望: 获取他们的完整列表 尽可能简单快速地克隆其中一些 这个脚本允许它。因此,上述问题的答案如下: 显示gitolite回购: ssh git@server_ip 以及以下克隆代码: #!/usr/bin/python import subprocess import sys import os import re FILTER = None # check shell arguments i

以下是我试图简化准备工作环境的过程。假设您的团队有许多Gitolite托管的存储库,并且您希望:

  • 获取他们的完整列表
  • 尽可能简单快速地克隆其中一些
这个脚本允许它。因此,上述问题的答案如下:

显示gitolite回购:

ssh git@server_ip
以及以下克隆代码:

#!/usr/bin/python

import subprocess
import sys
import os
import re

FILTER = None

# check shell arguments
if len(sys.argv) == 2:
    FILTER = sys.argv[1]

GIT_PATH = "git@server_ip"

# run system command and retrieve result
p = subprocess.Popen("ssh " + GIT_PATH, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()

output = re.sub(r'^[\S\s]*?(?=web/)', '', output)
output = re.sub(r'R|W', '', output)

output = output.split()


# filter function
def f(line):
    if line.find(FILTER) != -1:
         return True

    return False


if FILTER:
    output = filter(f, output)

yes = 'yes'
no = 'no'

for line in output:
    print(line)
    choice = input("Do you want to clone this repo (yes/no): ")

    if choice == yes:
        os.system("git clone " + GIT_PATH + ':/' + line.strip())
        print('Done!')

print('All work is done! Congrats!')
语法:python clone.py[filter] 指定筛选器时,只允许获取包含筛选器字符串作为子字符串的repo


欢迎提出改进建议!谢谢。

您这里有实际的、具体的问题吗?没有,实际上我想与其他人分享一些有用的片段,并可能获得一些建议。请编辑此内容以匹配stackoverflow格式,将您的问题作为问题,将您的答案作为答案。与拼写和语法一样,正确的形式也就是不要对做很多事情的人施加减速。在编写脚本时,总是有一个决定:是用shell脚本还是使用更高级的语言。如果最终创建复杂的数据结构,甚至只是关联数组,则更高级的语言非常有用。但是,如果您主要只是执行一系列
os.system()
调用,那么您可能应该使用shell脚本。既然您没有明确的问题,下面是我的改进建议(包括整体改进和可能改进您的技能):在bash的5行代码中重写此内容。