Browse Source

完成多平台

liuyuqi-dellpc 1 year ago
parent
commit
bf65657456

+ 19 - 0
README.md

@@ -22,7 +22,26 @@ python repo_sync.py --debug true --repo data/repo.txt --type github
 ```
 
 ```
+python main.py create --platform coding --repo_path F:\workspace\python\crawl_github
 python main.py pull --platform coding --repo_path F:\workspace\python\crawl_github
+python main.py push --platform coding --repo_path F:\workspace\python\crawl_github
+python main.py delete --platform coding --repo_path F:\workspace\python\crawl_github
+
+python main.py create --platform github --repo_path F:\workspace\python\crawl_github
+python main.py pull --platform github --repo_path F:\workspace\python\crawl_github
+python main.py push --platform github --repo_path F:\workspace\python\crawl_github
+python main.py delete --platform github --repo_path F:\workspace\python\crawl_github
+
+python main.py create --platform gitee --repo_path F:\workspace\python\crawl_github
+python main.py pull --platform gitee --repo_path F:\workspace\python\crawl_github
+python main.py push --platform gitee --repo_path F:\workspace\python\crawl_github
+python main.py delete --platform gitee --repo_path F:\workspace\python\crawl_github
+
+python main.py create --platform github --repo_path F:\workspace\python\crawl_github
+python main.py pull --platform github --repo_path F:\workspace\python\crawl_github
+python main.py push --platform github --repo_path F:\workspace\python\crawl_github
+python main.py delete --platform github --repo_path F:\workspace\python\crawl_github
+
 ```
 
 ## 计划任务

+ 2 - 2
repo_sync/platform/bitbucket.py

@@ -33,8 +33,8 @@ class BitbucketIE(BasePlatform):
 
     def clone(self):
         pass
-    def pull(self, repo_path: str):
-        return super().pull(repo_path)
+    def pull(self, local_repo_path: str):
+        return super().pull(local_repo_path)
     
 
     def push(self):

+ 4 - 1
repo_sync/platform/coding/coding.py

@@ -172,7 +172,10 @@ class CodingIE(BasePlatform):
         repo_name = local_repo_path.split(os.path.sep)[-1]
         print(f'pull repo:{self.username}/{repo_name} from coding')
         os.chdir(local_repo_path)
-        os.system('git remote remove origin_coding')
+        try:
+            os.system('git remote remove origin_coding')
+        except Exception as e:
+            pass
         os.system(
             f'git remote add origin_coding https://{self.username}:{self.token}@e.coding.net/{self.username}/{self.project_name}/{repo_name}.git'
         )

+ 3 - 3
repo_sync/platform/gitea.py

@@ -24,10 +24,10 @@ class GiteaIE(object):
 
     def clone(self):
         pass
-    def push(self):
+    def push(self, local_repo_path: str):
         pass
-    def pull(self, repo_path: str):
-        return super().pull(repo_path)
+    def pull(self, local_repo_path: str):
+        return super().pull(local_repo_path)
     
     @classmethod
     def suitable(cls, extractor: str) -> bool:

+ 8 - 4
repo_sync/platform/gitee.py

@@ -23,7 +23,7 @@ class GiteeIE(BasePlatform):
         self.sess.headers.update({'Content-Type': 'multipart/form-data'})
 
     def create_repo(self, repo_name: str):
-        """create a repo"""
+        """create a repo"""        
         url = f'{self._api}/user/repos'
         form_data = {'name': repo_name, 'private': True}
         r = self.sess.post(url, params=form_data)
@@ -72,22 +72,25 @@ class GiteeIE(BasePlatform):
         pass
     
     def pull(self, local_repo_path: str):
+        if local_repo_path[-1] == os.path.sep:
+            local_repo_path = local_repo_path[:-1]
         repo_name = local_repo_path.split(os.path.sep)[-1]
         print(f'pull repo:{self.username}/{repo_name} from gitee')
         os.chdir(local_repo_path)
         os.system('git remote remove origin_gitee')
         os.system(
-            f'git remote add origin_gitee https://gitee.com/{self.username}/{repo_name}.git'
+            f'git remote add origin_gitee https://{self.username}:{self.token}@gitee.com/{self.username}/{repo_name}.git'
         )
         os.system('git pull origin_gitee')
         os.system('git remote remove origin_gitee')
         os.chdir('..')
+        print('pull from gitee success')
                  
     def push(self, local_repo_path: str):
+        if local_repo_path[-1] == os.path.sep:
+            local_repo_path = local_repo_path[:-1]
         repo_name = local_repo_path.split(os.path.sep)[-1]
         print(f'push repo:{self.username}/{repo_name} to gitee')
-        # if not self.repo_exists(repo_name):
-        self.create_repo(repo_name)
         os.chdir(local_repo_path)
         os.system('git remote remove origin_gitee')
         os.system(
@@ -96,6 +99,7 @@ class GiteeIE(BasePlatform):
         os.system('git push -u origin_gitee')
         os.system('git remote remove origin_gitee')
         os.chdir('..')
+        print('push to gitee success')
 
     @classmethod
     def suitable(cls, extractor: str) -> bool:

+ 13 - 4
repo_sync/platform/github.py

@@ -54,6 +54,7 @@ class GithubIE(BasePlatform):
             print(
                 f'Failed to delete repository: {repo_name} from github. Error {response.status_code}: {response.text}'
             )
+        print(f'delete repo:{repo_name} from github success')
 
     def repo_exists(self, repo_name: str):
         """check if a repo exists"""
@@ -66,22 +67,30 @@ class GithubIE(BasePlatform):
         except Exception as e:
             return False
 
-    def pull(self, repo_path: str):
+    def pull(self, local_repo_path: str):
         """pull a repo"""
-        repo_name = repo_path.split(os.path.sep)[-1]
+        if local_repo_path[-1] == os.path.sep:
+            local_repo_path = local_repo_path[:-1]
+        repo_name = local_repo_path.split(os.path.sep)[-1]
         print(f'pull repo:{self.username}/{repo_name} from github')
         if not self.repo_exists(repo_name):
             self.create_repo(repo_name)
-        os.chdir(repo_path)
+        os.chdir(local_repo_path)
         os.system('git remote remove origin_github')
         os.system(
-            f'git remote add origin_github')
+            f'git remote add origin_github  https://{self.username}:{self.token}@github.com/{self.username}/{repo_name}.git')
+        os.system('git pull origin_github')
+        os.system('git remote remove origin_github')
+        os.chdir('..')
+        print('pull from github success')
 
     def clone(self, repo_name: str):
         pass
     
     def push(self, local_repo_path: str):
         """push a local repo to remote"""
+        if local_repo_path[-1] == os.path.sep:
+            local_repo_path = local_repo_path[:-1]
         repo_name = local_repo_path.split(os.path.sep)[-1]
         print(f'push repo:{self.username}/{repo_name} to github')
         if not self.repo_exists(repo_name):

+ 26 - 3
repo_sync/platform/gitlab.py

@@ -80,14 +80,36 @@ class GitlabIE(BasePlatform):
         self.save_csv()
         return repo_list
 
-    def pull(self):
-        pass
+    def pull(self, local_repo_path: str):
+        """push a local repo to remote
+        Args:
+            local_repo_path (str): local repo path
+        """
+        if local_repo_path[-1] == os.path.sep:
+            local_repo_path = local_repo_path[:-1]
+        repo_name = local_repo_path.split(os.path.sep)[-1]
+        print(f"push repo:{self.username}/{repo_name} to gitlab")
+        self.create_repo(repo_name)
+        pur_host = re.search(r'(?<=//)[^/]+', self.host).group()
+
+        os.chdir(local_repo_path)
+
+        os.system("git remote remove origin_gitlab")
+        os.system(
+            f"git remote add origin_gitlab https://{self.username}:{self.token}@{pur_host}/{self.username}/{repo_name}.git"
+        )
+        os.system("git pull -u origin_gitlab")
+        os.system("git remote remove origin_gitlab")
+        os.chdir("..")
+        print(f"pull repo:{self.username}/{repo_name} from gitlab success")
 
     def push(self, local_repo_path: str):
         """push a local repo to remote
         Args:
             local_repo_path (str): local repo path
         """
+        if local_repo_path[-1] == os.path.sep:
+            local_repo_path = local_repo_path[:-1]
         repo_name = local_repo_path.split(os.path.sep)[-1]
         print(f"push repo:{self.username}/{repo_name} to gitlab")
         self.create_repo(repo_name)
@@ -102,7 +124,8 @@ class GitlabIE(BasePlatform):
         os.system("git push -u origin_gitlab")
         os.system("git remote remove origin_gitlab")
         os.chdir("..")
-
+        print(f"push repo:{self.username}/{repo_name} to gitlab success")
+        
     def clone(self):
         pass
 

+ 18 - 1
repo_sync/platform/gogs.py

@@ -87,9 +87,26 @@ class GogsIE(BasePlatform):
         pass
     
     def pull(self, local_repo_path: str):
-        pass
+        if local_repo_path[-1] == os.path.sep:
+            local_repo_path = local_repo_path[:-1]
+        repo_name = local_repo_path.split(os.path.sep)[-1]
+        print(f'pull repo:{self.username}/{repo_name} from {self._host}')
+        self.create_repo(repo_name)
+
+        pur_host = re.search(r'(?<=//)[^/]+', self._host).group()
+
+        os.chdir(local_repo_path)
+        os.system('git remote remove origin_gogs')
+        os.system(
+            f'git remote add origin_gogs https://{self.username}:{self.token}@{pur_host}/{self.username}/{repo_name}.git'
+        )
+        os.system('git pull -u origin_gogs')
+        os.system('git remote remove origin_gogs')
+        os.chdir('..')
     
     def push(self, local_repo_path: str):
+        if local_repo_path[-1] == os.path.sep:
+            local_repo_path = local_repo_path[:-1]
         repo_name = local_repo_path.split(os.path.sep)[-1]
         print(f'push repo:{self.username}/{repo_name} to {self._host}')
         self.create_repo(repo_name)

+ 3 - 3
repo_sync/repo_sync.py

@@ -84,8 +84,6 @@ class RepoSync(object):
             token = self.params.get(f'{platform}_token', None)
             host = self.params.get(f'{platform}_host', None)
 
-            if command == 'create':
-                return
             if command == 'clone':
                 # current_platform(username, token, host = host).clone(name)
                 return
@@ -95,8 +93,10 @@ class RepoSync(object):
                     for row in reader:
                         repo = Repo()
                         repo.__dict__ = row
+                        if command == 'create':
+                            current_platform(username,token, host, self.params).create_repo(repo.name)
                         if command == 'push':
-                            current_platform(username,token,host, self.params).push(repo.local_path)
+                            current_platform(username,token, host, self.params).push(repo.local_path)
                         elif command == 'delete':
                             current_platform(username,token, host, self.params).delete(repo.name)
                         elif command =='pull':