summaryrefslogtreecommitdiff
path: root/kbas.py
diff options
context:
space:
mode:
authorPaul Sherwood <paul.sherwood@codethink.co.uk>2015-08-31 13:45:44 +0000
committerPaul Sherwood <paul.sherwood@codethink.co.uk>2015-08-31 13:45:44 +0000
commit12a56baafc6f12a46fbf71598b5bef8e367e7bbb (patch)
treeec62285295d1cc573d1ba8101b2658fa35b62d75 /kbas.py
parent295c9aef2dc72e4f31bb21075b09c09d2ef5e72a (diff)
downloadybd-12a56baafc6f12a46fbf71598b5bef8e367e7bbb.tar.gz
Move stuff around
Diffstat (limited to 'kbas.py')
-rwxr-xr-xkbas.py96
1 files changed, 96 insertions, 0 deletions
diff --git a/kbas.py b/kbas.py
new file mode 100755
index 0000000..7dc3a3a
--- /dev/null
+++ b/kbas.py
@@ -0,0 +1,96 @@
+#!/usr/bin/env python
+# Copyright (C) 2015 Codethink Limited
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# =*= License: GPL-2 =*=
+
+import logging
+import os
+import glob
+import shutil
+import time
+import tempfile
+import yaml
+from bottle import Bottle, request, response, template, static_file
+
+import app
+
+config = {}
+bottle = Bottle()
+
+
+class KeyedBinaryArtifactServer(object):
+
+ ''' Generic artifact cache server
+
+ Configuration can be found in the associated kbas.conf file.'''
+
+ def __init__(self):
+ app.load_configs(['./kbas.conf', './config/kbas.conf'])
+
+ # for development:
+ if app.config.get('mode') == 'development':
+ bottle.run(host=app.config['host'], port=app.config['port'],
+ debug=True, reloader=True)
+ else:
+ bottle.run(host=app.config['host'], port=app.config['port'])
+
+ @bottle.get('/<name>')
+ @bottle.get('/artifacts/<name>')
+ def list(name=""):
+ current_dir = os.getcwd()
+ os.chdir(app.config['artifact-dir'])
+ names = glob.glob('*' + name + '*')
+ content = [[x, time.ctime(os.path.getmtime(x))] for x in names]
+ os.chdir(current_dir)
+ return template('kbas', rows=sorted(content), css='css')
+
+ @bottle.get('/get/<cache_id>')
+ def get_artifact(cache_id):
+ path = os.path.join(cache_id, cache_id)
+ return static_file(path, root=app.config['artifact-dir'], download=True)
+
+ @bottle.get('/status')
+ def status():
+ return ('ybd kbas status coming soon...')
+
+ @bottle.post('/upload')
+ def post_artifact():
+ if app.config['password'] is 'insecure' or \
+ request.forms.get('password') != app.config['password']:
+ print 'Upload attempt: password fail'
+ response.status = 401 # unauthorized
+ return
+ cache_id = request.forms.get('filename')
+ if os.path.isdir(os.path.join(app.config['artifact-dir'], cache_id)):
+ response.status = 405 # method not allowed, this artifact exists
+ return
+
+ tempfile.tempdir = config['artifact-dir']
+ tmpdir = tempfile.mkdtemp()
+ try:
+ upload = request.files.get('file')
+ upload.save(os.path.join(tmpdir, cache_id))
+ os.rename(tmpdir, os.path.join(app.config['artifact-dir'], cache_id))
+ response.status = 201 # success!
+ return
+ except:
+ # this was a race, remove the tmpdir
+ shutil.rmtree(tmpdir)
+ response.status = 999 # method not allowed, this artifact exists
+
+ return
+
+if __name__ == '__main__':
+ KeyedBinaryArtifactServer().__init__()