edq-utils 0.2.3__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.
Potentially problematic release.
This version of edq-utils might be problematic. Click here for more details.
- edq/__init__.py +5 -0
- edq/cli/__init__.py +0 -0
- edq/cli/__main__.py +17 -0
- edq/cli/config/__init__.py +3 -0
- edq/cli/config/__main__.py +15 -0
- edq/cli/config/list.py +69 -0
- edq/cli/http/__init__.py +3 -0
- edq/cli/http/__main__.py +15 -0
- edq/cli/http/exchange-server.py +71 -0
- edq/cli/http/send-exchange.py +45 -0
- edq/cli/http/verify-exchanges.py +38 -0
- edq/cli/testing/__init__.py +3 -0
- edq/cli/testing/__main__.py +15 -0
- edq/cli/testing/cli-test.py +49 -0
- edq/cli/version.py +28 -0
- edq/core/__init__.py +0 -0
- edq/core/argparser.py +137 -0
- edq/core/argparser_test.py +124 -0
- edq/core/config.py +268 -0
- edq/core/config_test.py +1038 -0
- edq/core/log.py +101 -0
- edq/core/version.py +6 -0
- edq/procedure/__init__.py +0 -0
- edq/procedure/verify_exchanges.py +85 -0
- edq/py.typed +0 -0
- edq/testing/__init__.py +3 -0
- edq/testing/asserts.py +65 -0
- edq/testing/cli.py +360 -0
- edq/testing/cli_test.py +15 -0
- edq/testing/httpserver.py +578 -0
- edq/testing/httpserver_test.py +424 -0
- edq/testing/run.py +142 -0
- edq/testing/testdata/cli/data/configs/empty/edq-config.json +1 -0
- edq/testing/testdata/cli/data/configs/simple-1/edq-config.json +4 -0
- edq/testing/testdata/cli/data/configs/simple-2/edq-config.json +3 -0
- edq/testing/testdata/cli/data/configs/value-number/edq-config.json +3 -0
- edq/testing/testdata/cli/tests/config/list/config_list_base.txt +16 -0
- edq/testing/testdata/cli/tests/config/list/config_list_config_value_number.txt +10 -0
- edq/testing/testdata/cli/tests/config/list/config_list_ignore_config.txt +14 -0
- edq/testing/testdata/cli/tests/config/list/config_list_no_config.txt +8 -0
- edq/testing/testdata/cli/tests/config/list/config_list_show_origin.txt +13 -0
- edq/testing/testdata/cli/tests/config/list/config_list_skip_header.txt +10 -0
- edq/testing/testdata/cli/tests/help_base.txt +9 -0
- edq/testing/testdata/cli/tests/platform_skip.txt +5 -0
- edq/testing/testdata/cli/tests/version_base.txt +6 -0
- edq/testing/testdata/http/exchanges/simple.httpex.json +5 -0
- edq/testing/testdata/http/exchanges/simple_anchor.httpex.json +5 -0
- edq/testing/testdata/http/exchanges/simple_file.httpex.json +10 -0
- edq/testing/testdata/http/exchanges/simple_file_binary.httpex.json +10 -0
- edq/testing/testdata/http/exchanges/simple_file_get_params.httpex.json +14 -0
- edq/testing/testdata/http/exchanges/simple_file_multiple.httpex.json +13 -0
- edq/testing/testdata/http/exchanges/simple_file_name.httpex.json +11 -0
- edq/testing/testdata/http/exchanges/simple_file_post_multiple.httpex.json +13 -0
- edq/testing/testdata/http/exchanges/simple_file_post_params.httpex.json +14 -0
- edq/testing/testdata/http/exchanges/simple_headers.httpex.json +8 -0
- edq/testing/testdata/http/exchanges/simple_jsonresponse_dict.httpex.json +7 -0
- edq/testing/testdata/http/exchanges/simple_jsonresponse_list.httpex.json +9 -0
- edq/testing/testdata/http/exchanges/simple_params.httpex.json +9 -0
- edq/testing/testdata/http/exchanges/simple_post.httpex.json +5 -0
- edq/testing/testdata/http/exchanges/simple_post_params.httpex.json +9 -0
- edq/testing/testdata/http/exchanges/simple_post_urlparams.httpex.json +5 -0
- edq/testing/testdata/http/exchanges/simple_urlparams.httpex.json +5 -0
- edq/testing/testdata/http/exchanges/specialcase_listparams_explicit.httpex.json +8 -0
- edq/testing/testdata/http/exchanges/specialcase_listparams_url.httpex.json +5 -0
- edq/testing/testdata/http/files/a.txt +1 -0
- edq/testing/testdata/http/files/tiny.png +0 -0
- edq/testing/unittest.py +88 -0
- edq/util/__init__.py +3 -0
- edq/util/cli.py +151 -0
- edq/util/dirent.py +346 -0
- edq/util/dirent_test.py +1004 -0
- edq/util/encoding.py +18 -0
- edq/util/hash.py +41 -0
- edq/util/hash_test.py +89 -0
- edq/util/json.py +180 -0
- edq/util/json_test.py +228 -0
- edq/util/net.py +1047 -0
- edq/util/parse.py +33 -0
- edq/util/pyimport.py +94 -0
- edq/util/pyimport_test.py +119 -0
- edq/util/reflection.py +32 -0
- edq/util/time.py +75 -0
- edq/util/time_test.py +107 -0
- edq_utils-0.2.3.dist-info/METADATA +164 -0
- edq_utils-0.2.3.dist-info/RECORD +88 -0
- edq_utils-0.2.3.dist-info/WHEEL +5 -0
- edq_utils-0.2.3.dist-info/licenses/LICENSE +21 -0
- edq_utils-0.2.3.dist-info/top_level.txt +1 -0
edq/util/dirent.py
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Operations relating to directory entries (dirents).
|
|
3
|
+
|
|
4
|
+
These operations are designed for clarity and compatibility, not performance.
|
|
5
|
+
|
|
6
|
+
Only directories, files, and links will be handled.
|
|
7
|
+
Other types of dirents may result in an error being raised.
|
|
8
|
+
|
|
9
|
+
In general, all recursive operations do not follow symlinks by default and instead treat the link as a file.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import atexit
|
|
13
|
+
import os
|
|
14
|
+
import shutil
|
|
15
|
+
import tempfile
|
|
16
|
+
import typing
|
|
17
|
+
import uuid
|
|
18
|
+
|
|
19
|
+
DEFAULT_ENCODING: str = 'utf-8'
|
|
20
|
+
""" The default encoding that will be used when reading and writing. """
|
|
21
|
+
|
|
22
|
+
DEPTH_LIMIT: int = 10000
|
|
23
|
+
|
|
24
|
+
def exists(path: str) -> bool:
|
|
25
|
+
"""
|
|
26
|
+
Check if a path exists.
|
|
27
|
+
This will transparently call os.path.lexists(),
|
|
28
|
+
which will include broken links.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
return os.path.lexists(path)
|
|
32
|
+
|
|
33
|
+
def get_temp_path(prefix: str = '', suffix: str = '', rm: bool = True) -> str:
|
|
34
|
+
"""
|
|
35
|
+
Get a path to a valid (but not currently existing) temp dirent.
|
|
36
|
+
If rm is True, then the dirent will be attempted to be deleted on exit
|
|
37
|
+
(no error will occur if the path is not there).
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
path = None
|
|
41
|
+
while ((path is None) or exists(path)):
|
|
42
|
+
path = os.path.join(tempfile.gettempdir(), prefix + str(uuid.uuid4()) + suffix)
|
|
43
|
+
|
|
44
|
+
path = os.path.realpath(path)
|
|
45
|
+
|
|
46
|
+
if (rm):
|
|
47
|
+
atexit.register(remove, path)
|
|
48
|
+
|
|
49
|
+
return path
|
|
50
|
+
|
|
51
|
+
def get_temp_dir(prefix: str = '', suffix: str = '', rm: bool = True) -> str:
|
|
52
|
+
"""
|
|
53
|
+
Get a temp directory.
|
|
54
|
+
The directory will exist when returned.
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
path = get_temp_path(prefix = prefix, suffix = suffix, rm = rm)
|
|
58
|
+
mkdir(path)
|
|
59
|
+
return path
|
|
60
|
+
|
|
61
|
+
def mkdir(raw_path: str) -> None:
|
|
62
|
+
"""
|
|
63
|
+
Make a directory (including any required parent directories).
|
|
64
|
+
Does not complain if the directory (or parents) already exist
|
|
65
|
+
(this includes if the directory or parents are links to directories).
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
path = os.path.abspath(raw_path)
|
|
69
|
+
|
|
70
|
+
if (exists(path)):
|
|
71
|
+
if (os.path.isdir(path)):
|
|
72
|
+
return
|
|
73
|
+
|
|
74
|
+
raise ValueError(f"Target of mkdir already exists, and is not a dir: '{raw_path}'.")
|
|
75
|
+
|
|
76
|
+
_check_parent_dirs(raw_path)
|
|
77
|
+
|
|
78
|
+
os.makedirs(path, exist_ok = True)
|
|
79
|
+
|
|
80
|
+
def _check_parent_dirs(raw_path: str) -> None:
|
|
81
|
+
"""
|
|
82
|
+
Check all parents to ensure that they are all dirs (or don't exist).
|
|
83
|
+
This is naturally handled by os.makedirs(),
|
|
84
|
+
but the error messages are not consistent between POSIX and Windows.
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
path = os.path.abspath(raw_path)
|
|
88
|
+
|
|
89
|
+
parent_path = path
|
|
90
|
+
for _ in range(DEPTH_LIMIT):
|
|
91
|
+
new_parent_path = os.path.dirname(parent_path)
|
|
92
|
+
if (parent_path == new_parent_path):
|
|
93
|
+
# We have reached root (are our own parent).
|
|
94
|
+
return
|
|
95
|
+
|
|
96
|
+
parent_path = new_parent_path
|
|
97
|
+
|
|
98
|
+
if (os.path.exists(parent_path) and (not os.path.isdir(parent_path))):
|
|
99
|
+
raise ValueError(f"Target of mkdir contains parent ('{os.path.basename(parent_path)}') that exists and is not a dir: '{raw_path}'.")
|
|
100
|
+
|
|
101
|
+
raise ValueError("Depth limit reached.")
|
|
102
|
+
|
|
103
|
+
def remove(path: str) -> None:
|
|
104
|
+
"""
|
|
105
|
+
Remove the given path.
|
|
106
|
+
The path can be of any type (dir, file, link),
|
|
107
|
+
and does not need to exist.
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
if (not exists(path)):
|
|
111
|
+
return
|
|
112
|
+
|
|
113
|
+
if (os.path.isfile(path) or os.path.islink(path)):
|
|
114
|
+
os.remove(path)
|
|
115
|
+
elif (os.path.isdir(path)):
|
|
116
|
+
shutil.rmtree(path)
|
|
117
|
+
else:
|
|
118
|
+
raise ValueError(f"Unknown type of dirent: '{path}'.")
|
|
119
|
+
|
|
120
|
+
def same(a: str, b: str) -> bool:
|
|
121
|
+
"""
|
|
122
|
+
Check if two paths represent the same dirent.
|
|
123
|
+
If either (or both) paths do not exist, false will be returned.
|
|
124
|
+
If either paths are links, they are resolved before checking
|
|
125
|
+
(so a link and the target file are considered the "same").
|
|
126
|
+
"""
|
|
127
|
+
|
|
128
|
+
return (exists(a) and exists(b) and os.path.samefile(a, b))
|
|
129
|
+
|
|
130
|
+
def move(raw_source: str, raw_dest: str, no_clobber: bool = False) -> None:
|
|
131
|
+
"""
|
|
132
|
+
Move the source dirent to the given destination.
|
|
133
|
+
Any existing destination will be removed before moving.
|
|
134
|
+
"""
|
|
135
|
+
|
|
136
|
+
source = os.path.abspath(raw_source)
|
|
137
|
+
dest = os.path.abspath(raw_dest)
|
|
138
|
+
|
|
139
|
+
if (not exists(source)):
|
|
140
|
+
raise ValueError(f"Source of move does not exist: '{raw_source}'.")
|
|
141
|
+
|
|
142
|
+
# If dest is a dir, then resolve the path.
|
|
143
|
+
if (os.path.isdir(dest)):
|
|
144
|
+
dest = os.path.abspath(os.path.join(dest, os.path.basename(source)))
|
|
145
|
+
|
|
146
|
+
# Skip if this is self.
|
|
147
|
+
if (same(source, dest)):
|
|
148
|
+
return
|
|
149
|
+
|
|
150
|
+
# Check for clobber.
|
|
151
|
+
if (exists(dest)):
|
|
152
|
+
if (no_clobber):
|
|
153
|
+
raise ValueError(f"Destination of move already exists: '{raw_dest}'.")
|
|
154
|
+
|
|
155
|
+
remove(dest)
|
|
156
|
+
|
|
157
|
+
# Create any required parents.
|
|
158
|
+
os.makedirs(os.path.dirname(dest), exist_ok = True)
|
|
159
|
+
|
|
160
|
+
shutil.move(source, dest)
|
|
161
|
+
|
|
162
|
+
def copy(raw_source: str, raw_dest: str, no_clobber: bool = False) -> None:
|
|
163
|
+
"""
|
|
164
|
+
Copy a dirent or directory to a destination.
|
|
165
|
+
|
|
166
|
+
The destination will be overwritten if it exists (and no_clobber is false).
|
|
167
|
+
For copying the contents of a directory INTO another directory, use copy_contents().
|
|
168
|
+
|
|
169
|
+
No copy is made if the source and dest refer to the same dirent.
|
|
170
|
+
"""
|
|
171
|
+
|
|
172
|
+
source = os.path.abspath(raw_source)
|
|
173
|
+
dest = os.path.abspath(raw_dest)
|
|
174
|
+
|
|
175
|
+
if (same(source, dest)):
|
|
176
|
+
return
|
|
177
|
+
|
|
178
|
+
if (not exists(source)):
|
|
179
|
+
raise ValueError(f"Source of copy does not exist: '{raw_source}'.")
|
|
180
|
+
|
|
181
|
+
if (contains_path(source, dest)):
|
|
182
|
+
raise ValueError(f"Source of copy cannot contain the destination. Source: '{raw_source}', Destination: '{raw_dest}'.")
|
|
183
|
+
|
|
184
|
+
if (contains_path(dest, source)):
|
|
185
|
+
raise ValueError(f"Destination of copy cannot contain the source. Destination: '{raw_dest}', Source: '{raw_source}'.")
|
|
186
|
+
|
|
187
|
+
if (exists(dest)):
|
|
188
|
+
if (no_clobber):
|
|
189
|
+
raise ValueError(f"Destination of copy already exists: '{raw_dest}'.")
|
|
190
|
+
|
|
191
|
+
remove(dest)
|
|
192
|
+
|
|
193
|
+
mkdir(os.path.dirname(dest))
|
|
194
|
+
|
|
195
|
+
if (os.path.islink(source)):
|
|
196
|
+
# shutil.copy2() can generally handle (broken) links, but Windows is inconsistent (between 3.11 and 3.12) on link handling.
|
|
197
|
+
link_target = os.readlink(source)
|
|
198
|
+
os.symlink(link_target, dest)
|
|
199
|
+
elif (os.path.isfile(source)):
|
|
200
|
+
shutil.copy2(source, dest, follow_symlinks = False)
|
|
201
|
+
elif (os.path.isdir(source)):
|
|
202
|
+
mkdir(dest)
|
|
203
|
+
|
|
204
|
+
for child in sorted(os.listdir(source)):
|
|
205
|
+
copy(os.path.join(raw_source, child), os.path.join(raw_dest, child))
|
|
206
|
+
else:
|
|
207
|
+
raise ValueError(f"Source of copy is not a dir, fie, or link: '{raw_source}'.")
|
|
208
|
+
|
|
209
|
+
def copy_contents(raw_source: str, raw_dest: str, no_clobber: bool = False) -> None:
|
|
210
|
+
"""
|
|
211
|
+
Copy a file or the contents of a directory (excluding the top-level directory itself) into a destination.
|
|
212
|
+
If the destination exists, it must be a directory.
|
|
213
|
+
|
|
214
|
+
The source and destination should not be the same file.
|
|
215
|
+
|
|
216
|
+
For a file, this is equivalent to `mkdir -p dest && cp source dest`
|
|
217
|
+
For a dir, this is equivalent to `mkdir -p dest && cp -r source/* dest`
|
|
218
|
+
"""
|
|
219
|
+
|
|
220
|
+
source = os.path.abspath(raw_source)
|
|
221
|
+
dest = os.path.abspath(raw_dest)
|
|
222
|
+
|
|
223
|
+
if (same(source, dest)):
|
|
224
|
+
raise ValueError(f"Source and destination of contents copy cannot be the same: '{raw_source}'.")
|
|
225
|
+
|
|
226
|
+
if (exists(dest) and (not os.path.isdir(dest))):
|
|
227
|
+
raise ValueError(f"Destination of contents copy exists and is not a dir: '{raw_dest}'.")
|
|
228
|
+
|
|
229
|
+
mkdir(dest)
|
|
230
|
+
|
|
231
|
+
if (os.path.isfile(source) or os.path.islink(source)):
|
|
232
|
+
copy(source, os.path.join(dest, os.path.basename(source)), no_clobber = no_clobber)
|
|
233
|
+
elif (os.path.isdir(source)):
|
|
234
|
+
for child in sorted(os.listdir(source)):
|
|
235
|
+
copy(os.path.join(raw_source, child), os.path.join(raw_dest, child), no_clobber = no_clobber)
|
|
236
|
+
else:
|
|
237
|
+
raise ValueError(f"Source of contents copy is not a dir, fie, or link: '{raw_source}'.")
|
|
238
|
+
|
|
239
|
+
def read_file(raw_path: str, strip: bool = True, encoding: str = DEFAULT_ENCODING) -> str:
|
|
240
|
+
""" Read the contents of a file. """
|
|
241
|
+
|
|
242
|
+
path = os.path.abspath(raw_path)
|
|
243
|
+
|
|
244
|
+
if (not exists(path)):
|
|
245
|
+
raise ValueError(f"Source of read does not exist: '{raw_path}'.")
|
|
246
|
+
|
|
247
|
+
with open(path, 'r', encoding = encoding) as file:
|
|
248
|
+
contents = file.read()
|
|
249
|
+
|
|
250
|
+
if (strip):
|
|
251
|
+
contents = contents.strip()
|
|
252
|
+
|
|
253
|
+
return contents
|
|
254
|
+
|
|
255
|
+
def write_file(
|
|
256
|
+
raw_path: str, contents: typing.Union[str, None],
|
|
257
|
+
strip: bool = True, newline: bool = True,
|
|
258
|
+
encoding: str = DEFAULT_ENCODING,
|
|
259
|
+
no_clobber: bool = False) -> None:
|
|
260
|
+
"""
|
|
261
|
+
Write the contents of a file.
|
|
262
|
+
If clobbering, any existing dirent will be removed before write.
|
|
263
|
+
"""
|
|
264
|
+
|
|
265
|
+
path = os.path.abspath(raw_path)
|
|
266
|
+
|
|
267
|
+
if (exists(path)):
|
|
268
|
+
if (no_clobber):
|
|
269
|
+
raise ValueError(f"Destination of write already exists: '{raw_path}'.")
|
|
270
|
+
|
|
271
|
+
remove(path)
|
|
272
|
+
|
|
273
|
+
if (contents is None):
|
|
274
|
+
contents = ''
|
|
275
|
+
|
|
276
|
+
if (strip):
|
|
277
|
+
contents = contents.strip()
|
|
278
|
+
|
|
279
|
+
if (newline):
|
|
280
|
+
contents += "\n"
|
|
281
|
+
|
|
282
|
+
with open(path, 'w', encoding = encoding) as file:
|
|
283
|
+
file.write(contents)
|
|
284
|
+
|
|
285
|
+
def read_file_bytes(raw_path: str) -> bytes:
|
|
286
|
+
""" Read the contents of a file as bytes. """
|
|
287
|
+
|
|
288
|
+
path = os.path.abspath(raw_path)
|
|
289
|
+
|
|
290
|
+
if (not exists(path)):
|
|
291
|
+
raise ValueError(f"Source of read bytes does not exist: '{raw_path}'.")
|
|
292
|
+
|
|
293
|
+
with open(path, 'rb') as file:
|
|
294
|
+
return file.read()
|
|
295
|
+
|
|
296
|
+
def write_file_bytes(
|
|
297
|
+
raw_path: str, contents: typing.Union[bytes, str, None],
|
|
298
|
+
no_clobber: bool = False) -> None:
|
|
299
|
+
"""
|
|
300
|
+
Write the contents of a file as bytes.
|
|
301
|
+
If clobbering, any existing dirent will be removed before write.
|
|
302
|
+
"""
|
|
303
|
+
|
|
304
|
+
if (contents is None):
|
|
305
|
+
contents = b''
|
|
306
|
+
|
|
307
|
+
if (isinstance(contents, str)):
|
|
308
|
+
contents = contents.encode(DEFAULT_ENCODING)
|
|
309
|
+
|
|
310
|
+
path = os.path.abspath(raw_path)
|
|
311
|
+
|
|
312
|
+
if (exists(path)):
|
|
313
|
+
if (no_clobber):
|
|
314
|
+
raise ValueError(f"Destination of write bytes already exists: '{raw_path}'.")
|
|
315
|
+
|
|
316
|
+
remove(path)
|
|
317
|
+
|
|
318
|
+
with open(path, 'wb') as file:
|
|
319
|
+
file.write(contents)
|
|
320
|
+
|
|
321
|
+
def contains_path(parent: str, child: str) -> bool:
|
|
322
|
+
"""
|
|
323
|
+
Check if the parent path contains the child path.
|
|
324
|
+
This is pure lexical analysis, no dirent stats are checked.
|
|
325
|
+
Will return false if the (absolute) paths are the same
|
|
326
|
+
(this function does not allow a path to contain itself).
|
|
327
|
+
"""
|
|
328
|
+
|
|
329
|
+
if ((parent == '') or (child == '')):
|
|
330
|
+
return False
|
|
331
|
+
|
|
332
|
+
parent = os.path.abspath(parent)
|
|
333
|
+
child = os.path.abspath(child)
|
|
334
|
+
|
|
335
|
+
child = os.path.dirname(child)
|
|
336
|
+
for _ in range(DEPTH_LIMIT):
|
|
337
|
+
if (parent == child):
|
|
338
|
+
return True
|
|
339
|
+
|
|
340
|
+
new_child = os.path.dirname(child)
|
|
341
|
+
if (child == new_child):
|
|
342
|
+
return False
|
|
343
|
+
|
|
344
|
+
child = new_child
|
|
345
|
+
|
|
346
|
+
raise ValueError("Depth limit reached.")
|