fsspec 2023.6.0__py3-none-any.whl → 2023.9.1__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,3 +1,12 @@
1
+ from hashlib import md5
2
+ from itertools import product
3
+
4
+ import pytest
5
+
6
+ from fsspec.implementations.local import make_path_posix
7
+ from fsspec.tests.abstract.common import GLOB_EDGE_CASES_TESTS
8
+
9
+
1
10
  class AbstractGetTests:
2
11
  def test_get_file_to_existing_directory(
3
12
  self,
@@ -66,7 +75,6 @@ class AbstractGetTests:
66
75
  self,
67
76
  fs,
68
77
  fs_join,
69
- fs_path,
70
78
  fs_bulk_operations_scenario_0,
71
79
  local_fs,
72
80
  local_join,
@@ -117,6 +125,7 @@ class AbstractGetTests:
117
125
 
118
126
  target = local_target
119
127
  local_fs.mkdir(target)
128
+ assert local_fs.isdir(target)
120
129
 
121
130
  for source_slash, target_slash in zip([False, True], [False, True]):
122
131
  s = fs_join(source, "subdir")
@@ -125,9 +134,8 @@ class AbstractGetTests:
125
134
  t = target + "/" if target_slash else target
126
135
 
127
136
  # Without recursive does nothing
128
- # ERROR: erroneously creates new directory
129
- # fs.get(s, t)
130
- # assert fs.ls(target) == []
137
+ fs.get(s, t)
138
+ assert local_fs.ls(target) == []
131
139
 
132
140
  # With recursive
133
141
  fs.get(s, t, recursive=True)
@@ -136,6 +144,7 @@ class AbstractGetTests:
136
144
  assert local_fs.isfile(local_join(target, "subfile2"))
137
145
  assert local_fs.isdir(local_join(target, "nesteddir"))
138
146
  assert local_fs.isfile(local_join(target, "nesteddir", "nestedfile"))
147
+ assert not local_fs.exists(local_join(target, "subdir"))
139
148
 
140
149
  local_fs.rm(
141
150
  [
@@ -157,8 +166,29 @@ class AbstractGetTests:
157
166
  local_fs.rm(local_join(target, "subdir"), recursive=True)
158
167
  assert local_fs.ls(target) == []
159
168
 
160
- # Limit by maxdepth
161
- # ERROR: maxdepth ignored here
169
+ # Limit recursive by maxdepth
170
+ fs.get(s, t, recursive=True, maxdepth=1)
171
+ if source_slash:
172
+ assert local_fs.isfile(local_join(target, "subfile1"))
173
+ assert local_fs.isfile(local_join(target, "subfile2"))
174
+ assert not local_fs.exists(local_join(target, "nesteddir"))
175
+ assert not local_fs.exists(local_join(target, "subdir"))
176
+
177
+ local_fs.rm(
178
+ [
179
+ local_join(target, "subfile1"),
180
+ local_join(target, "subfile2"),
181
+ ],
182
+ recursive=True,
183
+ )
184
+ else:
185
+ assert local_fs.isdir(local_join(target, "subdir"))
186
+ assert local_fs.isfile(local_join(target, "subdir", "subfile1"))
187
+ assert local_fs.isfile(local_join(target, "subdir", "subfile2"))
188
+ assert not local_fs.exists(local_join(target, "subdir", "nesteddir"))
189
+
190
+ local_fs.rm(local_join(target, "subdir"), recursive=True)
191
+ assert local_fs.ls(target) == []
162
192
 
163
193
  def test_get_directory_to_new_directory(
164
194
  self,
@@ -184,9 +214,8 @@ class AbstractGetTests:
184
214
  t += "/"
185
215
 
186
216
  # Without recursive does nothing
187
- # ERROR: erroneously creates new directory
188
- # fs.get(s, t)
189
- # assert fs.ls(target) == []
217
+ fs.get(s, t)
218
+ assert local_fs.ls(target) == []
190
219
 
191
220
  # With recursive
192
221
  fs.get(s, t, recursive=True)
@@ -197,12 +226,21 @@ class AbstractGetTests:
197
226
  assert local_fs.isfile(
198
227
  local_join(target, "newdir", "nesteddir", "nestedfile")
199
228
  )
229
+ assert not local_fs.exists(local_join(target, "subdir"))
200
230
 
201
231
  local_fs.rm(local_join(target, "newdir"), recursive=True)
202
232
  assert local_fs.ls(target) == []
203
233
 
204
- # Limit by maxdepth
205
- # ERROR: maxdepth ignored here
234
+ # Limit recursive by maxdepth
235
+ fs.get(s, t, recursive=True, maxdepth=1)
236
+ assert local_fs.isdir(local_join(target, "newdir"))
237
+ assert local_fs.isfile(local_join(target, "newdir", "subfile1"))
238
+ assert local_fs.isfile(local_join(target, "newdir", "subfile2"))
239
+ assert not local_fs.exists(local_join(target, "newdir", "nesteddir"))
240
+ assert not local_fs.exists(local_join(target, "subdir"))
241
+
242
+ local_fs.rm(local_join(target, "newdir"), recursive=True)
243
+ assert not local_fs.exists(local_join(target, "newdir"))
206
244
 
207
245
  def test_get_glob_to_existing_directory(
208
246
  self,
@@ -219,20 +257,62 @@ class AbstractGetTests:
219
257
  target = local_target
220
258
  local_fs.mkdir(target)
221
259
 
222
- # for target_slash in [False, True]:
223
- for target_slash in [False]:
260
+ for target_slash in [False, True]:
224
261
  t = target + "/" if target_slash else target
225
262
 
226
263
  # Without recursive
227
264
  fs.get(fs_join(source, "subdir", "*"), t)
228
265
  assert local_fs.isfile(local_join(target, "subfile1"))
229
266
  assert local_fs.isfile(local_join(target, "subfile2"))
230
- # assert not local_fs.isdir(local_join(target, "nesteddir")) # ERROR
231
- assert not local_fs.isdir(local_join(target, "subdir"))
267
+ assert not local_fs.isdir(local_join(target, "nesteddir"))
268
+ assert not local_fs.exists(local_join(target, "nesteddir", "nestedfile"))
269
+ assert not local_fs.exists(local_join(target, "subdir"))
270
+
271
+ local_fs.rm(
272
+ [
273
+ local_join(target, "subfile1"),
274
+ local_join(target, "subfile2"),
275
+ ],
276
+ recursive=True,
277
+ )
278
+ assert local_fs.ls(target) == []
232
279
 
233
280
  # With recursive
281
+ for glob, recursive in zip(["*", "**"], [True, False]):
282
+ fs.get(fs_join(source, "subdir", glob), t, recursive=recursive)
283
+ assert local_fs.isfile(local_join(target, "subfile1"))
284
+ assert local_fs.isfile(local_join(target, "subfile2"))
285
+ assert local_fs.isdir(local_join(target, "nesteddir"))
286
+ assert local_fs.isfile(local_join(target, "nesteddir", "nestedfile"))
287
+ assert not local_fs.exists(local_join(target, "subdir"))
288
+
289
+ local_fs.rm(
290
+ [
291
+ local_join(target, "subfile1"),
292
+ local_join(target, "subfile2"),
293
+ local_join(target, "nesteddir"),
294
+ ],
295
+ recursive=True,
296
+ )
297
+ assert local_fs.ls(target) == []
234
298
 
235
- # Limit by maxdepth
299
+ # Limit recursive by maxdepth
300
+ fs.get(
301
+ fs_join(source, "subdir", glob), t, recursive=recursive, maxdepth=1
302
+ )
303
+ assert local_fs.isfile(local_join(target, "subfile1"))
304
+ assert local_fs.isfile(local_join(target, "subfile2"))
305
+ assert not local_fs.exists(local_join(target, "nesteddir"))
306
+ assert not local_fs.exists(local_join(target, "subdir"))
307
+
308
+ local_fs.rm(
309
+ [
310
+ local_join(target, "subfile1"),
311
+ local_join(target, "subfile2"),
312
+ ],
313
+ recursive=True,
314
+ )
315
+ assert local_fs.ls(target) == []
236
316
 
237
317
  def test_get_glob_to_new_directory(
238
318
  self,
@@ -259,27 +339,91 @@ class AbstractGetTests:
259
339
  assert local_fs.isdir(local_join(target, "newdir"))
260
340
  assert local_fs.isfile(local_join(target, "newdir", "subfile1"))
261
341
  assert local_fs.isfile(local_join(target, "newdir", "subfile2"))
262
- # ERROR - do not copy empty directory
263
- # assert not local_fs.exists(local_join(target, "newdir", "nesteddir"))
342
+ assert not local_fs.exists(local_join(target, "newdir", "nesteddir"))
343
+ assert not local_fs.exists(
344
+ local_join(target, "newdir", "nesteddir", "nestedfile")
345
+ )
346
+ assert not local_fs.exists(local_join(target, "subdir"))
347
+ assert not local_fs.exists(local_join(target, "newdir", "subdir"))
264
348
 
265
349
  local_fs.rm(local_join(target, "newdir"), recursive=True)
266
350
  assert local_fs.ls(target) == []
267
351
 
268
352
  # With recursive
269
- fs.get(fs_join(source, "subdir", "*"), t, recursive=True)
270
- assert local_fs.isdir(local_join(target, "newdir"))
271
- assert local_fs.isfile(local_join(target, "newdir", "subfile1"))
272
- assert local_fs.isfile(local_join(target, "newdir", "subfile2"))
273
- assert local_fs.isdir(local_join(target, "newdir", "nesteddir"))
274
- assert local_fs.isfile(
275
- local_join(target, "newdir", "nesteddir", "nestedfile")
276
- )
353
+ for glob, recursive in zip(["*", "**"], [True, False]):
354
+ fs.get(fs_join(source, "subdir", glob), t, recursive=recursive)
355
+ assert local_fs.isdir(local_join(target, "newdir"))
356
+ assert local_fs.isfile(local_join(target, "newdir", "subfile1"))
357
+ assert local_fs.isfile(local_join(target, "newdir", "subfile2"))
358
+ assert local_fs.isdir(local_join(target, "newdir", "nesteddir"))
359
+ assert local_fs.isfile(
360
+ local_join(target, "newdir", "nesteddir", "nestedfile")
361
+ )
362
+ assert not local_fs.exists(local_join(target, "subdir"))
363
+ assert not local_fs.exists(local_join(target, "newdir", "subdir"))
277
364
 
278
- local_fs.rm(local_join(target, "newdir"), recursive=True)
279
- assert local_fs.ls(target) == []
365
+ local_fs.rm(local_join(target, "newdir"), recursive=True)
366
+ assert not local_fs.exists(local_join(target, "newdir"))
367
+
368
+ # Limit recursive by maxdepth
369
+ fs.get(
370
+ fs_join(source, "subdir", glob), t, recursive=recursive, maxdepth=1
371
+ )
372
+ assert local_fs.isdir(local_join(target, "newdir"))
373
+ assert local_fs.isfile(local_join(target, "newdir", "subfile1"))
374
+ assert local_fs.isfile(local_join(target, "newdir", "subfile2"))
375
+ assert not local_fs.exists(local_join(target, "newdir", "nesteddir"))
376
+ assert not local_fs.exists(local_join(target, "subdir"))
377
+ assert not local_fs.exists(local_join(target, "newdir", "subdir"))
378
+
379
+ local_fs.rm(local_fs.ls(target, detail=False), recursive=True)
380
+ assert not local_fs.exists(local_join(target, "newdir"))
381
+
382
+ @pytest.mark.parametrize(
383
+ GLOB_EDGE_CASES_TESTS["argnames"],
384
+ GLOB_EDGE_CASES_TESTS["argvalues"],
385
+ )
386
+ def test_get_glob_edge_cases(
387
+ self,
388
+ path,
389
+ recursive,
390
+ maxdepth,
391
+ expected,
392
+ fs,
393
+ fs_join,
394
+ fs_glob_edge_cases_files,
395
+ local_fs,
396
+ local_join,
397
+ local_target,
398
+ ):
399
+ # Copy scenario 1g
400
+ source = fs_glob_edge_cases_files
401
+
402
+ target = local_target
403
+
404
+ for new_dir, target_slash in product([True, False], [True, False]):
405
+ local_fs.mkdir(target)
280
406
 
281
- # Limit by maxdepth
282
- # ERROR: this is not correct
407
+ t = local_join(target, "newdir") if new_dir else target
408
+ t = t + "/" if target_slash else t
409
+
410
+ fs.get(fs_join(source, path), t, recursive=recursive, maxdepth=maxdepth)
411
+
412
+ output = local_fs.find(target)
413
+ if new_dir:
414
+ prefixed_expected = [
415
+ make_path_posix(local_join(target, "newdir", p)) for p in expected
416
+ ]
417
+ else:
418
+ prefixed_expected = [
419
+ make_path_posix(local_join(target, p)) for p in expected
420
+ ]
421
+ assert sorted(output) == sorted(prefixed_expected)
422
+
423
+ try:
424
+ local_fs.rm(target, recursive=True)
425
+ except FileNotFoundError:
426
+ pass
283
427
 
284
428
  def test_get_list_of_files_to_existing_directory(
285
429
  self,
@@ -310,7 +454,14 @@ class AbstractGetTests:
310
454
  assert local_fs.isfile(local_join(target, "file2"))
311
455
  assert local_fs.isfile(local_join(target, "subfile1"))
312
456
 
313
- local_fs.rm(local_fs.find(target))
457
+ local_fs.rm(
458
+ [
459
+ local_join(target, "file1"),
460
+ local_join(target, "file2"),
461
+ local_join(target, "subfile1"),
462
+ ],
463
+ recursive=True,
464
+ )
314
465
  assert local_fs.ls(target) == []
315
466
 
316
467
  def test_get_list_of_files_to_new_directory(
@@ -358,13 +509,13 @@ class AbstractGetTests:
358
509
  fs.get(src, target, recursive=True)
359
510
  assert local_fs.isdir(target)
360
511
 
361
- if loop == 0:
362
- assert local_fs.isfile(local_join(target, "file"))
363
- assert not local_fs.exists(local_join(target, "src"))
364
- else:
365
- assert local_fs.isfile(local_join(target, "file"))
366
- assert local_fs.isdir(local_join(target, "src"))
367
- assert local_fs.isfile(local_join(target, "src", "file"))
512
+ if loop == 0:
513
+ assert local_fs.isfile(local_join(target, "file"))
514
+ assert not local_fs.exists(local_join(target, "src"))
515
+ else:
516
+ assert local_fs.isfile(local_join(target, "file"))
517
+ assert local_fs.isdir(local_join(target, "src"))
518
+ assert local_fs.isfile(local_join(target, "src", "file"))
368
519
 
369
520
  local_fs.rm(target, recursive=True)
370
521
 
@@ -375,3 +526,62 @@ class AbstractGetTests:
375
526
  assert local_fs.isdir(target)
376
527
  assert local_fs.isfile(local_join(target, "file"))
377
528
  assert not local_fs.exists(local_join(target, "src"))
529
+
530
+ def test_get_directory_without_files_with_same_name_prefix(
531
+ self,
532
+ fs,
533
+ fs_join,
534
+ local_fs,
535
+ local_join,
536
+ local_target,
537
+ fs_dir_and_file_with_same_name_prefix,
538
+ ):
539
+ # Create the test dirs
540
+ source = fs_dir_and_file_with_same_name_prefix
541
+ target = local_target
542
+
543
+ # Test without glob
544
+ fs.get(fs_join(source, "subdir"), target, recursive=True)
545
+
546
+ assert local_fs.isfile(local_join(target, "subfile.txt"))
547
+ assert not local_fs.isfile(local_join(target, "subdir.txt"))
548
+
549
+ local_fs.rm([local_join(target, "subfile.txt")])
550
+ assert local_fs.ls(target) == []
551
+
552
+ # Test with glob
553
+ fs.get(fs_join(source, "subdir*"), target, recursive=True)
554
+
555
+ assert local_fs.isdir(local_join(target, "subdir"))
556
+ assert local_fs.isfile(local_join(target, "subdir", "subfile.txt"))
557
+ assert local_fs.isfile(local_join(target, "subdir.txt"))
558
+
559
+ def test_get_with_source_and_destination_as_list(
560
+ self,
561
+ fs,
562
+ fs_join,
563
+ local_fs,
564
+ local_join,
565
+ local_target,
566
+ fs_10_files_with_hashed_names,
567
+ ):
568
+ # Create the test dir
569
+ source = fs_10_files_with_hashed_names
570
+ target = local_target
571
+
572
+ # Create list of files for source and destination
573
+ source_files = []
574
+ destination_files = []
575
+ for i in range(10):
576
+ hashed_i = md5(str(i).encode("utf-8")).hexdigest()
577
+ source_files.append(fs_join(source, f"{hashed_i}.txt"))
578
+ destination_files.append(
579
+ make_path_posix(local_join(target, f"{hashed_i}.txt"))
580
+ )
581
+
582
+ # Copy and assert order was kept
583
+ fs.get(rpath=source_files, lpath=destination_files)
584
+
585
+ for i in range(10):
586
+ file_content = local_fs.cat(destination_files[i]).decode("utf-8")
587
+ assert file_content == str(i)