Эх сурвалжийг харах

move functions to methods

Tobias Simetsreiter 5 жил өмнө
parent
commit
8e800d86a2
2 өөрчлөгдсөн 48 нэмэгдсэн , 31 устгасан
  1. 1 0
      .gitignore
  2. 47 31
      gitmirror.py

+ 1 - 0
.gitignore

@@ -1 +1,2 @@
 repositories
+__pycache__

+ 47 - 31
gitmirror.py

@@ -5,7 +5,7 @@ import sys
 from subprocess import run,Popen, PIPE
 
 
-
+gitlab_api_token="t9cAod1DpruzessGg8bq"
 local_base=f"./repositories"
 COM=sys.argv[0]
 
@@ -14,6 +14,7 @@ def parser():
     p = ArgumentParser()
     p.add_argument("source")
     p.add_argument("-b", "--base", default="./repositories",help="local basedir for mirror")
+    p.add_argument("-t", "--threads", default=4, type=int,help="number of worker threads")
     return p
 
 def main():
@@ -22,8 +23,9 @@ def main():
     repos = repos_from_lines(git_info(args.source))
     repo_obj = [Repo(args.source,r,args.base) for r in repos]
     for output in map_threaded(
-            pull_repo,
+            lambda r:r.pull(),
             repo_obj,
+            threads = args.threads,
             debug=lambda cur,tot,arg:print(f"{COM}: {cur}/{tot}: {arg}"),
         ):
         pass
@@ -33,13 +35,52 @@ class Repo():
         self.remote = remote
         self.path   = path
         self.base   = base
-
-    def source_uri(self):
+    
+    @property
+    def origin(self):
         return f"{self.remote}:{self.path}"
 
+    @property
     def local_path(self):
         return os.path.join(self.base, self.remote, self.path+".git")
 
+    @property
+    def basename(self):
+        return self.path_split[-1]
+
+    @property
+    def path_split(self):
+        return self.path.split("/")
+
+    @property
+    def namespace(self):
+        return self.path_split[:-1]
+
+    def pull(self):
+
+        if not os.path.isdir(os.path.join(self.local_path , "refs","remotes","origin")):
+
+            self.init()
+
+        self.update_local()
+
+        return True
+
+    def update_local(self):
+        proc = run(["git","fetch","-p","origin"], cwd=self.local_path)
+        if not proc.returncode == 0:
+            print(f"Error: {proc}, {local_repo}")
+            _rm(self.local_path)
+
+    def init(self):
+        _rm(self.local_path)
+        run(["mkdir","-p",self.local_path])
+        proc1 = run(["git","init","--bare"], cwd=self.local_path)
+        proc2 = run(["git","remote","add","origin",self.origin], cwd=self.local_path)
+        if not proc1.returncode == 0 and proc2.returncode == 0:
+            print(f"Error: {proc1}, {proc2}, {self.local_path}")
+            _rm(self.local_path)
+
     def __repr__(self):
         return f"Repo('{self.remote}','{self.path}','{self.base}')"
 
@@ -64,22 +105,12 @@ def repos_from_lines(lines):
         yield fields[2]
 
 
-def pull_repo(repo):
-
-    if not os.path.isdir(os.path.join(repo.local_path() , "refs")):
-
-        init_repo(repo.source_uri(), repo.local_path())
-
-    update_local_repo(repo.local_path())
-
-    return True
-
 '''
 :param target: A function to be mapped.
 :param args: An Array or Generator to be mapped.
 :return: Generator of function return values, but not in same order.
 '''
-def map_threaded(target, args=[], procs=4, debug=None):
+def map_threaded(target, args=[], threads=4, debug=None):
     import threading
     import queue
     inq = queue.Queue()
@@ -97,7 +128,7 @@ def map_threaded(target, args=[], procs=4, debug=None):
                 debug( num, num+inq.qsize(), arg)
             result = target(arg)
             outq.put(result)
-    while len(jobs) < procs:
+    while len(jobs) < threads:
         th = threading.Thread(target=worker)
         th.start()
         jobs.append(th)
@@ -118,21 +149,6 @@ def map_threaded(target, args=[], procs=4, debug=None):
     while not outq.empty():
         yield outq.get(block=True)
 
-def update_local_repo(local_repo):
-    proc = run(["git","fetch","-p","origin"], cwd=local_repo)
-    if not proc.returncode == 0:
-        print(f"Error: {proc}, {local_repo}")
-        _rm(local_repo)
-
-def init_repo(source_repo, local_repo):
-    _rm(local_repo)
-    run(["mkdir","-p",local_repo])
-    proc1 = run(["git","init","--bare"], cwd=local_repo)
-    proc2 = run(["git","remote","add","origin",source_repo], cwd=local_repo)
-    if not proc1.returncode == 0 and proc2.returncode == 0:
-        print(f"Error: {proc1}, {proc2}, {local_repo}")
-        _rm(local_repo)
-
 def _rm(di):
     run(["rm","-rf", di])