Commit f59ae2c7 authored by Mohamed, Fawzi Roberto (fawzi)'s avatar Mohamed, Fawzi Roberto (fawzi)
Browse files

h5Tool set

parent 4113d9cb
Pipeline #37125 failed with stages
in 32 minutes and 13 seconds
......@@ -62,6 +62,7 @@ object H5Tool extends StrictLogging {
case "count" => countCmd(list)
case "extract" => extractCmd(list)
case "rename" => renameCmd(list)
case "set" => setCmd(list)
case command =>
println(s"invalid command $command")
println(usage)
......@@ -249,7 +250,6 @@ object H5Tool extends StrictLogging {
val targetDirRe = "^--target-dir=(.*)$".r
var replacements: Seq[Replacements] = Seq()
var sourcePaths: Seq[Path] = Seq()
var sourcePathsFiles: Seq[Path] = Seq()
var targetDir: Option[Path] = None
var flatDir: Boolean = false
while (!list.isEmpty) {
......@@ -266,7 +266,8 @@ object H5Tool extends StrictLogging {
case sourcePathRe(p) =>
sourcePaths = sourcePaths :+ Paths.get(p)
case sourcePathsFileRe(p) =>
sourcePathsFiles = sourcePathsFiles :+ Paths.get(p)
val sFile = Source.fromFile(p)
sourcePaths ++= sFile.getLines().map(p => Paths.get(p))
case "--flat-dir" => flatDir = true
case "--source-paths" =>
sourcePaths ++= list.map { x: String => Paths.get(x) }
......@@ -276,7 +277,7 @@ object H5Tool extends StrictLogging {
}
}
def renameFile(inF: Path): Unit = {
for (inF <- sourcePaths) {
val targetPath = targetDir match {
case Some(dir) =>
val tPath = flatDir match {
......@@ -303,13 +304,96 @@ object H5Tool extends StrictLogging {
}
targetF.release()
}
for (inF <- sourcePaths)
renameFile(inF)
for (sFilePath <- sourcePathsFiles) {
val sFile = Source.fromFile(sFilePath.toFile)
for (p <- sFile.getLines()) {
renameFile(Paths.get(p))
}
private val setCmdUsage = """Usage:
|h5Tool set
| [--uploader=<uploader>]
| [--source-path=<pathToH5Archive>]
| [--source-paths-file=<pathToFileWithH5ArchivesPaths>]
| [--target-dir=<pathToOutputDirectory>]
| [--flat-dir]
| [--source-paths [path1 [path2 ...]]]
|
|Sets the uploader for all calculations in the archive.
|""".stripMargin
/**
* sets the uploader in h5 files
*/
def setCmd(args: List[String]): Unit = {
var list: List[String] = args
var uploaderRe = "^--uploader=(.*)$".r
val sourcePathRe = "^--source-path=(.*)$".r
val sourcePathsFileRe = "^--source-paths-file=(.*)$".r
val targetDirRe = "^--target-dir=(.*)$".r
var uploader: Option[String] = None
var sourcePaths: Seq[Path] = Seq()
var targetDir: Option[Path] = None
var flatDir: Boolean = false
while (!list.isEmpty) {
val arg = list.head
list = list.tail
arg match {
case "--help" | "-h" =>
println(renameCmdUsage)
return
case uploaderRe(upl) =>
uploader = Some(upl)
case targetDirRe(targetD) =>
targetDir = Some(Paths.get(targetD))
case sourcePathRe(p) =>
sourcePaths = sourcePaths :+ Paths.get(p)
case sourcePathsFileRe(p) =>
val sFile = Source.fromFile(p)
sourcePaths ++= sFile.getLines().map(p => Paths.get(p))
case "--flat-dir" => flatDir = true
case "--source-paths" =>
sourcePaths ++= list.map { x: String => Paths.get(x) }
list = Nil
case other =>
throw new Exception(s"unexpected argument '$other'")
}
}
uploader match {
case None => return
case Some(uploader_name) =>
for (inF <- sourcePaths) {
val targetPath = targetDir match {
case Some(dir) =>
val tPath = flatDir match {
case true =>
dir.resolve(inF.getFileName)
case false =>
dir.resolve(inF.getFileName.toString.take(3)).resolve(inF.getFileName)
}
if (!Files.exists(tPath.getParent))
Files.createDirectories(tPath.getParent, LocalEnv.directoryPermissionsAttributes)
Files.copy(inF, tPath, StandardCopyOption.REPLACE_EXISTING)
val lockFilePath = Paths.get(tPath.toString + ".lock")
Files.deleteIfExists(lockFilePath)
tPath
case None =>
inF
}
val targetF = FileH5.open(targetPath, write = true)
try {
for (arch <- targetF.archives()) {
for (calc <- arch.calculations()) {
val cInfo = calc.sectionTable(Seq("section_calculation_info"))
if (cInfo.isEmpty) {
cInfo.subValueTable("calculation_uploader_name")
assert(false, "to do")
}
}
}
} catch {
case NonFatal(e) =>
logger.error(s"Failure while doing replacements in file at $targetPath", e)
}
targetF.release()
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment