From 42f9f66793dd788667e4a8a3adce24a5dfe036d8 Mon Sep 17 00:00:00 2001 From: Matt Soucy Date: Wed, 17 Jun 2020 21:37:46 -0400 Subject: [PATCH] ls --- src/main/kotlin/me/msoucy/gbat/Main.kt | 87 +++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/me/msoucy/gbat/Main.kt b/src/main/kotlin/me/msoucy/gbat/Main.kt index 6520703..88f9661 100644 --- a/src/main/kotlin/me/msoucy/gbat/Main.kt +++ b/src/main/kotlin/me/msoucy/gbat/Main.kt @@ -1,7 +1,11 @@ package me.msoucy.gbat import java.io.File -import java.lang.System +import java.io.IOException +import java.util.concurrent.TimeUnit +import kotlin.math.pow +import kotlin.text.Regex +import kotlin.text.RegexOption import kotlin.text.startsWith import com.xenomachina.argparser.ArgParser @@ -46,22 +50,20 @@ fun validateGit(exe : String) : String { class GbatArgs(parser: ArgParser) { // Input options val interesting by parser.adding("--interesting", "-I", - help="Regular expression to determine which files should be included in calculations.", - initialValue=DEFAULT_INTERESTING_RES - ) { this } + help="Regular expression to determine which files should be included in calculations.") val not_interesting by parser.adding("--not-interesting", "-N", help="Regular expression to determine which files should not be included in calculations.") val case_sensitive by parser.flagging("Use case sensitive regexps when determining interesting files (default is case-insensitive)") val departed by parser.storing("--departed-file", "-D", help="File listing departed devs, one per line").default(null) val risk_file by parser.storing("--bus-risk-file", help="File of dev=float lines (e.g. ejorgensen=0.4) with custom bus risks for devs").default(null) - val default_bus_risk by parser.storing("--default-bus-risk", help="Default risk that a dev will be hit by a bus in your analysis timeframe (defaults to 0.1).").default(0.1) + val default_bus_risk by parser.storing("--default-bus-risk", help="Default risk that a dev will be hit by a bus in your analysis timeframe (defaults to 0.1).") { toDouble() }.default(0.1) // Multiprocessing options val num_git_procs by parser.storing("--num-git-procs", help="The number of git processes to run simultaneously (defaults to 3)") { toInt() }.default(3) val num_analyzer_procs by parser.storing("--num-analyzer-procs", help="The number of analyzer processes to run (defaults to 3)") { toInt() }.default(3) // Tuning options - val risk_threshold by parser.storing("--risk-threshold", help="Threshold past which to summarize risk (defaults to default bus risk cubed)") {toDouble()}.default(null) + val risk_threshold by parser.storing("--risk-threshold", help="Threshold past which to summarize risk (defaults to default bus risk cubed)") { toDouble() }.default(null) val creation_constant by parser.storing("--knowledge-creation-constant", help="How much knowledge a changed line should create if a new line creates 1 (defaults to 0.1)") {toDouble()}.default(null) // Misc options @@ -70,12 +72,81 @@ class GbatArgs(parser: ArgParser) { val output by parser.storing("Output directory for data files and html summary (defaults to \"output\"), error if already exists").default("output") // Directory - val root by parser.positional("The root directory to inspect") + val project_root by parser.positional("The root directory to inspect") +} + +fun List.runCommand(workingDir: File): Pair { + try { + val proc = ProcessBuilder(*this.toTypedArray()) + .directory(workingDir) + .redirectOutput(ProcessBuilder.Redirect.PIPE) + .redirectError(ProcessBuilder.Redirect.PIPE) + .start() + + proc.waitFor(60, TimeUnit.MINUTES) + return Pair(proc.inputStream.bufferedReader().readText(), + proc.errorStream.bufferedReader().readText()) + } catch(e: IOException) { + e.printStackTrace() + return Pair(null, null) + } +} + +class GitRepo(val project_root : File, val git_exe : String) { + fun ls() : String { + val cmd = listOf( + git_exe, + "ls-tree", + "--full-tree", + "--name-only", + "-r", + "HEAD", + project_root.absolutePath + ) + val (out, err) = cmd.runCommand(project_root) + return out ?: "" + } } fun main(args: Array) = mainBody { ArgParser(args).parseInto(::GbatArgs).run { println("Hello world") - println(validateGit(git_exe)) + println(validateGit(git_exe)) + + val outDir = File(output) + if(outDir.isDirectory) { + throw InvalidArgumentException("Output directory already exists") + } + + outDir.mkdirs() + + fun parse_interesting(theList : List) = + theList.map { + if(case_sensitive) { + Regex(it) + } else { + Regex(it, RegexOption.IGNORE_CASE) + } + } + + val risk_thresh = risk_threshold ?: default_bus_risk.pow(3) + val interesting_res = if (interesting.isEmpty()) DEFAULT_INTERESTING_RES else { + parse_interesting(interesting) + } + val not_interesting_res = if (not_interesting.isEmpty()) listOf() else { + parse_interesting(not_interesting) + } + + val project_root_file = File(project_root).also { + if(!it.isDirectory) + throw InvalidArgumentException("Provided project root does not exist") + } + println(risk_thresh) + println(interesting_res) + println(not_interesting_res) + println(project_root_file) + + val repo = GitRepo(project_root_file, validateGit(git_exe)) + println(repo.ls()) } }