From 0dd6930c0f7ebe96a282eba7f6a382e7bdeb7ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian-Samuel=20Geb=C3=BChr?= Date: Thu, 12 Jan 2023 16:01:55 +0100 Subject: [PATCH 1/7] refactor: Rename load_local_blocklist -> load_blocklist_file --- mastodon_blocklist_deploy/cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mastodon_blocklist_deploy/cli.py b/mastodon_blocklist_deploy/cli.py index 8ac9c8b..0b8d607 100644 --- a/mastodon_blocklist_deploy/cli.py +++ b/mastodon_blocklist_deploy/cli.py @@ -9,7 +9,7 @@ import toml from mastodon_blocklist_deploy.models import Instance -def load_local_blocklist(filename: str) -> [Instance]: +def load_blocklist_file(filename: str) -> [Instance]: with open(filename, "r") as f: data = toml.load(f) instances = [] @@ -89,7 +89,7 @@ def cli(): blocklist_filename = args.input_file else: blocklist_filename = "../blocklist.toml" - local_blocklist = load_local_blocklist(blocklist_filename) + local_blocklist = load_blocklist_file(blocklist_filename) if args.action == "diff": Instance.show_diffs(local_blocklist, remote_blocklist) From 6a2a13bd749aa9f8eb6ac1417b356fa4aadb65ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian-Samuel=20Geb=C3=BChr?= Date: Thu, 12 Jan 2023 16:04:56 +0100 Subject: [PATCH 2/7] refactor: Add missing return types --- mastodon_blocklist_deploy/cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mastodon_blocklist_deploy/cli.py b/mastodon_blocklist_deploy/cli.py index 0b8d607..b049471 100644 --- a/mastodon_blocklist_deploy/cli.py +++ b/mastodon_blocklist_deploy/cli.py @@ -36,14 +36,14 @@ private_comment = "{instance.private_comment}" f.write(toml_str) -def blocklist_json_to_instances(blocklist_json: str): +def blocklist_json_to_instances(blocklist_json: str) -> [Instance]: instances = [] for i in blocklist_json: instances.append(Instance(i)) return instances -def load_remote_blocklist(server: str, token: str): +def load_remote_blocklist(server: str, token: str) -> [Instance]: headers = { f'Authorization': f'Bearer {token}', } From 181ac45bbf83c65747009b1120889e0160fb3fb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian-Samuel=20Geb=C3=BChr?= Date: Thu, 12 Jan 2023 16:06:01 +0100 Subject: [PATCH 3/7] refactor: Rename load_remote_blocklist->load_blocklist_from_instance --- mastodon_blocklist_deploy/cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mastodon_blocklist_deploy/cli.py b/mastodon_blocklist_deploy/cli.py index b049471..c7d5607 100644 --- a/mastodon_blocklist_deploy/cli.py +++ b/mastodon_blocklist_deploy/cli.py @@ -43,7 +43,7 @@ def blocklist_json_to_instances(blocklist_json: str) -> [Instance]: return instances -def load_remote_blocklist(server: str, token: str) -> [Instance]: +def load_blocklist_from_instance(server: str, token: str) -> [Instance]: headers = { f'Authorization': f'Bearer {token}', } @@ -81,7 +81,7 @@ def cli(): with open(args.remote_blocklist) as f: remote_blocklist = blocklist_json_to_instances(json.load(f)) else: - remote_blocklist = load_remote_blocklist(server=args.server, token=args.token) + remote_blocklist = load_blocklist_from_instance(server=args.server, token=args.token) """Load local blocklist only when needed""" if args.action in ["diff", "deploy"]: From 0fca58810a71c0a1dc2952450919af8d686a84fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian-Samuel=20Geb=C3=BChr?= Date: Thu, 12 Jan 2023 16:22:03 +0100 Subject: [PATCH 4/7] feat: Allow token via environment variables --- mastodon_blocklist_deploy/cli.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/mastodon_blocklist_deploy/cli.py b/mastodon_blocklist_deploy/cli.py index c7d5607..d8ccaee 100644 --- a/mastodon_blocklist_deploy/cli.py +++ b/mastodon_blocklist_deploy/cli.py @@ -3,7 +3,7 @@ import argparse import json import logging import requests - +import os import toml from mastodon_blocklist_deploy.models import Instance @@ -76,12 +76,19 @@ def cli(): else: logging.basicConfig(level=logging.WARN) + if args.token: + token = args.token + else: + token = os.getenv('MBD_TOKEN') + + + """if there is a remote blocklist provided load this instead of fetching it from a server (for debugging reasons)""" if args.remote_blocklist: with open(args.remote_blocklist) as f: remote_blocklist = blocklist_json_to_instances(json.load(f)) else: - remote_blocklist = load_blocklist_from_instance(server=args.server, token=args.token) + remote_blocklist = load_blocklist_from_instance(server=args.server, token=token) """Load local blocklist only when needed""" if args.action in ["diff", "deploy"]: @@ -95,7 +102,7 @@ def cli(): Instance.show_diffs(local_blocklist, remote_blocklist) elif args.action == "deploy": diffs = Instance.list_diffs(local_blocklist, remote_blocklist) - Instance.apply_blocks_from_diff(diffs, args.server, args.token, args.no_delete) + Instance.apply_blocks_from_diff(diffs, args.server, token, args.no_delete) elif args.action == "export": if not args.output: print("Export currently requires to pass --output as well") From d9d3f02fda823959f3a3e2de3f4f703083ca57ac Mon Sep 17 00:00:00 2001 From: Georg Krause Date: Thu, 12 Jan 2023 16:31:58 +0100 Subject: [PATCH 5/7] feat: Add Dockerfile for development and deployment --- DEVELOPMENT.md | 17 +++++++++++++++++ Dockerfile | 12 ++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 DEVELOPMENT.md create mode 100644 Dockerfile diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md new file mode 100644 index 0000000..5556f57 --- /dev/null +++ b/DEVELOPMENT.md @@ -0,0 +1,17 @@ +# Development Guide + +## Docker + +In order to have a common development environment, its nice to use docker. Its quite easy. To build a new image, simply run + +`docker build . -t mastodon_blocklist_deploy` + +Now you can execute any commands using + +`docker run --rm mastodon_blocklist_deploy --help` + +If you want to avoid building new containers for each change, simply mount your code into the container using + +`docker run --rm -v $(pwd):/app mastodon_blocklist_deploy` + +Please be aware that changes to the package itself require a rebuild anyways. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0cccbfc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.11-slim + +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 + +COPY pyproject.toml poetry.lock README.md /app/ +COPY mastodon_blocklist_deploy /app/mastodon_blocklist_deploy +WORKDIR /app + +ENTRYPOINT ["mastodon_blocklist_deploy"] + +RUN pip install -e . From 6d2a4d82b47ef20fd9ae8a035bfa1cc11c58fb25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian-Samuel=20Geb=C3=BChr?= Date: Fri, 13 Jan 2023 08:12:29 +0100 Subject: [PATCH 6/7] fix: Avoid exception when input is missing for diff and deploy --- mastodon_blocklist_deploy/cli.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mastodon_blocklist_deploy/cli.py b/mastodon_blocklist_deploy/cli.py index d8ccaee..0341d24 100644 --- a/mastodon_blocklist_deploy/cli.py +++ b/mastodon_blocklist_deploy/cli.py @@ -96,7 +96,11 @@ def cli(): blocklist_filename = args.input_file else: blocklist_filename = "../blocklist.toml" - local_blocklist = load_blocklist_file(blocklist_filename) + try: + local_blocklist = load_blocklist_file(blocklist_filename) + except FileNotFoundError: + print("Local blocklist file was not found. Make sure to specify it's location via -i") + exit() if args.action == "diff": Instance.show_diffs(local_blocklist, remote_blocklist) From 8d5676d0b27fb2ce231bce1807cbe915505f1dfa Mon Sep 17 00:00:00 2001 From: Georg Krause Date: Thu, 12 Jan 2023 17:07:28 +0100 Subject: [PATCH 7/7] refactor: Avoid manually templating toml file entries --- mastodon_blocklist_deploy/cli.py | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/mastodon_blocklist_deploy/cli.py b/mastodon_blocklist_deploy/cli.py index 0341d24..89aa964 100644 --- a/mastodon_blocklist_deploy/cli.py +++ b/mastodon_blocklist_deploy/cli.py @@ -19,23 +19,6 @@ def load_blocklist_file(filename: str) -> [Instance]: return instances -def export_blocklist_toml(blocklist: [Instance], filname: str): - toml_str = "" - for instance in blocklist: - toml_str += f''' -[[instances]] -name = "{instance.domain}" -domain = "{instance.domain}" -severity = "{instance.severity}" -reject_media = {str(instance.reject_media).lower()} -reject_reports = {str(instance.reject_reports).lower()} -public_comment = "{instance.public_comment}" -private_comment = "{instance.private_comment}" - ''' - with open(filname, "w") as f: - f.write(toml_str) - - def blocklist_json_to_instances(blocklist_json: str) -> [Instance]: instances = [] for i in blocklist_json: @@ -109,9 +92,10 @@ def cli(): Instance.apply_blocks_from_diff(diffs, args.server, token, args.no_delete) elif args.action == "export": if not args.output: - print("Export currently requires to pass --output as well") + print(toml.dumps({"instances": [b.__dict__ for b in remote_blocklist]})) else: - export_blocklist_toml(remote_blocklist, args.output) + with open(args.output, "w") as f: + toml.dump({"instances": [b.__dict__ for b in remote_blocklist]}, f) if __name__ == "__main__":