Skip to content

Commit

Permalink
Merge branch 'main' into shrinking_docker
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbyt3r committed Apr 15, 2024
2 parents dbca4ee + 5701f91 commit 0296bf7
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 631 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.11.4 as build
FROM python:3.12.3 as build
MAINTAINER RAMS Project "[email protected]"
LABEL version.sideboard ="1.0"
WORKDIR /app
Expand Down
29 changes: 0 additions & 29 deletions pavement.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import sys
import glob
import pkg_resources
from itertools import chain
from os.path import abspath, dirname, exists, join

Expand Down Expand Up @@ -110,28 +109,6 @@ def pull_plugins():
sh('cd "{}";git pull'.format(plugin_dir))


@task
def assert_all_files_import_unicode_literals():
"""
error if a python file is found in sideboard or plugins that does not import unicode_literals; \
this is skipped for Python 3
"""
if sys.version_info[0] == 2:
all_files_found = []
cmd = ("find '%s' -name '*.py' ! -size 0 "
"-exec grep -RL 'from __future__ import.*unicode_literals.*$' {} \;")
for test_dir in chain(['sideboard'], collect_plugin_dirs(module=True)):
output = sh(cmd % test_dir, capture=True)
if output:
all_files_found.append(output)

if all_files_found:
print('the following files did not include "from __future__ import unicode_literals":')
print(''.join(all_files_found))
raise BuildFailure("there were files that didn't include "
'"from __future__ import unicode_literals"')


@task
def assert_all_projects_correctly_define_a_version():
"""
Expand Down Expand Up @@ -180,12 +157,6 @@ def run_all_assertions():
def create_plugin(options):
"""create a plugin skeleton to start a new project"""

# this is actually needed thanks to the skeleton using jinja2 (and six, although that's changeable)
try:
pkg_resources.get_distribution("sideboard")
except pkg_resources.DistributionNotFound:
raise BuildFailure("This command must be run from within a configured virtual environment.")

plugin_name = options.create_plugin.name

if getattr(options.create_plugin, 'drop', False) and (PLUGINS_DIR / path(plugin_name.replace('_', '-'))).exists():
Expand Down
18 changes: 9 additions & 9 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
cherrypy==18.8.0
configobj==5.0.6
Jinja2==3.1.2
cherrypy==18.9.0
configobj==5.0.8
Jinja2==3.1.3
paver==1.3.4
pip==23.2.1
psutil==5.9.5
pip==24.0
psutil==5.9.8
python-prctl==1.8.1; 'linux' in sys_platform
redis==4.6.0
redis==5.0.3
requests==2.31.0
rpctools==0.3.1
sh==2.0.4
sh==2.0.6
six==1.16.0
SQLAlchemy==1.4.49
wheel==0.41.0
SQLAlchemy==1.4.52
wheel==0.43.0
ws4py==0.5.1
8 changes: 2 additions & 6 deletions sideboard/lib/_cp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
import jinja2
import cherrypy

try:
from sideboard.lib._redissession import RedisSession
cherrypy.lib.sessions.RedisSession = RedisSession
except ImportError:
# cherrys not installed, so redis sessions not supported
pass
from sideboard.lib._redissession import RedisSession
cherrypy.lib.sessions.RedisSession = RedisSession

import sideboard.lib
from sideboard.lib import log, config, serializer
Expand Down
2 changes: 0 additions & 2 deletions sideboard/lib/_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,6 @@ def _register_rpc_services(rpc_services):
jservice = getattr(jproxy, service_name)
if rpc_services.get(host, {}).get('jsonrpc_only'):
service = jservice
else:
service = services._register_websocket(_ws_url(host, rpc_opts), ssl_opts=ssl_opts, connect_immediately=False)

services.register(service, service_name, _jsonrpc=jservice, _override=True)

Expand Down
2 changes: 1 addition & 1 deletion sideboard/lib/sa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def check_constraint_naming_convention(constraint, table):
for operator, text in replacements:
constraint_name = constraint_name.replace(operator, text)

constraint_name = re.sub('[\\W\\s]+', '_', constraint_name)
constraint_name = re.sub(r'[\W\s]+', '_', constraint_name)
if len(constraint_name) > 32:
constraint_name = uuid.uuid5(uuid.NAMESPACE_OID, str(constraint_name)).hex
return constraint_name
Expand Down
144 changes: 1 addition & 143 deletions sideboard/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,128 +4,15 @@

import six
import cherrypy
from cherrypy.lib import cpstats

import sideboard
from sideboard.internal import connection_checker
from sideboard.jsonrpc import _make_jsonrpc_handler
from sideboard.websockets import WebSocketDispatcher, WebSocketRoot, WebSocketAuthError
from sideboard.lib import log, listify, config, render_with_templates, services, threadlocal
from sideboard.lib._cp import auth_registry

default_auth_checker = auth_registry[config['default_authenticator']]['check']
from sideboard.lib import config, threadlocal


def reset_threadlocal():
threadlocal.reset(**{field: cherrypy.session.get(field) for field in config['ws.session_fields']})

cherrypy.tools.reset_threadlocal = cherrypy.Tool('before_handler', reset_threadlocal, priority=51)


def jsonrpc_reset(body):
reset_threadlocal()
threadlocal.set('client', body.get('websocket_client'))


def jsonrpc_auth(body):
jsonrpc_reset(body)
if not default_auth_checker():
raise cherrypy.HTTPError(401, 'not logged in')


@render_with_templates(config['template_dir'])
class Root(object):
def default(self, *args, **kwargs):
raise cherrypy.HTTPRedirect(config['default_url'])

def logout(self, return_to='/'):
cherrypy.session.pop('username', None)
raise cherrypy.HTTPRedirect('login?return_to=%s' % return_to)

def login(self, username='', password='', message='', return_to=''):
if not config['debug']:
return 'Login page only available in debug mode.'

if username:
if config['debug'] and password == config['debug_password']:
cherrypy.session['username'] = username
raise cherrypy.HTTPRedirect(return_to or config['default_url'])
else:
message = 'Invalid credentials'

return {
'message': message,
'username': username,
'return_to': return_to
}

def list_plugins(self):
from sideboard.internal.imports import plugins
plugin_info = {}
for plugin, module in plugins.items():
plugin_info[plugin] = {
'name': ' '.join(plugin.split('_')).title(),
'version': getattr(module, '__version__', None),
'paths': []
}
for path, app in cherrypy.tree.apps.items():
# exclude what Sideboard itself mounts and grafted mount points
if path and hasattr(app, 'root'):
plugin = app.root.__module__.split('.')[0]
plugin_info[plugin]['paths'].append(path)
return {
'plugins': plugin_info,
'version': getattr(sideboard, '__version__', None)
}

def connections(self):
return {'connections': connection_checker.check_all()}

ws = WebSocketRoot()
wsrpc = WebSocketRoot()

json = _make_jsonrpc_handler(services.get_services(), precall=jsonrpc_auth)
jsonrpc = _make_jsonrpc_handler(services.get_services(), precall=jsonrpc_reset)


class SideboardWebSocket(WebSocketDispatcher):
"""
This web socket handler will be used by browsers connecting to Sideboard web
sites. Therefore, the authentication mechanism is the default approach
of checking the session for a username and rejecting unauthenticated users.
"""
services = services.get_services()

@classmethod
def check_authentication(cls):
host, origin = cherrypy.request.headers['host'], cherrypy.request.headers['origin']
if ('//' + host.split(':')[0]) not in origin:
log.error('Javascript websocket connections must follow same-origin policy; origin %s does not match host %s', origin, host)
raise WebSocketAuthError('Origin and Host headers do not match')

if config['ws.auth_required'] and not cherrypy.session.get(config['ws.auth_field']):
log.warning('websocket connections to this address must have a valid session')
raise WebSocketAuthError('You are not logged in')

return WebSocketDispatcher.check_authentication()


app_config = {
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.join(config['module_root'], 'static')
},
'/ws': {
'tools.websockets.on': True,
'tools.websockets.handler_cls': SideboardWebSocket
}
}
if config['debug']:
app_config['/docs'] = {
'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.join(config['module_root'], 'docs', 'html'),
'tools.staticdir.index': 'index.html'
}
cherrypy_config = {}
for setting, value in config['cherrypy'].items():
if isinstance(value, six.string_types):
Expand All @@ -137,32 +24,3 @@ def check_authentication(cls):
value = value.encode('utf-8')
cherrypy_config[setting] = value
cherrypy.config.update(cherrypy_config)


# on Python 2, we need bytestrings for CherryPy config, see https://bitbucket.org/cherrypy/cherrypy/issue/1184
def recursive_coerce(d):
if isinstance(d, dict):
for k, v in d.items():
if sys.version_info[:2] == (2, 7) and isinstance(k, unicode):
del d[k]
d[k.encode('utf-8')] = recursive_coerce(v)
return d


def mount(root, script_name='', config=None):
assert script_name not in cherrypy.tree.apps, '{} has already been mounted, probably by another plugin'.format(script_name)
return orig_mount(root, script_name, recursive_coerce(config))

orig_mount = cherrypy.tree.mount
cherrypy.tree.mount = mount
root = Root()
if config['cherrypy']['tools.cpstats.on']:
root.stats = cpstats.StatsPage()
cherrypy.tree.mount(root, '', app_config)

if config['cherrypy']['profiling.on']:
# If profiling is turned on then expose the web UI, otherwise ignore it.
from sideboard.lib import Profiler
cherrypy.tree.mount(Profiler(config['cherrypy']['profiling.path']), '/profiler')

sys.modules.pop('six.moves.winreg', None) # kludgy workaround for CherryPy's autoreloader erroring on winreg for versions which have this
8 changes: 0 additions & 8 deletions sideboard/tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,6 @@ class Foo(object):
assert not is_listy(x)


def test_double_mount(request):
class Root(object):
pass
request.addfinalizer(lambda: cherrypy.tree.apps.pop('/test', None))
cherrypy.tree.mount(Root(), '/test')
pytest.raises(Exception, cherrypy.tree.mount, Root(), '/test')


def test_ajaz_serialization():
class Root(object):
@ajax
Expand Down
2 changes: 1 addition & 1 deletion sideboard/tests/test_sa.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class CrudableMixin(object):
}
)
@text_length_validation('string_model_attr', 2, 100)
@regex_validation('string_model_attr', '^[A-Za-z0-9\\.\\_\\-]+$', 'test thing')
@regex_validation('string_model_attr', r'^[A-Za-z0-9\.\_\-]+$', 'test thing')
@text_length_validation('overridden_desc', 1, 100)
@text_length_validation('nonexistant_field', 1, 100)
class CrudableClass(CrudableMixin, Base):
Expand Down
Loading

0 comments on commit 0296bf7

Please sign in to comment.