dycw-utilities 0.174.11__py3-none-any.whl → 0.174.13__py3-none-any.whl

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.3
2
2
  Name: dycw-utilities
3
- Version: 0.174.11
3
+ Version: 0.174.13
4
4
  Author: Derek Wan
5
5
  Author-email: Derek Wan <d.wan@icloud.com>
6
6
  Requires-Dist: atomicwrites>=1.4.1,<1.5
@@ -1,4 +1,4 @@
1
- utilities/__init__.py,sha256=Ed7ve87kqMRppt7gnxtu2swOV8yOK_hwjAE5eRBPgKg,61
1
+ utilities/__init__.py,sha256=MOUKZkLpGGyjxpjT7E-ok6qU-Ad2cKOMOyAdEHZoEGc,61
2
2
  utilities/aeventkit.py,sha256=OmDBhYGgbsKrB7cdC5FFpJHUatX9O76eTeKVVTksp2Y,12673
3
3
  utilities/altair.py,sha256=rUK99g9x6CYDDfiZrf-aTx5fSRbL1Q8ctgKORowzXHg,9060
4
4
  utilities/asyncio.py,sha256=aJySVxBY0gqsIYnoNmH7-1r8djKuf4vSsU69VCD08t8,16772
@@ -47,7 +47,7 @@ utilities/orjson.py,sha256=T_0SlK811ysg46d3orvIPY3JpBa4FRMpP2wlPQo7-gU,41854
47
47
  utilities/os.py,sha256=kjKKSQfnRqFTTZ315iavaaGd3gGuYNoSWlxVLCJjyQs,4852
48
48
  utilities/parse.py,sha256=g7Qm9eBOIeDId2tGA021CIaeF6jp1TI8rx4srdvlyoo,17937
49
49
  utilities/pathlib.py,sha256=EKZn-wWxH7MEWFrQGqHIoB-GJzyXeiEj8iDIgvkr8Wk,9325
50
- utilities/permissions.py,sha256=xw3mDS5UT3SO5_AZ0RXYMQi1hU-3LedAWHdHEsOAHu0,8697
50
+ utilities/permissions.py,sha256=vLXlWztSVYffbrxptne7ksj6dU1HLekm4fEvS4ny_4Q,8944
51
51
  utilities/pickle.py,sha256=MBT2xZCsv0pH868IXLGKnlcqNx2IRVKYNpRcqiQQqxw,653
52
52
  utilities/platform.py,sha256=0pYO5v7L2sU5UN87zHhEEhTKsZ9NIEM8N6UCr0F7bLY,2778
53
53
  utilities/polars.py,sha256=cNFBLWgOMUAp_Sz4xtlto17uZswZRrcfQYC95QKyaY4,87483
@@ -81,7 +81,7 @@ utilities/sqlalchemy.py,sha256=HQYpd7LFxdTF5WYVWYtCJeEBI71EJm7ytvCGyAH9B-U,37163
81
81
  utilities/sqlalchemy_polars.py,sha256=JCGhB37raSR7fqeWV5dTsciRTMVzIdVT9YSqKT0piT0,13370
82
82
  utilities/statsmodels.py,sha256=koyiBHvpMcSiBfh99wFUfSggLNx7cuAw3rwyfAhoKpQ,3410
83
83
  utilities/string.py,sha256=shmBK87zZwzGyixuNuXCiUbqzfeZ9xlrFwz6JTaRvDk,582
84
- utilities/subprocess.py,sha256=dIBguvLB0WepJKqgBxlzF6r8z9HW5JiGKLg-eslpuec,22549
84
+ utilities/subprocess.py,sha256=2FvELtLUkr8H2rMRMRr5Ah5LA-p3pZy9PjMeA_ZTw_U,26362
85
85
  utilities/tempfile.py,sha256=Lx6qa16lL1XVH6WdmD_G9vlN6gLI8nrIurxmsFkPKvg,3022
86
86
  utilities/testbook.py,sha256=j1KmaVbrX9VrbeMgtPh5gk55myAsn3dyRUn7jGbPbRk,1294
87
87
  utilities/text.py,sha256=7SvwcSR2l_5cOrm1samGnR4C-ZI6qyFLHLzSpO1zeHQ,13958
@@ -98,7 +98,7 @@ utilities/warnings.py,sha256=un1LvHv70PU-LLv8RxPVmugTzDJkkGXRMZTE2-fTQHw,1771
98
98
  utilities/whenever.py,sha256=F4ek0-OBWxHYrZdmoZt76N2RnNyKY5KrEHt7rqO4AQE,60183
99
99
  utilities/zipfile.py,sha256=24lQc9ATcJxHXBPc_tBDiJk48pWyRrlxO2fIsFxU0A8,699
100
100
  utilities/zoneinfo.py,sha256=tdIScrTB2-B-LH0ukb1HUXKooLknOfJNwHk10MuMYvA,3619
101
- dycw_utilities-0.174.11.dist-info/WHEEL,sha256=ZyFSCYkV2BrxH6-HRVRg3R9Fo7MALzer9KiPYqNxSbo,79
102
- dycw_utilities-0.174.11.dist-info/entry_points.txt,sha256=ykGI1ArwOPHqm2g5Cqh3ENdMxEej_a_FcOUov5EM5Oc,155
103
- dycw_utilities-0.174.11.dist-info/METADATA,sha256=tBWZRVdVIvaeaLE3LYeGlWuwUcbt9ZOm0kunrxZj42Y,1710
104
- dycw_utilities-0.174.11.dist-info/RECORD,,
101
+ dycw_utilities-0.174.13.dist-info/WHEEL,sha256=ZyFSCYkV2BrxH6-HRVRg3R9Fo7MALzer9KiPYqNxSbo,79
102
+ dycw_utilities-0.174.13.dist-info/entry_points.txt,sha256=ykGI1ArwOPHqm2g5Cqh3ENdMxEej_a_FcOUov5EM5Oc,155
103
+ dycw_utilities-0.174.13.dist-info/METADATA,sha256=BuUPpTMsSoLgdvmXiA-oOD_jsg6LYqlWsW_EFvJSTlM,1710
104
+ dycw_utilities-0.174.13.dist-info/RECORD,,
utilities/__init__.py CHANGED
@@ -1,3 +1,3 @@
1
1
  from __future__ import annotations
2
2
 
3
- __version__ = "0.174.11"
3
+ __version__ = "0.174.13"
utilities/permissions.py CHANGED
@@ -3,7 +3,9 @@ from __future__ import annotations
3
3
  from dataclasses import dataclass
4
4
  from functools import reduce
5
5
  from operator import or_
6
+ from pathlib import Path
6
7
  from stat import (
8
+ S_IMODE,
7
9
  S_IRGRP,
8
10
  S_IROTH,
9
11
  S_IRUSR,
@@ -14,12 +16,16 @@ from stat import (
14
16
  S_IXOTH,
15
17
  S_IXUSR,
16
18
  )
17
- from typing import Literal, Self, assert_never, override
19
+ from typing import TYPE_CHECKING, Literal, Self, assert_never, override
18
20
 
19
21
  from utilities.dataclasses import replace_non_sentinel
20
22
  from utilities.re import ExtractGroupsError, extract_groups
21
23
  from utilities.sentinel import Sentinel, sentinel
22
24
 
25
+ if TYPE_CHECKING:
26
+ from utilities.types import PathLike
27
+
28
+
23
29
  type PermissionsLike = Permissions | int | str
24
30
 
25
31
 
@@ -154,6 +160,10 @@ class Permissions:
154
160
  )
155
161
  raise PermissionsFromIntError(n=n)
156
162
 
163
+ @classmethod
164
+ def from_path(cls, path: PathLike, /) -> Self:
165
+ return cls.from_int(S_IMODE(Path(path).stat().st_mode))
166
+
157
167
  @classmethod
158
168
  def from_text(cls, text: str, /) -> Self:
159
169
  try:
utilities/subprocess.py CHANGED
@@ -1,11 +1,13 @@
1
1
  from __future__ import annotations
2
2
 
3
+ import shutil
3
4
  import sys
4
5
  from contextlib import contextmanager
5
6
  from dataclasses import dataclass
6
7
  from io import StringIO
7
8
  from pathlib import Path
8
9
  from shlex import join
10
+ from shutil import copyfile, copytree, move, rmtree
9
11
  from string import Template
10
12
  from subprocess import PIPE, CalledProcessError, Popen
11
13
  from threading import Thread
@@ -15,12 +17,14 @@ from typing import IO, TYPE_CHECKING, Literal, assert_never, overload, override
15
17
  from utilities.errors import ImpossibleCaseError
16
18
  from utilities.iterables import always_iterable
17
19
  from utilities.logging import to_logger
20
+ from utilities.permissions import ensure_perms
18
21
  from utilities.text import strip_and_dedent
19
22
  from utilities.whenever import to_seconds
20
23
 
21
24
  if TYPE_CHECKING:
22
25
  from collections.abc import Iterator
23
26
 
27
+ from utilities.permissions import PermissionsLike
24
28
  from utilities.types import (
25
29
  LoggerLike,
26
30
  MaybeIterable,
@@ -64,24 +68,67 @@ def cd_cmd(path: PathLike, /) -> list[str]:
64
68
  ##
65
69
 
66
70
 
67
- def chmod_cmd(path: PathLike, mode: str, /) -> list[str]:
68
- return ["chmod", mode, str(path)]
71
+ def chmod(path: PathLike, perms: PermissionsLike, /, *, sudo: bool = False) -> None:
72
+ if sudo: # pragma: no cover
73
+ run(*sudo_cmd(*chmod_cmd(path, perms)))
74
+ else:
75
+ Path(path).chmod(int(ensure_perms(perms)))
76
+
77
+
78
+ ##
79
+
80
+
81
+ def chmod_cmd(path: PathLike, perms: PermissionsLike, /) -> list[str]:
82
+ return ["chmod", str(ensure_perms(perms)), str(path)]
83
+
84
+
85
+ ##
86
+
87
+
88
+ def chown(
89
+ path: PathLike,
90
+ /,
91
+ *,
92
+ sudo: bool = False,
93
+ user: str | int | None = None,
94
+ group: str | int | None = None,
95
+ ) -> None:
96
+ if sudo: # pragma: no cover
97
+ match user, group:
98
+ case None, None:
99
+ ...
100
+ case str() | int() | None, str() | int() | None:
101
+ run(*sudo_cmd(*chown_cmd(path, user=user, group=group)))
102
+ case never:
103
+ assert_never(never)
104
+ else:
105
+ match user, group:
106
+ case None, None:
107
+ ...
108
+ case str() | int(), None:
109
+ shutil.chown(path, user, group)
110
+ case None, str() | int():
111
+ shutil.chown(path, user, group)
112
+ case str() | int(), str() | int():
113
+ shutil.chown(path, user, group)
114
+ case never:
115
+ assert_never(never)
69
116
 
70
117
 
71
118
  ##
72
119
 
73
120
 
74
121
  def chown_cmd(
75
- path: PathLike, /, *, user: str | None = None, group: str | None = None
122
+ path: PathLike, /, *, user: str | int | None = None, group: str | int | None = None
76
123
  ) -> list[str]:
77
124
  match user, group:
78
125
  case None, None:
79
126
  raise ChownCmdError
80
- case str(), None:
127
+ case str() | int(), None:
81
128
  ownership = "user"
82
- case None, str():
129
+ case None, str() | int():
83
130
  ownership = f":{group}"
84
- case str(), str():
131
+ case str() | int(), str() | int():
85
132
  ownership = f"{user}:{group}"
86
133
  case never:
87
134
  assert_never(never)
@@ -98,6 +145,47 @@ class ChownCmdError(Exception):
98
145
  ##
99
146
 
100
147
 
148
+ def copy_file(
149
+ src: PathLike,
150
+ dest: PathLike,
151
+ /,
152
+ *,
153
+ sudo: bool = False,
154
+ perms: PermissionsLike | None = None,
155
+ owner: str | int | None = None,
156
+ group: str | int | None = None,
157
+ ) -> None:
158
+ """Copy a file/directory from one location to another."""
159
+ mkdir(dest, sudo=sudo, parent=True)
160
+ if sudo:
161
+ run(*sudo_cmd(*cp_cmd(src, dest)))
162
+ else:
163
+ src, dest = map(Path, [src, dest])
164
+ if src.is_file():
165
+ _ = copyfile(src, dest)
166
+ elif src.is_dir():
167
+ _ = copytree(src, dest, dirs_exist_ok=True)
168
+ else:
169
+ raise CopyFileError(src=src, dest=dest)
170
+ if perms is not None:
171
+ chmod(dest, perms, sudo=sudo)
172
+ if (owner is not None) or (group is not None):
173
+ chown(dest, sudo=sudo, user=owner, group=group)
174
+
175
+
176
+ @dataclass(kw_only=True, slots=True)
177
+ class CopyFileError(Exception):
178
+ src: Path
179
+ dest: Path
180
+
181
+ @override
182
+ def __str__(self) -> str:
183
+ return f"Unable to copy {str(self.src)!r} to {str(self.dest)!r}; source does not exist"
184
+
185
+
186
+ ##
187
+
188
+
101
189
  def cp_cmd(src: PathLike, dest: PathLike, /) -> list[str]:
102
190
  return ["cp", "-r", str(src), str(dest)]
103
191
 
@@ -173,6 +261,45 @@ def mkdir_cmd(path: PathLike, /, *, parent: bool = False) -> list[str]:
173
261
  ##
174
262
 
175
263
 
264
+ def move_file(
265
+ src: PathLike,
266
+ dest: PathLike,
267
+ /,
268
+ *,
269
+ sudo: bool = False,
270
+ perms: PermissionsLike | None = None,
271
+ owner: str | int | None = None,
272
+ group: str | int | None = None,
273
+ ) -> None:
274
+ """Move a file/directory from one location to another."""
275
+ mkdir(dest, sudo=sudo, parent=True)
276
+ if sudo:
277
+ run(*sudo_cmd(*cp_cmd(src, dest)))
278
+ else:
279
+ src, dest = map(Path, [src, dest])
280
+ if src.exists():
281
+ _ = move(src, dest)
282
+ else:
283
+ raise MoveFileError(src=src, dest=dest)
284
+ if perms is not None:
285
+ chmod(dest, perms, sudo=sudo)
286
+ if (owner is not None) or (group is not None):
287
+ chown(dest, sudo=sudo, user=owner, group=group)
288
+
289
+
290
+ @dataclass(kw_only=True, slots=True)
291
+ class MoveFileError(Exception):
292
+ src: Path
293
+ dest: Path
294
+
295
+ @override
296
+ def __str__(self) -> str:
297
+ return f"Unable to move {str(self.src)!r} to {str(self.dest)!r}; source does not exist"
298
+
299
+
300
+ ##
301
+
302
+
176
303
  def mv_cmd(src: PathLike, dest: PathLike, /) -> list[str]:
177
304
  return ["mv", str(src), str(dest)]
178
305
 
@@ -180,6 +307,20 @@ def mv_cmd(src: PathLike, dest: PathLike, /) -> list[str]:
180
307
  ##
181
308
 
182
309
 
310
+ def remove(path: PathLike, /, *, sudo: bool = False) -> None:
311
+ if sudo: # pragma: no cover
312
+ run(*sudo_cmd(*rm_cmd(path)))
313
+ else:
314
+ path = Path(path)
315
+ if path.is_file():
316
+ path.unlink(missing_ok=True)
317
+ elif path.is_dir():
318
+ rmtree(path, ignore_errors=True)
319
+
320
+
321
+ ##
322
+
323
+
183
324
  def rm_cmd(path: PathLike, /) -> list[str]:
184
325
  return ["rm", "-rf", str(path)]
185
326
 
@@ -833,10 +974,15 @@ __all__ = [
833
974
  "RESTART_SSHD",
834
975
  "UPDATE_CA_CERTIFICATES",
835
976
  "ChownCmdError",
977
+ "CopyFileError",
978
+ "MoveFileError",
836
979
  "apt_install_cmd",
837
980
  "cd_cmd",
981
+ "chmod",
838
982
  "chmod_cmd",
983
+ "chown",
839
984
  "chown_cmd",
985
+ "copy_file",
840
986
  "cp_cmd",
841
987
  "echo_cmd",
842
988
  "expand_path",
@@ -846,7 +992,9 @@ __all__ = [
846
992
  "maybe_sudo_cmd",
847
993
  "mkdir",
848
994
  "mkdir_cmd",
995
+ "move_file",
849
996
  "mv_cmd",
997
+ "remove",
850
998
  "rm_cmd",
851
999
  "rsync",
852
1000
  "rsync_cmd",