Skip to content

Commit

Permalink
build: Reduce need for getting addons version
Browse files Browse the repository at this point in the history
The corresponding addons branch, i.e., version is determined from git ls-remote which needs to go online. This limits when it is used by limiting the scope of the variables just to where they are needed. Additionally, it caches the branch (version), so the wrapper function can be used more freely on multiple places. It refactors some of the relevant common code between HTML and Markdown builds.
  • Loading branch information
wenzeslaus committed Feb 20, 2025
1 parent 5f4fa06 commit c5d581c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 41 deletions.
31 changes: 30 additions & 1 deletion utils/mkdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,25 @@ def read_file(name):
return ""


_cached_version_branch = None

def get_version_branch(major_version, addons_git_repo_url):

Check failure on line 42 in utils/mkdocs.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (E302)

utils/mkdocs.py:42:1: E302 Expected 2 blank lines, found 1
"""Check if version branch for the current GRASS version exists,
if not, take branch for the previous version
For the official repo we assume that at least one version branch is present

Check failure on line 46 in utils/mkdocs.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (W293)

utils/mkdocs.py:46:1: W293 Blank line contains whitespace
Getting the latest version is expensive because we need to ask online for the latest
branch. Hence we cache the version branch in a global variable.
:param major_version int: GRASS GIS major version
:param addons_git_repo_url str: Addons Git ropository URL
:param addons_git_repo_url str: Addons Git repository URL
:return version_branch str: version branch
"""
global _cached_version_branch
if _cached_version_branch is not None:
return _cached_version_branch

version_branch = f"grass{major_version}"
if gs:
branch = gs.Popen(
Expand All @@ -73,9 +82,29 @@ def get_version_branch(major_version, addons_git_repo_url):
)
if version_branch not in gs.decode(branch):
version_branch = "grass{}".format(int(major_version) - 1)

_cached_version_branch = version_branch
return version_branch


def get_addons_url():
"""Function to get the addons URL

Check failure on line 92 in utils/mkdocs.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (W293)

utils/mkdocs.py:92:1: W293 Blank line contains whitespace
Getting the latest URL is expensive because we need to ask online for the latest
version.
"""
return urlparse.urljoin(
base_url,
urlparse.urljoin(
"grass-addons/tree/",
get_version_branch(
major,
urlparse.urljoin(base_url, "grass-addons/"),
),
),
)


def has_src_code_git(src_dir):
"""Has core module or addon source code Git
Expand Down
31 changes: 11 additions & 20 deletions utils/mkhtml.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

from mkdocs import (
read_file,
get_version_branch,
get_addons_url,
get_last_git_commit,
top_dir as topdir,
get_addon_path,
Expand All @@ -57,16 +57,6 @@
grass_git_branch + "/",
),
)
addons_url = urlparse.urljoin(
base_url,
urlparse.urljoin(
"grass-addons/tree/",
get_version_branch(
major,
urlparse.urljoin(base_url, "grass-addons/"),
),
),
)


def _get_encoding():
Expand Down Expand Up @@ -469,15 +459,8 @@ def to_title(name):
if not year:
year = str(datetime.now().year)

# check the names of scripts to assign the right folder
curdir = os.path.abspath(os.path.curdir)
if curdir.startswith(topdir + os.path.sep):
source_url = trunk_url
pgmdir = curdir.replace(topdir, "").lstrip(os.path.sep)
else:
# addons
source_url = addons_url
pgmdir = os.path.sep.join(curdir.split(os.path.sep)[-3:])

url_source = ""
addon_path = None
if os.getenv("SOURCE_URL", ""):
Expand All @@ -486,7 +469,7 @@ def to_title(name):
# Addon is installed from the local dir
if os.path.exists(os.getenv("SOURCE_URL")):
url_source = urlparse.urljoin(
addons_url,
get_addons_url(),
addon_path,
)
else:
Expand All @@ -495,6 +478,14 @@ def to_title(name):
addon_path,
)
else:
# check the names of scripts to assign the right folder
if curdir.startswith(topdir + os.path.sep):
source_url = trunk_url
pgmdir = curdir.replace(topdir, "").lstrip(os.path.sep)
else:
# addons
source_url = get_addons_url()
pgmdir = os.path.sep.join(curdir.split(os.path.sep)[-3:])
url_source = urlparse.urljoin(source_url, pgmdir)
if sys.platform == "win32":
url_source = url_source.replace(os.path.sep, "/")
Expand Down
29 changes: 9 additions & 20 deletions utils/mkmarkdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@

from mkdocs import (
read_file,
get_version_branch,
get_addons_url,
get_last_git_commit,
top_dir,
get_addon_path,
)


def parse_source(pgm):

Check failure on line 38 in utils/mkmarkdown.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (E302)

utils/mkmarkdown.py:38:1: E302 Expected 2 blank lines, found 1
"""Parse source code to get source code and log message URLs,
and date time of the last modification.
Expand All @@ -59,25 +58,8 @@ def parse_source(pgm):
grass_git_branch + "/",
),
)
addons_url = urlparse.urljoin(
base_url,
urlparse.urljoin(
"grass-addons/tree/",
get_version_branch(
major,
urlparse.urljoin(base_url, "grass-addons/"),
),
),
)

cur_dir = os.path.abspath(os.path.curdir)
if cur_dir.startswith(top_dir + os.path.sep):
source_url = main_url
pgmdir = cur_dir.replace(top_dir, "").lstrip(os.path.sep)
else:
# addons
source_url = addons_url
pgmdir = os.path.sep.join(cur_dir.split(os.path.sep)[-3:])

url_source = ""
addon_path = None
Expand All @@ -87,7 +69,7 @@ def parse_source(pgm):
# Addon is installed from the local dir
if os.path.exists(os.getenv("SOURCE_URL")):
url_source = urlparse.urljoin(
addons_url,
get_addons_url(),
addon_path,
)
else:
Expand All @@ -96,6 +78,13 @@ def parse_source(pgm):
addon_path,
)
else:
if cur_dir.startswith(top_dir + os.path.sep):
source_url = main_url
pgmdir = cur_dir.replace(top_dir, "").lstrip(os.path.sep)
else:
# addons
source_url = get_addons_url()
pgmdir = os.path.sep.join(cur_dir.split(os.path.sep)[-3:])
url_source = urlparse.urljoin(source_url, pgmdir)
if sys.platform == "win32":
url_source = url_source.replace(os.path.sep, "/")
Expand Down

0 comments on commit c5d581c

Please sign in to comment.