diff --git a/src/main/kotlin/de/mpicbg/scicomp/kutils/Bash.kt b/src/main/kotlin/de/mpicbg/scicomp/kutils/Bash.kt index 122e294..7790c81 100644 --- a/src/main/kotlin/de/mpicbg/scicomp/kutils/Bash.kt +++ b/src/main/kotlin/de/mpicbg/scicomp/kutils/Bash.kt @@ -11,8 +11,7 @@ data class BashResult(val exitCode: Int, val stdout: Iterable, val stder } -fun evalBash(cmd: String, showOutput: Boolean = false, - redirectStdout: File? = null, redirectStderr: File? = null, wd: File? = null): BashResult { +fun evalBash(cmd: String, showOutput: Boolean = false, wd: File? = null): BashResult { try { @@ -20,12 +19,15 @@ fun evalBash(cmd: String, showOutput: Boolean = false, val cmd = (if (wd != null) "cd '${wd.absolutePath}'\n" else "") + cmd - var pb = ProcessBuilder("/bin/bash", "-c", cmd) //.inheritIO(); + var pb = ProcessBuilder("/bin/bash", "-c", cmd) + if (showOutput) { + pb.inheritIO() + } pb.directory(File(".")); var p = pb.start(); - val outputGobbler = StreamGobbler(p.getInputStream(), if (showOutput) System.out else null) - val errorGobbler = StreamGobbler(p.getErrorStream(), if (showOutput) System.err else null) + val outputGobbler = StreamGobbler(p.getInputStream()) + val errorGobbler = StreamGobbler(p.getErrorStream()) // kick them off errorGobbler.start() @@ -40,7 +42,7 @@ fun evalBash(cmd: String, showOutput: Boolean = false, } -internal class StreamGobbler(var inStream: InputStream, val printStream: PrintStream?) : Thread() { +internal class StreamGobbler(var inStream: InputStream) : Thread() { var sb = StringBuilder() override fun run() { @@ -49,7 +51,6 @@ internal class StreamGobbler(var inStream: InputStream, val printStream: PrintSt val br = BufferedReader(isr) for (line in br.linesJ7()) { sb.append(line!! + "\n") - printStream?.println(line) } } catch (ioe: IOException) { ioe.printStackTrace() diff --git a/tools/orphan_images/README.md b/tools/orphan_images/README.md deleted file mode 100644 index 081e37d..0000000 --- a/tools/orphan_images/README.md +++ /dev/null @@ -1,21 +0,0 @@ -## Find Orphan Images - -Checks a couple of markdown documents for the absence/presence of images files in a directory, and removes orphane images, that is images which are not referenced in the markdown documents. - - -# Installation - -```bash -export PATH=/Users/brandl/projects/kotlin/kutils/tools/orphan_images:$PATH -orphimg.kt --help - -``` - -or use a url-cached copy - -```bash -alias orphimg=`kscript https://github.com/holgerbrandl/kutils/blob/master/tools/orphan_images/orphimg.kt' - - -orphimg --hell -``` diff --git a/tools/orphan_images/orphimg.kt b/tools/orphan_images/orphimg.kt deleted file mode 100644 index 53790cf..0000000 --- a/tools/orphan_images/orphimg.kt +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env kscript - -@file:DependsOn("de.mpicbg.scicomp:kutils:0.9.0") -@file:DependsOn("com.xenomachina:kotlin-argparser:2.0.7") - -import com.xenomachina.argparser.ArgParser -import com.xenomachina.argparser.mainBody -import java.io.File - -class MyArgs(parser: ArgParser) { - - val autoRemove by parser.flagging("-a", "--auto", help = "Automatically remove orphan images") - val fileNameOnly by parser.flagging("-f", "--file-only", help = "Just take the last path element (aka file name) for usage search") - val recursive by parser.flagging("-r", "--recursive", help = "Recursively scan the target directory for image files") - val imageDirectory by parser.positional("DIRECTORY", "The directory which should be cleaned for orphan images") - - val markdownFiles by parser.positionalList("MD-FILES", "Markdown files to be used as reference to detect orphaness") -} - -fun main(_args: Array) { - val args = mainBody { ArgParser(_args).parseInto(::MyArgs) } - - fun File.isImageFile() = listOf("jpg", "png").contains(extension) - - // see also https://stackoverflow.com/questions/2056221/recursively-list-files-in-java - val imageDir = File(args.imageDirectory) - - val imagesFiles = if (args.recursive) { - imageDir.walkTopDown().maxDepth(3).filter { it.isImageFile() } - } else { - imageDir.listFiles { it -> it.isImageFile() }.asSequence() - } - - - // ingest all markdown files - val mdContent = args.markdownFiles.flatMap { File(it).readLines() } - - // check for orphanness - val orphans = imagesFiles.filter { imgFile -> - val query = if (args.fileNameOnly) imgFile.name else imageDir.name + "/" + imgFile.name - - mdContent.none { - it.contains(query) - } - } - - orphans.map { println(it) } - - if(args.autoRemove){ - print("Removing orphans...") - orphans.forEach{it.delete()} - println("Done") - } -} -