同步多个git仓库

 

 
标签: Python  Git
 

代码片段(2)[全屏查看所有代码]

1. [代码][Python]代码     

01 $ cat gitsync
02 #!/usr/bin/python3
03  
04 #-*- coding: utf-8 -*-
05 ##############################################
07 # Author: Neo <[email protected]>
08 ##############################################
09  
10 #basedir=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))   
11  
12 try:
13     #sys.path.append(basedir + '/lib/python3.3/site-packages') 
14     import os,io,sys,subprocess
15     import logging, configparser
16     import threading
17     from optparse import OptionParser, OptionGroup
18     import time
19     from datetime import datetime  
20     import getpass
21 except ImportError as err:
22     print("Error: %s" %(err))
23  
24 workspace=os.path.expanduser('~/workspace')
25 config = None
26 inifile = 'git.ini'
27  
28 try:
29     if not os.path.exists(inifile):
30         raise Exception('Cannot open file', inifile)
31     config = configparser.SafeConfigParser()
32     config.read(inifile)
33  
34 except configparser.NoSectionError as err:
35     print("Error: %s %s" %(err, inifile))
36     sys.exit(1)
37 except Exception as err:
38     print("Error: %s %s" %(err, inifile))
39     sys.exit(1)
40 conf = {}
41  
42 if not os.path.isdir(workspace):
43     os.makedirs(workspace)
44 os.chdir(workspace)
45 for sect in config.sections():
46     print('===== '+sect+' =====')
47     conf = dict(config.items(sect))
48     source  = conf['origin'].split(':')[1]
49     #print(source)
50     if not os.path.isdir(source):
51         if not os.path.isdir(source+'/.git'):
52             cmd = 'git clone ' + conf['origin'+' '+ source;
53             os.system(cmd)
54             #print(cmd)
55         if os.path.isdir(source) :
56             os.chdir(source)
57             for remote in conf['remote'].split(',') :
58                 cmd = 'git remote add ' + remote
59                 os.system(cmd)
60                 #print(remote)
61                 #print(cmd)
62         os.chdir(workspace)
63     else:
64         os.chdir(source)   
65         os.system('git reset --hard')
66         os.system('git pull origin')
67  
68         cmd = 'git remote'
69         remote = subprocess.getoutput(cmd)
70         for current in remote.split("\n"):
71             if current != 'origin':
72                 print('----- '+current+' -----')
73                 push = 'git push '+current+ ' master'
74                 os.system(push)
75         os.chdir(workspace)

2. [代码][Python]代码     跳至 [1] [2] [全屏预览]

1 $ cat git.ini
3 origin=git@192.168.2.1:example.com/www.example.com
4 remote=mirror git@192.168.6.1:example.com/www.example.com,backupgit@175.40.28.99:example.com/www.example.com
5  
6 origin 是源,remote是目的地,可以写多个,使用逗号分隔。

你可能感兴趣的:(github,python,git,netkiller,gitsync)