Skip to content

Commit

Permalink
update: usage (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
metaist committed Sep 18, 2024
1 parent 3d2dba5 commit dc404fa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 52 deletions.
34 changes: 14 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ cosmofy src/cosmofy \

<!--[[[cog
from cosmofy.args import USAGE
cog.outl(f"\n```text\n{USAGE}```\n")
doc = USAGE.replace('COSMOFY_CACHE_DIR=/home/lev/.cache/cosmofy', 'COSMOFY_CACHE_DIR=~/.cache/cosmofy')
cog.outl(f"\n```text\n{doc}```\n")
]]]-->

```text
Expand All @@ -60,31 +61,24 @@ USAGE
GENERAL
-h, --help
Show this help message and exit.
--version
Show program version and exit.
--debug
Show debug messages.
-n, --dry-run
Do not make any file system changes.
--self-update
Update `cosmofy` to the latest version.
-h, --help Show this help message and exit.
--version Show program version and exit.
--debug Show debug messages.
-n, --dry-run Do not make any file system changes.
--self-update Update `cosmofy` to the latest version.
CACHE
--python-url URL
URL from which to download Cosmopolitan Python.
[env: COSMOFY_PYTHON_URL=https://cosmo.zip/pub/cosmos/bin/python]
[default: https://cosmo.zip/pub/cosmos/bin/python]
[env: COSMOFY_PYTHON_URL=None]
--cache PATH
Directory in which to cache Cosmopolitan Python downloads.
Use `false` or `0` to disable caching.
[env: COSMOFY_CACHE_DIR=/home/lev/.cache/cosmofy]
[default: ~/.cache/cosmofy]
[env: COSMOFY_CACHE_DIR=None]
--clone
Obtain python by cloning `cosmofy` and removing itself instead of
Expand Down Expand Up @@ -145,17 +139,17 @@ SELF-UPDATER
--receipt-url URL
URL to the published receipt.
[default: <release-url>.json]
[default: --release-url + .json]
[env: RECEIPT_URL=]
--release-url URL
URL to the file to download.
[default: <receipt-url-without.json>]
[default: --receipt-url without .json]
[env: RELEASE_URL=]
--release-version STRING
Release version.
[default: we run `output --version` and save first version-looking string]
[default: first version-like string in `$(${output} --version)`]
```

<!--[[[end]]]-->
Expand Down
45 changes: 20 additions & 25 deletions src/cosmofy/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@

log = logging.getLogger(__name__)

COSMOFY_PYTHON_URL = ENV.get(
"COSMOFY_PYTHON_URL", "https://cosmo.zip/pub/cosmos/bin/python"
)
DEFAULT_PYTHON_URL = "https://cosmo.zip/pub/cosmos/bin/python"
"""Default URL to download python from."""

COSMOFY_PYTHON_URL = ENV.get("COSMOFY_PYTHON_URL")
"""URL to download python from."""

COSMOFY_CACHE_DIR = Path(
ENV.get("COSMOFY_CACHE_DIR", Path.home() / ".cache" / "cosmofy")
)
DEFAULT_CACHE_DIR = Path.home() / ".cache" / "cosmofy"
"""Default cache directory."""

COSMOFY_CACHE_DIR = ENV.get("COSMOFY_CACHE_DIR")
"""Path to cache directory."""

RECEIPT_URL = ENV.get("RECEIPT_URL", "")
Expand All @@ -41,30 +43,23 @@
GENERAL
-h, --help
Show this help message and exit.
--version
Show program version and exit.
--debug
Show debug messages.
-n, --dry-run
Do not make any file system changes.
--self-update
Update `cosmofy` to the latest version.
-h, --help Show this help message and exit.
--version Show program version and exit.
--debug Show debug messages.
-n, --dry-run Do not make any file system changes.
--self-update Update `cosmofy` to the latest version.
CACHE
--python-url URL
URL from which to download Cosmopolitan Python.
[default: {DEFAULT_PYTHON_URL}]
[env: COSMOFY_PYTHON_URL={COSMOFY_PYTHON_URL}]
--cache PATH
Directory in which to cache Cosmopolitan Python downloads.
Use `false` or `0` to disable caching.
[default: {str(DEFAULT_CACHE_DIR).replace(str(Path.home()), '~')}]
[env: COSMOFY_CACHE_DIR={COSMOFY_CACHE_DIR}]
--clone
Expand Down Expand Up @@ -126,17 +121,17 @@
--receipt-url URL
URL to the published receipt.
[default: <release-url>.json]
[default: --release-url + .json]
[env: RECEIPT_URL={RECEIPT_URL}]
--release-url URL
URL to the file to download.
[default: <receipt-url-without.json>]
[default: --receipt-url without .json]
[env: RELEASE_URL={RELEASE_URL}]
--release-version STRING
Release version.
[default: we run `output --version` and save first version-looking string]
[default: first version-like string in `$(${{output}} --version)`]
"""


Expand Down Expand Up @@ -169,10 +164,10 @@ def for_real(self, value: bool) -> None:

# cache

python_url: str = COSMOFY_PYTHON_URL
python_url: str = COSMOFY_PYTHON_URL or DEFAULT_PYTHON_URL
"""URL from which to download Cosmopolitan Python."""

cache: Optional[Path] = COSMOFY_CACHE_DIR
cache: Optional[Path] = Path(COSMOFY_CACHE_DIR or DEFAULT_CACHE_DIR)
"""Directory for caching downloads."""

clone: bool = False
Expand Down
9 changes: 4 additions & 5 deletions src/cosmofy/bundler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

# pkg
from .args import Args
from .args import COSMOFY_PYTHON_URL
from .updater import download
from .updater import download_if_newer
from .updater import move_executable
Expand Down Expand Up @@ -156,18 +155,18 @@ def from_cache(
self, src: Path, dest: Path, archive: Optional[ZipFile2] = None
) -> ZipFile2:
"""Copy the archive from cache."""
log.debug(f"{self.banner}download (if newer): {COSMOFY_PYTHON_URL}")
log.debug(f"{self.banner}download (if newer): {self.args.python_url}")
if self.args.for_real:
download_if_newer(COSMOFY_PYTHON_URL, src)
download_if_newer(self.args.python_url, src)

self.fs_copy(src, dest)
return archive or _archive(dest)

def from_download(self, dest: Path, archive: Optional[ZipFile2] = None) -> ZipFile2:
"""Download archive."""
log.debug(f"{self.banner}download (fresh): {COSMOFY_PYTHON_URL} to {dest}")
log.debug(f"{self.banner}download (fresh): {self.args.python_url} to {dest}")
if self.args.for_real:
download(COSMOFY_PYTHON_URL, dest)
download(self.args.python_url, dest)
return archive or _archive(dest)

def setup_temp(self) -> Tuple[Path, Optional[ZipFile2]]:
Expand Down
4 changes: 2 additions & 2 deletions test/test_bundler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# pkg
from cosmofy import bundler
from cosmofy.args import Args
from cosmofy.args import COSMOFY_PYTHON_URL
from cosmofy.args import DEFAULT_PYTHON_URL
from cosmofy.bundler import _archive
from cosmofy.bundler import _pack_uint32
from cosmofy.bundler import Bundler
Expand Down Expand Up @@ -127,7 +127,7 @@ def test_from_download() -> None:

with patch("cosmofy.bundler.download") as mock:
real.from_download(dest)
mock.assert_called_once_with(COSMOFY_PYTHON_URL, dest)
mock.assert_called_once_with(DEFAULT_PYTHON_URL, dest)


def test_setup_temp() -> None:
Expand Down

0 comments on commit dc404fa

Please sign in to comment.