-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch.py
More file actions
65 lines (53 loc) · 2.03 KB
/
launch.py
File metadata and controls
65 lines (53 loc) · 2.03 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
import http.server
import socketserver
import webbrowser
import sys
from pathlib import Path
PORT = 8000
def _get_script_dir():
"""Base directory of script/exe. Handles PyInstaller --onefile (temp extraction)."""
if not getattr(sys, 'frozen', False):
return Path(__file__).parent.resolve()
exe_dir = Path(sys.executable).parent.resolve()
path_str = str(exe_dir).lower()
# PyInstaller onefile: exe runs from temp (_MEIPASS). Get real exe location.
if '_meipass' in path_str or ('_mei' in path_str and 'temp' in path_str):
if sys.platform == 'win32':
try:
import ctypes
buf = ctypes.create_unicode_buffer(1024)
n = ctypes.windll.kernel32.GetModuleFileNameW(None, buf, 1024)
if n > 0:
win_dir = Path(buf.value).parent.resolve()
if '_meipass' not in str(win_dir).lower():
return win_dir
except Exception:
pass
return Path.cwd()
return exe_dir
SCRIPT_DIR = _get_script_dir()
# Use script dir as root if index.html is there, otherwise use archive subfolder
if (SCRIPT_DIR / "index.html").is_file():
ROOT = SCRIPT_DIR
else:
ROOT = SCRIPT_DIR / "archive"
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=str(ROOT), **kwargs)
def log_request(self, code="-", size="-"):
if code != "-":
try:
code_int = int(code)
if 400 <= code_int < 600:
super().log_request(code, size)
except (ValueError, TypeError):
super().log_request(code, size)
try:
with socketserver.TCPServer(("127.0.0.1", PORT), Handler) as httpd:
url = f"http://127.0.0.1:{PORT}/index.html"
print(f"Serving DayOne Archive at {url}")
webbrowser.open(url)
httpd.serve_forever()
except KeyboardInterrupt:
print("\nShutting down server...")
sys.exit(0)