tasks.py 3.8 KB

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