dotlocalslashbin 0.0.23__tar.gz → 0.0.25__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dotlocalslashbin
3
- Version: 0.0.23
3
+ Version: 0.0.25
4
4
  Summary: Download and extract files to `~/.local/bin/`.
5
5
  Author-email: Keith Maxwell <keith.maxwell@gmail.com>
6
6
  Requires-Python: >=3.11
@@ -7,6 +7,8 @@
7
7
  # dependencies = []
8
8
  # ///
9
9
  """Download and extract files to `~/.local/bin/`."""
10
+
11
+ import gzip
10
12
  import tarfile
11
13
  from argparse import ArgumentParser, BooleanOptionalAction, Namespace
12
14
  from dataclasses import dataclass
@@ -14,7 +16,7 @@ from enum import Enum
14
16
  from hashlib import file_digest
15
17
  from pathlib import Path
16
18
  from shlex import split
17
- from shutil import copy
19
+ from shutil import copy, copyfileobj
18
20
  from stat import S_IEXEC
19
21
  from subprocess import run
20
22
  from tomllib import load
@@ -22,8 +24,7 @@ from urllib.error import HTTPError
22
24
  from urllib.request import urlopen
23
25
  from zipfile import ZipFile
24
26
 
25
-
26
- __version__ = "0.0.23"
27
+ __version__ = "0.0.25"
27
28
 
28
29
  _CACHE = Path("~/.cache/dotlocalslashbin/")
29
30
  _HOME = str(Path("~").expanduser())
@@ -38,7 +39,7 @@ class _CustomNamespace(Namespace):
38
39
  cache: Path
39
40
 
40
41
 
41
- Action = Enum("Action", ["command", "copy", "symlink", "untar", "unzip"])
42
+ Action = Enum("Action", ["command", "copy", "gunzip", "symlink", "untar", "unzip"])
42
43
 
43
44
 
44
45
  @dataclass(init=False)
@@ -123,19 +124,7 @@ def _process(item: Item) -> None:
123
124
 
124
125
  item.target.parent.mkdir(parents=True, exist_ok=True)
125
126
  item.target.unlink(missing_ok=True)
126
- if item.action == Action.copy:
127
- copy(item.downloaded, item.target)
128
- elif item.action == Action.symlink:
129
- item.target.symlink_to(item.downloaded)
130
- elif item.action == Action.unzip:
131
- with ZipFile(item.downloaded, "r") as file:
132
- file.extract(item.target.name, path=item.target.parent)
133
- elif item.action == Action.untar:
134
- _untar(item)
135
- elif item.action == Action.command and item.command is not None:
136
- kwargs = {"target": item.target, "downloaded": item.downloaded}
137
- run(split(item.command.format(**kwargs)), check=True)
138
-
127
+ _action(item)
139
128
  if not item.target.is_symlink():
140
129
  item.target.chmod(item.target.stat().st_mode | S_IEXEC)
141
130
 
@@ -172,21 +161,37 @@ def _download(item: Item) -> None:
172
161
  raise RuntimeError(msg)
173
162
 
174
163
 
175
- def _untar(item: Item) -> None:
176
- with tarfile.open(item.downloaded, "r") as file:
177
- for member in file.getmembers():
178
- if member.name in item.ignore:
179
- continue
180
- member.name = member.name.removeprefix(item.prefix)
181
- try:
182
- file.extract(member, path=item.target.parent, filter="tar")
183
- except TypeError: # before 3.11.4 e.g. Debian 12
184
- file.extract(member, path=item.target.parent)
164
+ def _action(item: Item) -> None:
165
+ if item.action == Action.copy:
166
+ copy(item.downloaded, item.target)
167
+ elif item.action == Action.symlink:
168
+ item.target.symlink_to(item.downloaded)
169
+ elif item.action == Action.unzip:
170
+ with ZipFile(item.downloaded, "r") as file:
171
+ file.extract(item.target.name, path=item.target.parent)
172
+ elif item.action == Action.gunzip:
173
+ with gzip.open(item.downloaded, "r") as fsrc, item.target.open("wb") as fdst:
174
+ copyfileobj(fsrc, fdst)
175
+ elif item.action == Action.untar:
176
+ with tarfile.open(item.downloaded, "r") as file:
177
+ for member in file.getmembers():
178
+ if member.name in item.ignore:
179
+ continue
180
+ member.name = member.name.removeprefix(item.prefix)
181
+ try:
182
+ file.extract(member, path=item.target.parent, filter="tar")
183
+ except TypeError: # before 3.11.4 e.g. Debian 12
184
+ file.extract(member, path=item.target.parent)
185
+ elif item.action == Action.command and item.command is not None:
186
+ cmd = item.command.format(target=item.target, downloaded=item.downloaded)
187
+ run(split(cmd), check=True)
185
188
 
186
189
 
187
190
  def _guess_action(item: Item) -> Action:
188
191
  if item.url.endswith((".tar.xz", ".tar.gz", ".tar")):
189
192
  guess = Action.untar
193
+ elif item.url.endswith((".gz",)):
194
+ guess = Action.gunzip
190
195
  elif item.url.endswith(".zip"):
191
196
  guess = Action.unzip
192
197
  elif item.url.startswith("/"):