From 79287b40f41dcbafdfaeac11c32b5072015b2b9c Mon Sep 17 00:00:00 2001 From: Fabio Petrillo Date: Sat, 24 Mar 2018 22:34:44 -0400 Subject: [PATCH] Challenge using parsing on unix tools --- main.go | 71 +++++++++++++++++++++++++++++++++++++++++++-------------- run.sh | 7 ++++++ 2 files changed, 61 insertions(+), 17 deletions(-) create mode 100755 run.sh diff --git a/main.go b/main.go index f62aa00..5a4ed3e 100644 --- a/main.go +++ b/main.go @@ -3,35 +3,72 @@ package main import ( "fmt" "time" + "os" + "bufio" + "strings" + "strconv" + "os/exec" ) -//timeTrack tracks the time it took to do things. -//It's a convenient method that you can use everywhere -//you feel like it func timeTrack(start time.Time, name string) { elapsed := time.Since(start) fmt.Printf("%s took %s\n", name, elapsed) } -//main is the entry point of our go program. It defers -//the execution of timeTrack so we can know how long it -//took for the main to complete. -//It also calls the compute and output the returned struct -//to stdout. func main() { defer timeTrack(time.Now(), "compute diff") fmt.Println(compute()) } -//compute parses the git diffs in ./diffs and returns -//a result struct that contains all the relevant informations -//about these diffs -// list of files in the diffs -// number of regions -// number of line added -// number of line deleted -// list of function calls seen in the diffs and their number of calls func compute() *result { + var r result - return nil + exec.Command("./run.sh").Run() + + r.files = calcFiles() + r.lineAdded, r.lineDeleted, r.regions = calcSummary() + r.functionCalls = calcCalls() + + return &r +} + +func calcFiles() []string { + return readLine("files.txt") +} + +func calcSummary() (int, int, int) { + summary := readLine("summary.txt") + lines := strings.Split(summary[0]," ") + + addedLines, _ := strconv.Atoi(lines[0]) + deletedLines, _ := strconv.Atoi(lines[1]) + regions, _ := strconv.Atoi(summary[1]) + + return addedLines, deletedLines, regions +} + +func calcCalls() map[string]int { + m := make(map[string]int) + + calls := readLine("calls.txt") + for _, call := range calls{ + c := strings.Split(call," ") + key := c[0] + value,_ := strconv.Atoi(c[1]) + m[key] = value + } + return m +} + +func readLine(path string) []string { + var lines []string + fileHandle, _ := os.Open(path) + defer fileHandle.Close() + fileScanner := bufio.NewScanner(fileHandle) + + for fileScanner.Scan() { + lines = append(lines, fileScanner.Text()) + } + + return lines } diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..50feb4e --- /dev/null +++ b/run.sh @@ -0,0 +1,7 @@ +#!/bin/bash +diffstat -l diffs/*.diff > files.txt& +diffstat -s diffs/*.diff | cut -d' ' -f5,7 > summary.txt; cat diffs/*.diff | grep '^@@' | wc -l >> summary.txt& +export MAX=$(cat diffs/* | grep $'^+\t' | grep -E '[A-Z]\(|[a-z]\(|[0-9]\(|\_\(' | awk 'BEGIN{max=-1}{n=split($0,c,"(")-1;if (n>max) max=n}END{print max}'); for i in $(seq 1 $MAX); do for j in $(seq 1 $i); do cat diffs/* | grep $'^+\t' | grep -E '[A-Z]\(|[a-z]\(|[0-9]\(|\_\(' | awk '{n=split($0,c,"(")-1;if (n!=0) print n,$0}' | grep "^$i" | cut -d"(" -f$j | grep -E '[A-Z]|[a-z]|[0-9]|\_$' | awk '{len=split($0,tokens," "); print tokens[len]}'; done; done | awk '/^[A-Z]|^[a-z]|^\_|^!/{gsub("!",""); print $0}' | grep -v 'for$' | grep -v 'while$' | grep -v 'if$' | cut -d\) -f2 | grep -E '[A-Z]|[a-z]|[0-9]|\_' | sort | uniq -c | awk '{s=$0; split(s,b," "); n=gsub(" ","");print b[2],b[1]}' > calls.txt + + +