forked from devcurmudgeon/ybd
/
kbas.py
executable file
·99 lines (85 loc) · 3.28 KB
/
kbas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/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
config = {}
app = Bottle()
class KeyedBinaryArtifactServer(object):
''' Generic artifact cache server
Configuration can be found in the associated kbas.conf file.'''
def __init__(self):
conf = './kbas.conf'
if not os.path.exists(conf):
conf = os.path.join(os.path.dirname(__file__), 'kbas.conf')
with open(conf) as f:
text = f.read()
for key, value in yaml.safe_load(text).items():
config[key] = value
# for development:
if config['mode'] == 'development':
app.run(host=config['host'], port=config['port'],
debug=True, reloader=True)
else:
app.run(host=config['host'], port=config['port'])
@app.get('/<name>')
@app.get('/artifacts/<name>')
def list(name=""):
current_dir = os.getcwd()
os.chdir(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')
@app.get('/get/<cache_id>')
def get_artifact(cache_id):
path = os.path.join(cache_id, cache_id)
return static_file(path, root=config['artifact-dir'], download=True)
@app.get('/status')
def status():
return ('ybd kbas status coming soon...')
@app.post('/upload')
def post_artifact():
if config['password'] is 'insecure' or \
request.forms.get('password') != 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(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(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__()