Skip to content

Commit

Permalink
Use ruamel.yaml to construct yaml config files.
Browse files Browse the repository at this point in the history
  • Loading branch information
domdfcoding committed Oct 24, 2020
1 parent 660007e commit 80b5f90
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 95 deletions.
67 changes: 38 additions & 29 deletions repo_helper/files/bots.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,45 +70,54 @@ def make_auto_assign_action(repo_path: pathlib.Path, templates: jinja2.Environme
dot_github = PathPlus(repo_path / ".github")
(dot_github / "workflows").maybe_make(parents=True)

if (dot_github / "workflow" / "assign.yml").is_file():
(dot_github / "workflow" / "assign.yml").unlink()
assign_workflow = dot_github / "workflows" / "assign.yml"
old_assign_workflow = dot_github / "workflow" / "assign.yml"
auto_assign_yml = dot_github / "auto_assign.yml"

if (dot_github / "workflow").is_dir():
(dot_github / "workflow").rmdir()
if old_assign_workflow.is_file():
old_assign_workflow.unlink()

if (dot_github / "workflows" / "assign.yml").is_file():
(dot_github / "workflows" / "assign.yml").unlink()
if old_assign_workflow.parent.is_dir():
old_assign_workflow.parent.rmdir()

(dot_github / "auto_assign.yml").write_clean(
f"""\
# {templates.globals['managed_message']}
---
if assign_workflow.is_file():
assign_workflow.unlink()

addReviewers: true
addAssignees: true
config: MutableMapping[str, Any] = {
"addReviewers": True,
"addAssignees": True,
}

# A list of reviewers to be added to pull requests (GitHub user name)
reviewers:
- {templates.globals['username']}
# A list of reviewers to be added to pull requests (GitHub user name)
config["reviewers"] = [templates.globals["username"]]

# A number of reviewers added to the pull request
# Set 0 to add all the reviewers
numberOfReviewers: 0
# A number of reviewers added to the pull request
# Set 0 to add all the reviewers
config["numberOfReviewers"] = 0

# A list of assignees, overrides reviewers if set
# assignees:
# - assigneeA
# A list of assignees, overrides reviewers if set
# assignees:
# - assigneeA

# A number of assignees to add to the pull request
# Set to 0 to add all of the assignees.
# Uses numberOfReviewers if unset.
# numberOfAssignees: 2
# A number of assignees to add to the pull request
# Set to 0 to add assignees.
# Uses numberOfReviewers if unset.
# numberOfAssignees: 2

# more settings at https://github.com/marketplace/actions/auto-assign-action
"""
)
# more settings at https://github.com/marketplace/actions/auto-assign-action

auto_assign_yml.write_lines([
f"# {templates.globals['managed_message']}",
"---",
yaml.round_trip_dump(config, default_flow_style=False), # type: ignore
"# more settings at https://github.com/marketplace/actions/auto-assign-action",
])

return [".github/workflows/assign.yml", ".github/workflow/assign.yml", ".github/auto_assign.yml"]
return [
assign_workflow.relative_to(repo_path).as_posix(),
old_assign_workflow.relative_to(repo_path).as_posix(),
auto_assign_yml.relative_to(repo_path).as_posix(),
]


@management.register("dependabot")
Expand Down
66 changes: 37 additions & 29 deletions repo_helper/files/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,35 +177,43 @@ def make_rtfd(repo_path: pathlib.Path, templates: jinja2.Environment) -> List[st
:param templates:
"""

with (repo_path / ".readthedocs.yml").open('w', encoding="UTF-8") as fp:
clean_writer(
f"""\
# {templates.globals['managed_message']}
# Read the Docs configuration file
---
# Required
version: 2
sphinx:
builder: html
configuration: {templates.globals["docs_dir"]}/conf.py
# Optionally build your docs in additional formats such as PDF and ePub
formats: all
python:
version: 3.8
install:
- requirements: requirements.txt
- requirements: {templates.globals["docs_dir"]}/requirements.txt
""",
fp
)
for file in templates.globals["additional_requirements_files"]:
clean_writer(f" - requirements: { file }", fp)

return [".readthedocs.yml"]
file = PathPlus(repo_path / ".readthedocs.yml")
file.parent.maybe_make()

sphinx_config = {
"builder": "html",
"configuration": f"{templates.globals['docs_dir']}/conf.py",
}

install_requirements = [
"requirements.txt",
f"{templates.globals['docs_dir']}/requirements.txt",
*templates.globals["additional_requirements_files"],
]

python_config = {
"version": 3.8,
"install": [{"requirements": r} for r in install_requirements]
}

# Formats: Optionally build your docs in additional formats such as PDF and ePub
config = {"version": 2, "sphinx": sphinx_config, "formats": "all", "python": python_config}

class Dumper(yaml.RoundTripDumper):
@functools.wraps(yaml.RoundTripDumper.__init__)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.sequence_dash_offset = 1
self.best_width = 4086

file.write_lines([
f"# {templates.globals['managed_message']}",
"# Read the Docs configuration file",
"---",
yaml.round_trip_dump(config, default_flow_style=False, Dumper=Dumper), # type: ignore
])

return [file.relative_to(repo_path).as_posix()]


@management.register("docutils_conf", ["enable_docs"])
Expand Down
17 changes: 1 addition & 16 deletions tests/test_files/test_bots/test_auto_assign_action.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
# This file is managed by 'repo_helper'. Don't edit it directly.
---

addReviewers: true
addAssignees: true

# A list of reviewers to be added to pull requests (GitHub user name)
reviewers:
- octocat

# A number of reviewers added to the pull request
# Set 0 to add all the reviewers
- octocat
numberOfReviewers: 0

# A list of assignees, overrides reviewers if set
# assignees:
# - assigneeA

# A number of assignees to add to the pull request
# Set to 0 to add all of the assignees.
# Uses numberOfReviewers if unset.
# numberOfAssignees: 2

# more settings at https://github.com/marketplace/actions/auto-assign-action
24 changes: 20 additions & 4 deletions tests/test_files/test_ci_cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ def test_travis_deploy_conda(tmp_pathplus, demo_environment, file_regression: Fi

def test_github_ci_case_1(tmp_pathplus, demo_environment, file_regression: FileRegressionFixture):
managed_files = make_github_ci(tmp_pathplus, demo_environment)
assert managed_files == [".github/workflows/python_ci.yml", ".github/workflows/python_ci_macos.yml"]
assert managed_files == [
".github/workflows/python_ci.yml",
".github/workflows/python_ci_macos.yml",
".github/workflows/python_ci_linux.yml",
]
assert (tmp_pathplus / managed_files[0]).is_file()
assert not (tmp_pathplus / managed_files[1]).is_file()
check_file_output(tmp_pathplus / managed_files[0], file_regression)
Expand All @@ -116,7 +120,11 @@ def test_github_ci_case_2(tmp_pathplus, demo_environment, file_regression: FileR
)

managed_files = make_github_ci(tmp_pathplus, demo_environment)
assert managed_files == [".github/workflows/python_ci.yml", ".github/workflows/python_ci_macos.yml"]
assert managed_files == [
".github/workflows/python_ci.yml",
".github/workflows/python_ci_macos.yml",
".github/workflows/python_ci_linux.yml",
]
assert not (tmp_pathplus / managed_files[0]).is_file()
assert (tmp_pathplus / managed_files[1]).is_file()
check_file_output(tmp_pathplus / managed_files[1], file_regression)
Expand Down Expand Up @@ -144,7 +152,11 @@ def test_github_ci_case_3(tmp_pathplus, demo_environment):
demo_environment.globals.update(dict(platforms=["Windows", "macOS"], ))

managed_files = make_github_ci(tmp_pathplus, demo_environment)
assert managed_files == [".github/workflows/python_ci.yml", ".github/workflows/python_ci_macos.yml"]
assert managed_files == [
".github/workflows/python_ci.yml",
".github/workflows/python_ci_macos.yml",
".github/workflows/python_ci_linux.yml",
]
assert (tmp_pathplus / managed_files[0]).is_file()
assert (tmp_pathplus / managed_files[1]).is_file()

Expand All @@ -155,7 +167,11 @@ def test_github_ci_case_3(tmp_pathplus, demo_environment):
assert (tmp_pathplus / managed_files[1]).is_file()

managed_files = make_github_ci(tmp_pathplus, demo_environment)
assert managed_files == [".github/workflows/python_ci.yml", ".github/workflows/python_ci_macos.yml"]
assert managed_files == [
".github/workflows/python_ci.yml",
".github/workflows/python_ci_macos.yml",
".github/workflows/python_ci_linux.yml",
]

assert not (tmp_pathplus / managed_files[0]).is_file()
assert not (tmp_pathplus / managed_files[1]).is_file()
Expand Down
10 changes: 2 additions & 8 deletions tests/test_files/test_docs/test_make_rtfd_case_1.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
# This file is managed by 'repo_helper'. Don't edit it directly.
# Read the Docs configuration file
---

# Required
version: 2

sphinx:
builder: html
configuration: doc-source/conf.py

# Optionally build your docs in additional formats such as PDF and ePub
formats: all

python:
version: 3.8
install:
- requirements: requirements.txt
- requirements: doc-source/requirements.txt
- requirements: requirements.txt
- requirements: doc-source/requirements.txt
12 changes: 3 additions & 9 deletions tests/test_files/test_docs/test_make_rtfd_case_2.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
# This file is managed by 'repo_helper'. Don't edit it directly.
# Read the Docs configuration file
---

# Required
version: 2

sphinx:
builder: html
configuration: userguide/conf.py

# Optionally build your docs in additional formats such as PDF and ePub
formats: all

python:
version: 3.8
install:
- requirements: requirements.txt
- requirements: userguide/requirements.txt
- requirements: hello_world/submodule/requirements.txt
- requirements: requirements.txt
- requirements: userguide/requirements.txt
- requirements: hello_world/submodule/requirements.txt

0 comments on commit 80b5f90

Please sign in to comment.