-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.py
173 lines (143 loc) · 5.58 KB
/
extract.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python3
"""cv-tbox Diversity Check / TSV File Extractor"""
###########################################################################
# extract.py
#
# From a directory containing downloaded Common Voice dataset files
# extracts only the .tsv files or all files into another location.
#
# Use:
# python extract.py
#
# This script is part of Common Voice ToolBox Package
#
# github: https://github.com/HarikalarKutusu/cv-tbox-split-maker
# Copyright: (c) Bülent Özden, License: AGPL v3.0
###########################################################################
# Standard Lib
import os
import sys
import re
import glob
import tarfile
from datetime import datetime
import multiprocessing as mp
# External Dependencies
import psutil
from tqdm import tqdm
# Module
import conf
from lib import dec3
# Globals
HERE: str = os.path.dirname(os.path.realpath(__file__))
if not HERE in sys.path:
sys.path.append(HERE)
MINIMAL_PROCS: int = 2
def extract_all_process(p: str) -> int:
"""Multiprocessing handler for full extraction"""
with tarfile.open(p) as tar:
tar.extractall(conf.CV_EXTRACTED_BASE_DIR)
return 1
def extract_tsv_process(p: str) -> int:
"""Multiprocessing handler for extraction"""
pat: re.Pattern[str] = re.compile(r"^.*\.tsv")
with tarfile.open(p) as tar:
tar.extractall(
conf.CV_EXTRACTED_BASE_DIR,
members=[m for m in tar.getmembers() if pat.search(m.name)],
)
return 1
def main(
is_delta: bool = False, extract_all: bool = False, forced: bool = False
) -> None:
"""Main process"""
# get compressed files list
all_files: list[str]
if is_delta:
all_files = glob.glob(
os.path.join(conf.CV_COMPRESSED_BASE_DIR, conf.CV_DELTA_VERSION, "*")
)
print("Searching for DELTA release files")
else:
all_files = glob.glob(
os.path.join(conf.CV_COMPRESSED_BASE_DIR, conf.CV_FULL_VERSION, "*")
)
print("Searching for FULL release files")
total_cnt: int = len(all_files)
print(f"Found {total_cnt} .tar.gz files")
start_time: datetime = datetime.now()
src_files: list[str] = all_files if conf.FORCE_CREATE or forced else []
dst_base: str = os.path.join(conf.CV_EXTRACTED_BASE_DIR, conf.CV_FULL_VERSION)
dst_check: str = ""
os.makedirs(dst_base, exist_ok=True)
if extract_all:
# remove already extracted ones from the list
if not (conf.FORCE_CREATE or forced):
print("Detecting already processed languages")
for p in all_files:
lc: str = (
os.path.split(p)[-1]
.replace(conf.CV_FULL_VERSION + "-", "")
.replace(".tar.gz", "")
.replace(".tar", "")
)
dst_check: str = os.path.join(dst_base, lc, "clips")
if not os.path.isdir(dst_check):
src_files.append(p)
src_cnt: int = len(src_files)
# Low number of cores only to prevent HDD trashing
proc_count: int = min(MINIMAL_PROCS, psutil.cpu_count(logical=False) or 1)
print(
f"Extracting ALL files from {src_cnt}/{total_cnt} compressed datasets in {proc_count} processes"
)
if conf.FORCE_CREATE:
print("Expanding even the destination exists (force_create)")
elif total_cnt > src_cnt:
print(f"Skipping {total_cnt - src_cnt} already extracted datasets")
chunk_size: int = src_cnt // proc_count + 0 if src_cnt % proc_count == 0 else 1
with mp.Pool(proc_count) as pool:
with tqdm(total=src_cnt) as pbar:
for _ in pool.imap_unordered(
extract_tsv_process, src_files, chunksize=chunk_size
):
pbar.update()
else:
# remove already extracted ones from the list
if not (conf.FORCE_CREATE or forced):
print("Detecting already processed languages")
for p in all_files:
lc: str = (
os.path.split(p)[-1]
.replace(conf.CV_FULL_VERSION + "-", "")
.replace(".tar.gz", "")
.replace(".tar", "")
)
dst_check = os.path.join(dst_base, lc, "validated.tsv")
if not os.path.isfile(dst_check):
src_files.append(p)
src_cnt: int = len(src_files)
# Real cores only to prevent excessive HDD head movements
proc_count: int = psutil.cpu_count(logical=False) or 1
print(
f"Extracting only .TSV files from {src_cnt}/{total_cnt} compressed datasets in {proc_count} processes"
)
if conf.FORCE_CREATE:
print("Expanding even the destination exists (force_create)")
elif total_cnt > src_cnt:
print(f"Skipping {total_cnt - src_cnt} already extracted datasets")
chunk_size: int = src_cnt // proc_count + 0 if src_cnt % proc_count == 0 else 1
with mp.Pool(proc_count) as pool:
with tqdm(total=src_cnt) as pbar:
for _ in pool.imap_unordered(
extract_tsv_process, src_files, chunksize=chunk_size
):
pbar.update()
# finalize
dur: int = (datetime.now() - start_time).seconds
print(f"Finished in {dur} sec - Avg={dec3(dur/src_cnt)} sec/dataset")
if __name__ == "__main__":
args: list[str] = sys.argv
arg_delta: bool = "--delta" in args
arg_all: bool = "--all" in args
arg_force: bool = "--force" in args
main(arg_delta, arg_all, arg_force)