tasks.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from pathlib import Path
  2. from invoke import task
  3. from jinja2 import Template
  4. @task
  5. def lint(c):
  6. """"""
  7. system = "damper"
  8. c.run(f"python3 -m black {system}")
  9. c.run(f"python3 -m pylint {system}")
  10. @task(name="docs", aliases=("html", "documentation"))
  11. def docs_html(c, output_directory="build/html"):
  12. """Build the documentation in HTML form."""
  13. c.run(f"cp damper/system.drawio docs/system.drawio")
  14. c.run(f"python3 -m sphinx docs {output_directory}")
  15. if output_directory == "docs/public":
  16. c.run(f"cp -r docs/public/* ./public")
  17. @task
  18. def clean(c):
  19. """Remove all artefacts."""
  20. patterns = ["build", "docs/build"]
  21. for pattern in patterns:
  22. c.run(f"rm -rf {pattern}")
  23. @task
  24. def test(c):
  25. """Run all tests under the tests directory."""
  26. c.run("python3 -m unittest discover tests 'test_*' -v")
  27. @task
  28. def coverage(c):
  29. """Run coverage from the tests directory."""
  30. c.run("coverage run --source . -m unittest discover tests 'test_*' -v")
  31. c.run("coverage html")
  32. @task
  33. def minimum(c):
  34. """Check the minimum required python version for the project."""
  35. c.run("vermin --no-parse-comments .")
  36. @task(name="migrate")
  37. def migrate_requirements(c):
  38. """Copy requirements from the requirements.txt file to pyproject.toml."""
  39. lines = Path("requirements.txt").read_text().split("\n")
  40. current = "damper"
  41. requirements = {current: [], "test": [], "doc": [], "graphical": [], "dev": []}
  42. for line in lines:
  43. if line.startswith("#"):
  44. candidate = line[1:].lower().strip().replace(" ", "_")
  45. if candidate in requirements.keys():
  46. current = candidate
  47. continue
  48. if line.strip() == "" or ("=" in line and "#" in line):
  49. continue
  50. requirements[current].append("".join(line.split()))
  51. template = Template(Path("docs/templates/pyproject.toml").read_text())
  52. Path("pyproject.toml").write_text(template.render(requirements=requirements))
  53. @task
  54. def release(c, version):
  55. """"""
  56. if version not in ["minor", "major", "patch"]:
  57. print("Version can be either major, minor or patch.")
  58. return
  59. from damper import __version_info__, __version__
  60. _major, _minor, _patch = __version_info__
  61. if version == "patch":
  62. _patch = _patch + 1
  63. elif version == "minor":
  64. _minor = _minor + 1
  65. _patch = 0
  66. elif version == "major":
  67. _major = _major + 1
  68. _minor = 0
  69. _patch = 0
  70. c.run(f"git checkout -b release-{_major}.{_minor}.{_patch} dev")
  71. c.run(f"sed -i 's/{__version__}/{_major}.{_minor}.{_patch}/g' damper/__init__.py")
  72. print(f"Update the readme for version {_major}.{_minor}.{_patch}.")
  73. input("Press enter when ready.")
  74. c.run(f"git add -u")
  75. c.run(f'git commit -m "Update changelog version {_major}.{_minor}.{_patch}"')
  76. c.run(f"git push --set-upstream origin release-{_major}.{_minor}.{_patch}")
  77. c.run(f"git checkout main")
  78. c.run(f"git merge --no-ff release-{_major}.{_minor}.{_patch}")
  79. c.run(f'git tag -a {_major}.{_minor}.{_patch} -m "Release {_major}.{_minor}.{_patch}"')
  80. c.run(f"git push")
  81. c.run(f"git checkout dev")
  82. c.run(f"git merge --no-ff release-{_major}.{_minor}.{_patch}")
  83. c.run(f"git push")
  84. c.run(f"git branch -d release-{_major}.{_minor}.{_patch}")
  85. c.run(f"git push origin --tags")