-
-
Save stephenmcgruer/0b84c426f2840003c542bcb25740a9d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"path" | |
"github.com/web-platform-tests/wpt.fyi/shared" | |
"strings" | |
"gopkg.in/yaml.v2" | |
) | |
func main() { | |
f, err := os.Open("processed-tests.txt") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer f.Close() | |
scanner := bufio.NewScanner(f) | |
for scanner.Scan() { | |
monorailResult := scanner.Text() | |
testname, bug := parseMonorailResult(monorailResult) | |
if testname == "" { | |
continue | |
} | |
toWPTMetadata(bug, testname[1:]) | |
} | |
if err := scanner.Err(); err != nil { | |
log.Fatal(err) | |
} | |
} | |
func parseMonorailResult(monorailResult string) (string, string) { | |
split := strings.Split(monorailResult, "=>") | |
testname := strings.TrimSpace(split[0]) | |
bugList := strings.TrimSpace(split[1]) | |
if bugList == "[]" { | |
return "", "" | |
} | |
bugs := strings.Split(bugList, ",") | |
if len(bugs) != 1 { | |
return "", "" | |
} | |
bug := bugs[0][1 : len(bugs[0])-1] | |
return testname, bug | |
} | |
// toWPTMetadata converts a (bug, testname) pair to a wpt-metadata META.yml | |
// entry and writes it to disk. | |
// | |
// Directories and META.yml files are created as needed, or an existing one may | |
// be updated. | |
func toWPTMetadata(bug string, testname string) { | |
url := "https://crbug.com/" + bug | |
dirpath := path.Dir(testname) | |
testpath := path.Base(testname) | |
createDirIfNeeded(dirpath) | |
// Open the META.yml file that we would write to, if it exists. In the case | |
// where it does exist we don't want to overwrite the file, so open in | |
// read-write-or-create mode. | |
filepath := dirpath + "/META.yml" | |
f, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE, 0755) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer f.Close() | |
fi, err := f.Stat() | |
if err != nil { | |
log.Fatal(err) | |
} | |
if fi.Size() == 0 { | |
// This is an entirely new META.yml file, so create a new | |
// shared.Metadata representation for our (testname, url) and write it out. | |
fmt.Println("CREATED-META-YML: for test", testname, "with url", url) | |
metadata := shared.Metadata{ | |
Links: []shared.MetadataLink{ | |
{ | |
Product: shared.ParseProductSpecUnsafe("chrome"), | |
URL: url, | |
Results: []shared.MetadataTestResult{{ | |
TestPath: testpath, | |
}}, | |
}, | |
}, | |
} | |
writeToFile(metadata, f) | |
return | |
} | |
data, err := ioutil.ReadAll(f) | |
if err != nil { | |
log.Fatal(err) | |
} | |
var metadata shared.Metadata | |
err = yaml.Unmarshal(data, &metadata) | |
if err != nil { | |
fmt.Println("ERROR for test", testname,"with url", url) | |
log.Fatal(err) | |
} | |
// First check whether we have an existing crbug link in this META.yml; if | |
// so we just want to append our testname to the set of tests associated | |
// with it. | |
hasAdded := false | |
for index, link := range metadata.Links { | |
if link.Product.String() != "chrome" { | |
continue | |
} | |
// Check whether we are already listed in this entry. | |
for _, result := range link.Results { | |
if result.TestPath == testpath { | |
fmt.Println("EXISTING-LINK: for test", testname, "with url", url) | |
return | |
} | |
} | |
if link.URL == url { | |
fmt.Println("APPEND-LINK: for test", testname, "with url", url) | |
metadata.Links[index].Results = append(link.Results, shared.MetadataTestResult{TestPath: testpath}) | |
hasAdded = true | |
break | |
} | |
} | |
// Otherwise, this is a brand new crbug entry in the META.yml, so add a new | |
// link to it. | |
// | |
// TODO: This does not handle the case where a single test may be | |
// associated with multiple links - should it? | |
if !hasAdded { | |
fmt.Println("NEW-LINK: for test", testname, "with url", url) | |
newLink := shared.MetadataLink{ | |
Product: shared.ParseProductSpecUnsafe("chrome"), | |
URL: url, | |
Results: []shared.MetadataTestResult{{ | |
TestPath: testpath, | |
}}, | |
} | |
metadata.Links = append(metadata.Links, newLink) | |
} | |
writeToFile(metadata, f) | |
} | |
// createDirIfNeeded is a helper function to create a directory if it doesn't | |
// already exist. It handles recursive directory creation. | |
func createDirIfNeeded(dir string) { | |
_, err := os.Stat(dir) | |
if !os.IsNotExist(err) { | |
return | |
} | |
os.MkdirAll(dir, os.ModePerm) | |
} | |
// writeToFile writes out a wpt.fyi shared.Metadata entry to a given file handle. | |
// | |
// TODO: This seems to cause a lot of existing metadata files to be completely | |
// rewritten; what's the underlying problem here? | |
func writeToFile(metadata shared.Metadata, f *os.File) { | |
// Clear out existing content. | |
f.Truncate(0) | |
metadataBytes, err := yaml.Marshal(metadata) | |
if err != nil { | |
fmt.Println("ERROR when writing to file") | |
log.Fatal(err) | |
} | |
_, err = f.WriteAt(metadataBytes, 0) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment