Skip to content

Commit

Permalink
feat: remote progress git clone from
Browse files Browse the repository at this point in the history
  • Loading branch information
lpmatos committed Jul 29, 2020
1 parent 405affb commit 4f68095
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 37 deletions.
2 changes: 1 addition & 1 deletion gitlabrc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-

VERSION = (1, 0, 33)
VERSION = (0, 0, 1)

__version__ = ".".join(map(str, VERSION))
43 changes: 19 additions & 24 deletions gitlabrc/cli.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# -*- coding: utf-8 -*-

import os
import sys
import re
from git import Repo
import shutil
import sys
import time
import shutil
from art import *
from tqdm import tqdm
from git import Repo

from .tree import Tree
from .process import Process
from .base import GitLabBase
from .method import CloneMethod
from .arguments import Arguments
from .progress import CloneProgress
from . import __version__ as VERSION
from .tree import Tree

# ==============================================================================
# FUNCTIONS
Expand All @@ -23,15 +23,11 @@
def pname():
return f"[gitlabrc - {str(os.getpid())}]"

# ==============================================================================

def check_git():
if shutil.which("git") == "None":
sys.stderr.write("Error: git executable not installed or not in $PATH" + "\n")
exit(2)

# ==============================================================================

def get_subgroups(gl, group, root=False, info=False):
if root:
return gl.groups.list(all=True, owned=True, query_parameters={"id": group})
Expand All @@ -40,16 +36,12 @@ def get_subgroups(gl, group, root=False, info=False):
else:
return group.subgroups.list(all=True)

# ==============================================================================

def get_projects(gl, group, root=False):
if root:
return gl.groups.get(group, lazy=True, include_subgroups=True).projects.list(all=True)
else:
return group.projects.list(all=True)

# ==============================================================================

def get_all_projects(gl, namespace):
projects = list()
root_projects = get_projects(gl, namespace, root=True)
Expand All @@ -58,30 +50,25 @@ def get_all_projects(gl, namespace):
if root_projects:
for project in root_projects:
projects.append(project)
print(pname() + " found " + project.path_with_namespace)

if rooot_subgroups:
for group in rooot_subgroups:
group_projects = get_projects(gl, group)
if group_projects:
for group_project in group_projects:
projects.append(group_project)
print(pname() + " found " + project.path_with_namespace)
group_subgroups = get_subgroups(gl, group)
if group_subgroups:
while True:
for group in group_subgroups:
relative_subgroup = get_subgroups(gl, group, info=True)
for project in get_projects(gl, relative_subgroup):
projects.append(project)
print(pname() + " found " + project.path_with_namespace)
group_subgroups = get_subgroups(gl, relative_subgroup)
if len(group_subgroups) == 0: next
if len(group_subgroups) == 0: break
return projects

# ==============================================================================

def main():
Art=text2art("GitLabRC")
print(Art)
Expand All @@ -103,15 +90,23 @@ def run(options):
sys.stderr.write("\nError: destination path does not exist " + options.path + "\n\n")
exit(1)

print("Getting projects...\n")

projects = get_all_projects(gl, namespace)

if options.tree:
tree = Tree()
projects_parse = [project.path_with_namespace for project in projects]
projects_parse_content = [[value + " " for value in elemento.split("/")] for elemento in projects_parse]
d = tree.make_tree(projects_parse_content)
tree.print_tree(d)
d = tree.make(projects_parse_content)
tree.show(d)
exit(0)

for project in projects:
print(f"{pname()} found {project.path_with_namespace}")

print(pname() + " mission accomplished in " + str(round(time.time() - t, 2)) + "s")
exit(0)

if not options.dryrun:
for index, project in enumerate(projects, start=1):
Expand All @@ -130,11 +125,11 @@ def run(options):
project_url = project.http_url_to_repo if options.method is CloneMethod.HTTP else project.ssh_url_to_repo

if not os.path.isdir(clone_path):
print(f"{pname()} cloning {project_url}")
Repo.clone_from(project_url, clone_path, branch="master")
print(f"\n{pname()} cloning {project_url}")
Repo.clone_from(project_url, clone_path, branch="master", progress=CloneProgress())
else:
print(f"{pname()} fetching {project_url}")
print(f"\n{pname()} fetching {project_url}")
Process().run_command(f"git -C {clone_path} fetch --all")

print(pname() + " mission accomplished in " + str(round(time.time() - t, 2)) + "s")
exit(0)
3 changes: 3 additions & 0 deletions gitlabrc/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@
Show all repositories without clone/fetch:
gitlabrc -u $GITLAB_URL -t $GITLAB_TOKEN -n msp/charts --dry-run
Show all repositories in tree representation:
gitlabrc -u $GITLAB_URL -t $GITLAB_TOKEN -n msp/charts --tree
"""
20 changes: 20 additions & 0 deletions gitlabrc/progress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from git import RemoteProgress

class CloneProgress(RemoteProgress):

def update(self, op_code, cur_count, max_count=None, message=""):
end = "\r"

if op_code & RemoteProgress.END:
end = "," + RemoteProgress.DONE_TOKEN + "\n"

op_code = op_code & RemoteProgress.OP_MASK

if op_code == RemoteProgress.COUNTING:
print("counting objects: %d %s" % (cur_count, str(message)), end=end)
elif op_code == RemoteProgress.COMPRESSING:
print("compressing objects: %d%% (%d/%d) %s" % ((cur_count/max_count) * 100, cur_count, max_count, str(message)), end=end)
elif op_code == RemoteProgress.WRITING:
print("writing objects: %d%% (%d/%d) %s" % ((cur_count/max_count) * 100, cur_count, max_count, str(message)), end=end)
elif op_code == RemoteProgress.RESOLVING:
print("remote: resolving deltas: %d%% (%d/%d) %s" % ((cur_count/max_count) * 100, cur_count, max_count, str(message)), end=end)
23 changes: 12 additions & 11 deletions gitlabrc/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@

class Tree:

def make_tree(self, information: Type[List[Text]]) -> DefaultDict:
@staticmethod
def make(information: Type[List[Text]]) -> DefaultDict:
tree = lambda: defaultdict(tree)
dictionary = tree()
for x in information:
curr = dictionary
for item in x:
curr = curr[item]
for element in information:
aux = dictionary
for item in element:
aux = aux[item]
return dictionary

def make_strs(self,
dictionary: DefaultDict,
indent: Optional[Type[int]] = 0
) -> List:
strs = []
for k, v in dictionary.items():
strs.append(" " * indent + str(k))
strs.extend(self.make_strs(v, indent+1))
return strs
strings = []
for key, value in dictionary.items():
strings.append(" " * indent + str(key))
strings.extend(self.make_strs(value, indent + 1))
return strings

def print_tree(self, dictionary: DefaultDict) -> NoReturn:
def show(self, dictionary: DefaultDict) -> NoReturn:
print("\n".join(self.make_strs(dictionary)))
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

# Build setup package.
setup(
name = NAME,
name = "GitLabRC",
version = VERSION,
description = DESCRIPTION,
long_description = open(join(here, "README.md"), "r").read(),
Expand Down

0 comments on commit 4f68095

Please sign in to comment.