tasks.py 3.6 KB

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