* added helper for running system commands from the groovy shell

This commit is contained in:
Reinhard Pointner 2011-11-29 06:03:41 +00:00
parent 1c8287169a
commit cfb6512063
3 changed files with 56 additions and 4 deletions

View File

@ -50,7 +50,7 @@ public class Main {
final ArgumentProcessor cli = new ArgumentProcessor(); final ArgumentProcessor cli = new ArgumentProcessor();
final ArgumentBean args = cli.parse(arguments); final ArgumentBean args = cli.parse(arguments);
if (args.printHelp() || args.printVersion() || isHeadless()) { if (args.printHelp() || args.printVersion() || (!args.runCLI() && isHeadless())) {
System.out.format("%s / %s%n%n", getApplicationIdentifier(), getJavaRuntimeIdentifier()); System.out.format("%s / %s%n%n", getApplicationIdentifier(), getJavaRuntimeIdentifier());
if (args.printHelp() || (!args.printVersion() && isHeadless())) { if (args.printHelp() || (!args.printVersion() && isHeadless())) {

View File

@ -1,12 +1,15 @@
// File selector methods
import static groovy.io.FileType.* import static groovy.io.FileType.*
File.metaClass.plus = { path -> new File(delegate, path) }
File.metaClass.listFiles = { c -> delegate.isDirectory() ? delegate.listFiles().findAll(c) : []}
File.metaClass.isVideo = { _types.getFilter("video").accept(delegate) } File.metaClass.isVideo = { _types.getFilter("video").accept(delegate) }
File.metaClass.isAudio = { _types.getFilter("audio").accept(delegate) } File.metaClass.isAudio = { _types.getFilter("audio").accept(delegate) }
File.metaClass.isSubtitle = { _types.getFilter("subtitle").accept(delegate) } File.metaClass.isSubtitle = { _types.getFilter("subtitle").accept(delegate) }
File.metaClass.isVerification = { _types.getFilter("verification").accept(delegate) } File.metaClass.isVerification = { _types.getFilter("verification").accept(delegate) }
File.metaClass.plus = { path -> new File(delegate, path) }
File.metaClass.hasFile = { c -> isDirectory() && listFiles().find{ c.call(it) }} File.metaClass.hasFile = { c -> isDirectory() && listFiles().find{ c.call(it) }}
File.metaClass.getFiles = { def files = []; traverse(type:FILES) { files += it }; return files } File.metaClass.getFiles = { def files = []; traverse(type:FILES) { files += it }; return files }
@ -22,7 +25,7 @@ String.metaClass.eachMediaFolder = { c -> new File(delegate).eachMediaFolder(c)
List.metaClass.eachMediaFolder = { c -> getFolders().findAll{ it.hasFile{ it.isVideo() } }.each(c) } List.metaClass.eachMediaFolder = { c -> getFolders().findAll{ it.hasFile{ it.isVideo() } }.each(c) }
// FileUtilities // File utility methods
import static net.sourceforge.tuned.FileUtilities.*; import static net.sourceforge.tuned.FileUtilities.*;
File.metaClass.getNameWithoutExtension = { getNameWithoutExtension(delegate.getName()) } File.metaClass.getNameWithoutExtension = { getNameWithoutExtension(delegate.getName()) }
@ -32,15 +35,31 @@ File.metaClass.isDerived = { f -> isDerived(delegate, f) }
File.metaClass.validateFileName = { validateFileName(delegate) } File.metaClass.validateFileName = { validateFileName(delegate) }
File.metaClass.validateFilePath = { validateFilePath(delegate) } File.metaClass.validateFilePath = { validateFilePath(delegate) }
File.metaClass.moveTo = { f -> renameFile(delegate, f) } File.metaClass.moveTo = { f -> renameFile(delegate, f) }
List.metaClass.mapByFolder = { mapByFolder(delegate) } List.metaClass.mapByFolder = { mapByFolder(delegate) }
List.metaClass.mapByExtension = { mapByExtension(delegate) } List.metaClass.mapByExtension = { mapByExtension(delegate) }
// Shell helper
import static com.sun.jna.Platform.*;
def run(String... cmd) {
cmd = cmd.toList()
if (isWindows()) {
cmd = ["cmd", "/c"] + cmd;
}
// run command and print output
def process = cmd.execute()
process.waitForProcessOutput(System.out, System.err)
return process.exitValue()
}
// Script helper
def require(cond) { if (!cond()) throw new Exception('Require failed') } def require(cond) { if (!cond()) throw new Exception('Require failed') }
// CLI bindings
def rename(args) { args = _defaults(args) def rename(args) { args = _defaults(args)
_guarded { _cli.rename(_files(args), args.query, args.format, args.db, args.lang, args.strict) } _guarded { _cli.rename(_files(args), args.query, args.format, args.db, args.lang, args.strict) }
} }

View File

@ -0,0 +1,33 @@
// Settings
def source = "X:/source"
def target = "Y:/target"
def episodeFormat = "{n}{'/Season '+s}/{episode}"
def movieFormat = "{movie}/{movie}"
def exclude(file) {
file =~ /\p{Punct}chunk/
}
/*
* Fetch subtitles and sort into folders
*/
"$source/TV".eachMediaFolder() { dir ->
def files = dir.listFiles { !exclude(it) }
// fetch subtitles
files += getSubtitles(file:files)
// sort episodes / subtitles
rename(file:files, db:'TVRage', format:"$target/TV/$episodeFormat")
}
"$source/Movies".eachMediaFolder() { dir ->
def files = dir.listFiles { !exclude(it) }
// fetch subtitles
files += getSubtitles(file:files)
// sort movies / subtitles
rename(file:files, db:'TheMovieDB', format:"$target/Movies/$movieFormat")
}