Skip to content

Commit

Permalink
Create directories automaticlly
Browse files Browse the repository at this point in the history
Remove the need of pre-creating directories and avoid the crash when
directories are missing
  • Loading branch information
xcompass committed Feb 15, 2024
1 parent 372a337 commit 5754e36
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
3 changes: 0 additions & 3 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,4 @@ export JNOTE_HOME="${JNOTE_HOME:-$DEFAULT_JNOTE_HOME}"
export JNOTE_SNAP="${JNOTE_SNAP:-$DEFAULT_JNOTE_SNAP}"
export JNOTE_INTSNAP="${JNOTE_INTSNAP:-$DEFAULT_JNOTE_INTSNAP}"

mkdir -p $JNOTE_HOME $JNOTE_SNAP $JNOTE_INTSNAP
chmod 777 $JNOTE_HOME $JNOTE_SNAP $JNOTE_INTSNAP

exec "$@"
30 changes: 30 additions & 0 deletions usr/share/jupyter-canvas-api/api-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
HOMEDIR = str(os.getenv('JNOTE_HOME', '/mnt/efs/stat-100a-home/')) # Home Directory Root
SNAPSHOT_DIR = str(os.getenv('JNOTE_SNAP', '/mnt/efs/stat-100a-snap/')) # Instructor Snapshot Directory
INTERMEDIARY_DIR = str(os.getenv('JNOTE_INTSNAP', '/mnt/efs/stat-100a-internal/')) # Intermediary Snapshot Directory
all_directories = [HOMEDIR, SNAPSHOT_DIR, INTERMEDIARY_DIR]
COURSE_CODE = str(os.getenv('JNOTE_COURSE_CODE', 'STAT100a')) # The API Course Code

UPLOAD_FOLDER = os.path.join('/tmp', 'uploads') # Temporary Upload Folder
Expand Down Expand Up @@ -100,6 +101,28 @@ def slugify(value, allow_unicode=False):
return re.sub(r'[-\s]+', '-', value).strip('-_')


def create_directories(directories):
"""
Decorators to create a directory if it doesn't exist.
Parameters:
- directories (list): List of the directories to be created.
"""
def decorator_create_directories(func):
@wraps(func)
def wrapper_create_directories(*args, **kwargs):
for directory_path in directories:
if not os.path.exists(directory_path):
try:
os.makedirs(directory_path)
logger.info(f"Created directory '{directory_path}'.")
except OSError as e:
logger.error(f"Error creating directory '{directory_path}': {e}")
return func(*args, **kwargs)
return wrapper_create_directories
return decorator_create_directories


# Default Flask HTTP 401 Error
@app.errorhandler(401)
def not_authorized(e):
Expand Down Expand Up @@ -151,6 +174,7 @@ def decorated(*args, **kwargs):
#
@app.route('/get_snapshot_file_list', methods=['POST'])
@requires_apikey
@create_directories(directories=all_directories)
def get_snapshot_file_list():
""" Get List of Snapshot Files for the Specified Student and Snapshot. """

Expand Down Expand Up @@ -224,6 +248,7 @@ def get_snapshot_file_list():
#
@app.route('/get_snapshot_list', methods=['POST'])
@requires_apikey
@create_directories(directories=all_directories)
def get_snapshot_list():
""" Get List of Snapshot Directories for the Specified Student. """

Expand Down Expand Up @@ -276,6 +301,7 @@ def get_snapshot_list():
#
@app.route('/get_snapshot_file', methods=['POST'])
@requires_apikey
@create_directories(directories=all_directories)
def get_snapshot_file():
""" Get the Specified File from Specified Student Snapshot. """

Expand Down Expand Up @@ -368,6 +394,7 @@ def get_snapshot_file():
#
@app.route('/get_snapshot_zip', methods=['POST'])
@requires_apikey
@create_directories(directories=all_directories)
def get_snapshot_zip():
""" Get Zip File of Specified Student Snapshot. """

Expand Down Expand Up @@ -473,6 +500,7 @@ def get_snapshot_zip():
#
@app.route('/put_student_report', methods=['POST'])
@requires_apikey
@create_directories(directories=all_directories)
def put_student_report():
""" Put Specified File into Specified Student Home Directory. """

Expand Down Expand Up @@ -555,6 +583,7 @@ def put_student_report():
#
@app.route('/snapshot', methods=['POST'])
@requires_apikey
@create_directories(directories=all_directories)
def snapshot():
""" Create a Snapshot of the Specified Student's Home Directory with the Specified Snapshot Name. """

Expand Down Expand Up @@ -655,6 +684,7 @@ def snapshot():
#
@app.route('/snapshot_all', methods=['POST'])
@requires_apikey
@create_directories(directories=all_directories)
def snapshot_all():
""" Create a Snapshot of tll the Student's Home Directories with the Specified Snapshot Name. """

Expand Down

0 comments on commit 5754e36

Please sign in to comment.