-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
370 lines (342 loc) · 9.49 KB
/
main.go
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
package main
import (
"context"
"encoding/xml"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
)
const maxWorkers = 8
func main() {
oFlag := flag.String("o", "", "provide target path where output datasets (tif/eer/mrc) are written")
iFlag := flag.String("i", "", "provide target path where EPU stores the metadata in mirrored folders (xmls, jpgs, mrcs)")
aFlag := flag.String("a", "", "provide path to EPU Atlas folder if you want to also store the overview Atlas with your dataset.")
flag.Parse()
var xRoot string
var yRoot string
xtest, err := filepath.Abs(*oFlag)
if err != nil {
log.Fatalf("-o Flag has been set incorrectly, %s", err)
} else {
xRoot = xtest
log.Println("Datasets:", xRoot)
}
ytest, err := filepath.Abs(*iFlag)
if err != nil {
log.Fatalf("-i Flag has been set incorrectly, %s", err)
} else {
yRoot = ytest
log.Println("EPU folder:", yRoot)
}
if err := SyncXMLFromYtoX(xRoot, yRoot, maxWorkers, *aFlag); err != nil {
log.Fatalf("Error syncing XML files: %v", err)
}
}
func SyncXMLFromYtoX(xDir, yDir string, workers int, atlas string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tasks := make(chan copyTask, 1000) // buffered channel to reduce blocking
var wg sync.WaitGroup
wg.Add(workers)
for i := 0; i < workers; i++ {
go func() {
defer wg.Done()
copyWorker(ctx, tasks)
}()
}
contents, err := os.ReadDir(xDir)
if err != nil {
fmt.Fprintln(os.Stderr, "Couldnt read --o", err)
}
for _, d := range contents {
xPath, err := filepath.Abs(filepath.Join(xDir, d.Name()))
if err != nil {
fmt.Fprintf(os.Stderr, "Filepath couldnt be determined correctly , %s", err)
}
// Only process directories
if !d.IsDir() {
continue
}
relPath, err := filepath.Rel(xDir, xPath)
if err != nil {
return err
}
yPath := filepath.Join(yDir, relPath)
info, err := os.Stat(yPath)
if os.IsNotExist(err) {
continue
} else if err != nil {
return err
}
if !info.IsDir() {
continue
} //till here we now found a folder that exists in x and y (dataset folder)
// if aFLag set correctly, grab the Atlas from another TFS directory.
fmt.Println("Currently working on project:", d.Name())
if atlas != "" {
err := getAtlas(yPath, atlas, xPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't obtain the Atlas because: %s \n", err)
}
}
// Walk that subfolder in Y for .xml files.
walkErr := filepath.WalkDir(yPath, func(path string, yd os.DirEntry, yerr error) error {
if yerr != nil {
if os.IsPermission(yerr) {
return filepath.SkipDir
}
return yerr
}
if yd.IsDir() && yd.Name() == "FoilHoles" { // SPA/EPU skip
return filepath.SkipDir
} else if yd.IsDir() && (yd.Name() == "Thumbnails") { // Tomo skip
return filepath.SkipDir
} else if yd.IsDir() && (yd.Name() == "SearchMaps") { // this is where we need the whole folder
target := filepath.Join(yPath, yd.Name())
filepath.WalkDir(target, func(path string, yd os.DirEntry, yerr error) error {
if yd.IsDir() { // start crawling, generate folders as we go
var new_leg string
if yd.Name() != "SearchMaps" {
new_leg = filepath.Join(target, yd.Name())
} else {
new_leg = filepath.Join(yPath, yd.Name())
}
cross, _ := filepath.Rel(yPath, new_leg)
newdir1 := filepath.Join(xPath, cross)
_, direrr1 := os.Stat(newdir1)
if os.IsNotExist(direrr1) {
os.Mkdir(newdir1, 0755)
fmt.Println("created", newdir1)
}
return nil
} else {
yRel, err := filepath.Rel(yPath, path)
if err != nil {
return err
}
xDestPath := filepath.Join(xPath, yRel)
_, infoerr := os.Stat(xDestPath)
if infoerr == nil {
return nil
}
select {
case tasks <- copyTask{src: path, dst: xDestPath}:
case <-ctx.Done():
return ctx.Err()
}
}
return nil
})
return filepath.SkipDir
} else if yd.IsDir() && (yd.Name() == "Batch") {
newdir := filepath.Join(xPath, "Batch")
_, direrr := os.Stat(newdir)
if os.IsNotExist(direrr) {
os.Mkdir(newdir, 0755)
} // make sure the Folder is created in X to be able to copy to it.
return nil
} else if yd.IsDir() { // want Data in EPU/SPA
return nil
}
// Only process .xml files
if strings.HasSuffix(strings.ToLower(yd.Name()), ".mdoc") {
yRel, err := filepath.Rel(yPath, path)
if err != nil {
return err
}
// Grab a special case in Tomo5 data where a y Flip occurs depending on the output file format, and leave a small text file to report on it.
xDestPath := filepath.Join(xPath, yRel)
message := filepath.Join(filepath.Dir(xDestPath), "HowToFlipMyTomoData.txt")
_, errmessage := os.Stat(message)
if !(errmessage == nil) {
pattern := `^Position_.*$`
re := regexp.MustCompile(pattern)
test, errtest := os.ReadDir(filepath.Dir(message))
if errtest != nil {
fmt.Fprintln(os.Stderr, "cant read directory", errtest)
}
for _, name := range test {
file := name.Name()
if re.Match([]byte(file)) {
switch filepath.Ext(file) {
case ".tiff":
data := []byte("This data was originally written as tiff by Tomo5!\n")
errw := os.WriteFile(message, data, 0644)
if errw != nil {
fmt.Fprintln(os.Stderr, "Printing flip instructions went wrong")
}
case ".eer":
data := []byte("This data is orginially written as eer by Tomo5!\n")
errw := os.WriteFile(message, data, 0644)
if errw != nil {
fmt.Fprintln(os.Stderr, "Printing flip instructions went wrong")
}
}
}
}
}
return nil
} else if !strings.HasSuffix(strings.ToLower(yd.Name()), ".xml") {
return nil
}
if strings.Contains(yd.Name(), "GridSquare") {
return nil
}
yRel, err := filepath.Rel(yPath, path)
if err != nil {
return err
}
xDestPath := filepath.Join(xPath, yRel)
_, infoerr := os.Stat(xDestPath)
if infoerr == nil {
return nil
}
select {
case tasks <- copyTask{src: path, dst: xDestPath}:
case <-ctx.Done():
return ctx.Err()
}
return nil
})
if walkErr != nil && walkErr != filepath.SkipDir {
return walkErr
}
}
close(tasks)
wg.Wait()
return nil
}
// queue copy function
func copyWorker(ctx context.Context, tasks <-chan copyTask) {
for {
select {
case <-ctx.Done():
return
case task, ok := <-tasks:
if !ok {
return
}
if err := copyFile(task.src, task.dst); err != nil {
log.Printf("copy error: %v", err)
}
}
}
}
type copyTask struct {
src string
dst string
}
// actual copy function
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return fmt.Errorf("failed to open src %s: %w", src, err)
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return fmt.Errorf("failed to create dst %s: %w", dst, err)
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return fmt.Errorf("failed to copy from %s to %s: %w", src, dst, err)
}
return err
}
// Func to grab the atlas if aFlag is set - fully optional, if set will copy the overall Atlas.mrc in the top level dataset directory
func getAtlas(yPath string, atlas string, xPath string) error {
contents, err := os.ReadDir(yPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Couldnt read path during Atlas search", err)
}
for _, d := range contents {
if d.Name() == "EpuSession.dm" || d.Name() == "Session.dm" {
sessionfile := yPath + string(filepath.Separator) + d.Name()
f, err := os.Open(sessionfile)
if err != nil {
panic(err)
}
defer f.Close()
elementName := "AtlasId"
value, err := findAtlasIDValue(f, elementName)
if err != nil {
fmt.Println("Error:", err)
return err
}
var fullpath string
// if os windows just use path
switch runtime.GOOS {
case "windows":
fullpath = value
// if os not windows change the Windows path to a linux compatible one and change drive to mountpoint *aFlag
default:
atlasdm := strings.ReplaceAll(value, `\`, string(filepath.Separator))
if len(atlasdm) > 1 && atlasdm[1] == ':' {
atlasdm = atlasdm[2:]
}
fullpath = atlas + atlasdm
}
targetpath := filepath.Dir(fullpath)
atlas_re := `^Atlas_.*\.mrc$`
find := regexp.MustCompile(atlas_re)
test, errtest := os.ReadDir(targetpath)
if errtest != nil {
return errtest
}
for _, name := range test {
file := name.Name()
if find.Match([]byte(file)) {
doublecopyprotect := xPath + string(filepath.Separator) + file
atlasfile := targetpath + string(filepath.Separator) + file
_, checkerr := os.Stat(doublecopyprotect)
switch checkerr {
case nil:
continue
default:
err := copyFile(atlasfile, doublecopyprotect)
if err != nil {
return err
}
}
}
}
}
}
return nil
}
// extract the Atlas Folder Path from the Session xml (thermo: .dm) file
func findAtlasIDValue(r io.Reader, elementName string) (string, error) {
decoder := xml.NewDecoder(r)
for {
token, err := decoder.Token()
if err != nil {
if err == io.EOF {
return "", fmt.Errorf("element %q not found in Session", elementName)
}
return "", err
}
switch se := token.(type) {
case xml.StartElement:
if se.Name.Local == elementName {
var val string
err := decoder.DecodeElement(&val, &se)
if err != nil {
return "", err
}
return val, nil
}
}
}
}