-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.go
More file actions
executable file
·382 lines (328 loc) · 10.7 KB
/
Copy pathsplit.go
File metadata and controls
executable file
·382 lines (328 loc) · 10.7 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package splitter
import (
"bytes"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"time"
. "github.com/nsip/go-generics"
fd "github.com/nsip/gotk/file-dir"
"github.com/nsip/gotk/strs"
lk "github.com/nsip/logkit"
"github.com/gosuri/uiprogress"
"github.com/gosuri/uiprogress/util/strutil"
ct "github.com/nsip/csv-tool"
qry "github.com/nsip/csv-tool/query"
spl "github.com/nsip/csv-tool/split"
spl2 "github.com/nsip/csv-tool/split2"
)
func EnableProgBar(enable bool) {
progBar = enable
}
// NrtSplit :
func NrtSplit(configurations ...string) (err error) {
// fit toml configuration for 'Trim', 'Split' etc
setConfig(configurations...)
// by default, if Trim & Split are both disabled, disable progress-bar anyway.
EnableProgBar(enableTrim || enableSplit)
// prepare a temporary dir for trim & split
const tempDir = "./ts_tmp/"
if err = os.RemoveAll(tempDir); err != nil {
return err
}
// -- progress bar 1 -- //
if progBar {
uip = uiprogress.New()
defer uip.Stop()
uip.Start()
files, _, err := fd.WalkFileDir(inFolderAbs, goSubFolder)
if err != nil {
lk.WarnOnErr("%v", err)
return err
}
// Spinner goroutine: animates a rotating character in the bar label so the
// display keeps moving even while a single large file is being processed.
go func() {
for {
time.Sleep(150 * time.Millisecond)
spinnerIdx++
}
}()
bar = uip.AddBar(len(files))
bar.AppendCompleted().PrependElapsed()
bar.PrependFunc(func(b *uiprogress.Bar) string {
return strutil.Resize(" Trimming & Splitting...:", 35)
})
bar.AppendFunc(func(b *uiprogress.Bar) string {
frames := []string{"|", "/", "-", "\\"}
return " " + frames[spinnerIdx%4] + " " + strutil.Resize(currentFile, 38)
})
}
mFileEmptyCSV := make(map[string][]byte)
err = filepath.Walk(inFolderAbs, func(fPath string, info os.FileInfo, err error) error {
lk.FailOnErr("error [%v] at a path [%q], check your config.toml [InFolder] \n", err, fPath)
// only process csv file
if info.IsDir() || filepath.Ext(fPath) != ".csv" {
return nil
}
// if NOT goSubFolder, if jump into deeper, then return
if !goSubFolder {
fDirAbs, err := filepath.Abs(filepath.Dir(fPath))
lk.FailOnErr("Error when walk through abs %s, @%v", inFolderAbs, err)
if inFolderAbs != fDirAbs {
return nil
}
}
// update progress bar label with the file currently being processed
if progBar {
currentFile = filepath.Base(fPath)
}
// trim inFolder Path for each file path, only keep 'filename.csv' or '/sub/filename.csv'
tailPath := fPath[len(inFolderAbs):]
// Split first
if enableSplit {
// fmt.Printf("Split Processing...: %v\n", path)
// split setting
if bySplit2 {
spl2.RmSchemaCol(true) // after splitting, remove those columns which are used by splitting
spl2.RmSchemaColInIgn(false) // keep all columns when a file cannot be split
spl2.StrictSchema(true, ignoreFolder4Split) // strict for split, if doesn't meet Schema, then ignore this csv
} else {
// spl.ForceSglProc(true)
spl.RmSchemaCol(true)
spl.RmSchemaColInIgn(false)
spl.StrictSchema(true, ignoreFolder4Split)
}
outFile := filepath.Join(out4Split, tailPath) // output file
outFolder := filepath.Dir(outFile) // output file's folder
// do split
var fPathsSplit, fPathsIgnore []string
if bySplit2 {
fPathsSplit, fPathsIgnore, err = spl2.Split(fPath, outFolder, splitSchema...)
} else {
fPathsSplit, fPathsIgnore, err = spl.Split(fPath, outFolder, splitSchema...)
}
if err != nil {
// A split error on one file must not abort the whole run; warn and move on.
lk.WarnOnErr("split failed for [%s]: %v — skipping to next file", fPath, err)
return nil
}
// trim columns also apply to split result if set
if enableTrim && trimColAfterSplit {
for _, splitPath := range fPathsSplit {
if ok, trimCheckErr := ct.FileHeaderHasAny(splitPath, trimCols...); trimCheckErr == nil && ok {
// Write trim output to a temp path; replace original only on success.
// This prevents a QueryFile error from leaving the split file empty.
tmpPath := splitPath + ".trim_tmp"
if trimErr := qry.QueryFile(splitPath, false, trimCols, '&', nil, tmpPath); trimErr != nil {
lk.WarnOnErr("trim-after-split failed for [%s]: %v — leaving file untrimmed", splitPath, trimErr)
os.Remove(tmpPath) // remove any empty/partial temp, leave original intact
} else if renameErr := os.Rename(tmpPath, splitPath); renameErr != nil {
lk.WarnOnErr("could not replace trimmed split file [%s]: %v — leaving file untrimmed", splitPath, renameErr)
os.Remove(tmpPath)
}
}
}
}
// find schema is valid, but empty content file to spread in future
for _, fPath := range fPathsIgnore {
hdr, n, err := ct.FileInfo(fPath)
if err != nil {
lk.Warn("%v @ %s", err, fPath)
return err
}
if n == 0 && IsSuper(hdr, splitSchema) {
emptyCsv, err := os.ReadFile(fPath)
if err != nil {
lk.Warn("%v @ %s", err, fPath)
return err
}
// Trim Columns if needed
rmHdr := splitSchema
if enableTrim && trimColAfterSplit {
rmHdr = Settify(append(rmHdr, trimCols...)...)
}
var buf bytes.Buffer
qry.Subset(emptyCsv, false, rmHdr, false, nil, io.Writer(&buf))
emptyCsv = buf.Bytes()
mFileEmptyCSV[fPath] = emptyCsv
}
}
}
if enableTrim {
lk.Log("Trim Processing: %v", fPath)
if ok, trimCheckErr := ct.FileHeaderHasAny(fPath, trimCols...); trimCheckErr == nil && ok {
outFolder := out4Trim
// if trim output folder is identical to original input folder, make a temp output, then overwrite the input
if out4Trim == inFolder {
outFolder = tempDir
}
outFile := filepath.Join(outFolder, tailPath)
// pre-truncate to prevent stale tail content on re-runs
// (QueryFile in digisan/csv-tool opens without O_TRUNC)
if fd.FileExists(outFile) {
if truncErr := os.Truncate(outFile, 0); truncErr != nil {
lk.WarnOnErr("could not truncate existing trim output [%s]: %v", outFile, truncErr)
}
}
if trimErr := qry.QueryFile(fPath, false, trimCols, '&', nil, outFile); trimErr != nil {
lk.WarnOnErr("trim failed for [%s]: %v — copying untrimmed file to output as fallback", fPath, trimErr)
fd.MustCreateDir(filepath.Dir(outFile))
func() {
srcF, openErr := os.Open(fPath)
if openErr != nil {
lk.WarnOnErr("fallback: could not open source [%s]: %v", fPath, openErr)
return
}
defer srcF.Close()
dstF, createErr := os.Create(outFile)
if createErr != nil {
lk.WarnOnErr("fallback: could not create output [%s]: %v", outFile, createErr)
return
}
defer dstF.Close()
if _, copyErr := io.Copy(dstF, srcF); copyErr != nil {
lk.WarnOnErr("fallback: copy failed [%s] -> [%s]: %v", fPath, outFile, copyErr)
}
}()
} else {
lk.Log("Done Trim Processing: %v", fPath)
}
}
}
// -- progress bar 2 -- //
if progBar {
bar.Incr()
}
return nil
}) // end of walk
lk.FailOnErr("error walking the path %q: %v\n", inFolderAbs, err)
// if temp folder was created as Trim.OutFolder is the same as InFolder, use temp folder to replace input folder
if fd.DirExists(tempDir) {
// copy each file to inFolder, then delete tempDir
err := filepath.WalkDir(tempDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
srcF, err := os.Open(path)
if err != nil {
lk.Warn("%v (%v)", err, path)
return err
}
defer srcF.Close()
subPath := strs.TrimHeadToFirst(path, filepath.Clean(tempDir))
// lk.Log("%v -- %v -- %v", path, tempDir, subPath)
dst := filepath.Join(inFolder, subPath)
// lk.Log("%v", dst)
dstF, err := os.Create(dst)
if err != nil {
lk.Warn("%v (%v)", err, dst)
return err
}
defer dstF.Close()
if _, err := io.Copy(dstF, srcF); err != nil {
lk.Warn("%v (%v) (%v)", err, path, dst)
return err
}
return nil
})
if err == nil {
lk.Log("removing temp dir [%v]", tempDir)
lk.WarnOnErr("%v", os.RemoveAll(tempDir))
}
// cause files missing issue !!!
// os.RemoveAll(inFolder)
// os.Rename(tempDir, inFolder)
}
// spread all valid schema & empty csv to each split folder
mOutRecord := make(map[string]struct{})
if enableSplit {
err = filepath.Walk(out4Split, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) != ".csv" || info.IsDir() {
return nil
}
mOutRecord[filepath.Dir(path)] = struct{}{}
return nil
})
lk.WarnOnErr("error walking the path %q: %v\n", inFolderAbs, err)
nSchema := len(splitSchema)
for outPath := range mOutRecord {
for emptyPath, csv := range mFileEmptyCSV {
emptyRoot := filepath.Base(filepath.Dir(emptyPath))
emptyFile := filepath.Base(emptyPath)
outDir := outPath
for i := 0; i < nSchema; i++ {
outDir = filepath.Dir(outDir)
}
if strings.HasSuffix(outDir, "/"+emptyRoot) || strings.HasSuffix(outDir, "\\"+emptyRoot) {
fd.MustWriteFile(filepath.Join(outPath, emptyFile), csv)
}
}
}
}
watched := []string{}
for _, m := range merges {
schema := AnysToTypes[string](m["Schema"].([]any))
watched = append(watched, schema...)
}
watched = Settify(watched...)
_, dirs4split, err := fd.WalkFileDir(out4Split, true)
if err != nil {
if !enableSplit {
if _, err = os.Stat(out4Split); os.IsNotExist(err) {
err = nil
}
}
}
if err != nil {
return err
}
mDirBase := make(map[string][]string)
for _, dir := range dirs4split {
base := filepath.Base(dir)
dir1 := filepath.Dir(dir)
if In(base, watched...) {
mDirBase[dir1] = append(mDirBase[dir1], base)
}
}
onConflict := func(existing []byte, incoming []byte) (overwrite bool, overwriteData []byte) {
iLF := bytes.Index(incoming, []byte{'\n'})
return true, append(existing, incoming[iLF:]...)
}
for dir1, folders := range mDirBase {
for _, folder := range folders {
dir := filepath.Join(dir1, folder)
for _, m := range merges {
if m["Enabled"].(bool) {
temp := filepath.Join(dir1, m["MergedName"].(string)+"#")
// merged := filepath.Join(dir1, m.MergedName)
schema := AnysToTypes[string](m["Schema"].([]any))
for _, s := range schema {
if s == folder {
// fmt.Println(dir, "=>", merged)
fd.MergeDir(temp, true, onConflict, dir)
}
}
}
}
}
}
err = nil
if enableSplit {
_, dirs4split, err = fd.WalkFileDir(out4Split, true)
if err != nil {
return err
}
for _, dir := range dirs4split {
if strings.HasSuffix(dir, "#") {
os.Rename(dir, dir[:len(dir)-1])
}
}
}
return err
}