simple-bcp 2.3.2__tar.gz → 2.4.0__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.
- {simple_bcp-2.3.2 → simple_bcp-2.4.0}/PKG-INFO +2 -1
- {simple_bcp-2.3.2 → simple_bcp-2.4.0}/pyproject.toml +1 -1
- {simple_bcp-2.3.2 → simple_bcp-2.4.0}/simple_bcp/_bcp.py +59 -11
- {simple_bcp-2.3.2 → simple_bcp-2.4.0}/LICENSE +0 -0
- {simple_bcp-2.3.2 → simple_bcp-2.4.0}/README.md +0 -0
- {simple_bcp-2.3.2 → simple_bcp-2.4.0}/simple_bcp/__init__.py +0 -0
- {simple_bcp-2.3.2 → simple_bcp-2.4.0}/simple_bcp/_connection_parameters.py +0 -0
- {simple_bcp-2.3.2 → simple_bcp-2.4.0}/simple_bcp/_encoding.py +0 -0
- {simple_bcp-2.3.2 → simple_bcp-2.4.0}/simple_bcp/_options.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: simple_bcp
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.4.0
|
|
4
4
|
Summary: A Simple yet powerful bcp wrapper
|
|
5
5
|
License: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -15,6 +15,7 @@ Classifier: Programming Language :: Python :: 3.11
|
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.12
|
|
16
16
|
Classifier: Programming Language :: Python :: 3.13
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
18
19
|
Requires-Dist: annotated-types (>=0.7.0,<0.8.0)
|
|
19
20
|
Requires-Dist: packaging (>=25.0,<26.0)
|
|
20
21
|
Requires-Dist: pydantic (>=2.11,<3.0)
|
|
@@ -4,6 +4,7 @@ import pathlib
|
|
|
4
4
|
import shlex
|
|
5
5
|
import shutil
|
|
6
6
|
import subprocess
|
|
7
|
+
from collections.abc import Collection
|
|
7
8
|
from datetime import datetime
|
|
8
9
|
from typing import Any, Literal
|
|
9
10
|
|
|
@@ -21,6 +22,9 @@ class _BcpMode(enum.Enum):
|
|
|
21
22
|
FORMAT = "format"
|
|
22
23
|
|
|
23
24
|
|
|
25
|
+
_MASK = "***"
|
|
26
|
+
|
|
27
|
+
|
|
24
28
|
def _resolve_to_str(value: str | bytes | None | Any) -> str:
|
|
25
29
|
if isinstance(value, bytes):
|
|
26
30
|
value = value.decode()
|
|
@@ -60,20 +64,36 @@ class BCP:
|
|
|
60
64
|
>>> print("the data is now copied to dest_table_name")
|
|
61
65
|
"""
|
|
62
66
|
|
|
63
|
-
def __init__(
|
|
67
|
+
def __init__(
|
|
68
|
+
self,
|
|
69
|
+
*,
|
|
70
|
+
bcp_executable_path: pathlib.Path | str | None = None,
|
|
71
|
+
secret_values: Collection[str] = (),
|
|
72
|
+
):
|
|
64
73
|
"""
|
|
65
74
|
:param bcp_executable_path:
|
|
66
75
|
Path to the `bcp` executable.
|
|
67
76
|
Defaults to `bcp`, which relies on the system's PATH environment variable.
|
|
68
77
|
Upon init, `bcp -v` command will be run.
|
|
78
|
+
:param secret_values:
|
|
79
|
+
Values to mask, wherever they appear, in the logged command and in `BcpProcessError`.
|
|
80
|
+
The password in `MsSqlDatabaseParameters` is always masked automatically,
|
|
81
|
+
so use this for any other secret, for example one inside a query.
|
|
69
82
|
"""
|
|
70
83
|
self._init_logger()
|
|
84
|
+
self._init_secret_values(secret_values=secret_values)
|
|
71
85
|
self._init_executable_path(executable_path=bcp_executable_path)
|
|
72
86
|
self._init_bcp_version()
|
|
73
87
|
|
|
74
88
|
def _init_logger(self):
|
|
75
89
|
self._logger = logging.getLogger(self.__class__.__name__)
|
|
76
90
|
|
|
91
|
+
def _init_secret_values(self, *, secret_values: Collection[str]):
|
|
92
|
+
if isinstance(secret_values, str):
|
|
93
|
+
# a string is a collection of its characters, which would mask every one of them
|
|
94
|
+
raise TypeError("secret_values must be a collection of strings, not a single string")
|
|
95
|
+
self._secret_values = tuple(secret_values)
|
|
96
|
+
|
|
77
97
|
def _init_executable_path(self, *, executable_path: pathlib.Path | str | None):
|
|
78
98
|
if executable_path is None:
|
|
79
99
|
default = shutil.which("bcp")
|
|
@@ -103,19 +123,47 @@ class BCP:
|
|
|
103
123
|
self._bcp_version = packaging.version.parse(raw_version)
|
|
104
124
|
self._logger.debug(f"BCP version: {self._bcp_version}", extra={"bcp_version": str(self._bcp_version)})
|
|
105
125
|
|
|
106
|
-
|
|
126
|
+
@staticmethod
|
|
127
|
+
def _mask_secrets(command: list[str], *, secret_values: Collection[str]) -> list[str]:
|
|
128
|
+
"""
|
|
129
|
+
Masks by value, wherever it appears: a secret inside a larger argument (a query, say) is masked too.
|
|
130
|
+
Known limit: if two secrets overlap without one containing the other (`56` and `678` in `1234567890`),
|
|
131
|
+
the longer is masked first and a fragment of the other stays visible (`12345***90`).
|
|
132
|
+
"""
|
|
133
|
+
# an empty secret would match between every two characters, and a longer secret that contains a shorter one
|
|
134
|
+
# would be broken up if the shorter one went first
|
|
135
|
+
secrets = sorted(filter(None, secret_values), key=len, reverse=True)
|
|
136
|
+
return [BCP._mask_text(argument, secrets=secrets) for argument in command]
|
|
137
|
+
|
|
138
|
+
@staticmethod
|
|
139
|
+
def _mask_text(text: str, *, secrets: list[str]) -> str:
|
|
140
|
+
for secret in secrets:
|
|
141
|
+
text = text.replace(secret, _MASK)
|
|
142
|
+
return text
|
|
143
|
+
|
|
144
|
+
def _log_command(self, loggable_command: str) -> None:
|
|
145
|
+
"""
|
|
146
|
+
Logs `loggable_command` as it is.
|
|
147
|
+
WARNING: pass the output of `_mask_secrets`, never the unmasked command, which holds the secrets.
|
|
148
|
+
"""
|
|
149
|
+
self._logger.debug(f"Running command: `{loggable_command}`", extra={"bcp_command": loggable_command})
|
|
150
|
+
|
|
151
|
+
def _run_bcp_command(self, command_args: list[str], *, secret_values: Collection[str] = ()) -> str:
|
|
107
152
|
command = [self._executable_path.as_posix()] + command_args
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
153
|
+
masked_command = self._mask_secrets(command, secret_values=[*self._secret_values, *secret_values])
|
|
154
|
+
loggable_command = shlex.join(masked_command)
|
|
155
|
+
self._log_command(loggable_command)
|
|
156
|
+
# check=False: the `CalledProcessError` that `check=True` raises holds the unmasked command
|
|
157
|
+
result = subprocess.run(command, capture_output=True, check=False)
|
|
158
|
+
if result.returncode != 0:
|
|
159
|
+
raise BcpProcessError(
|
|
160
|
+
returncode=result.returncode, cmd=masked_command, output=result.stdout, stderr=result.stderr
|
|
161
|
+
)
|
|
114
162
|
stdout = _resolve_to_str(result.stdout)
|
|
115
163
|
stderr = _resolve_to_str(result.stderr)
|
|
116
164
|
self._logger.debug(
|
|
117
165
|
f"BCP output: stdout={stdout} ; stderr={stderr}",
|
|
118
|
-
extra={"bcp_stdout": stdout, "bcp_stderr": stderr, "bcp_command":
|
|
166
|
+
extra={"bcp_stdout": stdout, "bcp_stderr": stderr, "bcp_command": loggable_command},
|
|
119
167
|
)
|
|
120
168
|
return stdout
|
|
121
169
|
|
|
@@ -209,7 +257,7 @@ class BCP:
|
|
|
209
257
|
bcp_options=bcp_options,
|
|
210
258
|
bcp_encoding_settings=bcp_encoding_settings,
|
|
211
259
|
)
|
|
212
|
-
self._run_bcp_command(command_args)
|
|
260
|
+
self._run_bcp_command(command_args, secret_values=[database_parameters.password])
|
|
213
261
|
return output_file_path
|
|
214
262
|
|
|
215
263
|
def download_table(
|
|
@@ -358,4 +406,4 @@ class BCP:
|
|
|
358
406
|
bcp_options=bcp_options,
|
|
359
407
|
bcp_encoding_settings=bcp_encoding_settings,
|
|
360
408
|
)
|
|
361
|
-
self._run_bcp_command(command_args)
|
|
409
|
+
self._run_bcp_command(command_args, secret_values=[database_parameters.password])
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|