Skip to content

Commit

Permalink
feat: enable signal feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lpmatos committed Jul 29, 2020
1 parent 89cd3e7 commit a23256b
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 35 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, 8)

__version__ = ".".join(map(str, VERSION))
6 changes: 5 additions & 1 deletion gitlabrc/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
from .cli import main

if __name__ == "__main__":
main()
try:
main()
except KeyboardInterrupt:
print("Keyboard Interrupted - CTRL + C")
exit()
7 changes: 6 additions & 1 deletion gitlabrc/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from .constants import CLI
from .settings import Config
from .method import CloneMethod
from .base import CloneMethod
from typing import NoReturn, Text, Optional, Type, Dict
from argparse import ArgumentParser, RawTextHelpFormatter

Expand Down Expand Up @@ -67,6 +67,11 @@ def _adding_arguments(self) -> NoReturn:
dest = "tree",
default = False,
help = "list all repositories in a tree representation without clone/fetch")
self._parser.add_argument("--signal",
action = "store_true",
dest = "signal",
default = False,
help = "enable signal handler to exit in a CTRL + C")
self._parser.add_argument("--version",
action = "store_true",
help = "show version")
8 changes: 8 additions & 0 deletions gitlabrc/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from enum import Enum
from typing import Text, Type, Callable
from dataclasses import dataclass, field
from gitlab import Gitlab, config, exceptions

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

@staticmethod
def parse(method: Type[Text]) -> Enum:
return CloneMethod[method.upper()]

@dataclass
class GitLabBase:
url: Type[Text] = field(default="https://gitlab.com")
Expand Down
15 changes: 10 additions & 5 deletions gitlabrc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
import re
import sys
import time
import signal
import shutil
from art import *
from git import Repo

from .log import Log
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 .base import GitLabBase, CloneMethod

# ==============================================================================
# GLOBAL
Expand All @@ -30,6 +30,10 @@
def pname():
return f"[gitlabrc - {str(os.getpid())}]"

def signal_handler(sig, frame):
print("You pressed Ctrl+C!")
sys.exit(0)

def check_git():
if shutil.which("git") == "None":
logger.error("Error: git executable not installed or not in $PATH")
Expand Down Expand Up @@ -84,9 +88,10 @@ def main():
if args.version:
print(f"Version: {VERSION}")
exit(1)
else:
print(f"Version: {VERSION}\n")
run(args)
if args.signal:
signal.signal(signal.SIGINT, signal_handler)
print(f"Version: {VERSION}\n")
run(args)

def run(options):
url, token, namespace, path = options.url, options.token, options.namespace, options.path
Expand Down
17 changes: 11 additions & 6 deletions gitlabrc/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ def __call__(cls, *args, **kwargs):

class Log(metaclass=SingletonLogger):

def __init__(self, log_path: Text, log_file: Text, log_level: Text, logger_name: Text) -> NoReturn:

def __init__(self,
log_path: Text,
log_file: Text,
log_level: Text,
logger_name: Text
) -> NoReturn:
self._log_path = log_path
self._log_file = log_file
self._log_level = log_level if log_level in ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"] else None
Expand All @@ -67,10 +71,11 @@ def __init__(self, log_path: Text, log_file: Text, log_level: Text, logger_name:
self._logger.addHandler(ContextHandler(BaseFileHandler()).get_handler(log_file=self.log_file, log_level=self.log_level, formatter=self.formatter))

def _base_configuration_log_colored(self) -> coloredlogs.install:
coloredlogs.install(level=self._log_level,
logger=self.logger,
fmt=self.formatter,
milliseconds=True)
coloredlogs.install(
level=self._log_level,
logger=self.logger,
fmt=self.formatter,
milliseconds=True)

@property
def log_path(self) -> Text:
Expand Down
9 changes: 0 additions & 9 deletions gitlabrc/method.py

This file was deleted.

3 changes: 0 additions & 3 deletions gitlabrc/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,3 @@ def run_command(command: Type[Text]) -> Text:
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)
2 changes: 1 addition & 1 deletion gitlabrc/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def update(self,
op_code = op_code & RemoteProgress.OP_MASK

if op_code == RemoteProgress.COUNTING:
print("counting objects: %d %s" % (cur_count, str(message)), end=end)
print(f"counting objects: {cur_count} {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:
Expand Down
5 changes: 4 additions & 1 deletion gitlabrc/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
class Config:

@staticmethod
def get_env(env: Type[Text], default: Optional[Type[Text]] = None) -> Text:
def get_env(
env: Type[Text],
default: Optional[Type[Text]] = None
) -> Text:
return environ.get(env, default)
15 changes: 8 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
from os.path import abspath, dirname, join
from setuptools import setup, find_packages

# Package meta-data.
# ==============================================================================
# CONSTANTS
# ==============================================================================

NAME = "gitlabrc"
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"
REQUIRES_PYTHON = ">=3.6.0"
VERSION = __version__

# What packages are required for this module to be executed?
REQUIRED = [
"art",
"tqdm",
Expand All @@ -23,15 +24,15 @@
"python-json-logger"
]

# Getting current location.
here = abspath(dirname(__file__))
# ==============================================================================
# BUILD PACKAGE
# ==============================================================================

# Build setup package.
setup(
name = "GitLabRC",
version = VERSION,
description = DESCRIPTION,
long_description = open(join(here, "README.md"), "r").read(),
long_description = open(join(abspath(dirname(__file__)), "README.md"), "r").read(),
long_description_content_type = "text/markdown",
author = AUTHOR,
author_email = EMAIL,
Expand Down

0 comments on commit a23256b

Please sign in to comment.