tasks.py 3.6 KB

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