Skip to content

Commit

Permalink
cleanup of __init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cnoam committed Apr 4, 2020
1 parent 406fd1c commit 98ad354
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 42 deletions.
2 changes: 1 addition & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# To run with test web server: python run.py

from serverpkg import app
from serverpkg.server import app

# see https://help.pythonanywhere.com/pages/Flask/

Expand Down
30 changes: 1 addition & 29 deletions serverpkg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1 @@
import sys,os
from flask import Flask
from .logger_init import init_logger
from . import job_status

if sys.version_info.major != 3:
raise Exception("must use python 3")

logger = init_logger('server')

UPLOAD_FOLDER = r'/tmp'
ALLOWED_EXTENSIONS = {'zip','gz','xz','py','sh','patch','java'} # TODO: replace with per exercise list

MAX_CONCURRENT_JOBS = os.cpu_count()
if MAX_CONCURRENT_JOBS is None:
MAX_CONCURRENT_JOBS = 2 # rumored bug in getting the cpu count

app = Flask(__name__, template_folder='./templates')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
app.config['matcher_dir'] = 'serverpkg/matchers'
app.config['runner_dir'] = 'serverpkg/runners'
app.config['assignment_config_file'] = 'hw_settings.json'
app.secret_key = b'3456o00sdf045'

_job_status_db = job_status.JobStatusDB()

from . import admin
from . import server
"""TODO: prepare doctring here of the server package"""
16 changes: 10 additions & 6 deletions serverpkg/admin.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
"""
Admin pages are placed in this module.
It is loaded from the server module.
"""

from flask import Flask, render_template, request, url_for, flash
from flask import render_template, request, url_for, flash
from flask_login import LoginManager, login_user, logout_user, login_required
from werkzeug.utils import redirect, secure_filename
from werkzeug.utils import redirect
from flask_login import UserMixin
from http import HTTPStatus
from serverpkg import app, logger

from . server import app, logger

login_manager = LoginManager()
login_manager.init_app(app)

# already in __init__ app.config['SECRET_KEY'] = "k490sk6257s" # a random string that will be used to sign cookies by flask


class User(UserMixin):
def __init__(self):
self.username = 'admin'
self.password = 'pass'
self.id = "77"


the_single_user = User()

@login_manager.user_loader
Expand Down Expand Up @@ -59,6 +62,7 @@ def show_ex_config():
contents = fin.read()
return render_template('dump_source_code.html', source = contents)


def _upload_and_save():
# check if the post request has the file part
if 'file' not in request.files:
Expand Down
77 changes: 71 additions & 6 deletions serverpkg/server.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
# written and tested on linux only
# It will not work on Windows
import sys, os
if sys.version_info.major != 3:
raise Exception("must use python 3")

from .logger_init import init_logger
from http import HTTPStatus
from flask import Flask, flash, request, redirect, url_for,render_template
from werkzeug.utils import secure_filename
import subprocess
from .asyncChecker import AsyncChecker
from . import show_jobs, job_status

from serverpkg import * # the right way?!

from .asyncChecker import AsyncChecker
from . import _job_status_db
from . import show_jobs


# -- prepare some global vars
app = Flask(__name__, template_folder='./templates')

logger = init_logger('server')


from . import admin

_job_status_db = job_status.JobStatusDB()

ALLOWED_EXTENSIONS = {'zip','gz','xz','py','sh','patch','java'} # TODO: replace with per exercise list
MAX_CONCURRENT_JOBS = os.cpu_count()
if MAX_CONCURRENT_JOBS is None:
MAX_CONCURRENT_JOBS = 2 # rumored bug in getting the cpu count

def _configure_app():
app.config['UPLOAD_FOLDER'] = '/tmp'
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
app.config['matcher_dir'] = 'serverpkg/matchers'
app.config['runner_dir'] = 'serverpkg/runners'
app.config['assignment_config_file'] = 'hw_settings.json'

app.config['LDAP_HOST'] = 'ldap://ccldap.technion.ac.il'
app.config['LDAP_BASE_DN'] = 'ou=tx,dc=technion,dc=ac,dc=il'
# app.config['LDAP_USERNAME'] = 'cn=cnoam,ou=Users,dc=il'
# app.config['LDAP_PASSWORD'] = '--'
app.secret_key = b'3456o00sdf045'


class SanityError(Exception):
Expand All @@ -27,6 +58,39 @@ def _running_on_dev_machine():
import socket
return socket.gethostname() == 'noam-cohen-u.iem.technion.ac.il'

# ---------------------

_configure_app()



# ssl
#LDAP_SCHEMA = environ.get('LDAP_SCHEMA', 'ldaps')
app.config['LDAP_PORT'] = DAP_PORT = os.environ.get('LDAP_PORT', 636)
# openLDAP
#app.config['LDAP_OPENLDAP'] = True
# Users
#app.config['LDAP_USER_OBJECT_FILTER'] = '(uid=%s)'
# Groups
#app.config['LDAP_GROUP_MEMBER_FILTER'] = '(|(&(objectClass=*)(member=%s)))'
#app.config['LDAP_GROUP_MEMBER_FILTER_FIELD'] = 'cn'
# Error Route
# @app.route('/unauthorized') <- corresponds with the path of this route when authentication fails
#app.config['LDAP_LOGIN_VIEW'] = 'unauthorized'
#ldap = LDAP(app)

#g.user = "nnn"
# @app.route('/ldap')
# #@ldap.login_required
# def test_ldap():
# test = ldap.bind_user(app.config['LDAP_USERNAME'], app.config['LDAP_PASSWORD'])
# print(test)

@app.errorhandler(401)
@app.route('/unauthorized')
def unauthorized_message(e):
return 'Unauthorized, username or password incorrect'
# ---------------------------------
@app.route('/',methods = ['GET'])
def index():
return render_template('index.html', running_locally = _running_on_dev_machine())
Expand Down Expand Up @@ -113,6 +177,7 @@ def _upload_file(course_num, ex_type, ex_number, compare_to_golden = False):
else:
flash("Please check the file type!")
return render_template('upload_homework.html',
file_types = str(ALLOWED_EXTENSIONS),
course_number=course_num, hw_number=ex_number,
num_jobs_running=_job_status_db.num_running_jobs())

Expand Down Expand Up @@ -194,7 +259,7 @@ def _get_config_for_ex(course_number, ex_type,ex_number):
try:
params = params[str(course_number)]
except KeyError:
logger.warn("course number not found in config file")
logger.warning("course number not found in config file")
raise KeyError("course number {} not found in the config file".format(course_number))
for e in params:
if e['id'] == ex_number:
Expand Down Expand Up @@ -283,7 +348,7 @@ def handle_file_blocking(package_under_test, reference_input, reference_output):
return utils.wrap_html_source(message)


#TODO: fix it - the file is not available during the test run in /tmp course_id = _get_configured_course_ids()
course_id = _get_configured_course_ids()
# moved to run.py
# if __name__ == '__main__':
# logger.warning("Starting the server as standalone")
Expand Down

0 comments on commit 98ad354

Please sign in to comment.