Compare commits

...

2 commits

Author SHA1 Message Date
dece cb3433e93c shrlok: various improvements 2022-07-05 16:42:15 +02:00
dece a0929c996c gitignore 2022-07-04 22:00:13 +02:00
2 changed files with 19 additions and 4 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
build/
dist/
*.egg-info/

View file

@ -48,6 +48,9 @@ class Handler(socketserver.StreamRequestHandler):
print("Unknown type.") print("Unknown type.")
return return
if not file_name:
return
print(f"{len(data)} bytes — {header}'{file_name}'.") print(f"{len(data)} bytes — {header}'{file_name}'.")
self.request.sendall(file_name.encode()) self.request.sendall(file_name.encode())
@ -57,7 +60,8 @@ def write_text(data: bytes, title=None):
html = HTML_TEMPLATE.format(title=title or "?", content=content) html = HTML_TEMPLATE.format(title=title or "?", content=content)
path = str(Path(ARGS.root) / "txt") path = str(Path(ARGS.root) / "txt")
if not os.path.isdir(path): if not os.path.isdir(path):
os.makedirs(path) print(f"Missing '{path}' folder.")
return None
with tempfile.NamedTemporaryFile( with tempfile.NamedTemporaryFile(
mode="wt", mode="wt",
dir=path, dir=path,
@ -65,19 +69,27 @@ def write_text(data: bytes, title=None):
delete=False delete=False
) as output_file: ) as output_file:
output_file.write(html) output_file.write(html)
os.chmod(output_file.name, 0o644)
return output_file.name return output_file.name
def main(): def main():
argp = argparse.ArgumentParser() argp = argparse.ArgumentParser(description="Share stuff through a socket.")
argp.add_argument("root", help="root path where to put files") argp.add_argument("root", help="root path where to put files")
argp.add_argument("-s", "--socket", help="socket path")
global ARGS global ARGS
ARGS = argp.parse_args() ARGS = argp.parse_args()
uid = os.getuid() socket_path = ARGS.socket
socket_path = f"/run/user/{uid}/shr.sock" if not socket_path:
runtime_dir = os.environ.get("RUNTIME_DIRECTORY")
if not runtime_dir:
exit("No socket path nor runtime directory specified.")
socket_path = os.path.join(runtime_dir, "shr.sock")
print("Socket path:", socket_path)
if os.path.exists(socket_path): if os.path.exists(socket_path):
os.unlink(socket_path) os.unlink(socket_path)
try: try:
with socketserver.UnixStreamServer(socket_path, Handler) as server: with socketserver.UnixStreamServer(socket_path, Handler) as server:
server.serve_forever() server.serve_forever()