xref: /aosp_15_r20/external/walt/server/main.py (revision bf47c6829f95be9dd55f4c5bbc44a71c90aad403)
1*bf47c682SAndroid Build Coastguard Worker#!/usr/bin/env python
2*bf47c682SAndroid Build Coastguard Worker#
3*bf47c682SAndroid Build Coastguard Worker# Copyright 2017 The Chromium OS Authors. All rights reserved.
4*bf47c682SAndroid Build Coastguard Worker#
5*bf47c682SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*bf47c682SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*bf47c682SAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*bf47c682SAndroid Build Coastguard Worker#
9*bf47c682SAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
10*bf47c682SAndroid Build Coastguard Worker#
11*bf47c682SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*bf47c682SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*bf47c682SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*bf47c682SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*bf47c682SAndroid Build Coastguard Worker# limitations under the License.
16*bf47c682SAndroid Build Coastguard Worker#
17*bf47c682SAndroid Build Coastguard Worker
18*bf47c682SAndroid Build Coastguard Worker"""
19*bf47c682SAndroid Build Coastguard WorkerRuns a server that receives log uploads from the WALT app
20*bf47c682SAndroid Build Coastguard WorkerUsage example:
21*bf47c682SAndroid Build Coastguard Worker    $ python main.py
22*bf47c682SAndroid Build Coastguard Worker"""
23*bf47c682SAndroid Build Coastguard Worker
24*bf47c682SAndroid Build Coastguard Workerimport time
25*bf47c682SAndroid Build Coastguard Workerimport os
26*bf47c682SAndroid Build Coastguard Worker
27*bf47c682SAndroid Build Coastguard Workertry:
28*bf47c682SAndroid Build Coastguard Worker    from bottle import route, template, run, request, static_file
29*bf47c682SAndroid Build Coastguard Workerexcept:
30*bf47c682SAndroid Build Coastguard Worker    print('Could not import bottle! Please install bottle, e.g. pip install bottle')
31*bf47c682SAndroid Build Coastguard Worker    raise
32*bf47c682SAndroid Build Coastguard Worker
33*bf47c682SAndroid Build Coastguard Worker@route('/')
34*bf47c682SAndroid Build Coastguard Workerdef index():
35*bf47c682SAndroid Build Coastguard Worker    if not os.path.isdir('logs/'):
36*bf47c682SAndroid Build Coastguard Worker        return 'No files uploaded yet'
37*bf47c682SAndroid Build Coastguard Worker    filenames = []
38*bf47c682SAndroid Build Coastguard Worker    for file in os.listdir('logs/'):
39*bf47c682SAndroid Build Coastguard Worker        if file.endswith('.txt'):
40*bf47c682SAndroid Build Coastguard Worker            filenames.append(file)
41*bf47c682SAndroid Build Coastguard Worker    return template('make_table', filenames=filenames)
42*bf47c682SAndroid Build Coastguard Worker
43*bf47c682SAndroid Build Coastguard Worker
44*bf47c682SAndroid Build Coastguard Worker@route('/logs/<filename>')
45*bf47c682SAndroid Build Coastguard Workerdef static(filename):
46*bf47c682SAndroid Build Coastguard Worker    return static_file(filename, root='logs')
47*bf47c682SAndroid Build Coastguard Worker
48*bf47c682SAndroid Build Coastguard Worker
49*bf47c682SAndroid Build Coastguard Worker@route('/upload', method='POST')
50*bf47c682SAndroid Build Coastguard Workerdef upload():
51*bf47c682SAndroid Build Coastguard Worker    body = request.body.getvalue()
52*bf47c682SAndroid Build Coastguard Worker    request.body.close()
53*bf47c682SAndroid Build Coastguard Worker    filename = 'logs/' + str(int(time.time()*1000)) + '.txt'
54*bf47c682SAndroid Build Coastguard Worker    if not os.path.exists(os.path.dirname(filename)):
55*bf47c682SAndroid Build Coastguard Worker        try:
56*bf47c682SAndroid Build Coastguard Worker            os.makedirs(os.path.dirname(filename))
57*bf47c682SAndroid Build Coastguard Worker        except OSError as e:
58*bf47c682SAndroid Build Coastguard Worker            if e.errno != errno.EEXIST:
59*bf47c682SAndroid Build Coastguard Worker                raise
60*bf47c682SAndroid Build Coastguard Worker    with open(filename, 'w') as file:
61*bf47c682SAndroid Build Coastguard Worker        file.write(body)
62*bf47c682SAndroid Build Coastguard Worker    return 'success'
63*bf47c682SAndroid Build Coastguard Worker
64*bf47c682SAndroid Build Coastguard Worker
65*bf47c682SAndroid Build Coastguard Workerrun(host='localhost', port=8080)
66*bf47c682SAndroid Build Coastguard Worker
67