22
33import logging
44import requests
5+ import subprocess
56from pathlib import Path
67
78URL_SIMULATORS = "https://raw.githubusercontent.com/OPM/opm-simulators/master/python/docstrings_simulators.json"
89URL_COMMON = "https://raw.githubusercontent.com/OPM/opm-common/master/python/docstrings_common.json"
910URL_DUNE_MODULE = "https://raw.githubusercontent.com/OPM/opm-simulators/master/dune.module"
1011
11- def get_script_dir ():
12- """Return the directory of the script."""
13- script_path = Path (__file__ ).resolve ()
14- script_dir = script_path .parent
15- return script_dir
12+ def get_git_root () -> Path :
13+ """Return the absolute path of the opm-python-documentation repository's root."""
14+ try :
15+ output = subprocess .check_output (
16+ ["git" , "rev-parse" , "--show-toplevel" ],
17+ stderr = subprocess .STDOUT
18+ )
19+ except subprocess .CalledProcessError :
20+ # Handle the error if we're not inside a Git repo, etc.
21+ raise RuntimeError ("Not a valid Git repository or other error occurred." )
22+ # Check that the parent directory is the opm-python-documentation repository
23+ root = output .decode ("utf-8" ).strip ()
24+ if not root .endswith ("opm-python-documentation" ):
25+ raise RuntimeError ("Not in the opm-python-documentation repository." )
26+ return Path (root )
1627
1728def convert_pr_to_commit_hash (repo : str , pr_number : str ) -> str :
1829 """Convert a PR number to a commit hash."""
@@ -24,7 +35,6 @@ def convert_pr_to_commit_hash(repo: str, pr_number: str) -> str:
2435
2536def download_docstring_file (url : str ) -> None :
2637 """Download a docstrings file from a URL (either opm-simulators or opm-common)."""
27- # Ask command line user question if to use master or PR branch
2838 if "opm-simulators" in url :
2939 repo = "opm-simulators"
3040 filename = "docstrings_simulators.json"
@@ -44,9 +54,9 @@ def download_docstring_file(url: str) -> None:
4454 logging .info (f"Downloading docstrings file from { url } " )
4555 response = requests .get (url )
4656 response .raise_for_status () # Raises 404 if the file is not found
47- script_dir = get_script_dir ()
48- save_path = script_dir . parent / "python" / filename
49- with open (save_path , "wb" ) as file :
57+ git_root_dir = get_git_root ()
58+ save_path = git_root_dir / "python" / filename
59+ with open (str ( save_path ) , "wb" ) as file :
5060 file .write (response .content )
5161 logging .info (f"Saved docstrings file to { save_path } " )
5262
@@ -55,17 +65,17 @@ def download_dune_module() -> None:
5565 logging .info ("Downloading dune.module file" )
5666 response = requests .get (URL_DUNE_MODULE )
5767 response .raise_for_status ()
58- script_dir = get_script_dir ()
59- save_path = script_dir . parent / "dune.module"
68+ git_root_dir = get_git_root ()
69+ save_path = git_root_dir / "dune.module"
6070 with open (save_path , "wb" ) as file :
6171 file .write (response .content )
6272 logging .info (f"Saved dune.module file to { save_path } " )
6373
6474def main () -> None :
75+ logging .basicConfig (level = logging .INFO )
6576 download_docstring_file (URL_SIMULATORS )
6677 download_docstring_file (URL_COMMON )
6778 download_dune_module ()
6879
6980if __name__ == '__main__' :
70- logging .basicConfig (level = logging .INFO )
7181 main ()
0 commit comments