forked from rsc/rsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkzip.go
183 lines (165 loc) · 3.84 KB
/
mkzip.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
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Mkzip creates a zip file from a 'proto' file describing the contents.
package main
import (
"archive/zip"
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
)
func usage() {
fmt.Fprintf(os.Stderr, "usage: mkzip [-r root] src.proto out.zip\n")
os.Exit(2)
}
func sysfatal(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "mkzip: %s\n", fmt.Sprintf(format, args...))
os.Exit(2)
}
var root = flag.String("r", ".", "interpret source paths relative to this directory")
type stack struct {
name string
src string
depth int
}
func main() {
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) != 2 {
usage()
}
rf, err := os.Open(args[0])
if err != nil {
sysfatal("%v", err)
}
r := bufio.NewScanner(rf)
zf, err := os.Create(args[1])
if err != nil {
sysfatal("%v", err)
}
z := zip.NewWriter(zf)
lineno := 0
addfile := func(info os.FileInfo, dst string, src string) {
zh, err := zip.FileInfoHeader(info)
if err != nil {
sysfatal("%s:%d: %s: %v", args[0], lineno, src, err)
}
zh.Name = dst
zh.Method = zip.Deflate
if info.IsDir() && !strings.HasSuffix(dst, "/") {
zh.Name += "/"
}
w, err := z.CreateHeader(zh)
if err != nil {
sysfatal("%s:%d: %s: %v", args[0], lineno, src, err)
}
if info.IsDir() {
return
}
r, err := os.Open(src)
if err != nil {
sysfatal("%s:%d: %s: %v", args[0], lineno, src, err)
}
defer r.Close()
if _, err := io.Copy(w, r); err != nil {
sysfatal("%s:%d: %s: %v", args[0], lineno, src, err)
}
}
var stk []stack
for r.Scan() {
line := r.Text()
lineno++
s := strings.TrimLeft(line, "\t")
prefix, line := line[:len(line)-len(s)], s
if i := strings.Index(line, "#"); i >= 0 {
line = line[:i]
}
f := strings.Fields(line)
if len(f) == 0 {
continue
}
if strings.HasPrefix(line, " ") {
sysfatal("%s:%d: must use tabs for indentation", args[0], lineno)
}
depth := len(prefix)
println(line, depth)
for len(stk) > 0 && depth <= stk[len(stk)-1].depth {
stk = stk[:len(stk)-1]
}
parent := ""
psrc := *root
if len(stk) > 0 {
parent = stk[len(stk)-1].name
psrc = stk[len(stk)-1].src
}
if strings.Contains(f[0], "/") {
sysfatal("%s:%d: destination name cannot contain slash", args[0], lineno)
}
name := path.Join(parent, f[0])
src := filepath.Join(psrc, f[0])
for _, attr := range f[1:] {
i := strings.Index(attr, "=")
if i < 0 {
sysfatal("%s:%d: malformed attribute %q", args[0], lineno, attr)
}
key, val := attr[:i], attr[i+1:]
switch key {
case "src":
src = val
default:
sysfatal("%s:%d: unknown attribute %q", args[0], lineno, attr)
}
}
println(name, f[0])
stk = append(stk, stack{name: name, src: src, depth: depth})
if f[0] == "*" || f[0] == "+" {
if f[0] == "*" {
dir, err := ioutil.ReadDir(psrc)
if err != nil {
sysfatal("%s:%d: %v", args[0], lineno, err)
}
for _, d := range dir {
addfile(d, path.Join(parent, d.Name()), filepath.Join(psrc, d.Name()))
}
} else {
err := filepath.Walk(psrc, func(src string, info os.FileInfo, err error) error {
println(psrc, src)
if err != nil {
return err
}
if src == psrc {
return nil
}
if psrc == "." {
psrc = ""
}
name := path.Join(parent, filepath.ToSlash(src[len(psrc):]))
addfile(info, name, src)
return nil
})
if err != nil {
sysfatal("%s:%d: %v", args[0], lineno, err)
}
}
continue
}
fi, err := os.Stat(src)
if err != nil {
sysfatal("%s:%d: %v", args[0], lineno, err)
}
addfile(fi, name, src)
}
if err := z.Close(); err != nil {
sysfatal("finishing zip file: %v", err)
}
}