Skip to content

Commit db18240

Browse files
committed
Initial commit.
0 parents  commit db18240

14 files changed

Lines changed: 1396 additions & 0 deletions
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Build and Test
2+
3+
on:
4+
push:
5+
schedule:
6+
- cron: "0 9 * * SUN"
7+
workflow_dispatch:
8+
9+
jobs:
10+
build:
11+
name: Build
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
os: [ubuntu-latest, macos-latest, windows-latest]
16+
python-version: ["3.10", "3.11", "3.12", "3.13"]
17+
defaults:
18+
run:
19+
shell: bash
20+
runs-on: ${{ matrix.os }}
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v4
24+
- name: Install uv and set the python version
25+
uses: astral-sh/setup-uv@v5
26+
with:
27+
python-version: ${{ matrix.python-version }}
28+
29+
- name: Install the project
30+
run: uv sync --all-extras --dev
31+
32+
# - name: Run doit
33+
# run: uv run doit
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Check Ruff formatting
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v4
10+
- name: Install Python
11+
uses: actions/setup-python@v5
12+
with:
13+
python-version: "3.12"
14+
- name: Set Project Name
15+
run: echo "REPOSITORY_NAME=$(echo '${{ github.repository }}' | awk -F '/' '{print $2}' | sed 's/-/_/g')" >> $GITHUB_ENV
16+
- name: Run Ruff Format
17+
uses: astral-sh/ruff-action@v3
18+
with:
19+
version: 0.9.9
20+
args: format --check
21+
src: "${{env.REPOSITORY_NAME}}"
22+
# - name: Run Ruff Linter
23+
# uses: astral-sh/ruff-action@v3
24+
# with:
25+
# version: 0.9.9
26+
# args: check
27+
# src: "${{env.REPOSITORY_NAME}}"
28+
# - name: Static type checking
29+
# run: |
30+
# uv run mypy "${{env.REPOSITORY_NAME}}"
31+
# continue-on-error: true

.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
build/
2+
dist/
3+
*.egg*/
4+
5+
.doit.*
6+
7+
.pytest_cache/
8+
.mypy_cache/
9+
.ruff_cache/
10+
__pycache__/
11+
12+
.vscode/
13+
14+
.venv/
15+
venv/
16+
17+
*.bak
18+
*.bak/
19+
20+
# TODO: mkdocs build files
21+
# TODO: sphinx build files

.pre-commit-config.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
repos:
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
# Ruff version.
4+
rev: v0.9.9
5+
hooks:
6+
# Run the formatter (automatically fixes in-place)
7+
- id: ruff-format
8+
# Run the linter
9+
- id: ruff
10+
args: [ --fix ]
11+
- repo: local
12+
hooks:
13+
- id: mypy
14+
name: mypy
15+
entry: uv run mypy
16+
require_serial: true
17+
language: system
18+
types: [python]

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.13

LICENSE.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Copyright (c) 2025 Big Ladder Software, LLC
2+
3+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4+
5+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6+
7+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8+
9+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10+
11+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[![Build and Test](https://github.com/bigladder/cachebin/actions/workflows/build-and-test.yaml/badge.svg)](https://github.com/bigladder/cachebin/actions/workflows/build-and-test.yaml)
2+
3+
cachebin
4+
========
5+
6+
`cachebin` is a python package, inspired by [DotSlash](https://dotslash-cli.com/docs/), that is designed to facilitate fetching an executable binary, verifying it (eventually!), and then running it. It maintains a local cache of fetched binaries so that subsequent invocations are fast.
7+
8+
Binaries are managed by a `BinaryManager` object. A growing list of such objects is maintained in [recipies.py](cachebin/recipies.py). Example usage can be found in the [test directory](test/test_cachebin.py).

cachebin/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .cachebin import BinaryManager # noqa: F401

cachebin/cachebin.py

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
import tarfile
2+
import zipfile
3+
from pathlib import Path
4+
from platform import machine, system
5+
from stat import S_IXGRP, S_IXOTH, S_IXUSR
6+
from subprocess import PIPE, Popen
7+
from typing import Callable
8+
9+
import py7zr
10+
import requests
11+
12+
SYSTEM_CACHE_DIR = {
13+
"linux": Path().home() / ".cache" / "cachebin",
14+
"darwin": Path().home() / "Library" / "Caches" / "cachebin",
15+
"windows": Path().home() / "AppData" / "Local" / "cachebin",
16+
}
17+
18+
19+
def download_file(url: str, directory_path: Path | str, force: bool = False) -> Path:
20+
"""
21+
Downloads a file from the given URL and saves it to the specified directory.
22+
23+
Args:
24+
url (str): The URL of the file to download.
25+
directory_path (Path): The directory where the file will be saved.
26+
27+
Returns:
28+
Path of the downloaded file.
29+
"""
30+
directory_path = Path(directory_path) # Ensure directory_path is a Path object
31+
response = requests.get(url, stream=True)
32+
response.raise_for_status() # Raise an error for bad responses
33+
34+
directory_path.mkdir(parents=True, exist_ok=True)
35+
36+
filename = url.split("/")[-1] # Extract the filename from the URL
37+
file_path = directory_path / filename
38+
39+
if not file_path.exists() or force:
40+
print(f"Downloading {url} to {file_path}...")
41+
with open(file_path, "wb") as file:
42+
for chunk in response.iter_content(chunk_size=8192):
43+
file.write(chunk)
44+
45+
return file_path
46+
47+
48+
def extract_archive(archive_path: Path | str, extract_path: Path | str) -> Path: # noqa: PLR0912
49+
"""
50+
Extracts a compressed archive to the specified directory.
51+
52+
Args:
53+
archive_path (Path): The path to the archive file.
54+
extract_to (Path): The directory where the archive will be extracted.
55+
"""
56+
archive_path = Path(archive_path)
57+
extract_path = Path(extract_path)
58+
if not archive_path.exists():
59+
raise FileNotFoundError(f"Archive {archive_path} does not exist.")
60+
61+
extract_path.mkdir(parents=True, exist_ok=True)
62+
63+
archive: zipfile.ZipFile | tarfile.TarFile | py7zr.SevenZipFile
64+
if archive_path.name.endswith(("tar.gz", "tgz")):
65+
archive = tarfile.open(archive_path, "r:gz")
66+
elif archive_path.name.endswith("tar.bz2"):
67+
archive = tarfile.open(archive_path, "r:bz2")
68+
elif archive_path.name.endswith("tar.xz"):
69+
archive = tarfile.open(archive_path, "r:xz")
70+
elif archive_path.name.endswith("tar"):
71+
archive = tarfile.open(archive_path, "r:")
72+
elif archive_path.name.endswith("zip"):
73+
archive = zipfile.ZipFile(archive_path, "r")
74+
elif archive_path.name.endswith("7z"):
75+
archive = py7zr.SevenZipFile(archive_path, "r")
76+
else:
77+
raise RuntimeError(f"Unsupported archive format: {archive_path}")
78+
79+
extracted_parent_directory = extract_path
80+
81+
top_item: zipfile.ZipInfo | tarfile.TarInfo | py7zr.FileInfo
82+
if isinstance(archive, zipfile.ZipFile):
83+
top_item = archive.infolist()[0]
84+
if top_item.is_dir():
85+
extracted_parent_directory = extract_path / top_item.filename
86+
87+
elif isinstance(archive, tarfile.TarFile):
88+
top_item = archive.getmembers()[0]
89+
if top_item.isdir():
90+
extracted_parent_directory = extract_path / top_item.name
91+
92+
elif isinstance(archive, py7zr.SevenZipFile):
93+
top_item = archive.list()[0]
94+
if top_item.is_directory:
95+
extracted_parent_directory = extract_path / top_item.filename
96+
97+
if not extracted_parent_directory.exists():
98+
print(f"Extracting {archive_path} to {extract_path}...")
99+
archive.extractall(path=extract_path)
100+
return extracted_parent_directory
101+
102+
103+
def make_executable(file_path: str | Path) -> None:
104+
file_path = Path(file_path)
105+
current_permissions = file_path.stat().st_mode
106+
# Add the executable bit for the owner, group, and others
107+
file_path.chmod(current_permissions | S_IXUSR | S_IXGRP | S_IXOTH)
108+
109+
110+
class BinaryVersion:
111+
def __init__(self, version: str, parent: "BinaryManager"):
112+
self.parent = parent
113+
self.version = version
114+
self.url = self.parent.url_pattern.format(
115+
version=self.version,
116+
platform=self.parent._platform_string,
117+
extension=self.parent._extension,
118+
package_name=self.parent.package_name,
119+
)
120+
self.archive_name = self.url.split("/")[-1]
121+
self.archive_path = download_file(self.url, self.parent._downloads_directory)
122+
self.binary_directory_path = (
123+
extract_archive(self.archive_path, self.parent._package_directory / self.version)
124+
/ self.parent._extracted_bin_path
125+
)
126+
127+
def call(self, command: str, *args: str) -> str:
128+
"""
129+
Calls the binary with the specified command and arguments.
130+
131+
Args:
132+
command (str): The command to execute.
133+
*args (str): Additional arguments for the command.
134+
135+
Returns:
136+
str: The output of the command.
137+
"""
138+
binary_path = self.binary_directory_path / command
139+
if not binary_path.exists():
140+
raise FileNotFoundError(f"Binary {binary_path} does not exist.")
141+
142+
make_executable(binary_path)
143+
144+
creation_flag = (
145+
0x08000000 if self.parent._system == "windows" else 0
146+
) # set creation flag to not open in new console on windows
147+
process = Popen([str(binary_path), *args], stdout=PIPE, stderr=PIPE, creationflags=creation_flag)
148+
stdout, stderr = process.communicate()
149+
if process.returncode != 0:
150+
raise RuntimeError(f"Command failed with error: {stderr.decode('utf-8')}")
151+
return stdout.decode("utf-8")
152+
153+
154+
class BinaryManager:
155+
def __init__(
156+
self,
157+
package_name: str,
158+
url_pattern: str,
159+
get_archive_extension: Callable[[str], str], # returns archive extension based on system
160+
get_platform_string: Callable[[str, str], str] = lambda system,
161+
architecture: f"{system}-{architecture}", # returns platform string used in url_pattern
162+
get_extracted_bin_path: Callable[[str], str] = lambda _: "bin", # returns extracted bin path based on system
163+
cache_directory: Path | str | None = None,
164+
):
165+
self.package_name = package_name
166+
self.url_pattern = url_pattern
167+
self._system = system().lower()
168+
self._architecture = machine().lower()
169+
self._platform_string = get_platform_string(self._system, self._architecture)
170+
self._extension = get_archive_extension(self._system)
171+
self._extracted_bin_path = get_extracted_bin_path(self._system)
172+
173+
self._cache_directory: Path
174+
if cache_directory is None:
175+
cache_directory = SYSTEM_CACHE_DIR.get(self._system)
176+
if cache_directory is None:
177+
raise ValueError(f"Unsupported system: {self._system}")
178+
self._cache_directory = cache_directory
179+
else:
180+
self._cache_directory = Path(cache_directory)
181+
self._downloads_directory = self._cache_directory / "downloads"
182+
self._archive_directory = self._cache_directory / self.package_name
183+
self._packages_directory = self._cache_directory / "packages"
184+
self._package_directory = self._packages_directory / self.package_name
185+
self._versions: dict[str, BinaryVersion] = {}
186+
187+
def get_version(self, version: str) -> BinaryVersion:
188+
"""
189+
Adds a version to the manager.
190+
191+
Args:
192+
version (str): The version to add.
193+
194+
Returns:
195+
BinaryVersion object for the added version.
196+
"""
197+
if version not in self._versions:
198+
self._versions[version] = BinaryVersion(version, self)
199+
return self._versions[version]

cachebin/recipies.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from .cachebin import BinaryManager
2+
3+
4+
def process_map(map: dict[str, str], key_description: str, key: str) -> str:
5+
result = map.get(key)
6+
if result is None:
7+
raise ValueError(f"Unsupported {key_description}: {key}")
8+
return result
9+
10+
11+
pandoc_manager = BinaryManager(
12+
package_name="pandoc",
13+
url_pattern="https://github.com/jgm/{package_name}/releases/download/{version}/{package_name}-{version}-{platform}.{extension}",
14+
get_platform_string=lambda system, architecture: process_map(
15+
{
16+
"darwin-x86_64": "x86_64-macOS",
17+
"darwin-aarch64": "arm64-macOS",
18+
"linux-x86_64": "linux-amd64",
19+
"linux-aarch64": "linux-arm64",
20+
"windows-x86_64": "windows-x86_64",
21+
},
22+
"platform",
23+
f"{system}-{architecture}",
24+
),
25+
get_archive_extension=lambda system: "tar.gz" if system == "linux" else "zip",
26+
)
27+
28+
tinytex_manager = BinaryManager(
29+
package_name="tinytex",
30+
url_pattern="https://github.com/rstudio/tinytex-releases/releases/download/{version}/TinyTeX-1-{version}.{extension}",
31+
get_archive_extension=lambda system: process_map(
32+
{"windows": "zip", "linux": "tar.gz", "darwin": "tgz"}, "system", system
33+
),
34+
get_extracted_bin_path=lambda system: f"bin/universal-{system}" if system == "darwin" else f"bin/{system}",
35+
)
36+
37+
pandoc_crossref_manager = BinaryManager(
38+
package_name="pandoc-crossref",
39+
url_pattern="https://github.com/lierdakil/{package_name}/releases/download/{version}/{package_name}-{platform}.{extension}",
40+
get_platform_string=lambda system, architecture: process_map(
41+
{
42+
"darwin-x86_64": "macOS-X64",
43+
"darwin-aarch64": "macOS-ARM64",
44+
"linux-x86_64": "Linux-X64",
45+
"windows-x86_64": "Windows-X64",
46+
},
47+
"platform",
48+
f"{system}-{architecture}",
49+
),
50+
get_archive_extension=lambda system: "7z" if system == "windows" else "tar.xz",
51+
get_extracted_bin_path=lambda _: "",
52+
)

0 commit comments

Comments
 (0)