-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from dmonllao/MDL-66004
MDL-66004
- Loading branch information
Showing
12 changed files
with
539 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
2.1.0 | ||
2.2.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import os | ||
|
||
|
||
def print_version(): | ||
"""Prints moodlemlbackend package version""" | ||
|
||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import os | ||
import re | ||
|
||
from functools import wraps | ||
|
||
from flask import request | ||
|
||
|
||
def check_access(f): | ||
'''Checks the access to the route.''' | ||
|
||
@wraps(f) | ||
def access_wrapper(*args, **kwargs): | ||
|
||
# Check that the environment var is properly set. | ||
envvarname = "MOODLE_MLBACKEND_PYTHON_USERS" | ||
if envvarname not in os.environ: | ||
raise Exception( | ||
envvarname + ' environment var is not set in the server.') | ||
|
||
if re.match(os.environ[envvarname], '[^A-Za-z0-9_\-,$]'): | ||
raise Exception( | ||
'The value of ' + envvarname + ' environment var does not ' + | ||
' adhere to [^A-Za-z0-9_\-,$]') | ||
|
||
users = os.environ[envvarname].split(',') | ||
|
||
if (request.authorization is None or | ||
request.authorization.username is None or | ||
request.authorization.password is None): | ||
# Response for the client. | ||
return 'No user and/or password included in the request.', 401 | ||
|
||
for user in users: | ||
userdata = user.split(':') | ||
if len(userdata) != 2: | ||
raise Exception('Incorrect format for ' + | ||
envvarname + ' environment var. It should ' + | ||
'contain a comma-separated list of ' + | ||
'username:password.') | ||
|
||
if (userdata[0] == request.authorization.username and | ||
userdata[1] == request.authorization.password): | ||
|
||
# If all good we return the return from 'f' passing the | ||
# original list of params to it. | ||
return f(*args, **kwargs) | ||
|
||
# Response for the client. | ||
return 'Incorrect user and/or password provided by Moodle.', 401 | ||
|
||
return access_wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import shutil | ||
import os | ||
|
||
from functools import wraps, update_wrapper | ||
|
||
from moodlemlbackend.webapp.util import get_request_value | ||
|
||
|
||
# We can not set LocalFS_setup_base_dir as a nested class because they | ||
# have problems to access the outer class.''' | ||
|
||
|
||
class LocalFS(object): | ||
|
||
def get_localbasedir(self): | ||
if self.localbasedir is None: | ||
raise Exception('localbasedir is not set') | ||
|
||
return self.localbasedir | ||
|
||
def set_localbasedir(self, basedir): | ||
self.localbasedir = basedir | ||
|
||
def get_model_dir(self, hashkey, fetch_model=False): | ||
'''Returns the model dir in the local fs for the provided key. | ||
fetch_model param is ignored here.''' | ||
|
||
uniquemodelid = get_request_value('uniqueid') | ||
dirhash = get_request_value(hashkey) | ||
|
||
# The dir in the local filesystem is namespaced by uniquemodelid and | ||
# the dirhash which determines where the results should be stored. | ||
modeldir = os.path.join(self.get_localbasedir(), | ||
uniquemodelid, dirhash) | ||
|
||
return modeldir | ||
|
||
def delete_dir(self): | ||
|
||
uniquemodelid = get_request_value('uniqueid') | ||
|
||
# All files related to this version of the model in moodle are in | ||
# /uniquemodelid. | ||
modeldir = os.path.join(self.get_localbasedir(), uniquemodelid) | ||
|
||
if os.path.exists(modeldir): | ||
# The directory may not exist. | ||
shutil.rmtree(modeldir, True) | ||
|
||
|
||
class LocalFS_setup_base_dir(object): | ||
|
||
def __init__(self, storage, fetch_model, push_model): | ||
'''Checks that the local directory is set in ENV. | ||
fetch_model and push_model are ignored in local_fs.''' | ||
|
||
self.storage = storage | ||
|
||
localbasedir = os.environ["MOODLE_MLBACKEND_PYTHON_DIR"] | ||
|
||
if os.path.exists(localbasedir) is False: | ||
raise IOError( | ||
'The base dir does not exist. ' + | ||
'Set env MOODLE_MLBACKEND_PYTHON_DIR to an existing dir') | ||
|
||
os.access(localbasedir, os.W_OK) | ||
|
||
storage.set_localbasedir(localbasedir) | ||
|
||
def __call__(self, f): | ||
|
||
@wraps(f) | ||
def wrapper(*args, **kwargs): | ||
'''Execute the decorated function passing the call args.''' | ||
|
||
update_wrapper(self, f) | ||
return f(*args, **kwargs) | ||
return wrapper |
Oops, something went wrong.