Ejemplo n.º 1
0
    def __init__(self):
        ep = dict()
        try:
            from click.globals import get_current_context

            ctx = get_current_context()
            peer = ctx.obj["PEER"]
            gtest = ctx.obj["GTEST"]
        # To be activated when dropping Python 3.5
        # except (ModuleNotFoundError, RuntimeError):
        except:
            peer, gtest = None, None
        if peer:
            if ":" in peer:
                ep["domain"], ep["port"] = peer.rsplit(":", 1)
            else:
                ep["domain"], ep["port"] = peer, "443"
        else:
            ep["domain"], ep["port"] = (
                G1_TEST_DEFAULT_ENDPOINT if gtest else G1_DEFAULT_ENDPOINT
            )
        if ep["domain"].startswith("[") and ep["domain"].endswith("]"):
            ep["domain"] = ep["domain"][1:-1]
        self.ep = ep
        api = "BMAS" if ep["port"] == "443" else "BASIC_MERKLED_API"
        self.BMA_ENDPOINT = " ".join([api, ep["domain"], ep["port"]])
Ejemplo n.º 2
0
def __load_config_and_client(require_local):
    ctx = get_current_context()

    try:
        ctx.obj["config"] = Config.get_config(ctx.obj["config_dir"],
                                              ctx.obj["work_dir"],
                                              ctx.obj["config_overrides"])
    except ConfigDirectoryNotFoundException:
        if not require_local:
            ctx.obj["config"] = Config.get_global_config(
                ctx.obj["config_overrides"])
        else:
            raise ChisubmitException(
                "This command must be run in a directory configured to use chisubmit."
            )

    api_url = ctx.obj["config"].get_api_url()
    api_key = ctx.obj["config"].get_api_key()
    ssl_verify = ctx.obj["config"].get_ssl_verify()

    if api_url is None:
        raise ChisubmitException("Configuration value 'api-url' not found")

    if api_key is None:
        raise ChisubmitException("No chisubmit credentials were found!")

    ctx.obj["client"] = Chisubmit(api_key,
                                  base_url=api_url,
                                  ssl_verify=ssl_verify)
Ejemplo n.º 3
0
 def process_common_args(args=(), prog=None):
     """Traverse all subcommands and execute their parsers to update common options"""
     prog, args = prog or Path(sys.argv[0]).name, args or sys.argv[1:]
     ctx = get_current_context()
     cmd = ctx.command
     while isinstance(cmd, Group):
         name, cmd, args = cmd.resolve_command(ctx, args)
         ctx = cmd.make_context(name, args, ctx)
         # Not done for the full top-level args because it will have already been done by the point this is called:
         cmd.parse_args(ctx, args)
Ejemplo n.º 4
0
 def new_func(*args, **kwargs):
     ctx = get_current_context()
     if ensure:
         obj = ctx.ensure_object(object_type)
     else:
         obj = ctx.find_object(object_type)
     if obj is None:
         raise RuntimeError('Managed to invoke callback without a '
                            'context object of type %r existing'
                            % object_type.__name__)
     return ctx.invoke(f, ctx, obj, *args[2:], **kwargs)
Ejemplo n.º 5
0
 def get(self, save=None):
     # try to get from Click
     ctx = get_current_context(silent=True)
     if ctx:
         return ctx.obj
     else:
         if save:
             for name in self.options.keys():
                 if name in save:
                     self.vals[name] = save[name]
         return self.vals
Ejemplo n.º 6
0
 def new_func(*args, **kwargs):
     ctx = get_current_context()
     try:
         return f(*args, **kwargs)
     except UnknownObjectException, uoe:
         print
         print "ERROR: There was an error processing this request"
         print
         print "URL: %s" % uoe.url
         print "HTTP method: %s" % uoe.method
         print "Error: Not found (404)"
         if ctx.obj["debug"]:
             print
             uoe.print_debug_info()
Ejemplo n.º 7
0
 def new_func(*args, **kwargs):
     ctx = get_current_context()
     try:
         return f(*args, **kwargs)
     except UnknownObjectException, uoe:
         print
         print "ERROR: There was an error processing this request"
         print
         print "URL: %s" % uoe.url
         print "HTTP method: %s" % uoe.method
         print "Error: Not found (404)"
         if ctx.obj["debug"]:
             print
             uoe.print_debug_info()
Ejemplo n.º 8
0
def __load_config_and_client(require_local):
    ctx = get_current_context()
    
    try:
        ctx.obj["config"] = Config.get_config(ctx.obj["config_dir"], ctx.obj["work_dir"], ctx.obj["config_overrides"])
    except ConfigDirectoryNotFoundException:
        if not require_local:
            ctx.obj["config"] = Config.get_global_config(ctx.obj["config_overrides"])
        else:
            raise ChisubmitException("This command must be run in a directory configured to use chisubmit.")

    api_url = ctx.obj["config"].get_api_url()
    api_key = ctx.obj["config"].get_api_key()
    
    if api_url is None:
        raise ChisubmitException("Configuration value 'api-url' not found")

    if api_key is None:
        raise ChisubmitException("No chisubmit credentials were found!")

    ctx.obj["client"] = Chisubmit(api_key, base_url=api_url)    
Ejemplo n.º 9
0
 def wrapper(*args, **kwargs):
     ctx = get_current_context()
     mwdb_options = {}
     api_url = kwargs.pop("api_url")
     if api_url:
         mwdb_options["api_url"] = api_url
     config_path = kwargs.pop("config_path")
     if config_path:
         mwdb_options["config_path"] = config_path
     mwdb = MWDB(**mwdb_options)
     try:
         return fn(mwdb=mwdb, *args, **kwargs)
     except NotAuthenticatedError:
         click.echo(
             "Error: Not authenticated. Use `mwdb login` first to set credentials.",
             err=True,
         )
         ctx.abort()
     except MWDBError as error:
         click.echo(
             "{}: {}".format(error.__class__.__name__, error.args[0]), err=True
         )
         ctx.abort()
Ejemplo n.º 10
0
 def new_func(*args, **kwargs):
     ctx = get_current_context()
     try:
         return f(*args, **kwargs)
     except UnknownObjectException as uoe:
         print()
         print("ERROR: There was an error processing this request")
         print()
         print("URL: %s" % uoe.url)
         print("HTTP method: %s" % uoe.method)
         print("Error: Not found (404)")
         if ctx.obj["debug"]:
             print()
             uoe.print_debug_info()
     except UnauthorizedException as ue:
         print()
         print("ERROR: Your chisubmit credentials are invalid")
         print()
         print("URL: %s" % ue.url)
         print("HTTP method: %s" % ue.method)
         print("Error: Unauthorized (401)")
         if ctx.obj["debug"]:
             print()
             ue.print_debug_info()            
     except BadRequestException as bre:
         print()
         print("ERROR: There was an error processing this request")
         print()
         print("URL: %s" % bre.url)
         print("HTTP method: %s" % bre.method)
         print("Error(s):")
         bre.print_errors()
         if ctx.obj["debug"]:
             print()
             bre.print_debug_info()
     except ChisubmitRequestException as cre:
         print("ERROR: chisubmit server returned an HTTP error")
         print()
         print("URL: %s" % cre.url)
         print("HTTP method: %s" % cre.method)
         print("Status code: %i" % cre.status)
         print("Message: %s" % cre.reason)
         if ctx.obj["debug"]:
             print()
             cre.print_debug_info()
     except SSLError as ssle:
         print("ERROR: SSL certificate error when connecting to server")
         print("URL: %s" % ssle.request.url)
         if ctx.obj["debug"]:
             print("Reason:", ssle)
     except ConnectionError as ce:
         print("ERROR: Could not connect to server")
         print("URL: %s" % ce.request.url)
         if ctx.obj["debug"]:
             print("Reason:", ce)
     except ChisubmitException as ce:
         print("ERROR: %s" % ce)
         if ctx.obj["debug"]:
             ce.print_exception()
     except click.UsageError:
         raise
     except click.core.Exit:
         raise
     except Exception as e:
         handle_unexpected_exception()
         
     ctx.exit(CHISUBMIT_FAIL)
Ejemplo n.º 11
0
 def wrapper(*args, **kwargs):
     ctx = get_current_context()
     ctx.ensure_object(cls)
     return func(*args, **kwargs)
Ejemplo n.º 12
0
    def new_func(*args, **kwargs):
        ctx = get_current_context()
        try:
            return f(*args, **kwargs)
        except UnknownObjectException as uoe:
            print()
            print("ERROR: There was an error processing this request")
            print()
            print("URL: %s" % uoe.url)
            print("HTTP method: %s" % uoe.method)
            print("Error: Not found (404)")
            if ctx.obj["debug"]:
                print()
                uoe.print_debug_info()
        except UnauthorizedException as ue:
            print()
            print("ERROR: Your chisubmit credentials are invalid")
            print()
            print("URL: %s" % ue.url)
            print("HTTP method: %s" % ue.method)
            print("Error: Unauthorized (401)")
            if ctx.obj["debug"]:
                print()
                ue.print_debug_info()
        except BadRequestException as bre:
            print()
            print("ERROR: There was an error processing this request")
            print()
            print("URL: %s" % bre.url)
            print("HTTP method: %s" % bre.method)
            print("Error(s):")
            bre.print_errors()
            if ctx.obj["debug"]:
                print()
                bre.print_debug_info()
        except ChisubmitRequestException as cre:
            print("ERROR: chisubmit server returned an HTTP error")
            print()
            print("URL: %s" % cre.url)
            print("HTTP method: %s" % cre.method)
            print("Status code: %i" % cre.status)
            print("Message: %s" % cre.reason)
            if ctx.obj["debug"]:
                print()
                cre.print_debug_info()
        except SSLError as ssle:
            print("ERROR: SSL certificate error when connecting to server")
            print("URL: %s" % ssle.request.url)
            if ctx.obj["debug"]:
                print("Reason:", ssle)
        except ConnectionError as ce:
            print("ERROR: Could not connect to server")
            print("URL: %s" % ce.request.url)
            if ctx.obj["debug"]:
                print("Reason:", ce)
        except ChisubmitException as ce:
            print("ERROR: %s" % ce)
            if ctx.obj["debug"]:
                ce.print_exception()
        except click.UsageError:
            raise
        except click.core.Exit:
            raise
        except Exception as e:
            handle_unexpected_exception()

        ctx.exit(CHISUBMIT_FAIL)