Esempio n. 1
0
def update_entry_handler(request, context):
    log.info('put call got request: {0}'.format(request))
    response = dict(success=False, message='')

    if isinstance(request, str) or isinstance(request, unicode):
        try:
            log.info('Got string request, converting to JSON.')
            request = json.loads(request)
        except ValueError:
            log.warning('got malformed JSON request: {0}'.format(request))
            msg = get_error_message(BAD_REQUEST, 'Malformed JSON in request.')
            raise PutCallException(msg)

    if not validate_request_params(request):
        msg = get_error_message(BAD_REQUEST,
                                'Parameter mismatch: validation failed.')
        raise PutCallException(msg)

    try:
        # this time return JSON, not stringified
        response = request.update(dict(calles='PUT'))
        return response

    except PutCallException:
        log.info('Error on response')
        log.info('response was {0}'.format(response))
        msg = get_error_message(BAD_REQUEST, response['message'])
        raise PutCallException(msg)

    except:
        log.error('exception in update: {0}'.format(request))
        log.error(traceback.print_exc())
        msg = get_error_message(INTERNAL_SERVER_ERROR,
                                'Error in PUT call request !')
        raise PutCallException(msg)
def patch_handler(request, context):
    log.debug('got patch request: {0}'.format(request))
    response = dict(success=False, message='')

    if isinstance(request, str) or isinstance(request, unicode):
        try:
            log.info('Got string request, converting to JSON.')
            request = json.loads(request)
        except ValueError:
            log.warning('got malformed JSON request: {0}'.format(request))
            msg = get_error_message(BAD_REQUEST, 'Malformed JSON in request.')
            raise PatchCallException(msg)

    if not validate_request_params(request):
        log.warning('Request validation failed.')
        msg = get_error_message(
            BAD_REQUEST, 'Parameter mismatch: please see documentation.')
        raise PatchCallException(msg)

    try:
        response['message'] = json.dumps(request.update(dict(called='PATCH')))
        return response

    except PatchCallException:
        log.info('Error response from VREG.')
        log.info('response was: {0}'.format(response))
        msg = get_error_message(BAD_REQUEST, response['message'])
        raise PatchCallException(msg)

    except:
        log.error('exception in reset: {0}'.format(request))
        log.error(traceback.print_exc())
        msg = get_error_message(INTERNAL_SERVER_ERROR,
                                'Error in patch request !')
        raise PatchCallException(msg)
Esempio n. 3
0
def execute_handler(request_handler, evt, ctx, timeout):
    try:
        start_time = time.time()
        data = base64.b64decode(evt.decode())
        event = json.loads(data)
        signal.alarm(int(timeout))
        result = request_handler(event, ctx)
    except Exception as user_ex:
        end_time = time.time()
        exc_info = sys.exc_info()
        message = get_error_message(user_ex, exc_info[2], True)
        if isinstance(user_ex, MemoryError):
            message = "Out of Memory"
        elif isinstance(user_ex, TimeoutError):
            message = "Time out"
        return False, message, round(end_time - start_time, 2)
    else:
        end_time = time.time()
        signal.alarm(0)
        if not isinstance(result, (str, bytes)):
            try:
                return True, make_json(result), round(end_time - start_time, 2)
            except TypeError:
                return True, str(result), round(end_time - start_time, 2)
        return True, str(result), round(end_time - start_time, 2)
Esempio n. 4
0
def update_nodes(graph, max_allowed_error):
    lines = read_csv("{0}/matching_error.csv".format(var.CERE_REPLAY_PATH))
    for line in lines:
        #for region_name, error in matching.iteritems():
        #find the node in the graph
        for n,d in graph.nodes(data=True):
            if line["Codelet Name"] == d['_name']:
                d['_invivo'] = float(line["Invivo"])
                d['_invitro'] = float(line["Invitro"])
                d['_tested'] = True
                if float(line["Error"]) <= max_allowed_error:
                    d['_matching'] = True
                else:
                    d['_valid'] = False
                d['_error'] = float(line["Error"])
                if utils.is_invalid(d['_name']):
                    d['_error_message'] = utils.get_error_message(d['_name'])
                invocations = read_csv("{0}/invocations_error.csv".format(var.CERE_REPLAY_PATH))
                del d['_invocations'][:]
                for inv in invocations:
                    if inv["Codelet Name"] == d['_name']:
                        d['_invocations'].append({"Cluster":inv["Cluster"], "Invocation":inv["Invocation"],
                          "Part":round(float(inv["Part"]), 2), "Invivo (cycles)":"{:e}".format(float(inv["Invivo"])),
                          "Invitro (cycles)":"{:e}".format(float(inv["Invitro"])), "Error (%)":round(float(inv["Error"]), 2)})
                d['_tested'] = True
                d['_to_test'] = False
    save_graph(graph)
    plot(graph)
    def handle_exception(self, exc):
        if isinstance(exc, tuple(self.expected_exceptions.keys())):
            drf_exception_class = self.expected_exceptions[exc.__class__]
            drf_exception = drf_exception_class(get_error_message(exc))

            return super().handle_exception(drf_exception)

        return super().handle_exception(exc)
Esempio n. 6
0
    def clean(self, value):
        try:
            captcha_response = captcha.submit(
                value.get('recaptcha_challenge_field'),
                value.get('recaptcha_response_field'),
                get_private_key(),
                '', # remote user's IP address.  See footnotes.
            )
        except Exception: # this is probably bad.
            error_message = get_error_message('recaptcha-not-reachable')
            raise ValidationError(error_message)

        if not captcha_response.is_valid:
            error_message = get_error_message(captcha_response.error_code)
            raise ValidationError(error_message)

        return value
Esempio n. 7
0
def ru_morpher(imgpaths, width=500, height=600, num_frames=20, fps=10, \
               out_frames=None, out_video=None, alpha=False, plot=False,
               obj=None, sessionid=None):
    """
    Create a morph sequence from multiple images in imgpaths
    :param imgpaths: array or generator of image paths
    :param callback: callback function on each point
    """
    oBack = {'status': 'ok', 'msg': ''}
    try:
        video = videoer.Video(out_video, fps, width, height)
        images_points_gen = load_valid_image_points(imgpaths, (height, width))
        src_img, src_points = next(images_points_gen)
        iStep = 0
        for dest_img, dest_points in images_points_gen:

            debugMsg("ru_morpher step {}".format(iStep))

            morph(src_img,
                  src_points,
                  dest_img,
                  dest_points,
                  video,
                  width,
                  height,
                  num_frames,
                  fps,
                  out_frames,
                  out_video,
                  alpha,
                  plot,
                  obj=obj,
                  sessionid=sessionid,
                  result_type="image")

            # Set the new source = old destination
            src_img, src_points = dest_img, dest_points

            iStep += 1

        # Check if any faces could be found in the image
        if iStep == 0:
            # This means that the points could not be found on the image
            print('debug point #3 in: ru_morpher')
            # No points were found
            oBack['status'] = "error"
            oBack['msg'] = "Er kan geen gezicht gevonden worden in dit beeld"
            debugMsg(oBack['msg'])

        debugMsg("ru_morpher video.end")
        video.end()
    except:
        sMsg = get_error_message()
        DoError("ru_morpher: ")
        oBack['status'] = 'error'
        oBack['msg'] = sMsg
    finally:
        return oBack
Esempio n. 8
0
def add_something_handler(request, context):
    log.info('POST call - got request: {0}'.format(request))
    response = dict(success=False,
                    message='')

    if isinstance(request, str) or isinstance(request, unicode):
        try:
            log.info('Got string request, converting to JSON.')
            request = json.loads(request)
        except ValueError:
            log.warning('got malformed JSON request: {0}'.format(request))
            raise PostCallException('Malformed JSON in request.')

    # special no op call, if noop is the only key in the request, just return the context
    # and a short message
    if 'noop' in request and request.get('noop'):
        if not request.get('skiplog'):
            log.info('NoOp called !')
            log.info('Nothing else was called, just Ping-Pong.')
        response = dict(message='No Op call successful',
                        context=context,
                        success=True)
        return response

    if not validate_request_params(request):
        msg = get_error_message(BAD_REQUEST, 'Parameter mismatch: validation failed.')
        raise PostCallException(msg)

    try:
        response['message'] = json.loads(request.update(dict(called='POST')))

    except PostCallException:
        log.info('Error returned.')
        log.info('response was: {0}'.format(response))
        msg = get_error_message(BAD_REQUEST, response['message'])
        raise PostCallException(msg)

    except:
        log.error('exception in a post call: {0}'.format(request))
        log.error(traceback.print_exc())
        msg = get_error_message(INTERNAL_SERVER_ERROR, 'Error in POST request !')
        raise PostCallException(msg)

    return response
Esempio n. 9
0
 def get_error_message(self, code, message):
     if code == 3819:  # ER_CHECK_CONSTRAINT_VIOLATED
         constraint = message.split("'")[1]
         if constraint == 'astronomer_date_check':
             return 'Data zgonu powinna późniejsza niż data urodzenia.'
     elif code == 1217:  # ER_ROW_IS_REFERENCED
         return 'Istnieją obserwacje wykonane przez tego astronoma. Spróbuj zmienić te obserwacje lub je usunąć.'
     elif code == -1:
         return 'Astronom o tym samym imieniu już istnieje.'
     return get_error_message(code, message)
Esempio n. 10
0
 def get_error_message(self, code, message):
     if code == 3819:  # ER_CHECK_CONSTRAINT_VIOLATED
         constraint = message.split("'")[1]
         if constraint == 'galaxy_group_right_ascension_check':
             return 'Rektasencja powinna być pomiędzy 0 a 24.'
         elif constraint == 'galaxy_group_declination_check':
             return 'Deklinacja powinna być pomiędzy -90 a 90.'
         elif constraint == 'galaxy_group_distance_check':
             return 'Dystans powinien być większy lub równy 0.'
     elif code == 1217:  # ER_ROW_IS_REFERENCED
         return 'W tej grupie istnieją galaktyki lub istnieją obserwacje tej grupy. Spróbuj usunąć lub zmienić je najpierw.'
     return get_error_message(code, message)
Esempio n. 11
0
 def get_error_message(self, code, message):
     if code == 3819:  # ER_CHECK_CONSTRAINT_VIOLATED
         constraint = message.split("'")[1]
         if constraint == 'satellite_period_check':
             return 'Okres musi być większy lub równy 0.'
         elif constraint == 'satellite_inclination_check':
             return 'Inklinacja powinna być pomiędzy 0 a 180 stopni.'
         elif constraint == 'satellite_date_check':
             return 'Data zniszczenia musi być później niż data startu.'
     elif code == 1217:  # ER_ROW_IS_REFERENCED
         return 'Istnieją obserwacje tego satelity lub jest on w katalogu. Spróbuj usunąć lub zmienić obserwacje i usunąć obiekt z katalogów.'
     return get_error_message(code, message)
Esempio n. 12
0
def handle_node_package(package_name: str, mode: str, metadata: Metadata):
    """
    Installs a node/npm package handling metadata for the method

    #### Arguments
        package_name (str): The name of the node/npm package to be installed
        version (str): The version of the node/npm package to be installed
        mode (str): The method (installation/uninstallation)
        metadata (`Metadata`): Metadata for the method
    """    
    version_proc = Popen(mslex.split('npm --version'), stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
    version, err = version_proc.communicate()
    version = version.decode().strip()

    if err:
        click.echo(click.style('npm Or node Is Not Installed. Exit Code [0011]', fg='bright_yellow'))
        utils.disp_error_msg(utils.get_error_message('0011', 'install', package_name, None, metadata, packet.json_name), metadata)
        utils.handle_exit('ERROR', None, metadata)


    if mode == 'install':
        proc = Popen(mslex.split(f'npm i {package_name} -g'), stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
        write(f'npm v{version} :: Collecting {package_name}', 'bright_green', metadata)
        package_version = None
        for line in proc.stdout:
            line = line.decode()

            if 'node install.js' in line:
                write(f'npm v{version} :: Running `node install.js` for {package_name}', 'bright_green', metadata)
            if package_name in line and '@' in line and 'install' in line or ' postinstall' in line:
                package_version = line.split()[1]
                write(f'npm v{version} :: {package_version} Installing To <=> "{line.split()[3]}"', 'bright_green', metadata)

            if 'Success' in line and package_name in line or 'added' in line:
                write(f'npm v{version} :: Successfully Installed {package_version}', 'bright_green', metadata)
            if 'updated' in line:
                if package_version:
                    write(f'npm v{version} :: Sucessfully Updated {package_version}', 'bright_green', metadata)
                else:
                    write(f'npm v{version} :: Sucessfully Updated {package_name}', 'bright_green', metadata)


    else:
        proc = Popen(mslex.split(f'npm uninstall -g {package_name}'), stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
        for line in proc.stdout:
            line = line.decode()
            if 'up to date' in line:
                write(f'npm v{version} :: Could Not Find Any Existing Installations Of {package_name}', 'bright_yellow', metadata)
            if 'removed' in line:
                number = line.split(' ')[1].strip()
                time = line.split(' ')[4].strip()
                write(f'npm v{version} :: Sucessfully Uninstalled {package_name} And {number} Other Dependencies in {time}', 'bright_green', metadata)
Esempio n. 13
0
 def get_error_message(self, code, message):
     if code == 3819:  # ER_CHECK_CONSTRAINT_VIOLATED
         constraint = message.split("'")[1]
         if constraint == 'latitude_check':
             return 'Szerokość geograficzna powinna być pomiędzy -90 a 90 stopni.'
         elif constraint == 'longitude_check':
             return 'Długość geograficzna powinna być pomiędzy -180 a 180 stopni.'
         elif constraint == 'coordinates_present':
             return 'Podaj obie współrzędne.'
     elif code == 1217:  # ER_ROW_IS_REFERENCED
         return 'Istnieją obserwacje wykonane w tym obserwatorium. Spróbuj zmienić ich obserwatorium lub je usunąć.'
     elif code == -1:
         return 'Obserwatorium o tym samym kodzie IAU już istnieje.'
     return get_error_message(code, message)
Esempio n. 14
0
 def get_error_message(self, code, message):
     if code == 3819:  # ER_CHECK_CONSTRAINT_VIOLATED
         constraint = message.split("'")[1]
         if constraint == 'meteor_shower_right_ascension_check':
             return 'Rektasencja powinna być pomiędzy 0 a 24.'
         elif constraint == 'meteor_shower_declination_check':
             return 'Deklinacja powinna być pomiędzy -90 a 90.'
         elif constraint == 'meteor_shower_velocity_check':
             return 'Prędkość powinna być większa lub równa 0.'
         elif constraint == 'meteor_shower_zhr_check':
             return 'ZHR powinno być większe lub równe 0.'
     elif code == 1217:  # ER_ROW_IS_REFERENCED
         return 'Istnieją obserwacje tego roju meteorów lub jest on w katalogu. Spróbuj usunąć lub zmienić obserwacje i usunąć obiekt z katalogów.'
     return get_error_message(code, message)
def delete_handler(request, context):
    log.info('delete_call - got request: {0}'.format(request))
    response = dict(success=False, message='')

    if isinstance(request, str) or isinstance(request, unicode):
        try:
            log.info('Got string request, converting to JSON.')
            request = json.loads(request)
        except ValueError:
            log.warning('got malformed JSON request: {0}'.format(request))
            msg = get_error_message(BAD_REQUEST, 'Malformed JSON in request.')
            raise DeleteCallException(msg)

    if not validate_request_params(request):
        msg = get_error_message(BAD_REQUEST,
                                'Parameter mismatch, validation failed.')
        raise DeleteCallException(msg)

    try:
        # do some stuff to delete an entry in the system here
        # maybe send AMQP message, do database work, etc ...
        response['message'] = 'delete call successful'
        return response

    except DeleteCallException:
        log.info('Error response')
        log.info('response was: {0}'.format(response))
        msg = get_error_message(BAD_REQUEST, response['message'])
        raise DeleteCallException(msg)

    except:
        log.error('exception in delete call: {0}'.format(request))
        log.error(traceback.print_exc())
        msg = get_error_message(INTERNAL_SERVER_ERROR,
                                'Error in delete call request !')
        raise DeleteCallException(msg)
Esempio n. 16
0
def check_for_image_points(path):
    points = []
    try:
        print("check_for_image_points 1")
        if os.path.isfile(path):
            img = cv2.imread(path)
            print("check_for_image_points 2: read image")
            points = locator.face_points(img)
            print("check_for_image_points 3: {}".format(len(points)))
        else:
            print("check_for_image_points - file does not exist: {}".format(
                path))
    except:
        sMsg = get_error_message()
        DoError("check_for_image_points: ")
    return (len(points) > 0)
Esempio n. 17
0
def execute_invoke(environ):
    try:
        # get_fc_logger().info(constant.LOG_TAIL_START_PREFIX_INVOKE + get_request_id())

        # validate request
        if get_request_id() is None:
            return constant.STATUS_ERR, "request id is none"
        if environ[constant.HTTP_REQUEST_METHOD] != constant.METHOD_POST:
            return constant.STATUS_ERR, "http method should be post"
        if environ[constant.HTTP_PATH_INFO] != constant.INVOKE_PATH:
            return constant.STATUS_ERR, "path should be /invoke"

        evt, ctx, handler = parse_params(environ)

        # load user handler
        md5 = environ[constant.HTTP_FUNCTION_MD5]
        code_path = environ[constant.HTTP_FUNCTION_CODE_PATH]
        load_success, request_handler = load_handler(handler, md5, code_path)
        if not load_success:
            status, message = request_handler()
            process_mem_info = psutil.Process(os.getpid()).memory_info()
            return status, gen_return_dict(process_mem_info, message, 0)

        # limit memory
        process_mem_info = psutil.Process(os.getpid()).memory_info()
        limit_as = int(
            process_mem_info.vms) + int(ctx.function.memory_size) * 1024 * 1024
        soft, hard = resource.getrlimit(resource.RLIMIT_AS)
        resource.setrlimit(resource.RLIMIT_AS, (limit_as, hard))

        # execute handler
        execute_success, resp_msg, time_usage = execute_handler(
            request_handler, evt, ctx, environ[constant.HTTP_FUNCTION_TIMEOUT])
        if not execute_success:
            ret_code = constant.STATUS_USER_ERR
        else:
            ret_code = constant.STATUS_OK

        return ret_code, gen_return_dict(process_mem_info, resp_msg,
                                         time_usage)
    except Exception as ex:
        exc_info = sys.exc_info()
        ret = get_error_message(ex, exc_info[2])
        process_mem_info = psutil.Process(os.getpid()).memory_info()
        return constant.STATUS_ERR, gen_return_dict(process_mem_info, ret, 0)
    finally:
        pass
Esempio n. 18
0
 def get_error_message(self, code, message):
     if code == 3819:  # ER_CHECK_CONSTRAINT_VIOLATED
         constraint = message.split("'")[1]
         if constraint == 'small_body_period_check':
             return 'Okres musi być większy lub równy 0.'
         elif constraint == 'small_body_eccentricity_check':
             return 'Ekscentryczność powinna być większa lub równa 0.'
         elif constraint == 'small_body_semi_major_axis_check':
             return 'Półoś wielka powinna być większa lub równa 0.'
         elif constraint == 'small_body_inclination_check':
             return 'Inklinacja powinna być pomiędzy 0 a 180 stopni.'
         elif constraint == 'small_body_mass_check':
             return 'Masa powinna być większa lub równa 0.'
         elif constraint == 'small_body_diameter_check':
             return 'Średnica powinna być większa lub równa 0.'
     elif code == 1217:  # ER_ROW_IS_REFERENCED
         return 'To małe ciało jest orbitowane lub istnieją obserwacje tego obiektu. Spróbuj usunąć lub zmienić je najpierw.'
     return get_error_message(code, message)
Esempio n. 19
0
 def get_error_message(self, code, message):
     if code == 3819:  # ER_CHECK_CONSTRAINT_VIOLATED
         constraint = message.split("'")[1]
         if constraint == 'star_right_ascension_check':
             return 'Rektasencja powinna być pomiędzy 0 a 24.'
         elif constraint == 'star_declination_check':
             return 'Deklinacja powinna być pomiędzy -90 a 90.'
         elif constraint == 'star_distance_check':
             return 'Dystans powinien być większy lub równy 0.'
         elif constraint == 'star_parallax_check':
             return 'Paralaksa powinna być większa lub równa 0.'
         elif constraint == 'star_mass_check':
             return 'Masa powinna być większa lub równa 0.'
         elif constraint == 'star_radius_check':
             return 'Promień powinien być większy lub równy 0.'
     elif code == 1217:  # ER_ROW_IS_REFERENCED
         return 'Ta gwiazda jest orbitowana przez inne ciało lub istnieją obserwacje tej gwiazdy. Spróbuj usunąć lub zmienić je najpierw.'
     return get_error_message(code, message)
Esempio n. 20
0
def update_nodes(graph, max_allowed_error):
    lines = read_csv("{0}/matching_error.csv".format(var.CERE_REPLAY_PATH))
    for line in lines:
        #for region_name, error in matching.iteritems():
        #find the node in the graph
        for n, d in graph.nodes(data=True):
            if line["Codelet Name"] == d['_name']:
                d['_invivo'] = float(line["Invivo"])
                d['_invitro'] = float(line["Invitro"])
                d['_tested'] = True
                if float(line["Error"]) <= max_allowed_error:
                    d['_matching'] = True
                else:
                    d['_valid'] = False
                d['_error'] = float(line["Error"])
                if utils.is_invalid(d['_name']):
                    d['_error_message'] = utils.get_error_message(d['_name'])
                invocations = read_csv("{0}/invocations_error.csv".format(
                    var.CERE_REPLAY_PATH))
                del d['_invocations'][:]
                for inv in invocations:
                    if inv["Codelet Name"] == d['_name']:
                        d['_invocations'].append({
                            "Cluster":
                            inv["Cluster"],
                            "Invocation":
                            inv["Invocation"],
                            "Part":
                            round(float(inv["Part"]), 2),
                            "Invivo (cycles)":
                            "{:e}".format(float(inv["Invivo"])),
                            "Invitro (cycles)":
                            "{:e}".format(float(inv["Invitro"])),
                            "Error (%)":
                            round(float(inv["Error"]), 2)
                        })
                d['_tested'] = True
                d['_to_test'] = False
    save_graph(graph)
    plot(graph)
Esempio n. 21
0
def handle_vscode_extension(package_name: str, requested_version: str, mode: str, metadata: Metadata):
    """
    Installs a visual studio code package handling metadata for the method

    #### Arguments
        package_name (str): The name of the visual studio code package to be installed
        version (str): The version of the visual studio code package to be installed
        mode (str): The method (installation/uninstallation)
        metadata (`Metadata`): Metadata for the method
    """
    
    base_c = 'code'

    output = Popen(mslex.split('code --version'), stdin=PIPE,
                   stdout=PIPE, stderr=PIPE, shell=True)
    version, _ = output.communicate()
    version = version.decode()
    if output.returncode != 0:
        output = Popen(mslex.split('code-insiders --version'),
                       stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
        version, _ = output.communicate()
        version = version.decode()
        base_c = 'code-insiders'

    if output.returncode != 0:
        click.echo(click.style(
            'Visual Studio Code Or vscode Is Not Installed. Exit Code [0111]', fg='bright_yellow'))
        utils.disp_error_msg(utils.get_error_message(
            '0111', 'install', package_name, None, metadata, package_name), metadata)
        utils.handle_exit('error', '', metadata)

    version = version.strip().split('\n')[0]

    if mode == 'install':
        add_str = f"@{requested_version}" if requested_version else ""
        command = f'{base_c} --install-extension {package_name}{add_str} --force'

        proc = Popen(mslex.split(command), stdin=PIPE,
                     stdout=PIPE, stderr=PIPE, shell=True)

        success = False

        for line in proc.stdout:
            line = line.decode()

            if 'Installing extensions' in line:
                if metadata.no_color:
                    write(
                        f'Code v{version} :: Installing {package_name}', 'white', metadata)

                else:
                    write(
                        f'Code v{version} :: Installing {Fore.LIGHTMAGENTA_EX}{package_name}{Fore.RESET}', 'bright_green', metadata)
   
            if 'is already installed' in line:
                success = True
                if metadata.no_color:
                    write(
                        f'Code v{version} :: {package_name} Is Already Installed!', 'white', metadata)
                else:
                    write(
                        f'{Fore.LIGHTGREEN_EX}Code v{version} :: {Fore.LIGHTMAGENTA_EX}{package_name}{Fore.LIGHTYELLOW_EX} Is Already Installed!', 'white', metadata)
            
            if 'was successfully installed' in line:
                if metadata.no_color:
                    write(
                        f'Code v{version} :: Successfully Installed {package_name}', 'white', metadata)
                    success = True
                else:
                    write(
                        f'{Fore.LIGHTGREEN_EX}Code v{version} :: Successfully Installed {Fore.LIGHTMAGENTA_EX}{package_name}{Fore.RESET}', 'bright_green', metadata)

        if not success:
            write(
        f'{Fore.LIGHTGREEN_EX}Code v{version} :: Successfully Installed {Fore.LIGHTMAGENTA_EX}{package_name}{Fore.RESET}', 'bright_green', metadata)

    if mode == 'uninstall':
        add_str = f"@{requested_version}" if requested_version else ""
        command = f'{base_c} --uninstall-extension {package_name}{add_str} --force'

        proc = Popen(mslex.split(command), stdin=PIPE,
                     stdout=PIPE, stderr=PIPE, shell=True)

        for line in proc.stdout:
            line = line.decode()
            
            if 'Uninstalling' in line:
                if metadata.no_color:
                    write(f'Code v{version} :: Uninstalling {package_name}', 'white', metadata)

                else:
                    write(
                        f'Code v{version} :: Uninstalling {Fore.LIGHTMAGENTA_EX}{package_name}{Fore.RESET}', 'bright_green', metadata)
            if 'is not installed' in line:
                if metadata.no_color:
                    write(f'Code v{version} :: {package_name} is not installed!', 'white', metadata)

                else:
                    write(f'{Fore.LIGHTGREEN_EX}Code v{version} :: {Fore.LIGHTMAGENTA_EX}{package_name}{Fore.LIGHTYELLOW_EX} is not installed!', 'white', metadata)
            if 'was successfully uninstalled' in line:
                if metadata.no_color:
                    write(f'Code v{version} :: Successfully Uninstalled {package_name}', 'white', metadata)

                else:
                    write(f'{Fore.LIGHTGREEN_EX}Code v{version} :: Successfully Uninstalled {Fore.LIGHTMAGENTA_EX}{package_name}{Fore.RESET}', 'bright_green', metadata)
Esempio n. 22
0
def handle_python_package(package_name: str, version: str, mode: str, metadata: Metadata):
    """
    Installs a python package handling metadata for the method

    #### Arguments
        package_name (str): The name of the python package to be installed
        version (str): The version of the python package to be installed
        mode (str): The method (installation/uninstallation)
        metadata (`Metadata`): Metadata for the method
    """
    command = ''

    valid = Popen(mslex.split('pip --version'),
                  stdin=PIPE, stdout=PIPE, stderr=PIPE)
    _, err = valid.communicate()

    if err:
        click.echo(click.style(
            'Python Is Not Installed. Exit Code [0011]', fg='red'))
        utils.disp_error_msg(utils.get_error_message(
            '0011', 'install', package_name, None, metadata, package_name), metadata)
        utils.handle_exit('ERROR', None, metadata)

    if mode == 'install':
        command = 'python -m pip install --upgrade --no-input'

        command += f' {package_name}'
        if version != 'latest':
            command += f'=={version}'

        proc = Popen(mslex.split(command), stdin=PIPE,
                     stdout=PIPE, stderr=PIPE)

        py_version = sys.version.split()
        for line in proc.stdout:
            line = line.decode('utf-8')

            if f'Collecting {package_name}' in line:
                write(
                    f'Python v{py_version[0]} :: Collecting {package_name}', 'bright_green', metadata)
            if 'Downloading' in line and package_name in line:
                write(
                    f'Python v{py_version[0]} :: Downloading {package_name}', 'bright_green', metadata)

            if 'Installing collected packages' in line and package_name in line:
                write(
                    f'Python v{py_version[0]} :: Installing {package_name}', 'bright_green', metadata)

            if f'Requirement ' in line and package_name in line:
                write(
                    f'Python v{py_version[0]} :: {package_name} Is Already Installed And On The Latest Version ==> {line.split()[-1]}', 'bright_yellow', metadata)
                break

            if 'Successfully installed' in line and package_name in line:
                ver = line.split('-')[1]
                write(
                    f'Python v{py_version[0]} :: Successfully Installed {package_name} {ver}', 'bright_green', metadata)

            if 'You should consider upgrading via' in line:
                wants = utils.confirm(
                    'Would you like to upgrade your pip version?')
                if wants:
                    write('Updating Pip Version', 'bright_green', metadata)
                    Popen(mslex.split('python -m pip install --upgrade pip'))

    elif mode == 'uninstall':
        command = 'python -m pip uninstall --no-input --yes'

        command += f' {package_name}'
        if version:
            command += f'=={version}'

        proc = Popen(mslex.split(command), stdin=PIPE,
                     stdout=PIPE, stderr=PIPE)

        py_version = sys.version.split()

        for line in proc.stdout:
            line = line.decode('utf-8')
            if 'Uninstalling' in line and package_name in line:
                write(
                    f'Python v{py_version[0]} :: Uninstalling {package_name}', 'bright_green', metadata)

            if 'Successfully uninstalled' in line and package_name in line:
                ver = line.split('-')[1]
                write(
                    f'Python v{py_version[0]} :: Successfully Uninstalled {package_name} {ver}', 'bright_green', metadata)

        _, err = proc.communicate()

        if err:
            err = err.decode('utf-8')
            if f'WARNING: Skipping {package_name}' in err:
                write(
                    f'Python v{py_version[0]} :: Could Not Find Any Installations Of {package_name}', 'bright_yellow', metadata)
Esempio n. 23
0
 def test_get_error_response(self):
     correct = '500--Error message'
     self.assertEqual(
         correct, get_error_message(INTERNAL_SERVER_ERROR, 'Error message'))
Esempio n. 24
0
 def test_get_error_message_additional_args(self):
     correct = '400--some message--more information'
     self.assertEqual(
         correct,
         get_error_message(BAD_REQUEST, 'some message', 'more information'))
Esempio n. 25
0
 def get_error_message(self, code, message):
     if code == 1062:  # ER_DUP_ENTRY
         return 'Taka sama obserwacja już istnieje.'
     elif code == 1644:
         return 'Tylko jedna obserwacja obiektu może być odkryciem.'
     return get_error_message(code, message)
Esempio n. 26
0
 def get_error_message(self, code, message):
     if code == -1:
         return 'Katalog o tej samej nazwie już istnieje.'
     return get_error_message(code, message)
Esempio n. 27
0
def get_stuff_handler(request, context):
    """
    Main function to be called as lambda handler.
    The parameters are specified in the AWS mapping in AWS Gateway and are the sum of
    request parameters from specification and the parsed headers.

    special call available for test purpose:
    this returns a message with the context used in the call, logging can be skipped if
    "skiplog" is ste to True, so even the connection with Cloudwatch can be switched
        {
            "noop": True,
            "skiplog": False
        }

    :param request: JSON
        {
            "value1": "something",
            "value2": "something else",
            "value3": "foo"
        }

    :param context: request context from AWS lambda

    :raises: GetCallException

    :return: JSON
    """

    if isinstance(request, str):
        try:
            log.info('Got string request, converting to JSON.')
            request = json.loads(request)
        except ValueError:
            log.warning('Malformed Json in request: {0}'.format(request))
            msg = get_error_message(BAD_REQUEST, 'Malformed JSON in request.')
            raise GetCallException(msg)

    # special no op call, if noop is the only key in the request, just return the context
    # and a short message
    # this can be used in 'keep warm' calls to keep the lambda function from being "scaled down', 'hibernated',
    # whatever ...
    # this way the calls can be easily distinguished from 'real' requests
    if 'noop' in request and request.get('noop'):
        if not request.get('skiplog'):
            log.info('NoOp called !')
            log.info('context returned: {0}'.format(context))
        response = dict(message='No Op call successful',
                        context=context,
                        success=True)
        return response

    log.debug('got request: {0}'.format(request))
    response = dict(success=False, message='')

    if not validate_request_params(request):
        # we need to use exceptions here now, so we can match the
        # errorMessage in the context response for HTTP response types
        msg = get_error_message(BAD_REQUEST,
                                'Parameter mismatch: validation failed.')
        raise GetCallException(msg)

    try:
        client = GetStuffViaAMQPClient()
        response = client.call(request, routing_key=ROUTING_KEY)
        log.info('found stuff with ID: {0}'.format(request.get('stuff_id')))

        if not response['success']:
            raise GetCallException()

        return response

    except AmqpRpcError:
        log.error(
            'Error connecting to AMQP exchange: {0}'.format(AMQP_EXCHANGE))
        msg = get_error_message(INTERNAL_SERVER_ERROR,
                                'Error in request for GET call!')
        raise GetCallException(msg)

    except GetCallException:
        log.info('Error response from VREG.')
        log.info('response was: {0}'.format(response))
        msg = get_error_message(BAD_REQUEST, response['message'])
        raise GetCallException(msg)

    except:
        log.error('unexpected exception in get call: {0}'.format(request))
        log.error(traceback.print_exc())
        msg = get_error_message(INTERNAL_SERVER_ERROR,
                                'Error in request for GET call!')
        raise GetCallException(msg)
Esempio n. 28
0
def handle_sublime_extension(package_name: str, mode: str, metadata: Metadata):
    """
    Installs a sublime text package handling metadata for the method

    #### Arguments
        package_name (str): The name of the sublime text package to be installed
        version (str): The version of the sublime text package to be installed
        mode (str): The method (installation/uninstallation)
        metadata (`Metadata`): Metadata for the method
    """
    if mode != 'install':
        return
    if utils.find_existing_installation('sublime-text-3', 'Sublime Text 3'):
        location = PathManager.get_appdata_directory().replace('\electric', '') + \
            '\Sublime Text 3'
        if os.path.isdir(location) and os.path.isfile(fr'{location}\Packages\User\Package Control.sublime-settings'):
            with open(fr'{location}\Packages\User\Package Control.sublime-settings', 'r') as f:
                lines = f.readlines()
                idx = 0
                for line in lines:
                    if '"Package Control",' in line.strip():
                        idx = lines.index(line)

                if ']' in lines[idx + 1].strip():
                    lines[idx] = "        \"Package Control\""

            with open(fr'{location}\Packages\User\Package Control.sublime-settings', 'w') as f:
                f.writelines(lines)

            with open(fr'{location}\Packages\User\Package Control.sublime-settings', 'r') as f:
                json = js.load(f)
                current_packages = json['installed_packages']
                if package_name in current_packages:
                    write(f'{package_name} Is Already Installed!',
                          'white', metadata)
                    sys.exit()

                current_packages.append(package_name)
            updated_packages = current_packages
            del json['installed_packages']
            json['installed_packages'] = updated_packages
            with open(fr'{location}\Packages\User\Package Control.sublime-settings', 'w+') as f:
                f.write(js.dumps(json, indent=4))
            write(
                f'Successfully Added {package_name} to Sublime Text 3', 'white', metadata)
        else:
            if not os.path.isdir(location):
                os.mkdir(location)
            if not os.path.isdir(fr'{location}\Installed Packages'):
                os.mkdir(fr'{location}\Installed Packages')

            # Package Control Not Installed
            with Halo('Installing Package Control', text_color='cyan'):
                urlretrieve('https://packagecontrol.io/Package%20Control.sublime-package',
                            fr'{location}\Installed Packages\Package Control.sublime-package')

            if not os.path.isdir(fr'{location}\Packages'):
                os.mkdir(fr'{location}\Packages')
            if not os.path.isdir(fr'{location}\Packages\User'):
                os.mkdir(fr'{location}\Packages\User')

            with open(fr'{location}\Packages\User\Package Control.sublime-settings', 'w+') as f:
                f.write(
                    js.dumps({
                        "bootstrapped": True,
                        "installed_packages": [
                            "Package Control"
                        ]},
                        indent=4
                    )
                )

            handle_sublime_extension(package_name, mode, metadata)
    else:
        click.echo(click.style(
            'Sublime Text 3 Is Not Installed. Exit Code [0112]', fg='bright_yellow'))
        utils.disp_error_msg(utils.get_error_message(
            '0112', 'install', package_name, None, metadata, package_name), metadata)
        utils.handle_exit('error', '', metadata)
Esempio n. 29
0
 def get_error_message(self, code, message):
     return get_error_message(code, message)
Esempio n. 30
0
def handle_atom_package(package_name: str, mode: str, requested_version: str, metadata: Metadata):
    """
    Installs an atom package handling metadata

    #### Arguments
        package_name (str): The name of the atom package to be installed
        version (str): The version of the atom package to be installed
        mode (str): The method (installation/uninstallation)
        metadata (`Metadata`): Metadata for the method
    """
    if mode == 'install':

        proc = Popen('apm'.split(),
                        stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
        proc.communicate()

        if proc.returncode != 0:
            click.echo(click.style('Atom Is Not Installed. Exit Code [0113]', fg='bright_yellow'))
            utils.disp_error_msg(utils.get_error_message('0113', 'install', package_name, None, metadata, package_name), metadata)
            utils.handle_exit('error', '', metadata)


        with Halo(f'apm :: Installing {package_name}', text_color='cyan') as h:
            add_str = f"@{requested_version}" if requested_version else ""
            command = f'apm install {package_name}' + add_str

            proc = Popen(
                command, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
            
            for line in proc.stdout:
                line = line.decode()
                if 'failed' in line:
                    h.fail(
                        f' Failed to Install {package_name} to <=> {line.split()[3]}')

                if 'done' in line:
                    h.stop()
                    click.echo(click.style(
                        f' Successfully Installed {package_name} to <=> {line.split()[3]}', 'bright_green'))

    if mode == 'uninstall':
        proc = Popen('apm'.split(),
                        stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
        proc.communicate()

        if proc.returncode != 0:
            click.echo(click.style('Atom Is Not Installed. Exit Code [0113]', fg='bright_yellow'))
            utils.disp_error_msg(utils.get_error_message('0113', 'install', package_name, None, metadata, package_name), metadata)
            utils.handle_exit('error', '', metadata)


        with Halo(f'apm :: Uninstalling {package_name}', text_color='cyan') as h:
            add_str = f"@{requested_version}" if requested_version else ""
            command = f'apm deinstall {package_name}' + add_str
            proc = Popen(
                command, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)

            for line in proc.stdout:
                line = line.decode()
                
                if 'failed' in line:
                    h.fail(f' Failed to Uninstall {package_name}')

                if 'done' in line:
                    h.stop()
                    click.echo(click.style(
                        f' Successfully Uninstalled {package_name}', 'bright_green'))
Esempio n. 31
0
 def get_error_message(self, code, message):
     if code == 1217:  # ER_ROW_IS_REFERENCED
         return 'W tej konstelacji znajdują się gwiazdy lub galaktyki. Spróbuj zmienić ich konstelację lub je usunąć.'
     elif code == -1:
         return 'Konstelacja z tym samym skrótem IAU już istnieje.'
     return get_error_message(code, message)