-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathio.go
151 lines (139 loc) · 3.64 KB
/
io.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
/*
Copyright 2018 The Bazel Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package razel
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"strings"
)
func parseDCFFromPath(path string) (map[string]string, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
fields, err := parseDCF(f)
if err != nil {
return fields, fmt.Errorf("parsing %q: %w", path, err)
}
return fields, nil
}
// Parses the DESCRIPTION file of R packages, which are formatted as Debian
// Control Files. In R, the relevant function to parse these files is read.dcf.
func parseDCF(r io.Reader) (map[string]string, error) {
fields := make(map[string]string)
s := bufio.NewScanner(r)
var key, value string
for s.Scan() {
line := s.Text()
if line == "" {
// R only allows one DCF paragraph, so we can safely skip any blank lines
// as leading or trailing lines.
continue
}
if line[0] == ' ' || line[0] == '\t' {
// Continuation line.
if key == "" {
// Can not have a continuation line without a key.
return fields, fmt.Errorf("can not start file with a continuation line")
}
// Standardize all continuation space as ' '.
line = strings.TrimSpace(line)
if value != "" {
value += " " + line
} else {
value = line
}
} else {
// New key.
if key != "" {
fields[key] = value
}
elements := strings.SplitN(line, ":", 2)
if len(elements) != 2 {
return fields, fmt.Errorf("bad line: %q has no ':'", line)
}
key, value = strings.TrimSpace(elements[0]), strings.TrimSpace(elements[1])
if key == "" {
return fields, fmt.Errorf("bad line: %q has an empty key", line)
}
}
}
if key != "" {
fields[key] = value
}
if s.Err() != nil {
return fields, fmt.Errorf("scanning lines: %w", s.Err())
}
return fields, nil
}
var pkgRexp = regexp.MustCompilePOSIX("^[[:space:]]*([^[:space:]]+)([[:space:]]+\\(.*\\))?[[:space:]]*$")
func parseDeps(depsLine string) ([]string, error) {
var deps []string
for _, dep := range strings.Split(depsLine, ",") {
if dep == "" {
continue
}
matches := pkgRexp.FindStringSubmatch(dep)
if len(matches) == 0 {
return nil, fmt.Errorf("unable to parse R package dependency %q", dep)
}
dep = matches[1]
if dep != "" {
deps = append(deps, matches[1])
}
}
return deps, nil
}
func readExcludePatterns(path string) ([]*regexp.Regexp, error) {
var patterns []*regexp.Regexp
pats, err := readLinesFromPath(path)
if err != nil {
return nil, err
}
for _, pat := range pats {
pat = strings.TrimSpace(pat)
if pat == "" {
continue
}
r, err := regexp.Compile(`(?i)` + pat)
if err != nil {
return nil, fmt.Errorf("in %q, can not compile regular expression %q", path, pat)
}
patterns = append(patterns, r)
}
return patterns, nil
}
func readLinesFromPath(path string) ([]string, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
lines, err := readLines(f)
if err != nil {
return lines, fmt.Errorf("reading %q: %w", path, err)
}
return lines, err
}
func readLines(r io.Reader) ([]string, error) {
s := bufio.NewScanner(r)
var lines []string
for s.Scan() {
lines = append(lines, s.Text())
}
return lines, s.Err()
}