Skip to content

Commit

Permalink
feat: adding three and process
Browse files Browse the repository at this point in the history
  • Loading branch information
lpmatos committed Jul 28, 2020
1 parent 91e7af2 commit aeabf4b
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 16 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, 0)
VERSION = (1, 0, 12)

__version__ = ".".join(map(str, VERSION))
73 changes: 58 additions & 15 deletions gitlabrc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,54 @@
import subprocess
from .arguments import Arguments
from .method import CloneMethod
from .process import Process
from . import __version__ as VERSION

from anytree import Node, RenderTree
from anytree.exporter import DictExporter, JsonExporter
from anytree.importer import DictImporter

def pname():
return f"[gitlabrc - {str(os.getpid())}]"

def make_node(name, parent, url):
node = Node(name=name, parent=parent, url=url)
node.root_path = "/".join([str(n.name) for n in node.path])
return node

def add_projects(parent, projects):
for project in projects:
node = make_node(project.name, parent, url=project.http_url_to_repo)

def get_projects(group, parent):
projects = group.projects.list(as_list=False)
add_projects(parent, projects)

def get_subgroups(group, parent, gitlab):
subgroups = group.subgroups.list(as_list=False)
for subgroup_def in subgroups:
subgroup = gitlab.groups.get(subgroup_def.id)
node = make_node(subgroup.name, parent, url=subgroup.web_url)
get_subgroups(subgroup, node, gitlab)
get_projects(subgroup, node)

def load_gitlab_tree(gitlab, root):
groups = gitlab.groups.list(as_list=False)
for group in groups:
if group.parent_id is None:
node = make_node(group.name, root, url=group.web_url)
get_subgroups(group, node, gitlab)
get_projects(group, node)

def print_tree_native(root, url):
for pre, _, node in RenderTree(root):
line = ""
if node.is_root:
line = "%s%s [%s]" % (pre, "root", url)
else:
line = "%s%s [%s]" % (pre, node.name, node.root_path)
print(line)

def main():
Art=text2art("GitLabRC")
print(Art)
Expand All @@ -28,6 +71,10 @@ def main():
def perform(options):
url, token, namespace = options.url, options.token, options.namespace

root = Node("", root_path="", url=url)

gl = gitlab.Gitlab(url, token)

if not url:
sys.stderr.write("\nError: we need gitlab url information\n\n")
exit(1)
Expand All @@ -44,6 +91,10 @@ def perform(options):
sys.stderr.write("\nError: destination path does not exist " + options.path + "\n\n")
exit(1)

if options.dryrun:
load_gitlab_tree(gl, root)
print_tree_native(root, url)

git_path = shutil.which("git")
if git_path == "None":
sys.stderr.write("Error: git executable not installed or not in $PATH" + "\n")
Expand All @@ -53,8 +104,6 @@ def perform(options):

t = time.time()

gl = gitlab.Gitlab(url, token)

group = gl.groups.get(namespace, lazy=True, include_subgroups=True)

projects = []
Expand All @@ -80,7 +129,8 @@ def perform(options):
subgroups = real_group.subgroups.list(all=True)
if len(subgroups) == 0: next
if len(subgroups) == 0: break

print([project.path_with_namespace for project in projects])

if not options.dryrun:
for project in projects:
print(pname() + " clone/fetch project " + project.path_with_namespace)
Expand All @@ -96,20 +146,13 @@ def perform(options):
clone_path = re.sub("/+", "/", clone_path)
print(pname() + " folder " + clone_path)
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(pname() + " cloning " + project_url)
try:
subprocess.run(["git", "clone", project_url, clone_path])
except:
sys.stderr.write("Unexpected error while cloning: terminating\n")
exit(2)
print(f"{pname()} cloning {project_url}")
Process().run_command(f"git clone {project_url} {clone_path}")
else:
print(pname() + " fetching " + project_url)
try:
subprocess.run(["git", "-C", clone_path, "fetch", "--all"])
except:
sys.stderr.write("Unexpected error while fetching: terminating\n")
exit(3)
print(f"{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)
10 changes: 10 additions & 0 deletions gitlabrc/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import logging
import logging.handlers

logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-10s %(processName)s %(name)s %(message)s",
datefmt="%Y-%m-%d-%H-%M-%S"
)

log = logging.getLogger(__name__)
24 changes: 24 additions & 0 deletions gitlabrc/process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-

import subprocess
from typing import Text, Callable

class Process:

@staticmethod
def run_command(command: Text) -> Text:
try:
if not isinstance(command, str):
raise ValueError(f"We spec a string value, not {type(command)}")
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True, universal_newlines=True)
output, errors = process.communicate()
if process.returncode != 0:
sys.stderr.write(f"Run command failed - status returncode - {process.returncode} - {error}")
exit(1)
return (output, errors)
except subprocess.CalledProcessError as error:
sys.stderr.write(f"Subprocess error when run the command {command} - {error}")
exit(1)
except Exception as error:
sys.stderr.write(f"Error general exception in run the command {command} - {error}")
exit(1)
3 changes: 3 additions & 0 deletions graph.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
digraph tree {
"shared/common/images" [label="shared/common/images"];
}

0 comments on commit aeabf4b

Please sign in to comment.