pyselfupdate 0.7.0__tar.gz → 0.8.1__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.5
2
2
  Name: pyselfupdate
3
- Version: 0.7.0
3
+ Version: 0.8.1
4
4
  Summary: Self-update and update notification for Python CLIs installed with uv tool
5
5
  Project-URL: Repository, https://github.com/datapointchris/pyselfupdate
6
6
  Project-URL: Issues, https://github.com/datapointchris/pyselfupdate/issues
@@ -49,8 +49,9 @@ uv add pyselfupdate # the library alone, with no dependencies
49
49
  uv add 'pyselfupdate[typer]' # adds typer and the ready-made `update` command
50
50
  ```
51
51
 
52
- Python 3.11 is the floor. It is the first release carrying `tomllib`, which
53
- reads uv's receipt and lock without a TOML dependency.
52
+ The supported Python versions are `requires-python` in `pyproject.toml`, which
53
+ uv checks before it installs. The floor is the first release carrying
54
+ `tomllib`.
54
55
 
55
56
  ## Starting from scratch
56
57
 
@@ -124,7 +125,7 @@ Nothing is printed when any of these hold:
124
125
  | `NO_AUTO_UPDATE` or `MYTOOL_NO_AUTO_UPDATE` is set | Opted out |
125
126
  | `--help` or `-h` comes before any `--` on the command line | typer runs the callback even for a subcommand's help screen, which is not work |
126
127
  | stdout or stderr is not a terminal | `mytool list > out 2>&1` must stay clean |
127
- | `CI`, `BUILD_NUMBER`, `RUN_ID`, `GITHUB_ACTIONS` or `CODESPACES` is set | Not a human |
128
+ | A variable in `pyselfupdate.notifier.CI_VARIABLES`, such as `CI`, is set | Not a human |
128
129
  | Installed from a local path, an editable checkout, a branch or a commit | Nothing to compare against |
129
130
  | Checked within the interval | One request per interval, not per invocation |
130
131
 
@@ -137,6 +138,12 @@ each variable is its name uppercased, with `-` written as `_`.
137
138
  A command writing into a pager, a log or a structured-output mode calls
138
139
  `notify(config, interactive=False)`. Nothing about the streams reveals that.
139
140
 
141
+ The line names `mytool update` by default. A tool that registers the command
142
+ under another name passes the same name, as in
143
+ `add_update_command(app, CONFIG, name='self-update')` beside
144
+ `notify(CONFIG, update_command='self-update')`. A tool with no update command
145
+ passes `update_command=None`, and the line ends after the versions.
146
+
140
147
  ## `update` — rebuilds the environment it runs in, so it runs last
141
148
 
142
149
  `update` runs `uv tool install --force`, which rebuilds the virtual environment
@@ -229,6 +236,11 @@ later `uv tool upgrade` stays held to them.
229
236
  | Carries none | Installs unlocked and sets `lock_missing`. `lock_warning` names the tag, and `run_update` and `update_and_reexec` print it |
230
237
  | Will not clone, uv will not export its lock, or git or uv is not on `PATH` | Raises `LockUnreadableError` and installs nothing |
231
238
 
239
+ A tool that is one member of a uv workspace installs as
240
+ `git+<repo>@<tag>#subdirectory=<member>`. `update` keeps the subdirectory, and
241
+ reads the workspace's one `uv.lock` at the repository root for that member's
242
+ dependencies alone.
243
+
232
244
  An update reinstalls the tool's own extras, as in `mytool[fast]`, with their
233
245
  dependencies held to the lock. Every `--with` and `--with-editable`
234
246
  requirement is passed back to uv as well.
@@ -26,8 +26,9 @@ uv add pyselfupdate # the library alone, with no dependencies
26
26
  uv add 'pyselfupdate[typer]' # adds typer and the ready-made `update` command
27
27
  ```
28
28
 
29
- Python 3.11 is the floor. It is the first release carrying `tomllib`, which
30
- reads uv's receipt and lock without a TOML dependency.
29
+ The supported Python versions are `requires-python` in `pyproject.toml`, which
30
+ uv checks before it installs. The floor is the first release carrying
31
+ `tomllib`.
31
32
 
32
33
  ## Starting from scratch
33
34
 
@@ -101,7 +102,7 @@ Nothing is printed when any of these hold:
101
102
  | `NO_AUTO_UPDATE` or `MYTOOL_NO_AUTO_UPDATE` is set | Opted out |
102
103
  | `--help` or `-h` comes before any `--` on the command line | typer runs the callback even for a subcommand's help screen, which is not work |
103
104
  | stdout or stderr is not a terminal | `mytool list > out 2>&1` must stay clean |
104
- | `CI`, `BUILD_NUMBER`, `RUN_ID`, `GITHUB_ACTIONS` or `CODESPACES` is set | Not a human |
105
+ | A variable in `pyselfupdate.notifier.CI_VARIABLES`, such as `CI`, is set | Not a human |
105
106
  | Installed from a local path, an editable checkout, a branch or a commit | Nothing to compare against |
106
107
  | Checked within the interval | One request per interval, not per invocation |
107
108
 
@@ -114,6 +115,12 @@ each variable is its name uppercased, with `-` written as `_`.
114
115
  A command writing into a pager, a log or a structured-output mode calls
115
116
  `notify(config, interactive=False)`. Nothing about the streams reveals that.
116
117
 
118
+ The line names `mytool update` by default. A tool that registers the command
119
+ under another name passes the same name, as in
120
+ `add_update_command(app, CONFIG, name='self-update')` beside
121
+ `notify(CONFIG, update_command='self-update')`. A tool with no update command
122
+ passes `update_command=None`, and the line ends after the versions.
123
+
117
124
  ## `update` — rebuilds the environment it runs in, so it runs last
118
125
 
119
126
  `update` runs `uv tool install --force`, which rebuilds the virtual environment
@@ -206,6 +213,11 @@ later `uv tool upgrade` stays held to them.
206
213
  | Carries none | Installs unlocked and sets `lock_missing`. `lock_warning` names the tag, and `run_update` and `update_and_reexec` print it |
207
214
  | Will not clone, uv will not export its lock, or git or uv is not on `PATH` | Raises `LockUnreadableError` and installs nothing |
208
215
 
216
+ A tool that is one member of a uv workspace installs as
217
+ `git+<repo>@<tag>#subdirectory=<member>`. `update` keeps the subdirectory, and
218
+ reads the workspace's one `uv.lock` at the repository root for that member's
219
+ dependencies alone.
220
+
209
221
  An update reinstalls the tool's own extras, as in `mytool[fast]`, with their
210
222
  dependencies held to the lock. Every `--with` and `--with-editable`
211
223
  requirement is passed back to uv as well.
@@ -1,12 +1,9 @@
1
1
  """GitHub as a release source.
2
2
 
3
- Uses `urllib.request` rather than httpx2. The package makes one JSON request and
4
- downloads nothing, and tools with no HTTP client of their own take it for
5
- self-update, so a client library would land in their dependency tree for this
6
- one call. `_CredentialScopedRedirects` covers the redirect hazard a client would.
7
-
8
- It moves to httpx2 when this package needs a second request shape: a client
9
- object, streaming, an asset download.
3
+ Uses `urllib.request`, because the package has no runtime dependencies. The
4
+ HTTP client weighed against that, and when to revisit it, are under the
5
+ zero-dependency constraint in the repository's CLAUDE.md.
6
+ `_CredentialScopedRedirects` covers the redirect hazard a client would.
10
7
  """
11
8
 
12
9
  from __future__ import annotations
@@ -78,6 +78,10 @@ class Installation:
78
78
  # or refuses it.
79
79
  revision: str = ''
80
80
 
81
+ # The directory inside the repository holding the tool's project, for a
82
+ # uv workspace member installed with `#subdirectory=`. Empty at the root.
83
+ subdirectory: str = ''
84
+
81
85
  extras: tuple[str, ...] = ()
82
86
 
83
87
  # The receipt's other requirements, from `--with` and `--with-editable`, as
@@ -157,9 +161,9 @@ def read_installation(tool: str) -> Installation:
157
161
  else:
158
162
  rebuilt.append(line)
159
163
 
160
- def installed_as(kind: InstallKind, url: str = '', revision: str = '') -> Installation:
164
+ def installed_as(kind: InstallKind, url: str = '', revision: str = '', subdirectory: str = '') -> Installation:
161
165
  extras = tuple(own.get('extras') or ())
162
- return Installation(tool, kind, url, revision, extras, tuple(rebuilt), _source_flags(recorded), tuple(unrebuildable))
166
+ return Installation(tool, kind, url, revision, subdirectory, extras, tuple(rebuilt), _source_flags(recorded), tuple(unrebuildable))
163
167
 
164
168
  # A local checkout, however uv spelled it. Reinstalling one would
165
169
  # discard the working copy the user is developing against.
@@ -170,7 +174,8 @@ def read_installation(tool: str) -> Installation:
170
174
  git = own.get('git')
171
175
  if git:
172
176
  url, query, _ = _split_git(str(git))
173
- return installed_as(InstallKind.GIT, url=url, revision=query.get('rev', ''))
177
+ subdirectory = query.get('subdirectory') or str(own.get('subdirectory') or '')
178
+ return installed_as(InstallKind.GIT, url=url, revision=query.get('rev', ''), subdirectory=subdirectory)
174
179
 
175
180
  return installed_as(InstallKind.INDEX)
176
181
 
@@ -328,7 +333,8 @@ def requirement_for(installation: Installation, ref: str) -> str:
328
333
  """
329
334
  named = f'{installation.tool}[{",".join(installation.extras)}]' if installation.extras else installation.tool
330
335
  if installation.kind is InstallKind.GIT:
331
- return f'{named} @ git+{installation.url}@{ref}'
336
+ subdirectory = f'#subdirectory={installation.subdirectory}' if installation.subdirectory else ''
337
+ return f'{named} @ git+{installation.url}@{ref}{subdirectory}'
332
338
  return f'{named}>={ref.removeprefix("v")}'
333
339
 
334
340
 
@@ -361,7 +367,7 @@ class Pins:
361
367
  return flags
362
368
 
363
369
 
364
- def read_lock(url: str, ref: str, *, extras: Sequence[str] = ()) -> Pins | None:
370
+ def read_lock(url: str, ref: str, *, extras: Sequence[str] = (), package: str = '', subdirectory: str = '') -> Pins | None:
365
371
  """What the `uv.lock` at `ref` of a git repository pins, or None when it has none.
366
372
 
367
373
  `extras` are the extras the tool is installed with. Each is passed to
@@ -369,6 +375,10 @@ def read_lock(url: str, ref: str, *, extras: Sequence[str] = ()) -> Pins | None:
369
375
  dependencies out. `--all-extras` fails on a project that declares two
370
376
  extras as conflicting.
371
377
 
378
+ `subdirectory` names a uv workspace member, whose lock sits at the
379
+ repository root. The export then takes only `package`'s dependencies, and
380
+ the extras walk starts at that member.
381
+
372
382
  Reads a shallow clone of the one ref and lets `uv export` interpret the lock.
373
383
  The lock is also read directly for one thing the export drops: the extras
374
384
  a dependent requests of a git dependency, which `_override_extras` restores.
@@ -400,7 +410,8 @@ def read_lock(url: str, ref: str, *, extras: Sequence[str] = ()) -> Pins | None:
400
410
  lock = checkout / LOCK_NAME
401
411
  if not lock.is_file():
402
412
  return None
403
- export = [uv, *EXPORT, *chain.from_iterable(('--extra', extra) for extra in extras)]
413
+ member = ('--package', package) if subdirectory and package else ()
414
+ export = [uv, *EXPORT, *member, *chain.from_iterable(('--extra', extra) for extra in extras)]
404
415
  try:
405
416
  exported = run_command(export, cwd=checkout, timeout=LOCK_READ_TIMEOUT_SECONDS)
406
417
  except subprocess.TimeoutExpired as error:
@@ -408,14 +419,14 @@ def read_lock(url: str, ref: str, *, extras: Sequence[str] = ()) -> Pins | None:
408
419
  if exported.returncode != 0:
409
420
  raise LockUnreadableError(f'uv export would not read the uv.lock at {ref}: {_reason(exported)}')
410
421
  try:
411
- requested = _override_extras(tomllib.loads(lock.read_text(encoding='utf-8')), extras)
422
+ requested = _override_extras(tomllib.loads(lock.read_text(encoding='utf-8')), extras, root=subdirectory or '.')
412
423
  except (OSError, tomllib.TOMLDecodeError) as error:
413
424
  raise LockUnreadableError(f'cannot read the uv.lock at {ref}: {error}') from error
414
425
  return _pins_from_export(exported.stdout, requested)
415
426
 
416
427
 
417
- def _override_extras(lock: dict, extras: Sequence[str]) -> dict[str, tuple[str, ...]]:
418
- """The extras requested of each package, walking dependency edges from the project at the lock's root.
428
+ def _override_extras(lock: dict, extras: Sequence[str], root: str = '.') -> dict[str, tuple[str, ...]]:
429
+ """The extras requested of each package, walking dependency edges from the project at `root` in the lock.
419
430
 
420
431
  An override replaces a requirement whole, extras included, and `uv export`
421
432
  writes `gitdep @ git+...` where the tool declared `gitdep[x] @ git+...`.
@@ -430,7 +441,7 @@ def _override_extras(lock: dict, extras: Sequence[str]) -> dict[str, tuple[str,
430
441
  packages: dict[str, list[dict]] = {}
431
442
  for package in lock.get('package') or []:
432
443
  packages.setdefault(package.get('name', ''), []).append(package)
433
- project = ({'editable': '.'}, {'virtual': '.'})
444
+ project = ({'editable': root}, {'virtual': root})
434
445
  roots: list[str] = []
435
446
  for name, entries in packages.items():
436
447
  roots.extend(name for package in entries if package.get('source') in project)
@@ -71,6 +71,7 @@ def notify(
71
71
  out: TextIO | None = None,
72
72
  defer: bool = True,
73
73
  interactive: bool | None = None,
74
+ update_command: str | None = 'update',
74
75
  ) -> Outcome:
75
76
  """Check at most once per interval and print one line when behind.
76
77
 
@@ -86,9 +87,24 @@ def notify(
86
87
  already knows it is writing somewhere a human will not read -- into a pager,
87
88
  a log, a structured-output mode -- since nothing about the streams
88
89
  themselves reveals that.
90
+
91
+ `update_command` is the subcommand the notice tells the reader to run, as
92
+ in `<tool> <update_command>`. Pass the name given to `add_update_command`
93
+ when it is not `update`, or None for a tool with no update command, which
94
+ prints the notice without a hint. This layer cannot see the consumer's
95
+ command tree, so the consumer names the verb rather than the library
96
+ guessing one. A hint naming a command the tool lacks costs the reader a
97
+ failed retry at the moment they act on it.
89
98
  """
90
99
  try:
91
- return _notify(config, interval=interval, out=out, defer=defer, interactive=interactive)
100
+ return _notify(
101
+ config,
102
+ interval=interval,
103
+ out=out,
104
+ defer=defer,
105
+ interactive=interactive,
106
+ update_command=update_command,
107
+ )
92
108
  except SelfUpdateError:
93
109
  return Outcome(skip=Skip.FAILED)
94
110
  except Exception: # noqa: BLE001 - a notice may never break the caller
@@ -143,6 +159,7 @@ def _notify(
143
159
  out: TextIO | None,
144
160
  defer: bool,
145
161
  interactive: bool | None,
162
+ update_command: str | None,
146
163
  ) -> Outcome:
147
164
  resolved = config.resolved()
148
165
  tool = resolved.tool
@@ -177,7 +194,9 @@ def _notify(
177
194
  if not result.update_available:
178
195
  return Outcome(checked=True, current=result.current, latest=result.latest)
179
196
 
180
- message = f'{tool} {result.latest} available (running {result.current}) — run `{tool} update`'
197
+ message = f'{tool} {result.latest} available (running {result.current})'
198
+ if update_command:
199
+ message += f' — run `{tool} {update_command}`'
181
200
  _emit(message, out=out, defer=defer)
182
201
  return Outcome(checked=True, current=result.current, latest=result.latest)
183
202
 
@@ -167,7 +167,9 @@ def install_release(config: Config, result: Result, installation: Installation,
167
167
 
168
168
  if installation.kind is InstallKind.GIT:
169
169
  ref = result.release.install_ref()
170
- pins = read_lock(installation.url, ref, extras=installation.extras)
170
+ pins = read_lock(
171
+ installation.url, ref, extras=installation.extras, package=installation.tool, subdirectory=installation.subdirectory
172
+ )
171
173
  else:
172
174
  ref, pins = result.latest, None
173
175
  run_install(
File without changes
File without changes