liuyuqi-dellpc 2 years ago
parent
commit
2718cafe93
10 changed files with 204 additions and 1 deletions
  1. 20 1
      README.md
  2. 6 0
      conf/config.yml
  3. 1 0
      data/repo.txt
  4. 12 0
      main.py
  5. 6 0
      repo_sync/__init__.py
  6. 29 0
      repo_sync/base_repo.py
  7. 18 0
      repo_sync/coding_repo.py
  8. 39 0
      repo_sync/github_repo.py
  9. 73 0
      repo_sync/sync_utils.py
  10. 0 0
      requirements.txt

+ 20 - 1
README.md

@@ -1,3 +1,22 @@
 # repo_sync
 
-跨平台项目同步工具
+跨平台项目同步工具
+
+由于项目众多,单一通过自己的代码托管平台存在一定风险。此项目将定期将代码同步到其他平台,以防止代码丢失。
+
+* github
+* gitlab
+* gitee
+* coding
+
+## Usage
+
+比如把 data/repo.txt 中的项目同步到 github 上:
+```
+python repo_sync.py --debug true --repo data/repo.txt --type github
+```
+
+把 data/repo.txt 中的项目同步到 gitlab 上:
+```
+python repo_sync.py --type gitlab
+```

+ 6 - 0
conf/config.yml

@@ -0,0 +1,6 @@
+token:
+  github: xx1
+  gitlab: xx2
+  coding: xx3
+  gitee: xx4
+

+ 1 - 0
data/repo.txt

@@ -0,0 +1 @@
+https://git.yoqi.me/lyq/repo_sync

+ 12 - 0
main.py

@@ -0,0 +1,12 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+'''
+@Contact :   liuyuqi.gov@msn.cn
+@Time    :   2023/04/27 02:55:59
+@License :   Copyright © 2017-2022 liuyuqi. All Rights Reserved.
+@Desc    :   
+'''
+from repo_sync import main
+
+if __name__=='__main__':
+    main()

+ 6 - 0
repo_sync/__init__.py

@@ -0,0 +1,6 @@
+from repo_sync.base_repo import BaseRepo
+from repo_sync.sync_utils import SyncUtils
+
+def main():
+    sync_utils = SyncUtils()
+    sync_utils.run()

+ 29 - 0
repo_sync/base_repo.py

@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+'''
+@Contact :   liuyuqi.gov@msn.cn
+@Time    :   2023/04/27 02:55:45
+@License :   Copyright © 2017-2022 liuyuqi. All Rights Reserved.
+@Desc    :   
+'''
+import requests
+
+class BaseRepo(object):
+    '''
+    Repo class
+    '''
+    def __init__(self, repo_name, repo_url, repo_branch, repo_path, repo_type, debug=False):
+        self.sess= requests.Session()
+        self.repo_name = repo_name
+        self.repo_url = repo_url
+        self.repo_branch = repo_branch
+        self.repo_path = repo_path
+        self.repo_type = repo_type
+
+    def sync(self):
+        '''
+        sync repo
+        '''
+        raise NotImplementedError
+    
+ 

+ 18 - 0
repo_sync/coding_repo.py

@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+'''
+@Contact :   liuyuqi.gov@msn.cn
+@Time    :   2023/04/27 03:16:43
+@License :   Copyright © 2017-2022 liuyuqi. All Rights Reserved.
+@Desc    :   coding repo
+'''
+
+from repo_sync.base_repo import BaseRepo
+
+class CodingRepo(BaseRepo):
+    
+    def __init__(self):
+        pass
+    
+    def sync(self):
+        pass

+ 39 - 0
repo_sync/github_repo.py

@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+'''
+@Contact :   liuyuqi.gov@msn.cn
+@Time    :   2023/04/27 02:59:42
+@License :   Copyright © 2017-2022 liuyuqi. All Rights Reserved.
+@Desc    :   github repo
+'''
+import os,sys,re
+import logging
+import argparse
+
+from repo_sync.base_repo import BaseRepo
+
+class GitHubRepo(BaseRepo):
+    '''
+    GitHubRepo class
+    '''
+    def __init__(self, repo_name, repo_url, repo_branch, repo_path, repo_type):
+        super(GitHubRepo, self).__init__(repo_name, repo_url, repo_branch, repo_path, repo_type)
+        self.repo_type = 'github'
+        self.repos=None
+        
+    def sync(self):
+        '''
+        sync repo
+        '''
+        self.logger.info("sync repo: %s", self.repo_name)
+        self.sess.headers={
+            "Authorization": "token   6b0b9"
+        }
+        if self.repos==None:
+            self.repos = self.sess.get("https://api.github.com/user/repos").json()
+        # if repo is not exist, create it
+        if self.repo_name not in [repo["name"] for repo in self.repos]:
+            self.logger.info("create repo: %s", self.repo_name)
+            self.sess.post("https://api.github.com/user/repos", json={"name": self.repo_name})
+
+        os.system("git push %s %s" % (self.repo_url, self.repo_path))

+ 73 - 0
repo_sync/sync_utils.py

@@ -0,0 +1,73 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+'''
+@Contact :   liuyuqi.gov@msn.cn
+@Time    :   2023/04/27 03:09:58
+@License :   Copyright © 2017-2022 liuyuqi. All Rights Reserved.
+@Desc    :   sync utils
+'''
+import os
+import sys
+import time
+import json
+import logging
+import argparse
+from repo_sync.coding_repo import CodingRepo
+from repo_sync.github_repo import GitHubRepo
+from repo_sync.github_repo import Coding
+
+class SyncUtils:
+    '''
+    SyncUtils class
+    '''
+    def __init__(self, debug=False):
+        self.args = None
+        self.logger = None
+        self.init_logger(debug)
+        self.init_args()
+
+    def run(self):
+        '''
+        run repo
+        '''
+        repos= []
+        with open("repos.text", "r") as f:
+            repos = f.readlines()
+        for repo in repos:
+            
+            if not os.path.exists(repo):
+                self.logger.info("clone repo: %s", repo["name"])
+                os.system("git clone %s" % repo["url"])
+
+            self.logger.info("sync repo: %s", repo["name"])
+            repo = None
+            if self.args.type == "github":
+                repo = GitHubRepo(self.args.repository, "", "", "", "")
+            elif self.args.type == "coding":
+                repo = CodingRepo(self.args.repository, "", "", "", "")
+            repo.sync()
+
+    def init_logger(self, debug:bool):
+        '''
+        init logger
+        '''
+        self.logger = logging.getLogger(self.repo_name)
+        if debug:
+            self.logger.setLevel(logging.DEBUG)
+        else:
+            self.logger.setLevel(logging.INFO)
+        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
+        console_handler = logging.StreamHandler()
+        console_handler.setLevel(logging.DEBUG)
+        console_handler.setFormatter(formatter)
+        self.logger.addHandler(console_handler)
+
+    def init_args(self):
+        '''
+        init args
+        '''
+        parser = argparse.ArgumentParser()
+        parser.add_argument('-d', '--debug', action='store_true', help='debug mode')
+        parser.add_argument('-type', '--type', action='store_true',default="github", help='github,gitlab,gitee,coding')
+        parser.add_argument('-repo', '--repository', action='store_true', default="github", help='run repo')
+        self.args = parser.parse_args()

+ 0 - 0
requirements.txt