|
| 1 | +""" |
| 2 | +Sometimes unconventional solutions are necessary to tackle unconventional situations. |
| 3 | +We inherit from the wheel package to directly package the application into it, |
| 4 | +avoiding the need to invent custom formats. |
| 5 | +This allows for efficient utilization of a unified parser supported by Python itself. |
| 6 | +
|
| 7 | +Originally hosted on PyPI, but unfortunately removed for unknown reasons. |
| 8 | +Thus, the name "fext-cli" is taken, ensuring no conflicts. |
| 9 | +Currently, GitHub Releases are used to store the compiled application in a wheel package. |
| 10 | +
|
| 11 | +For initial installation, the script from "github.com/fextpkg/get" is used. |
| 12 | +Self-update functionality is expected soon. |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | +from os.path import basename |
| 17 | + |
| 18 | +from setuptools import setup |
| 19 | +from setuptools.command.install import install |
| 20 | + |
| 21 | + |
| 22 | +def retrieve_env_variable(key: str) -> str: |
| 23 | + """ |
| 24 | + Retrieves value from the environment variable. |
| 25 | +
|
| 26 | + :raise RuntimeError: If it's not set. |
| 27 | + """ |
| 28 | + if not (v := os.environ.get(key)): |
| 29 | + raise RuntimeError(f"Environment variable {key} is not specified") |
| 30 | + |
| 31 | + return v |
| 32 | + |
| 33 | + |
| 34 | +class Env: |
| 35 | + # Environment variables names. |
| 36 | + # Package version. |
| 37 | + VERSION: str = retrieve_env_variable("FEXT_VERSION") |
| 38 | + # Path to executable file. |
| 39 | + EXE_FILE: str = retrieve_env_variable("FEXT_EXE_FILE") |
| 40 | + |
| 41 | + |
| 42 | +class OverrideCommand(install): |
| 43 | + """ |
| 44 | + Built-in setuptools commands don't support straightforward addition of binary files. |
| 45 | + More precisely, they **don't allow** adding them to scripts. |
| 46 | + We understand Python's stance on this matter, |
| 47 | + but we want to **avoid impacting** Python in any way because its execution consumes many resources. |
| 48 | +
|
| 49 | + To address this issue, we modified this command to create an empty-package |
| 50 | + containing only metadata and a scripts directory with the binary file itself. |
| 51 | +
|
| 52 | + Unfortunately, no builder can be configured as flexibly as setuptools itself. |
| 53 | + Consequently, none can support such commands without workarounds. |
| 54 | + It's not the best solution, but at least it's easy to maintain. |
| 55 | +
|
| 56 | + Yes, direct invocation of ``setup.py`` is deprecated, but there's currently **no alternative**. |
| 57 | + """ |
| 58 | + |
| 59 | + # Working directory. |
| 60 | + source_dir: str = os.path.dirname(os.path.realpath(__file__)) |
| 61 | + |
| 62 | + def run(self): |
| 63 | + """ |
| 64 | + The magical installation command that creates a bit of mess inside the package. |
| 65 | + """ |
| 66 | + # As a precaution, run the original command just in case. |
| 67 | + super().run() |
| 68 | + |
| 69 | + # If the directory hasn't been created yet, create it. |
| 70 | + if not os.path.isdir(self.install_scripts): |
| 71 | + os.makedirs(self.install_scripts) |
| 72 | + |
| 73 | + # Specify both the external and internal paths to the executable file. |
| 74 | + source: str = os.path.join(self.source_dir, Env.EXE_FILE) |
| 75 | + target: str = os.path.join(self.install_scripts, basename(source)) |
| 76 | + |
| 77 | + # If it happens that it already exists, remove it to avoid errors. |
| 78 | + if os.path.isfile(target): |
| 79 | + os.remove(target) |
| 80 | + |
| 81 | + # And perform a dirty trick. |
| 82 | + self.copy_file(source, target) |
| 83 | + |
| 84 | + |
| 85 | +class Builder: |
| 86 | + def __init__(self) -> None: |
| 87 | + # Prepare data |
| 88 | + self.description, self.description_type = self.get_description() |
| 89 | + |
| 90 | + def setup(self) -> None: |
| 91 | + """ |
| 92 | + Builds the package using ``setuptools``. |
| 93 | + """ |
| 94 | + setup( |
| 95 | + # General information. |
| 96 | + name="fext-cli", |
| 97 | + version=Env.VERSION, |
| 98 | + description="Fast & Modern package manager", |
| 99 | + long_description=self.description, |
| 100 | + long_description_content_type=self.description_type, |
| 101 | + license="MIT", |
| 102 | + author="Andrew Krylov", |
| 103 | + author_email="any@lunte.dev", |
| 104 | + url="https://github.com/fextpkg/cli", |
| 105 | + keywords=["fast", "modern", "package", "manager"], |
| 106 | + # Ignore errors related to empty package |
| 107 | + # while simultaneously optimizing the package size. |
| 108 | + packages=[], |
| 109 | + # Leverage the ability to store external files. |
| 110 | + include_package_data=True, |
| 111 | + # The magic lies in overriding the installation command. |
| 112 | + cmdclass={"install": OverrideCommand}, |
| 113 | + # Don't generate in ".egg" format. |
| 114 | + zip_safe=False, |
| 115 | + ) |
| 116 | + |
| 117 | + @staticmethod |
| 118 | + def get_description() -> tuple[str, str]: |
| 119 | + """ |
| 120 | + Retrieves the text and type of README file. |
| 121 | + """ |
| 122 | + with open("README.md", "r", encoding="utf-8") as f: |
| 123 | + return f.read(), "text/markdown" |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + Builder().setup() |
0 commit comments