""" Module that contains some tools used by the whole server. """ import time import json import subprocess from django.urls.resolvers import get_resolver from django.http.request import HttpRequest from django.conf import settings from monitorT import const as mon_t_const def stream_output_command(command): """ Method to get a stream of data with the output of `command`. Parameters ---------- command: str Command string in a shell fashion. Returns ------- Popen: Popen object """ output = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True) return output def fetch_method(url, params): """ Fetch method of the server via HTTP POST. Parameters ---------- url: str URL to the method to be executed params: dict Dictionary containing the parameters. Returns ------- Any: Result """ method, args, kwargs = url_to_method(url=url) request = HttpRequest() request.method = 'POST' request._body = json.dumps(params).encode() result = method(request, *args, **kwargs) result = json.loads(result.content.decode()) return result def url_to_method(url=None): """ Method to transform the url of the method to a function Parameters ---------- url: str Url pointing to the method Returns ------- function: Function object list: The argument needed by this function dict: Other keyword arguments needed by this function """ resolver = get_resolver() myfunc, myargs, mykwargs = resolver.resolve(url) return myfunc, myargs, mykwargs def wait_to_result(method, params): """ Method for wait to result when the method is managed by tasks. Parameters ---------- method: str Url pointing to the method params: dict Arguments of the function Returns ------- Any: Result """ result = fetch_method(method, params) if 'task_id' in result: task_id = result['task_id'] status = mon_t_const.INPROGRESS_KEY while status == mon_t_const.INPROGRESS_KEY: result = fetch_method('/monitor/info', {'user': params['user'], 'task_id': task_id}) status = result['status'] time.sleep(1) else: if status == mon_t_const.ERROR_KEY: raise Exception(result['description']) result = fetch_method('/monitor/result', {'user': params['user'], 'task_id': task_id}) return result def wait_result_eptisa(method, params, task): """ Method for wait to result when the method is managed by EPTISA. Parameters ---------- method: str Url pointing to the method params: dict Arguments of the function Returns ------- Any: Result """ result = fetch_method(method, params) if 'task_id' in result: task_id = result['task_id'] status = mon_t_const.INPROGRESS_KEY while status == mon_t_const.INPROGRESS_KEY: result = fetch_method('/monitor/info', {'user': params['user'], 'task_id': task_id}) status = result['status'] task.set_progress(result['progress']) else: if status == mon_t_const.ERROR_KEY: raise Exception(result['description']) def dprint(text): """ Debug printing Parameters ---------- text: str Text to print """ if settings.DEBUG: print(text) def get_client_ip(request): """ Get the client IP from request HEADERS Parameters ---------- request: Django.HttpRequest Returns ------- str: IP """ x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: ip = x_forwarded_for.split(',')[0] else: ip = request.META.get('REMOTE_ADDR') return ip