-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
128 lines (93 loc) · 3.36 KB
/
Copy pathutil.py
File metadata and controls
128 lines (93 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
import glob
from typing import List
import pyarrow.fs as pafs
import logging
logger = logging.getLogger(__name__)
def _is_s3_path(path: str) -> bool:
"""Check if a path is a S3 path."""
return path.startswith("s3://")
def check_path_exists(path: str) -> bool:
"""Check if a path exists (supports both S3 and local paths)."""
if _is_s3_path(path):
return check_s3_path_exists(path)
else:
return check_local_path_exists(path)
def check_local_path_exists(path: str) -> bool:
"""Check if a local path exists."""
return os.path.exists(path)
def check_s3_path_exists(path: str) -> bool:
"""Check if a S3 path exists."""
fs = pafs.S3FileSystem(region="us-east-2")
# Remove s3:// prefix if present
if path.startswith("s3://"):
path = path[5:]
# Remove trailing slash
path = path.rstrip("/")
logger.info(f"Listing parquet files in s3://{path}")
# Use FileSelector with recursive=True
selector = pafs.FileSelector(path, recursive=True)
file_infos = fs.get_file_info(selector)
return len(file_infos) > 0
def list_parquet_files(path: str) -> List[str]:
"""
List all parquet files in a directory recursively (supports both GCS and local paths).
Args:
path: Path to directory (GCS path like gs://bucket/path/ or local path)
Returns:
List of full paths to parquet files
"""
if _is_s3_path(path):
return list_s3_parquet_files(path)
else:
return list_local_parquet_files(path)
def list_local_parquet_files(path: str) -> List[str]:
"""
List all parquet files in a local directory recursively.
Args:
path: Local path to directory
Returns:
List of full local paths to parquet files
"""
path = path.rstrip("/")
logger.info(f"Listing parquet files in {path}")
parquet_files = []
if os.path.isfile(path):
# Single file
if path.endswith('.parquet'):
parquet_files.append(path)
elif os.path.isdir(path):
# Directory - search recursively
pattern = os.path.join(path, "**", "*.parquet")
parquet_files = glob.glob(pattern, recursive=True)
else:
# Could be a glob pattern
parquet_files = [f for f in glob.glob(path, recursive=True) if f.endswith('.parquet')]
logger.info(f"Found {len(parquet_files)} parquet files")
return parquet_files
def list_s3_parquet_files(path: str) -> List[str]:
"""
List all parquet files in a S3 directory recursively using PyArrow.
Args:
path: S3 path (e.g., s3://bucket/path/)
Returns:
List of full S3 paths to parquet files
"""
# Create filesystem
fs = pafs.S3FileSystem(region="us-east-2")
# Remove gs:// prefix if present
if path.startswith("s3://"):
path = path[5:]
# Remove trailing slash
path = path.rstrip("/")
logger.info(f"Listing parquet files in s3://{path}")
# Use FileSelector with recursive=True
selector = pafs.FileSelector(path, recursive=True)
file_infos = fs.get_file_info(selector)
# Filter for parquet files
parquet_files = []
for file_info in file_infos:
if file_info.type == pafs.FileType.File and file_info.path.endswith('.parquet'):
parquet_files.append(f"s3://{file_info.path}")
logger.info(f"Found {len(parquet_files)} parquet files")
return parquet_files