-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbatch.go
113 lines (95 loc) · 2.14 KB
/
batch.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
package main
import (
"bufio"
"os"
"path/filepath"
"runtime"
"strings"
)
type taskResult struct {
input string
e error
}
var (
chTaskResult chan *taskResult
)
func runTask(input string, outdir string) {
var (
maker = NewEpubMaker(logger)
folder VirtualFolder
tr = &taskResult{input: input}
duokan = !getFlagBool("noduokan")
ver = EPUB_VERSION_300
)
if getFlagBool("epub2") {
ver = EPUB_VERSION_200
}
if folder, tr.e = OpenVirtualFolder(input); tr.e != nil {
logger.Printf("%s: failed to open source folder/file.\n", input)
} else if tr.e = maker.Process(folder, duokan); tr.e == nil {
tr.e = maker.SaveTo(outdir, ver)
}
chTaskResult <- tr
}
func processBatchFile(f *os.File, outdir string) (count int, e error) {
scanner := bufio.NewScanner(f)
for scanner.Scan() {
name := strings.TrimSpace(scanner.Text())
if len(name) > 0 {
go runTask(name, outdir)
count++
}
}
if e = scanner.Err(); e != nil {
logger.Println("error reading batch file.")
}
return
}
func processBatchFolder(f *os.File, outdir string) (count int, e error) {
names, e := f.Readdirnames(-1)
if e != nil {
logger.Println("error reading source folder.")
return 0, e
}
for _, name := range names {
name = filepath.Join(f.Name(), name)
go runTask(name, outdir)
count++
}
return count, nil
}
func RunBatch() {
var input *os.File = nil
if inpath := getArg(0, ""); len(inpath) == 0 {
onCommandLineError()
} else if f, e := os.Open(inpath); e != nil {
logger.Fatalf("failed to open '%s'.\n", inpath)
} else {
input = f
}
defer input.Close()
outpath := getArg(1, "")
runtime.GOMAXPROCS(runtime.NumCPU() + 1)
chTaskResult = make(chan *taskResult)
defer close(chTaskResult)
var count int
var e error
if fi, _ := input.Stat(); fi.IsDir() {
count, e = processBatchFolder(input, outpath)
} else {
count, e = processBatchFile(input, outpath)
}
if e != nil && count == 0 {
return
}
failed := 0
for i := 0; i < count; i++ {
if (<-chTaskResult).e != nil {
failed++
}
}
logger.Printf("total: %d succeeded: %d failed: %d\n", count, count-failed, failed)
}
func init() {
AddCommandHandler("b", RunBatch)
}