-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathextractor.go
101 lines (82 loc) · 2.81 KB
/
extractor.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
package util
import (
"encoding/json"
"log"
"os"
"path/filepath"
)
// Gets the path of the JSON file in the database scratch directory that is used
// to store extraction results.
func extractionResultsPath() string {
return filepath.Join(ScratchDir(), "extraction.json")
}
// Represents results of an extractor run that are of interest to the autobuilder.
type ExtractionResult struct {
// The number of packages that were extracted.
PackageCount int `json:"packageCount"`
// Indicates whether there are Go sources for this project.
HasSources bool `json:"hasSources"`
}
// Represents a mapping of module roots to extraction results.
type ExtractionResults map[string]ExtractionResult
/* Returns the total number of packages extracted */
func (results ExtractionResults) TotalPackageCount() int {
result := 0
for _, v := range results {
result += v.PackageCount
}
return result
}
/* Returns a value indicating whether any Go source files were found */
func (results ExtractionResults) HasSources() bool {
for _, v := range results {
if v.HasSources {
return true
}
}
return false
}
// Reads extraction results produced by the extractor from a well-known location in the
// database scratch directory and stores them in `results`. Returns `nil` if successful
// or an error if not. Note that if the file does not exist, `results` are not modified
// and `nil` is returned. If it matters whether the file was created by an extractor
// run, then this should be checked explicitly.
func ReadExtractionResults(results *ExtractionResults) error {
path := extractionResultsPath()
if FileExists(path) {
contents, err := os.ReadFile(path)
if err != nil {
log.Printf("Found %s, but could not read it: %s\n", path, err)
return err
}
if err = json.Unmarshal(contents, results); err != nil {
log.Printf("Failed to unmarshal JSON from %s: %s\n", path, err)
return err
}
}
return nil
}
// Writes an extraction `result` for the module at root `wd` to a well-known location in the
// database scratch directory. If the file with extraction results exists already, it is updated.
// Note: this assumes that multiple copies of the extractor are not run concurrently.
func WriteExtractionResult(wd string, result ExtractionResult) {
path := extractionResultsPath()
results := make(ExtractionResults)
// Create the scratch directory, if needed.
if !DirExists(ScratchDir()) {
os.Mkdir(ScratchDir(), 0755)
}
// Read existing extraction results, if there are any.
ReadExtractionResults(&results)
// Store the new extraction result.
results[wd] = result
// Write all results to the file as JSON.
bytes, err := json.Marshal(results)
if err != nil {
log.Printf("Unable to marshal: %s\n", err)
}
err = os.WriteFile(path, bytes, 0644)
if err != nil {
log.Printf("Failed to write to %s: %s\n", path, err)
}
}