Skip to content

Commit

Permalink
lazybox: add lazybox
Browse files Browse the repository at this point in the history
  • Loading branch information
cfig committed Sep 20, 2023
1 parent d558c77 commit 7c8e997
Show file tree
Hide file tree
Showing 11 changed files with 490 additions and 140 deletions.
45 changes: 41 additions & 4 deletions helper/src/main/kotlin/cfig/helper/Helper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ class Helper {
log.error("fail to execute [$cmd]")
ret = false
}
log.debug("stdout [$outStream]")
log.debug("stderr [$errStream]")
log.debug("stdout [{}]", outStream)
log.debug("stderr [{}]", errStream)
return arrayOf(ret, outStream.toByteArray(), errStream.toByteArray())
}

Expand All @@ -284,8 +284,8 @@ class Helper {
} catch (e: ExecuteException) {
log.error("fail to execute [$cmd]")
}
log.debug("stdout [$outStream]")
log.debug("stderr [$errStream]")
log.debug("stdout [{}]", outStream)
log.debug("stderr [{}]", errStream)
return arrayOf(outStream.toByteArray(), errStream.toByteArray())
}

Expand Down Expand Up @@ -397,6 +397,43 @@ class Helper {
return readFully(data, coordinate.first, coordinate.second)
}

fun adbCmd(cmd: String): String {
val outputStream = ByteArrayOutputStream()
val exec = DefaultExecutor()
exec.streamHandler = PumpStreamHandler(outputStream)
val cmdline = "adb shell $cmd"
log.info(cmdline)
exec.execute(CommandLine.parse(cmdline))
//println(outputStream)
return outputStream.toString().trim()
}

fun str2range(str: String): List<IntRange> {
return str.trim().let { ranges ->
val ret = mutableListOf<IntRange>()
ranges.split(",").forEach { rangeItem ->
rangeItem.split("-").let { v ->
when (v.size) {
1 -> {
if (v.get(0).isNotEmpty()) {
ret.add(IntRange(v.get(0).toInt(), v.get(0).toInt()))
} else {
//pass
}
}
2 -> {
ret.add(IntRange(v.get(0).toInt(), v.get(1).toInt()))
}
else -> {
throw IllegalArgumentException("invalid range: [$rangeItem]")
}
}
}
}
ret
}
}

private val log = LoggerFactory.getLogger("Helper")
}
}
21 changes: 21 additions & 0 deletions helper/src/main/kotlin/cfig/helper/ZipHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package cfig.helper
import cc.cfig.io.Struct
import cfig.helper.Helper.Companion.check_call
import cfig.helper.Helper.Companion.check_output
import org.apache.commons.compress.archivers.tar.TarArchiveEntry
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream
import org.apache.commons.compress.archivers.zip.*
import org.apache.commons.compress.compressors.CompressorOutputStream
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream
Expand All @@ -38,6 +40,7 @@ import java.io.*
import java.net.URI
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream
Expand Down Expand Up @@ -590,5 +593,23 @@ class ZipHelper {
}
log.info("compress(bzip2) done: $compressedFile")
}

fun makeTar(compressedFile: String, srcDir: String) {
FileOutputStream(compressedFile).use { fos ->
val appendTarEntry: (TarArchiveOutputStream, String) -> Unit = { tarOut, entry ->
tarOut.putArchiveEntry(TarArchiveEntry(File(entry)))
tarOut.write(File(entry).readBytes())
tarOut.closeArchiveEntry()
}
TarArchiveOutputStream(fos).use { to ->
Files.walk(Paths.get(srcDir))
.filter { Files.isRegularFile(it) }
.forEach {
log.info("tar << $it")
appendTarEntry(to, it.toString())
}
}
}
}
} // end-of-companion
}
47 changes: 47 additions & 0 deletions lazybox/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Kotlin application project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.3/userguide/building_java_projects.html in the Gradle documentation.
*/
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.9.0"
application
}

repositories {
mavenCentral()
}

dependencies {
implementation("org.apache.commons:commons-exec:1.3")
implementation("com.fasterxml.jackson.core:jackson-annotations:2.14.0")
implementation("com.fasterxml.jackson.core:jackson-databind:2.14.0")
implementation("org.slf4j:slf4j-api:2.0.9")
implementation("org.apache.commons:commons-compress:1.21")
implementation(project(mapOf("path" to ":helper")))
// Use the Kotlin JUnit 5 integration.
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
// Use the JUnit 5 integration.
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.9.3")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}

application {
// Define the main class for the application.
mainClass.set("cfig.lazybox.AppKt")
}

tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}
33 changes: 33 additions & 0 deletions lazybox/src/main/kotlin/cfig/lazybox/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cfig.lazybox

import cfig.lazybox.sysinfo.CpuInfo
import cfig.lazybox.sysinfo.SysInfo
import com.fasterxml.jackson.databind.ObjectMapper
import org.slf4j.LoggerFactory
import java.io.File
import kotlin.system.exitProcess

class App

fun main(args: Array<String>) {
val log = LoggerFactory.getLogger(App::class.java)
if (args.isEmpty()) {
println("Usage: args: (Array<String>) ...")
println(" or: function [arguments]...")
println("\nCurrently defined functions:")
println("\tcpuinfo sysinfo")
exitProcess(0)
}
if (args.get(0) == "cpuinfo") {
val ret = CpuInfo.construct()
File("cpuinfo.json").writeText(
ObjectMapper()
.writerWithDefaultPrettyPrinter()
.writeValueAsString(ret)
)
log.info("cpuinfo.json is ready")
}
if (args.get(0) == "sysinfo") {
SysInfo().run()
}
}
Loading

0 comments on commit 7c8e997

Please sign in to comment.