Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | import http.server |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | import multiprocessing |
| 3 | import os |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 4 | import traceback |
| 5 | import signal |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 6 | from socketserver import ThreadingMixIn |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 8 | class HTTPServer(ThreadingMixIn, http.server.HTTPServer): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 9 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 10 | def server_start(self, root_dir, logger): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | os.chdir(root_dir) |
| 12 | self.serve_forever() |
| 13 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 14 | class HTTPRequestHandler(http.server.SimpleHTTPRequestHandler): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 15 | |
| 16 | def log_message(self, format_str, *args): |
| 17 | pass |
| 18 | |
| 19 | class HTTPService(object): |
| 20 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 21 | def __init__(self, root_dir, host='', logger=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | self.root_dir = root_dir |
| 23 | self.host = host |
| 24 | self.port = 0 |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 25 | self.logger = logger |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | |
| 27 | def start(self): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 28 | if not os.path.exists(self.root_dir): |
| 29 | self.logger.info("Not starting HTTPService for directory %s which doesn't exist" % (self.root_dir)) |
| 30 | return |
| 31 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | self.server = HTTPServer((self.host, self.port), HTTPRequestHandler) |
| 33 | if self.port == 0: |
| 34 | self.port = self.server.server_port |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 35 | self.process = multiprocessing.Process(target=self.server.server_start, args=[self.root_dir, self.logger]) |
| 36 | |
| 37 | # The signal handler from testimage.bbclass can cause deadlocks here |
| 38 | # if the HTTPServer is terminated before it can restore the standard |
| 39 | #signal behaviour |
| 40 | orig = signal.getsignal(signal.SIGTERM) |
| 41 | signal.signal(signal.SIGTERM, signal.SIG_DFL) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | self.process.start() |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 43 | signal.signal(signal.SIGTERM, orig) |
| 44 | |
| 45 | if self.logger: |
| 46 | self.logger.info("Started HTTPService on %s:%s" % (self.host, self.port)) |
| 47 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | |
| 49 | def stop(self): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 50 | if hasattr(self, "server"): |
| 51 | self.server.server_close() |
| 52 | if hasattr(self, "process"): |
| 53 | self.process.terminate() |
| 54 | self.process.join() |
| 55 | if self.logger: |
| 56 | self.logger.info("Stopped HTTPService on %s:%s" % (self.host, self.port)) |
| 57 | |