Skip to content

Commit

Permalink
feat: tree command output
Browse files Browse the repository at this point in the history
  • Loading branch information
lpmatos committed Jul 28, 2020
1 parent d484668 commit f1d32e9
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 77 deletions.
5 changes: 0 additions & 5 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
exclude .gitignore
include README.md
include LICENSE
prune .cache
prune .git
prune build
prune dist
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, 12)
VERSION = (1, 0, 18)

__version__ = ".".join(map(str, VERSION))
12 changes: 7 additions & 5 deletions gitlabrc/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-

from .cli import main

main()
# -*- coding: utf-8 -*-

from .cli import main

if __name__ == "__main__":
main()

6 changes: 5 additions & 1 deletion gitlabrc/arguments.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-

import os
from .constants import CLI
from .settings import Config
from .method import CloneMethod
Expand Down Expand Up @@ -63,6 +62,11 @@ def _adding_arguments(self) -> NoReturn:
dest = "dryrun",
default = False,
help = "list all repositories without clone/fetch")
self._parser.add_argument("--tree",
action = "store_true",
dest = "tree",
default = False,
help = "list all repositories using anytree")
self._parser.add_argument("--version",
action = "store_true",
help = "show version")
86 changes: 34 additions & 52 deletions gitlabrc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,31 @@
from .method import CloneMethod
from .process import Process
from . import __version__ as VERSION
from collections import defaultdict

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

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 make_tree(lst):
tree = lambda: defaultdict(tree)
d = tree()
for x in lst:
curr = d
for item in x:
curr = curr[item]
return d

def make_strs(d, indent=0):
strs = []
for k, v in d.items():
strs.append(" " * indent + str(k))
strs.extend(make_strs(v, indent+1))
return strs

def print_tree(d):
print('\n'.join(make_strs(d)))

def main():
Art=text2art("GitLabRC")
Expand All @@ -70,9 +50,7 @@ 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:
Expand All @@ -82,18 +60,14 @@ def perform(options):
if not token:
sys.stderr.write("\nError: we need gitlab token information\n\n")
exit(1)

if not namespace:
sys.stderr.write("\nError: we need gitlab namespace information\n\n")
exit(1)

if not os.path.isdir(options.path):
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)
if not namespace:
sys.stderr.write("\nError: we need gitlab namespace information\n\n")
exit(1)

git_path = shutil.which("git")
if git_path == "None":
Expand All @@ -117,19 +91,27 @@ def perform(options):
for group in gl.groups.list(all=True, owned=True, query_parameters={"id": namespace}):
for project in group.projects.list(all=True):
projects.append(project)
print(pname() + " found " + project.path_with_namespace)
if not options.tree:
print(pname() + " found " + project.path_with_namespace)

subgroups = group.subgroups.list(all=True)
while True:
for subgroup in subgroups:
real_group = gl.groups.get(subgroup.id, lazy=True)
for project in real_group.projects.list(all=True):
projects.append(project)
print(pname() + " found " + project.path_with_namespace)
if not options.tree:
print(pname() + " found " + project.path_with_namespace)
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 options.tree:
lista = [project.path_with_namespace for project in projects]
lista = [[value + " " for value in elemento.split("/")] for elemento in lista]
d = make_tree(lista)
print_tree(d)
exit(0)

if not options.dryrun:
for project in projects:
Expand Down
3 changes: 1 addition & 2 deletions gitlabrc/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from typing import Text, Type

class CloneMethod(Enum):
HTTP = 1
SSH = 2
HTTP, SSH = 1, 2

@staticmethod
def parse(method: Type[Text]) -> Enum:
Expand Down
20 changes: 14 additions & 6 deletions gitlabrc/process.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

import sys
import subprocess
from typing import Text, Type

Expand All @@ -9,16 +10,23 @@ class Process:
def run_command(command: Type[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)
sys.stderr.write(f"We spec a string value, not {type(command)}")
sys.exit(1)
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)
sys.stderr.write(f"Run command failed - status process returncode - {process.returncode}")
sys.exit(1)
return (output, errors)
except subprocess.CalledProcessError as error:
sys.stderr.write(f"Subprocess error when run the command {command} - {error}")
exit(1)
sys.exit(1)
except Exception as error:
sys.stderr.write(f"Error general exception in run the command {command} - {error}")
exit(1)
sys.exit(1)
1 change: 0 additions & 1 deletion gitlabrc/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ class Config:
@staticmethod
def get_env(env: Type[Text], default: Optional[Type[Text]] = None) -> Text:
return environ.get(env, default)

10 changes: 6 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Package meta-data.
NAME = "gitlabrc"
DESCRIPTION = "GitlabRC is a CLI that help you to clone all projects inside a specific namespace in Gitlab"
DESCRIPTION = "GitlabRC is a python CLI that help you to clone projects inside namespace (groups) in Gitlab"
URL = "https://github.com/lpmatos/gitlabrc"
EMAIL = "luccapsm@gmail.com"
AUTHOR = "Lucca Pessoa da Silva Matos"
Expand All @@ -15,10 +15,11 @@

# What packages are required for this module to be executed?
REQUIRED = [
"art",
"python-gitlab"
]

# Getting current location of this file.
# Getting current location.
here = abspath(dirname(__file__))

# Build setup package.
Expand All @@ -37,10 +38,11 @@
include_package_data = True,
license = "MIT license",
keywords = [
"gitlab",
"git",
"cli",
"python"
"python",
"gitlab",
"gitlab-cli"
],
entry_points = {
"console_scripts" : [
Expand Down

0 comments on commit f1d32e9

Please sign in to comment.