tasks.py 2.8 KB

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