svnno****@sourc*****
svnno****@sourc*****
2010年 1月 28日 (木) 17:39:15 JST
Revision: 103 http://sourceforge.jp/projects/ngms/svn/view?view=rev&revision=103 Author: ikemoto Date: 2010-01-28 17:39:15 +0900 (Thu, 28 Jan 2010) Log Message: ----------- [NMShell] mkdir implemented and tested by ITPL; mode option is not yet implemented Modified Paths: -------------- trunk/source/NMShell/src/info/ngms/commands/mkdir.scala Property Changed: ---------------- trunk/source/NMShell/src/info/ngms/commands/mkdir.scala Modified: trunk/source/NMShell/src/info/ngms/commands/mkdir.scala =================================================================== --- trunk/source/NMShell/src/info/ngms/commands/mkdir.scala 2010-01-28 08:05:16 UTC (rev 102) +++ trunk/source/NMShell/src/info/ngms/commands/mkdir.scala 2010-01-28 08:39:15 UTC (rev 103) @@ -1,45 +1,373 @@ -/* - * Next Generation Management System Project - * Copyright(c) 2009, NGMS Project Team All Rights Reserved. - */ -package info.ngms.commands - -import info.ngms.nmshell.NMCommand -import info.ngms.nmshell.NMCommandContext -import info.ngms.nmshell.NMCommandStream -import info.ngms.nmshell.NMCommandStreamKind -import info.ngms.nmshell.NMCommandParameterInfo -import info.ngms.nmshell.NMRawStream -import info.ngms.nmshell.RawStream -import info.ngms.nmshell.NMShellEnvironment -import info.ngms.nmtree.NMTree -import info.ngms.nmtree.NMPath -import info.ngms.nmtree.NMTreeElements - -class mkdir extends NMCommand { - val name = "mkdir" - - private var targetDirnames : Option[Array[String]] = None - - def parseOption( args : Array[String] ) : Unit = { - if (args.length > 0) targetDirnames = Some(args) - } - - def parameters : List[ NMCommandParameterInfo ] = { - Nil - } - - def doWork( env : NMCommandContext ) : Unit = { - val current = NMShellEnvironment.currentPath - targetDirnames match { - case Some(names) => - names foreach { name => - NMTree.createDir(current.concat(name)) - } - case None => print_err(env, "no enough arguments\n") - } - } - - def inputStreamKind = RawStream() - def outputStreamKind = RawStream() -} +/* + * Next Generation Management System Project + * Copyright(c) 2009, NGMS Project Team All Rights Reserved. + */ +package info.ngms.commands + +//import scala.util.parsing.combinator.RegexParsers +//import scala.util.parsing.combinator.Parsers +//import scala.util.parsing.combinator._ +import info.ngms.nmshell.NMCommand +import info.ngms.nmshell.NMCommandContext +import info.ngms.nmshell.NMCommandParameterInfo +import info.ngms.nmshell.NMCommandStreamKind +import info.ngms.nmshell.RawStream +import info.ngms.nmshell.NMShellEnvironment +import info.ngms.nmtree.NMPath +import info.ngms.nmtree.NMTree +import info.ngms.nmtree.LowLevelException +import org.apache.commons.cli.CommandLine +import org.apache.commons.cli.HelpFormatter +import org.apache.commons.cli.Options +import org.apache.commons.cli.PosixParser + +/** + * 指定された名前で新しくディレクトリを作成します。 + * <p> + * 書式 mkdir [options] directory... + * </p> + * + * <p>オプション</p> + * <dl> + * <dt>-m mode, --mode mode</dt> + * <dd>作成したディレクトリのアクセス権限を mode に指定します。</dd> + * <dt>-p, --parents</dt> + * <dd>存在しない親のパスを含めてディレクトリを作成します。 + * 指定したディレクトリが全て既に存在していても、エラーとはなりません。</dd> + * <dt>-v, --verbose</dt> + * <dd>作成したディレクトリ名を表示します。</dd> + * </dl> + * + * @version $Id$ + * @auther ikemo****@itpl***** + */ +class mkdir extends NMCommand { + + /** + * このクラスの名称をもとに、このコマンドの名称を返します。 + * コマンドの名称はすべて小文字となります。 + * + * @return このコマンドの名称 + */ + def commandName() : String = { + val splittedClassName : Array[String] = getClass().getName().split("\\.") + return splittedClassName(splittedClassName.length - 1).toLowerCase() + } + + /** + * このコマンドの名称です。 + */ + val name : String = commandName() + + /** + * 与えられたコマンドライン引数をもとに、オプション引数を解析します。 + * + * @args コマンドライン引数の配列 + */ + def parseOption( args : Array[String] ) : Unit = { + commandLine = createCommandLine(args) + } + + private[this] var commandLine : CommandLine = null + + private[this] val commandLineOptions : Options = new Options() + + private[this] def setupCommandLineOptions() : Unit = { + commandLineOptions. + addOption("m", "mode", true, "set file mode"). + addOption("p", "parents", false, "no error if existing, make parent directories as needed"). + addOption("v", "verbose", false, "print a message for each created directory") + } + +// private object ModeParsers extends RegexParsers { +// +// def modes : Parser[List[UsersAndOperation]] = {repsep(mode, ",")} +// +// def mode : Parser[UsersAndOperation] = {(opt(users)) ~ operatorAndOperands ^^ { +// case Some(users) ~ operatorAndOperands => new UsersAndOperation(users, operatorAndOperands) +// case None ~ operatorAndOperands => new UsersAndOperation(null, operatorAndOperands) +// } +// } +// +//// def users : Parser[List[String]] = {rep("u" | "a")} +// def users : Parser[List[String]] = {rep("u" | "g" | "o" | "a")} +// +// def operatorAndOperands : Parser[OperatorAndOperands] = { +// operator ~ rep(operand) ^^ { +// case operator ~ operands => new OperatorAndOperands(operator, operands) +// } +// } +// +// def operator : Parser[String] = {"+" | "-" | "="} +// +// def operand : Parser[String] = {"r" | "w" | "x"} +// +// class UsersAndOperation (val users : List[String], val operation : OperatorAndOperands) { +// override def toString : String = { +// return "UsersAndOperation(" + users + ", " + operation + ")" +// } +// } +// +// class OperatorAndOperands (val operator : String, val operands : List[String]) { +// override def toString : String = { +// return "OperatorAndOperands(" + operator + ", " + operands + ")" +// } +// } +// +// } + + private[commands] def createCommandLine( args : Array[String] ) : CommandLine = { +//println("args.length: " + args.length) +//args.foreach((arg: String) => {println("arg: '" + arg + "'")}) + setupCommandLineOptions() + return new PosixParser().parse(commandLineOptions, args) + } + + /** + * + */ + def parameters : List[ NMCommandParameterInfo ] = { + Nil + } + + /** + * コマンドの受けつけるストリームの種類を取得する + * + * @return 入力ストリームの種類 + */ + def inputStreamKind : NMCommandStreamKind = new RawStream() + + /** + * コマンドの出力するストリームの種類を取得する + * + * @return 出力ストリームの種類 + */ + def outputStreamKind : NMCommandStreamKind = new RawStream() + + private[this] var context : NMCommandContext = null + +// private[this] var modeSetting : ModeSetting = null +// +// private[this] class ModeSetting (modes : List[ModeParsers.UsersAndOperation]) { +// +// var readableByOwnerOnly : Boolean = false +// +// var readableByAll : Boolean = false +// +// var unreadableByOwnerOnly : Boolean = false +// +// var unreadableByAll : Boolean = false +// +// var writableByOwnerOnly : Boolean = false +// +// var writableByAll : Boolean = false +// +// var unwritableByOwnerOnly : Boolean = false +// +// var unwritableByAll : Boolean = false +// +// var executableByOwnerOnly : Boolean = false +// +// var executableByAll : Boolean = false +// +// var unexecutableByOwnerOnly : Boolean = false +// +// var unexecutableByAll : Boolean = false +// +//// outPrintln(modes.toString) +// +// modes.foreach((mode : ModeParsers.UsersAndOperation) => { +// mode.users match { +// case null => {setupByUser("a", mode.operation)} +// case _ => { +// mode.users.foreach((user : String) => {setupByUser(user, mode.operation)}) +// } +// } +// }) +// +// private def setupByUser(user : String, operatorAndOperands : ModeParsers.OperatorAndOperands) : Unit = { +// user match { +// case "u" => { +// operatorAndOperands.operator match { +// case "+" => { +// operatorAndOperands.operands.foreach((operand : String) => { +// operand match { +// case "r" => {readableByOwnerOnly = true} +// case "w" => {writableByOwnerOnly = true} +// case "x" => {executableByOwnerOnly = true} +// } +// }) +// } +// case "-" => { +// operatorAndOperands.operands.foreach((operand : String) => { +// operand match { +// case "r" => {readableByOwnerOnly = false} +// case "w" => {writableByOwnerOnly = false} +// case "x" => {executableByOwnerOnly = false} +// } +// }) +// } +// case "=" => { +// operatorAndOperands.operands.foreach((operand : String) => { +// operand match { +// case "r" => {readableByOwnerOnly = true} +// case "w" => {writableByOwnerOnly = true} +// case "x" => {executableByOwnerOnly = true} +// } +// }) +// if( ! operatorAndOperands.operands.contains("r") ) { +// readableByOwnerOnly = false +// } +// if( ! operatorAndOperands.operands.contains("w") ) { +// writableByOwnerOnly = false +// } +// if( ! operatorAndOperands.operands.contains("x") ) { +// executableByOwnerOnly = false +// } +// } +// } +// } +// case "a" => { +// operatorAndOperands.operator match { +// case "+" => { +// operatorAndOperands.operands.foreach((operand : String) => { +// operand match { +// case "r" => {readableByAll = true} +// case "w" => {writableByAll = true} +// case "x" => {executableByAll = true} +// } +// }) +// } +// case "-" => { +// operatorAndOperands.operands.foreach((operand : String) => { +// operand match { +// case "r" => {readableByAll = false} +// case "w" => {writableByAll = false} +// case "x" => {executableByAll = false} +// } +// }) +// } +// case "=" => { +// operatorAndOperands.operands.foreach((operand : String) => { +// operand match { +// case "r" => {readableByAll = true} +// case "w" => {writableByAll = true} +// case "x" => {executableByAll = true} +// } +// }) +// if( ! operatorAndOperands.operands.contains("r") ) { +// readableByAll = false +// } +// if( ! operatorAndOperands.operands.contains("w") ) { +// writableByAll = false +// } +// if( ! operatorAndOperands.operands.contains("x") ) { +// executableByAll = false +// } +// } +// } +// } +// } +// } +// +// } + + /** + * このコマンドの機能を実行します。 + * + * @param givenContext プロセスの現在の状態 + */ + def doWork ( givenContext : NMCommandContext ) : Unit = { +//println("NMShellEnvironment.currentPath.canonical: " + NMShellEnvironment.currentPath.canonical) + context = givenContext + val specifiedNewDirs : Array[String] = commandLine.getArgs() + if (specifiedNewDirs.length == 0) { + errPrintln(name + ": missing operand") + printUsage(context) + return + } + if (commandLine.hasOption("m")) { + errPrintln(name + ": mode option is currently not supported") + return +// val modeOptionValue : String = commandLine.getOptionValue("m") +// val parseResult : ModeParsers.ParseResult[List[ModeParsers.UsersAndOperation]] = +// ModeParsers.parseAll(ModeParsers.modes, modeOptionValue) +// if ( ! parseResult.successful ) { +// errPrintln(name + ": invalid mode `" + modeOptionValue + "'") +// return +// } +// modeSetting = new ModeSetting(parseResult.get) + } + specifiedNewDirs.foreach((dir: String) => {processArg(context, dir)}) + } + +// private[this] def setFileMode(path : NMPath) : Boolean = { +// var failed : Boolean = true +// return failed +// } + + private[this] def processArg(context : NMCommandContext, dir : String) : Unit = { + val newDirPath : NMPath = if(pathNameIsAbsolute(dir)) { +//println("creating directory with the given absolute path: " + dir) + new NMPath(dir) + } else { +//println("creating directory with the given relative path: " + dir) + new NMPath(NMShellEnvironment.currentPath.canonical + NMPath.separator + dir) + } + createDir(newDirPath) + } + + private[this] def pathNameIsAbsolute(pathName : String) : Boolean = { + return pathName.startsWith(NMPath.root) + } + + private[this] def createDir(newDirPath : NMPath) : Unit = { +//print(context, name + ": creating directory `" + newDirPath + "' ...\n") + if(newDirPath.exists) { + errPrintln(errorMessage(newDirPath) + ": File exists") + return + } + if(!newDirPath.parent.exists) { + if (commandLine.hasOption("p")) { + createDir(newDirPath.parent) + } else { + errPrintln(errorMessage(newDirPath) + ": No such file or directory") + return + } + } + try { + NMTree.createDir(newDirPath) + if (commandLine.hasOption("v")) { + outPrintln(name + ": " + "created directory `" + newDirPath.canonical) + } + } catch { + case exception: LowLevelException => { + errPrintln(name + ": " + exception.getMessage()) + } + case exception: SecurityException => { + errPrintln(errorMessage(newDirPath) + "' :") + errPrintln(name + ": " + exception.getMessage()) + } + } + } + + private[this] def errorMessage(path : NMPath) : String = { + return name + ": cannot create directory `" + path.canonical + "'" + } + + private[this] def printUsage(context : NMCommandContext) : Unit = { + new HelpFormatter().printHelp(name + " [OPTION]... DIRECTORY...\n", + "Create the DIRECTORY(ies), if they do not already exist.", commandLineOptions, null) + } + + private[this] val lineSeparator = "\n" + + private[this] def outPrintln(text : String) : Unit = { + print(context, text + lineSeparator) + } + + private[this] def errPrintln(text : String) : Unit = { + print_err(context, text + lineSeparator) + } + +} Property changes on: trunk/source/NMShell/src/info/ngms/commands/mkdir.scala ___________________________________________________________________ Added: svn:keywords + Id