Skip to content

Commit

Permalink
🖋️ Drop Hugo version file and writing to it
Browse files Browse the repository at this point in the history
  • Loading branch information
agriyakhetarpal committed Jan 25, 2025
1 parent c6152c0 commit 5a0139c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,3 @@ hugo/binaries/*

# Hugo builder cache
hugo_cache/

# Hugo version file
hugo/VERSION
87 changes: 43 additions & 44 deletions hugo/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,67 @@

from __future__ import annotations

# Reduce expenses for various imports
from functools import lru_cache
from os import execv, path
from platform import machine
from subprocess import check_call
from sys import argv, maxsize
import os
from pathlib import Path
from sys import platform as sysplatform

with open(path.join(path.dirname(__file__), "VERSION")) as f: # noqa: PTH123, PTH120, PTH118
HUGO_VERSION = f.read().strip()

HUGO_VERSION = "0.135.0"
FILE_EXT = ".exe" if sysplatform == "win32" else ""
HUGO_PLATFORM = {"darwin": "darwin", "linux": "linux", "win32": "windows"}[sysplatform]

HUGO_PLATFORM = {
"darwin": "darwin",
"linux": "linux",
"win32": "windows",
}[sysplatform]

HUGO_ARCH = {
"x86_64": "amd64",
"arm64": "arm64",
"AMD64": "amd64",
"aarch64": "arm64",
"x86": "386",
"s390x": "s390x",
"ppc64le": "ppc64le",
}[machine()]
def get_hugo_arch():
from platform import machine
from sys import maxsize as sysmaxsize

# platform.machine returns AMD64 on Windows because the architecture is
# 64-bit (even if one is running a 32-bit Python interpreter). Therefore
# we use sys. maxsize to handle this special case on Windows
HUGO_ARCH = {
"x86_64": "amd64",
"arm64": "arm64",
"AMD64": "amd64",
"aarch64": "arm64",
"x86": "386",
"s390x": "s390x",
"ppc64le": "ppc64le",
}[machine()]

if not (maxsize > 2**32) and sysplatform == "win32":
HUGO_ARCH = "386"
# platform.machine returns AMD64 on Windows because the architecture is
# 64-bit (even if one is running a 32-bit Python interpreter). Therefore
# we use sys. maxsize to handle this special case on Windows

if not (sysmaxsize > 2**32) and sysplatform == "win32":
HUGO_ARCH = "386"

@lru_cache(maxsize=1)
def hugo_executable():
"""
Returns the path to the Hugo executable.
"""
return path.join( # noqa: PTH118
path.dirname(__file__), # noqa: PTH120
"binaries",
f"hugo-{HUGO_VERSION}-{HUGO_PLATFORM}-{HUGO_ARCH}" + FILE_EXT,
)
return HUGO_ARCH


HUGO_ARCH = get_hugo_arch()

DIR = Path(__file__).parent.resolve()

HUGO_EXECUTABLE = Path(
DIR / "binaries" / f"hugo-{HUGO_VERSION}-{HUGO_PLATFORM}-{HUGO_ARCH}{FILE_EXT}",
).resolve()

MESSAGE = (
f"Running Hugo {HUGO_VERSION} via hugo-python-distributions at {hugo_executable()}"
f"Running Hugo {HUGO_VERSION} via hugo-python-distributions at {HUGO_EXECUTABLE}"
)


def __call():
"""
Hugo binary entry point. Passes all command-line arguments to Hugo.
"""
print(f"\033[95m{MESSAGE}\033[0m")

from sys import argv as sysargv

if sysplatform == "win32":
# execvp broken on Windows, use subprocess instead to not launch a new shell
print(f"\033[95m{MESSAGE}\033[0m")
check_call([hugo_executable(), *argv[1:]])
from subprocess import check_call

check_call([HUGO_EXECUTABLE] + sysargv[1:])
else:
print(f"\033[95m{MESSAGE}\033[0m")
execv(hugo_executable(), ["hugo", *argv[1:]])
os.execv(HUGO_EXECUTABLE, ["hugo"] + sysargv[1:])


if __name__ == "__main__":
__call()
14 changes: 9 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import platform
import re
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -53,7 +54,7 @@
class HugoWriter(build_py):
"""
A custom pre-installation command that writes the version of Hugo being built
to hugo/VERSION so that the version is available to read at runtime.
directly to hugo/cli.py so that the version is available at runtime.
"""

def initialize_options(self) -> None:
Expand All @@ -63,10 +64,13 @@ def finalize_options(self) -> None:
return super().finalize_options()

def run(self) -> None:
"""Write the version of Hugo being built to hugo/VERSION."""
with open("hugo/VERSION", "w") as version_file: # noqa: PTH123
version_file.write(HUGO_VERSION)
version_file.write("\n")
cli_file_path = Path(__file__).parent / "hugo" / "cli.py"
content = cli_file_path.read_text()
version = re.sub(
r'HUGO_VERSION = "[0-9.]+"', f'HUGO_VERSION = "{HUGO_VERSION}"', content
)
with open(cli_file_path, "w") as cli_file: # noqa: PTH123
cli_file.write(version)

super().run()

Expand Down

0 comments on commit 5a0139c

Please sign in to comment.