Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docsite support #16

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions andeboxlib/actions/ansibletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
from ..context import ansible_collection_tree, determine_collection, binary_path, copy_exclude_lines


class AnsibleTestError(AndeboxException):
pass


class AnsibleTestAction(AndeboxAction):
name = "test"
help = "runs ansible-test in a temporary environment"
Expand All @@ -35,19 +31,19 @@ class AnsibleTestAction(AndeboxAction):
def make_parser(cls, subparser):
action_parser = super(AnsibleTestAction, cls).make_parser(subparser)
action_parser.epilog = "Notice the use of '--' to delimit andebox's options from ansible-test's"
action_parser.usage = "%(prog)s usage: andebox test [-h] [--keep] -- [ansible_test_params ...]"
action_parser.usage = "%(prog)s [-h] [--keep] -- [ansible_test_params ...]"

def run(self, args):
namespace, collection = determine_collection(args.collection)
with ansible_collection_tree(namespace, collection, args.keep) as collection_dir:
if args.requirements:
self.install_requirements(args.venv)
if args.exclude_from_ignore:
self.exclude_from_ignore(args.exclude_from_ignore, args.ansible_test_params, collection_dir)
rc = subprocess.call([binary_path(args.venv, "ansible-test")] + args.ansible_test_params, cwd=collection_dir)

if rc != 0:
raise AnsibleTestError("Error running ansible-test (rc={0})".format(rc))
try:
namespace, collection = determine_collection(args.collection)
with ansible_collection_tree(namespace, collection, args.keep) as collection_dir:
if args.requirements:
self.install_requirements(args.venv)
if args.exclude_from_ignore:
self.exclude_from_ignore(args.exclude_from_ignore, args.ansible_test_params, collection_dir)
subprocess.run([binary_path(args.venv, "ansible-test")] + args.ansible_test_params, cwd=collection_dir, check=True)
except Exception as e:
raise AndeboxException("Error running ansible-test") from e

def exclude_from_ignore(self, exclude_from_ignore, ansible_test_params, coll_dir):
files = [f for f in ansible_test_params if os.path.isfile(f)]
Expand All @@ -59,16 +55,13 @@ def exclude_from_ignore(self, exclude_from_ignore, ansible_test_params, coll_dir
for ts_entry in ts_dir:
if ts_entry.name.startswith('ignore-') and ts_entry.name.endswith('.txt'):
copy_exclude_lines(os.path.join(src_dir, ts_entry.name),
os.path.join(dest_dir, ts_entry.name),
files)
os.path.join(dest_dir, ts_entry.name), files)

@staticmethod
def install_requirements(venv):
rc = subprocess.call([binary_path(venv, "ansible-galaxy"),
"collection",
"install",
"-r",
os.path.join('.', 'tests', 'integration', 'requirements.yml')])

if rc != 0:
raise AnsibleTestError("Error installing dependencies (rc={0})".format(rc))
subprocess.run([
binary_path(venv, "ansible-galaxy"), "collection", "install", "-r",
os.path.join('.', 'tests', 'integration', 'requirements.yml')
],
check=True
)
62 changes: 62 additions & 0 deletions andeboxlib/actions/docsite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
# (c) 2024, Alexei Znamensky <russoz@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

import os
import subprocess
import webbrowser
from contextlib import contextmanager
from pathlib import Path

from .base import AndeboxAction
from ..exceptions import AndeboxException
from ..context import ansible_collection_tree, determine_collection, binary_path


@contextmanager
def set_dir(path):
previous = Path().absolute()
try:
os.chdir(path)
yield
finally:
os.chdir(previous)


class DocsiteAction(AndeboxAction):
name = "docsite"
help = "builds collection docsite"
args = [
dict(names=("--keep", "-k"),
specs=dict(action="store_true", help="Keep temporary collection directory after execution")),
dict(names=("--venv", "-V"),
specs=dict(help="""path to the virtual environment where andebox and ansible are installed""")),
dict(names=("--open", "-o"),
specs=dict(action="store_true", help="Open browser pointing to main page after build")),
dict(names=("--dest-dir", "-d"),
specs=dict(help="Directory which should contain the docsite", default=".builtdocs")),
]

def run(self, args):
try:
namespace, collection = determine_collection(args.collection)

with ansible_collection_tree(namespace, collection, args.keep) as collection_dir:
os.makedirs(args.dest_dir, mode=0o755, exist_ok=True)
if not os.path.exists(os.path.join(args.dest_dir, "build.sh")):
subprocess.run([
binary_path(args.venv, "antsibull-docs"),
"sphinx-init", "--use-current", "--lenient", f"{namespace}.{collection}", "--dest-dir", args.dest_dir
],
cwd=collection_dir,
check=True
)

with set_dir(args.dest_dir):
subprocess.run([binary_path(args.venv, "python"), "-m", "pip", "install", "-qr", "requirements.txt"], check=True)
subprocess.run(["./build.sh"], check=True)
except Exception as e:
raise AndeboxException("Error running when building docsite") from e

if args.open:
webbrowser.open(f"{os.path.join(args.dest_dir, 'build', 'html', 'index.html')}")
2 changes: 1 addition & 1 deletion andeboxlib/actions/toxtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ToxTestAction(AndeboxAction):
def make_parser(cls, subparser):
action_parser = super(ToxTestAction, cls).make_parser(subparser)
action_parser.epilog = "Notice the use of '--' to delimit andebox's options from tox's"
action_parser.usage = "%(prog)s usage: andebox tox-test [-h] [--env ENV] -- [ansible_test_params ...]"
action_parser.usage = "%(prog)s [-h] [--env ENV] -- [ansible_test_params ...]"

def run(self, args):
if not os.path.exists(self.tox_ini_filename):
Expand Down
2 changes: 1 addition & 1 deletion andeboxlib/actions/vagrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class VagrantAction(AndeboxAction):
def make_parser(cls, subparser):
action_parser = super(VagrantAction, cls).make_parser(subparser)
action_parser.epilog = "Notice the use of '--' to delimit the vagrant command from the one running inside the VM"
action_parser.usage = "%(prog)s usage: andebox vagrant [-h] [-n name] -- <andebox-cmd> [andebox-cmd-opts [-- test-params]]"
action_parser.usage = "%(prog)s [-hsd] [-n name] [-V VENV] -- <andebox-cmd> [andebox-cmd-opts [-- test-params]]"

def run(self, args):
import vagrant # pylint: disable=import-outside-toplevel
Expand Down
3 changes: 2 additions & 1 deletion andeboxlib/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
from .actions.runtime import RuntimeAction
from .actions.toxtest import ToxTestAction
from .actions.vagrant import VagrantAction
from .actions.docsite import DocsiteAction


_actions = [AnsibleTestAction, IgnoreLinesAction, RuntimeAction, ToxTestAction, VagrantAction]
_actions = [AnsibleTestAction, IgnoreLinesAction, RuntimeAction, ToxTestAction, VagrantAction, DocsiteAction]


def _make_parser():
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ansible-core
antsibull-docs
pyyaml>=5.4
python-vagrant
fabric
Expand Down
Loading