From 066e77d4931178384bb749828f0a2ee16d923ccd 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 01/10] 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 eaccce8c6e1079042bfed6515cc0be38489e96b4 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 02/10] 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 da984d80e4cdc4d480a0faf63e027f819ee1e709 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 03/10] refactor: Rename load_remote_blocklist->load_blocklist_from_instance --- mastodon_blocklist_deploy/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mastodon_blocklist_deploy/cli.py b/mastodon_blocklist_deploy/cli.py index b049471..a9833b1 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}', } From ce5c1ae39dade60e419a379d82289135c6919dc0 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 04/10] 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 0b49740e83cb5a4a6e83c8f24b47c61d1d2eb2da 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 05/10] 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 ddc2ba1b432aabe8599509b547a5b48cde4e2614 Mon Sep 17 00:00:00 2001 From: Georg Krause Date: Thu, 12 Jan 2023 16:31:58 +0100 Subject: [PATCH 06/10] 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 229608a0904275dfa882c1a143fa450ac2bd79ee Mon Sep 17 00:00:00 2001 From: Georg Krause Date: Thu, 12 Jan 2023 17:07:28 +0100 Subject: [PATCH 07/10] 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 d8ccaee..d24f2be 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: @@ -105,9 +88,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__": From 2066c0332d754a974a9fae36d61d855d9c11a2d0 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 08/10] 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 58998e1c176fe4c2cf37f8c581df04f8d77c2962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian-Samuel=20Geb=C3=BChr?= Date: Wed, 25 Jan 2023 23:35:04 +0100 Subject: [PATCH 09/10] fix: Allow name to be empty in local blocklist --- mastodon_blocklist_deploy/models.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mastodon_blocklist_deploy/models.py b/mastodon_blocklist_deploy/models.py index 5997ab0..1fad913 100644 --- a/mastodon_blocklist_deploy/models.py +++ b/mastodon_blocklist_deploy/models.py @@ -38,7 +38,10 @@ class Instance: self.reject_reports = instance_dict["reject_reports"] def parse_local_block(self, instance_dict): - self.name = instance_dict["name"] + try: + self.name = instance_dict["name"] + except KeyError: + pass self.domain = instance_dict["domain"] self.severity = instance_dict["severity"] self.public_comment = instance_dict["public_comment"] From 8a8a725002087fdcdbcff121ed5a375b2065d90a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian-Samuel=20Geb=C3=BChr?= Date: Wed, 25 Jan 2023 23:36:25 +0100 Subject: [PATCH 10/10] fix: Remove id from export As this wil not be the same id on other instances exporting it does not make sense --- mastodon_blocklist_deploy/cli.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mastodon_blocklist_deploy/cli.py b/mastodon_blocklist_deploy/cli.py index 89aa964..7bdb6e1 100644 --- a/mastodon_blocklist_deploy/cli.py +++ b/mastodon_blocklist_deploy/cli.py @@ -38,6 +38,9 @@ def load_blocklist_from_instance(server: str, token: str) -> [Instance]: else: raise ConnectionError(f"Could not connect to the server ({response.status_code}: {response.reason})") +def remove_key_from_dict(dict, key): + del dict[key] + return dict def cli(): parser = argparse.ArgumentParser(description='Deploy blocklist updates to a mastodon server') @@ -92,10 +95,10 @@ def cli(): Instance.apply_blocks_from_diff(diffs, args.server, token, args.no_delete) elif args.action == "export": if not args.output: - print(toml.dumps({"instances": [b.__dict__ for b in remote_blocklist]})) + print(toml.dumps({"instances": [remove_key_from_dict(b.__dict__, 'id') for b in remote_blocklist]})) else: with open(args.output, "w") as f: - toml.dump({"instances": [b.__dict__ for b in remote_blocklist]}, f) + toml.dump({"instances": [remove_key_from_dict(b.__dict__, 'id') for b in remote_blocklist]}, f) if __name__ == "__main__":