tasks.py 3.2 KB

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