|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strconv" |
| 10 | +) |
| 11 | + |
| 12 | +// This is a script used to generate test data for the diff library. It uses pairs of files |
| 13 | +// to get diffed acquired from |
| 14 | +// https://github.com/vmarkovtsev/treediff/blob/49356e7f85c261ed88cf46326791765c58c22b5b/dataset/flask.tar.xz |
| 15 | +// It uses https://github.com/bblfsh/client-go#Installation to convert python sources into |
| 16 | +// uast yaml files. |
| 17 | +// This program outputs commands on the stdout - they are to be piped to sh/bash. It's a good |
| 18 | +// idea to inspect them before running by just reading the textual output of the program. |
| 19 | +// The program needs to be ran with proper commandline arguments. Example below. The cli arguments |
| 20 | +// are also documented if you run `./uast-diff-create-testdata --help`. |
| 21 | +// $ ./uast-diff-create-testdata -d ~/data/sourced/treediff/python-dataset -f smalltest.txt -o . | sh - |
| 22 | + |
| 23 | +var datasetPath = flag.String("d", "./", "Path to the python-dataset (unpacked flask.tar.gz)") |
| 24 | +var testnamesPath = flag.String("f", "./smalltest.txt", "File with testnames") |
| 25 | +var outPath = flag.String("o", "./", "Output directory") |
| 26 | + |
| 27 | +func firstFile(dirname string, fnamePattern string) string { |
| 28 | + fns, err := filepath.Glob(filepath.Join(dirname, fnamePattern)) |
| 29 | + if err != nil { |
| 30 | + panic(err) |
| 31 | + } |
| 32 | + return fns[0] |
| 33 | +} |
| 34 | + |
| 35 | +func main() { |
| 36 | + flag.Parse() |
| 37 | + _, err := os.Open(*datasetPath) |
| 38 | + if err != nil { |
| 39 | + panic(err) |
| 40 | + } |
| 41 | + |
| 42 | + testnames, err := os.Open(*testnamesPath) |
| 43 | + if err != nil { |
| 44 | + panic(err) |
| 45 | + } |
| 46 | + |
| 47 | + scanner := bufio.NewScanner(testnames) |
| 48 | + scanner.Split(bufio.ScanLines) |
| 49 | + |
| 50 | + i := 0 |
| 51 | + for scanner.Scan() { |
| 52 | + name := scanner.Text() |
| 53 | + if name == "" { |
| 54 | + continue |
| 55 | + } |
| 56 | + src := firstFile(*datasetPath, name+"_before*.src") |
| 57 | + dst := firstFile(*datasetPath, name+"_after*.src") |
| 58 | + iStr := strconv.Itoa(i) |
| 59 | + fmt.Println("bblfsh-cli -l python " + src + " -o yaml > " + |
| 60 | + filepath.Join(*outPath, iStr+"_src.uast")) |
| 61 | + fmt.Println("bblfsh-cli -l python " + dst + " -o yaml > " + |
| 62 | + filepath.Join(*outPath, iStr+"_dst.uast")) |
| 63 | + i = i + 1 |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments