add webdev sub command

This commit is contained in:
holger krekel
2023-12-09 16:42:03 +01:00
parent 81c4a6170f
commit 6bffb5470d
5 changed files with 39 additions and 86 deletions

View File

@@ -11,7 +11,7 @@ def read_config(inipath):
class Config:
def __init__(self, inipath, params):
self._inipath = inipath
self.mailname = params["mailname"]
self.mailname = self.mail_domain = params["mailname"]
self.max_user_send_per_minute = int(params["max_user_send_per_minute"])
self.filtermail_smtp_port = int(params["filtermail_smtp_port"])
self.postfix_reinject_port = int(params["postfix_reinject_port"])

View File

@@ -413,7 +413,7 @@ def deploy_chatmail(mail_domain: str, mail_server: str, dkim_selector: str) -> N
pkg_root = importlib.resources.files(__package__)
chatmail_ini = pkg_root.joinpath("../../../chatmail.ini").resolve()
config = read_config(chatmail_ini, mailname=mail_domain)
config = read_config(chatmail_ini)
check_config(config)
www_path = pkg_root.joinpath("../../../www").resolve()

View File

@@ -4,6 +4,8 @@ along with command line option and subcommand parsing.
"""
import importlib.resources
import argparse
import subprocess
import os
from pathlib import Path
import iniconfig
@@ -70,8 +72,17 @@ def get_parser():
install_parser = add_subcommand(subparsers, install_cmd)
add_config_option(install_parser)
install_parser.add_argument(
"--dry-run",
dest="dry_run",
action="store_true",
help="don't actually modify the server",
)
add_subcommand(subparsers, webdev_cmd)
return parser
def write_initial_config(inipath, mailname, out):
inidir = importlib.resources.files(__package__).joinpath("ini")
content = inidir.joinpath("chatmail.ini.f").read_text().format(mailname=mailname)
@@ -102,20 +113,38 @@ def write_initial_config(inipath, mailname, out):
def init_cmd(args, out):
"""Initialize chatmail config file."""
if args.chatmail_ini.exists():
out.red(f"Path exists, not modifying: {args.xdcget_ini}")
out.red(f"Path exists, not modifying: {args.chatmail_ini}")
raise SystemExit(1)
write_initial_config(args.chatmail_ini, args.chatmail_domain, out)
def install_cmd(args, out):
"""Install or update chatmail services on the remote server. """
"""Install or update chatmail services on the remote server."""
import pyinfra
try:
config = read_config(args.chatmail_ini)
except Exception as ex:
out.red(ex)
raise SystemExit(1)
XXX
popen_args = ["pyinfra"]
if args.dry_run:
popen_args.append("--dry")
popen_args.extend(["--ssh-user", "root", config.mailname])
popen_args.append("deploy-chatmail/src/deploy_chatmail/deploy.py")
out(f"{os.getcwd()} $ {' '.join(popen_args)}")
env = os.environ.copy()
env["CHATMAIL_DOMAIN"] = config.mailname
subprocess.check_call(popen_args, env=env)
def webdev_cmd(args, out):
"""Run web development loop for static local web pages."""
from .www import main
main()
def main(args=None):

View File

@@ -66,12 +66,12 @@ def _build_webpages(src_dir, build_dir, config):
def main():
chatmail_domain = "example.testrun.org"
path = importlib.resources.files(__package__)
reporoot = path.joinpath("../../../").resolve()
inipath = reporoot.joinpath("chatmail.ini")
config = read_config(inipath, mailname=chatmail_domain)
config["webdev"] = True
config = read_config(inipath)
config.webdev = True
assert config.mailname
www_path = reporoot.joinpath("www")
src_path = www_path.joinpath("src")
stats = None

View File

@@ -21,84 +21,8 @@ class TestCmdline:
config = read_config(inipath.strpath)
assert config.mailname == "chat.example.org"
def test_no_args_description(self, capsys):
with pytest.raises(SystemExit) as excinfo:
main([])
assert excinfo.value.code == 0
out, err = capsys.readouterr()
assert "Collect webxdc" in out
assert " init " in out and "Initialize config" in out
def test_version(self, capsys):
with pytest.raises(SystemExit) as excinfo:
main(["--version"])
assert excinfo.value.code == 0
out, err = capsys.readouterr()
assert out.strip() == xdcget.__version__
def test_init_not_overwrite(self, tmpdir):
tmpdir.chdir()
main(["init"])
main(["init", "chat.example.org"])
with pytest.raises(SystemExit):
main(["init"])
def test_update_from_different_dir(self, config_example1, tmp_path):
p = tmp_path.joinpath("somewhere")
p.mkdir()
os.chdir(p)
main(["--config", "../xdcget.ini", "update"])
def test_prune_index(self, iniconfig):
iniconfig.add_source(
app_id="webxdc-poll",
source_code_url="https://codeberg.org/webxdc/poll",
)
iniconfig.add_lock_entry(
app_id="webxdc-poll",
name="Poll",
tag_name="v1.0.1",
url="https://codeberg.org/attachments/d53543bd-d805-4aba-926d-88eefc7a9eef",
date="2023-07-05T20:30:48Z",
cache_relname="webxdc-poll-v1.0.1.xdc",
)
iniconfig.add_lock_entry(
app_id="webxdc-checklist",
name="Checklist",
tag_name="v0.0.2",
url="https://codeberg.org/attachments/65d05b8d-a97c-4fb6-a534-e308c382f874",
date="2023-07-07T18:05:19Z",
cache_relname="webxdc-checklist-v0.0.2.xdc",
)
config = iniconfig.create()
assert "webxdc-poll" in config.index_path.read_text()
assert "webxdc-checklist" in config.index_path.read_text()
main(["update"])
assert "webxdc-poll" in config.index_path.read_text()
assert "webxdc-checklist" not in config.index_path.read_text()
def test_update_empty(self, iniconfig):
iniconfig.create()
with pytest.raises(SystemExit):
main(["update"])
def test_update_no_network(self, capfd, config_example1, monkeypatch):
main(["update"])
p = config_example1.export_dir.joinpath("xdcget.lock")
assert p.exists()
assert len(p.read_text()) > 50
monkeypatch.delattr(sys.modules["requests"], "get")
main(["update", "--offline"])
def test_export_json(self, capfd, config_example1, monkeypatch):
main(["update"])
p = config_example1.export_dir.joinpath("xdcget-lock.json")
assert p.exists()
with p.open() as f:
app_list = json.load(f)
assert len(app_list) == 2
checklist, poll = app_list
p = config_example1.export_dir.joinpath(checklist["icon_relname"])
assert p.exists() and "icon" in p.name
p = config_example1.export_dir.joinpath(poll["icon_relname"])
assert p.exists() and "icon" in p.name
main(["init", "chat.example.org"])