-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpowerline.py
More file actions
118 lines (82 loc) · 2.89 KB
/
Copy pathpowerline.py
File metadata and controls
118 lines (82 loc) · 2.89 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import subprocess
import logging
import getpass
from flask import Flask, request, abort
from systemd.journal import JournaldLogHandler
# #########
# Constants
# #########
HOMEDIR = "/home"
STEP_FILE_NAME = ".cuteworks-powerline-steps"
# #######
# Globals
# #######
request_count = 0
endpoint_map = {}
# ################
# Initialize Flask
# ################
app = Flask(__name__)
@app.before_request
def before_request():
global request_count
request_count = request_count + 1
@app.route('/')
def splash():
logger.debug("serving splash endpoint")
return "Hello world, powerline running as " + getpass.getuser() + " and has served " + str(request_count) +\
" requests this session"
def route_handler():
endpoint = request.endpoint
logger.debug("handling request for route: " + endpoint)
if endpoint not in endpoint_map:
logger.debug("endpoint not mapped: " + endpoint)
abort(500)
script = endpoint_map[endpoint]
if not os.access(script, os.X_OK):
abort(500, "script not executable")
logger.debug("executing: " + endpoint + " -> " + script)
subprocess.call([script])
return "executed as " + getpass.getuser()
# ##############
# Set up logging
# ##############
logger = logging.getLogger("cuteworks-powerline")
journald_handler = JournaldLogHandler()
journald_handler.setFormatter(logging.Formatter('[%(levelname)s] %(message)s'))
logger.addHandler(journald_handler)
logger.setLevel(logging.DEBUG)
# ###############
# Read step files
# ###############
logger.debug("traversing home directories and parsing step files")
homedirs = [f.path for f in os.scandir(HOMEDIR) if (f.is_dir() and f.name != "lost+found")]
for p in homedirs:
if not os.path.exists(p + "/" + STEP_FILE_NAME):
logger.debug(" no step file in " + p)
continue
logger.debug(" step file in " + p)
with open(p + "/" + STEP_FILE_NAME) as f:
for line in f:
split = line.rstrip().split(":")
if len(split) != 2:
logger.debug(" invalid format, skipping this line: " + line)
continue
endpoint = split[0]
script = split[1]
if endpoint in endpoint_map:
logger.debug(" skipping endpoint /" + endpoint + ", already mapped to " + endpoint_map[endpoint])
continue
if not os.path.exists(script):
logger.debug(" script does not exist or is unreadable, skipping this script: " + script)
continue
logger.debug(" mapping endpoint: /" + endpoint + " -> " + script)
endpoint_map[endpoint] = script
app.add_url_rule("/" + endpoint, endpoint, route_handler, methods=["GET", "POST"])
# ##################
# Launch application
# ##################
logger.debug("ready")
if __name__ == '__main__':
app.run(port=22026, host="0.0.0.0")