This repository has been archived by the owner on Oct 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
analyzer.go
186 lines (160 loc) · 4.96 KB
/
analyzer.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
/*
* Copyright 2014 Google Inc. All rights reserved.
*
* 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 androidlint
import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"github.com/golang/protobuf/proto"
notepb "github.com/google/shipshape/shipshape/proto/note_proto"
ctxpb "github.com/google/shipshape/shipshape/proto/shipshape_context_proto"
rangepb "github.com/google/shipshape/shipshape/proto/textrange_proto"
)
const (
androidManifest = "AndroidManifest.xml"
report = "lint-report.xml"
)
var (
exitStatus = "exit status 1"
lintBin = "lint"
)
// Analyzer is a wrapper around the Android lint tool. This analyzer looks for
// any files in the depot path that exist within an Android project. If any
// exist, it runs Android lint on the entire project.
type Analyzer struct{}
func (Analyzer) Category() string { return "AndroidLint" }
// Analyze runs android lint on all Android projects that are implicitly
// referenced by the list of files on the depot path. In case of an error, it
// returns partial results.
func (ala Analyzer) Analyze(ctx *ctxpb.ShipshapeContext) ([]*notepb.Note, error) {
var notes []*notepb.Note
// Get the list of Android Projects
projects := getAndroidProjects(ctx.FilePath)
for prj := range projects {
tempReport, err := ioutil.TempFile("", report)
if err != nil {
return notes, fmt.Errorf("Could not create temp report file %s: %v", report, err)
}
defer os.Remove(tempReport.Name())
// TODO(ciera): Add project options (--classpath) when we have build information.
// TODO(clconway): The path to the binary should be configurable, especially since
// the "lint" command name is overloaded.
cmd := exec.Command(lintBin,
"--showall",
"--quiet",
"--exitcode",
"--xml", tempReport.Name(),
prj)
out, err := cmd.CombinedOutput()
log.Printf("lint output is %q", out)
switch err := err.(type) {
case nil:
// no issues. Do nothing.
case *exec.ExitError:
if err.Error() != exitStatus {
return notes, fmt.Errorf("Unexpected error code from android lint: %v", err)
}
// Get the results from xml
data, xmlErr := ioutil.ReadFile(tempReport.Name())
if xmlErr != nil {
return notes, fmt.Errorf("could not read %s : %v", tempReport.Name(), xmlErr)
}
var issues IssuesList
xmlErr = xml.Unmarshal(data, &issues)
if xmlErr != nil {
return notes, fmt.Errorf("could not unmarshal XML from %s: %v", tempReport.Name(), xmlErr)
}
// Create a bunch of notes to return, one per line from the output.
for _, issue := range issues.Issues {
notes = append(notes, ¬epb.Note{
Category: proto.String(ala.Category()),
Subcategory: proto.String(issue.Subcategory),
Description: proto.String(issue.Message),
Location: ¬epb.Location{
SourceContext: ctx.SourceContext,
Path: proto.String(filepath.Join(prj, issue.Location.File)),
Range: &rangepb.TextRange{
StartLine: proto.Int(issue.Location.Line),
StartColumn: proto.Int(issue.Location.Column),
},
},
})
}
case *exec.Error:
return notes, err
default:
return notes, err
}
}
return notes, nil
}
// Methods for determining where the android projects exist
func getAndroidProjects(paths []string) map[string]bool {
pPaths := make(map[string]bool)
for _, path := range paths {
project, ok := getProject(filepath.Clean(path))
if ok {
pPaths[project] = true
}
}
return pPaths
}
func getProject(path string) (string, bool) {
fi, err := os.Stat(path)
if err != nil {
log.Printf("Could not find path %s: %v", path, err)
return "", false
}
if fi.IsDir() {
isAndroid := isAndroidRoot(path)
if isAndroid || path == "." || path == string(filepath.Separator) {
return path, isAndroid
}
}
return getProject(filepath.Dir(path))
}
func isAndroidRoot(path string) bool {
files, err := ioutil.ReadDir(path)
if err != nil {
log.Printf("had invalid path in android lint: %s %v", path, err)
return false
}
for _, file := range files {
if file.Name() == androidManifest {
return true
}
}
return false
}
// Types below are for extracting the messages
// out of the XML report
type IssuesList struct {
Issues []Issue `xml:"issue"`
}
type Issue struct {
Subcategory string `xml:"id,attr"`
Message string `xml:"message,attr"`
Location Location `xml:"location"`
}
type Location struct {
File string `xml:"file,attr"`
Line int `xml:"line,attr"`
Column int `xml:"column,attr"`
}