/ dodo.py
dodo.py
  1  # SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-or-later OR CERN-OHL-S-2.0+ OR Apache-2.0
  2  import os
  3  from pathlib import Path
  4  
  5  from doit import get_var
  6  from doit.tools import create_folder
  7  
  8  ### config
  9  
 10  
 11  DOIT_CONFIG = {
 12      "default_tasks": ["docs", "unittest"],
 13  }
 14  
 15  
 16  ### support functions
 17  
 18  
 19  def get_var_env(name, default=None):
 20      """Uses get_var to get a command line variable, also checks
 21      environment variables for default value
 22  
 23      If os.environ[name.upper()] exists that value will override the
 24      default value given.
 25      """
 26      try:
 27          default = os.environ[name.upper()]
 28      except:
 29          # Keep the specified default
 30          pass
 31      return get_var(name, default=default)
 32  
 33  
 34  ### globals
 35  
 36  
 37  top_dir = Path(__file__).parent
 38  docs_dir = top_dir.joinpath("docs")
 39  
 40  pdkmaster_dir = top_dir.joinpath("pdkmaster")
 41  test_dir = top_dir.joinpath("test")
 42  dist_dir = top_dir.joinpath("dist")
 43  
 44  pip = get_var_env("pip", default="pip3")
 45  python = get_var_env("python", default="python3")
 46  sphinx_build = get_var_env("sphinx_build", default="sphinx-build")
 47  coverage = get_var_env("coverage", default="coverage")
 48  
 49  ### main tasks
 50  
 51  
 52  pdkmaster_py_files = tuple(pdkmaster_dir.rglob("*.py"))
 53  
 54  
 55  #
 56  # docs
 57  def task_docs():
 58      """Create the documentation with Sphinx"""
 59  
 60      docs_src_dir = docs_dir.joinpath("src")
 61      docs_html_dir = docs_dir.joinpath("html")
 62  
 63      docs_rst_files = tuple(docs_src_dir.rglob("*.rst"))
 64      docs_html_files = tuple(
 65          docs_html_dir.joinpath(f.name.replace(".rst", ".html"))
 66          for f in docs_rst_files
 67      )
 68      docs_html_dir = docs_dir.joinpath("html")
 69  
 70      return {
 71          "title": lambda _: "Creating the documentation",
 72          "file_dep": (
 73              *pdkmaster_py_files,
 74              *docs_rst_files,
 75              docs_src_dir.joinpath("conf.py"),
 76          ),
 77          "targets": docs_html_files,
 78          "actions": (
 79              (create_folder, (docs_html_dir,)),
 80              f"{sphinx_build} -b html {docs_src_dir} {docs_html_dir}",
 81          )
 82      }
 83  
 84  #
 85  # test
 86  test_py_files = tuple(test_dir.rglob("*.py"))
 87  test_report_file = test_dir.joinpath("cover_report.log")
 88  def task_unittest():
 89      """Run unittests with and coverage"""
 90  
 91      def check_100pct():
 92          with test_report_file.open() as f:
 93              for line in f:
 94                  if line.startswith("TOTAL"):
 95                      assert line.split()[-1] == "100%", "Code not fully covered"
 96                      break
 97              else:
 98                  raise Exception("TOTAL not found in report file")
 99  
100      return {
101          "title": lambda _: "Running unittests and coverage",
102          "file_dep": (*pdkmaster_py_files, *test_py_files),
103          "targets": (test_report_file,),
104          "actions": (
105              f"{coverage} run --include 'pdkmaster/*' -m unittest discover -s test -p '*.py' 2>&1",
106              f"{coverage} report -m 2>&1 | tee {test_report_file}",
107              check_100pct,
108          )
109      }