def process_user_csv(params, user_csv_file, algorithm, dev_mode, sandboxing, jail): """Process a single user csv file. Args: params (dict): Parameters for the request. user_csv_file (string): Path to user csv file. algorithm (dict): Dictionary with keys `code` and `className` specifying algorithm code and className. dev_mode (bool): Should the algorithm run in development mode or production mode. sandboxing (bool): Should sandboxing be used or not. jail (codejail.Jail): Jail object. Returns: Result of the execution. Raises: SafeExecException: If the execution wasn't successful. """ username = os.path.splitext(os.path.basename(user_csv_file))[0] globals_dict = { 'params': params, } user_specific_code = textwrap.dedent(""" def run_code(): import bandicoot algorithmobj = {}() bandicoot_user = bandicoot.read_csv( '{}', '', describe={}, warnings={}) return algorithmobj.map(params, bandicoot_user) result = run_code() """.format(algorithm['className'], username, str(dev_mode), str(dev_mode))) code = "{}\n{}".format(algorithm['code'], user_specific_code) if sandboxing: jail.safe_exec(code, globals_dict, files=[user_csv_file]) else: not_safe_exec(code, globals_dict, files=[user_csv_file]) result = globals_dict['result'] return result
def safe_exec(self, *args, **kwargs): safe_exec.not_safe_exec(*args, **kwargs)