Beispiel #1
0
  public static void main(String args[]) throws IOException {
    // process command line args
    CliOptions cliOptions = new CliOptions();
    cliOptions.processArgs(css_, args);

    // connect to cassandra server if host argument specified.
    if (css_.hostName != null) {
      connect(css_.hostName, css_.thriftPort);
    } else {
      // If not, client must connect explicitly using the "connect" CLI statement.
      cliClient_ = new CliClient(css_, null);
    }

    ConsoleReader reader = new ConsoleReader();
    reader.setBellEnabled(false);

    String historyFile = System.getProperty("user.home") + File.separator + HISTORYFILE;

    reader.setHistory(new History(new File(historyFile)));

    printBanner();

    String line;
    while ((line = reader.readLine(PROMPT + "> ")) != null) {
      processLine(line);
    }
  }
Beispiel #2
0
 @Override
 public String promptForPassword(final String message) throws PrompterException {
   try {
     ConsoleReader reader = new ConsoleReader();
     return reader.readLine(message, '*');
   } catch (Exception e) {
     throw new PrompterException(e.getMessage(), e);
   }
 }
Beispiel #3
0
  public void refreshCommands() {
    for (Object c : new ArrayList(reader.getCompletors())) {
      reader.removeCompletor((Completor) c);
    }

    Completor[] list =
        new Completor[] {new SimpleCompletor(server.getAllCommands()), new NullCompletor()};
    reader.addCompletor(new ArgumentCompletor(list));
    sender.recalculatePermissions();
  }
 @Override
 public void printLine(String message) {
   try {
     console.printString(message);
     console.printNewline();
   } catch (IOException e) {
     System.err.println(
         "Failed to print '" + message + "' to the console: " + e.getLocalizedMessage());
   }
 }
Beispiel #5
0
  @SuppressWarnings("unchecked")
  void run() throws IOException {
    inConsole = true;
    myCommands = buildMyCommands();
    if (cl.getCommand() == null) {
      System.out.println("Welcome to Hedwig!");
      System.out.println("JLine support is enabled");

      console = new ConsoleReader();
      JLineHedwigCompletor completor = new JLineHedwigCompletor(admin);
      console.addCompletor(completor);

      // load history file
      History history = new History();
      File file =
          new File(
              System.getProperty(
                  "hw.history",
                  new File(System.getProperty("user.home"), HW_HISTORY_FILE).toString()));
      if (LOG.isDebugEnabled()) {
        LOG.debug("History file is " + file.toString());
      }
      history.setHistoryFile(file);
      // set history to console reader
      console.setHistory(history);
      // load history from history file
      history.moveToFirstEntry();

      while (history.next()) {
        String entry = history.current();
        if (!entry.equals("")) {
          addToHistory(commandCount, entry);
        }
        commandCount++;
      }
      System.out.println("JLine history support is enabled");

      String line;
      while ((line = console.readLine(getPrompt())) != null) {
        executeLine(line);
        history.addToHistory(line);
      }
    }

    inConsole = false;
    processCmd(cl);
    try {
      myCommands.get(EXIT).runCmd(new String[0]);
    } catch (Exception e) {
    }
  }
Beispiel #6
0
 public String colorize(String string) {
   if (!string.contains("\u00A7")) {
     return string;
   } else if ((!jLine || !reader.getTerminal().isANSISupported()) && jTerminal == null) {
     return ChatColor.stripColor(string);
   } else {
     return string
             .replace(ChatColor.RED.toString(), "\033[1;31m")
             .replace(ChatColor.YELLOW.toString(), "\033[1;33m")
             .replace(ChatColor.GREEN.toString(), "\033[1;32m")
             .replace(ChatColor.AQUA.toString(), "\033[1;36m")
             .replace(ChatColor.BLUE.toString(), "\033[1;34m")
             .replace(ChatColor.LIGHT_PURPLE.toString(), "\033[1;35m")
             .replace(ChatColor.BLACK.toString(), "\033[0;0m")
             .replace(ChatColor.DARK_GRAY.toString(), "\033[1;30m")
             .replace(ChatColor.DARK_RED.toString(), "\033[0;31m")
             .replace(ChatColor.GOLD.toString(), "\033[0;33m")
             .replace(ChatColor.DARK_GREEN.toString(), "\033[0;32m")
             .replace(ChatColor.DARK_AQUA.toString(), "\033[0;36m")
             .replace(ChatColor.DARK_BLUE.toString(), "\033[0;34m")
             .replace(ChatColor.DARK_PURPLE.toString(), "\033[0;35m")
             .replace(ChatColor.GRAY.toString(), "\033[0;37m")
             .replace(ChatColor.WHITE.toString(), "\033[1;37m")
         + "\033[0m";
   }
 }
 @Override
 public void printColumns(Collection<String> col) {
   try {
     console.printColumns(col);
   } catch (IOException e) {
     System.err.println(
         "Failed to print columns '" + col + "' to the console: " + e.getLocalizedMessage());
   }
 }
Beispiel #8
0
  public static void main(String[] args) throws Exception {
    log.info("* reading config.json");
    JSONObject config = ConfigParser.parseConfig(TextFile.get("config.json"));
    String host = config.<String>get("server-addr");
    int port = config.<Integer>get("server-port");
    String pass = config.<String>get("password");

    MechaClient client = new MechaClient(host, port, pass);

    ConsoleReader reader = new ConsoleReader();
    reader.setBellEnabled(false);

    String line;
    while ((line = reader.readLine("mecha-cli> ")) != null) {
      line = line.trim();
      if (line.equals("")) continue;
      client.exec(line);
    }
  }
Beispiel #9
0
 public synchronized String readlineInteractive(ThreadContext tc, String prompt) {
   try {
     if (cr == null) cr = new ConsoleReader(is, new OutputStreamWriter(tc.gc.out));
     String line = cr.readLine(prompt);
     if (line == null) {
       eof = true;
       line = "";
     }
     return line;
   } catch (IOException e) {
     throw ExceptionHandling.dieInternal(tc, e);
   }
 }
    private CommandContextImpl(jline.ConsoleReader console) {
      this.console = console;

      console.setUseHistory(true);
      String userHome = SecurityActions.getSystemProperty("user.home");
      File historyFile = new File(userHome, ".jboss-cli-history");
      try {
        console.getHistory().setHistoryFile(historyFile);
      } catch (IOException e) {
        System.err.println(
            "Failed to setup the history file "
                + historyFile.getAbsolutePath()
                + ": "
                + e.getLocalizedMessage());
      }

      this.history = new HistoryImpl();
      operationCandidatesProvider = new DefaultOperationCandidatesProvider(this);

      operationHandler = new OperationRequestHandler();

      cmdCompleter = new CommandCompleter(cmdRegistry, this);
    }
Beispiel #11
0
 protected boolean continueOrQuit() throws IOException {
   System.out.println("Press <Return> to continue, or Q to cancel ...");
   int ch;
   if (null != console) {
     ch = console.readCharacter(CONTINUE_OR_QUIT);
   } else {
     do {
       ch = terminal.readCharacter(System.in);
     } while (ch != 'q' && ch != 'Q' && ch != '\n');
   }
   if (ch == 'q' || ch == 'Q') {
     return false;
   }
   return true;
 }
Beispiel #12
0
  // execute krun in debug mode (i.e. step by step execution)
  // isSwitch variable is true if we enter in debug execution from normal
  // execution (we use the search command with --graph option)
  @SuppressWarnings("unchecked")
  public static void debugExecution(
      Term kast,
      String lang,
      KRunResult<SearchResults> state,
      org.kframework.kil.loader.Context context) {
    try {
      // adding autocompletion and history feature to the stepper internal
      // commandline by using the JLine library
      ConsoleReader reader = new ConsoleReader();
      reader.setBellEnabled(false);

      List<Completor> argCompletor = new LinkedList<Completor>();
      argCompletor.add(
          new SimpleCompletor(
              new String[] {
                "help",
                "abort",
                "resume",
                "step",
                "step-all",
                "select",
                "show-search-graph",
                "show-node",
                "show-edge",
                "save",
                "load",
                "read"
              }));
      argCompletor.add(new FileNameCompletor());
      List<Completor> completors = new LinkedList<Completor>();
      completors.add(new ArgumentCompletor(argCompletor));
      reader.addCompletor(new MultiCompletor(completors));

      new File(K.compiled_def + K.fileSeparator + "main.maude").getCanonicalPath();
      RunProcess rp = new RunProcess();
      KRun krun = obtainKRun(context);
      krun.setBackendOption("io", false);
      KRunDebugger debugger;
      try {
        if (state == null) {
          Term t = makeConfiguration(kast, "", rp, (K.term != null), context);
          debugger = krun.debug(t);
          System.out.println("After running one step of execution the result is:");
          AnsiConsole.out.println(debugger.printState(debugger.getCurrentState()));
        } else {
          debugger = krun.debug(state.getResult().getGraph());
        }
      } catch (UnsupportedBackendOptionException e) {
        Error.report("Backend \"" + K.backend + "\" does not support option " + e.getMessage());
        throw e; // unreachable
      }
      while (true) {
        System.out.println();
        String input = reader.readLine("Command > ");
        if (input == null) {
          // probably pressed ^D
          System.out.println();
          System.exit(0);
        }

        // construct the right command line input when we specify the
        // "step" option with an argument (i.e. step=3)
        input = input.trim();
        String[] tokens = input.split(" ");
        // store the tokens that are not a blank space
        List<String> items = new ArrayList<String>();
        for (int i = 0; i < tokens.length; i++) {
          if ((!(tokens[i].equals(" ")) && (!tokens[i].equals("")))) {
            items.add(tokens[i]);
          }
        }
        StringBuilder aux = new StringBuilder();
        // excepting the case of a command like: help
        if (items.size() > 1) {
          for (int i = 0; i < items.size(); i++) {
            if (i > 0) {
              aux.append("=");
              aux.append(items.get(i));
            } else if (i == 0) {
              aux.append(items.get(i));
            }
          }
          input = aux.toString();
        }
        // apply trim to remove possible blank spaces from the inserted
        // command
        String[] cmds = new String[] {"--" + input};
        CommandlineOptions cmd_options = new CommandlineOptions();
        CommandLine cmd = cmd_options.parse(cmds);
        // when an error occurred during parsing the commandline
        // continue the execution of the stepper
        if (cmd == null) {
          continue;
        } else {
          if (cmd.hasOption("help")) {
            printDebugUsage(cmd_options);
          }
          if (cmd.hasOption("abort")) {
            System.exit(0);
          }
          if (cmd.hasOption("resume")) {
            try {
              debugger.resume();
              AnsiConsole.out.println(debugger.printState(debugger.getCurrentState()));
            } catch (IllegalStateException e) {
              Error.silentReport(
                  "Wrong command: If you previously used the step-all command you must"
                      + K.lineSeparator
                      + "seletc first a solution with step command before executing steps of rewrites!");
            }
          }
          // one step execution (by default) or more if you specify an
          // argument
          if (cmd.hasOption("step")) {
            // by default execute only one step at a time
            String arg = new String("1");
            String[] remainingArguments = null;
            remainingArguments = cmd.getArgs();
            if (remainingArguments.length > 0) {
              arg = remainingArguments[0];
            }
            try {
              int steps = Integer.parseInt(arg);
              debugger.step(steps);
              AnsiConsole.out.println(debugger.printState(debugger.getCurrentState()));
            } catch (NumberFormatException e) {
              Error.silentReport("Argument to step must be an integer.");
            } catch (IllegalStateException e) {
              Error.silentReport(
                  "Wrong command: If you previously used the step-all command you must"
                      + K.lineSeparator
                      + "select first a solution with step command before executing steps of rewrites!");
            }
          }
          if (cmd.hasOption("step-all")) {
            // by default compute all successors in one transition
            String arg = new String("1");
            String[] remainingArguments = null;
            remainingArguments = cmd.getArgs();
            if (remainingArguments.length > 0) {
              arg = remainingArguments[0];
            }
            try {
              int steps = Integer.parseInt(arg);
              SearchResults states = debugger.stepAll(steps);
              AnsiConsole.out.println(states);

            } catch (NumberFormatException e) {
              Error.silentReport("Argument to step-all must be an integer.");
            } catch (IllegalStateException e) {
              Error.silentReport(
                  "Wrong command: If you previously used the step-all command you must"
                      + K.lineSeparator
                      + "select first a solution with step command before executing steps of rewrites!");
            }
          }
          // "select n7" or "select 3" are both valid commands
          if (cmd.hasOption("select")) {
            String arg = new String();
            arg = cmd.getOptionValue("select").trim();
            try {
              int stateNum = Integer.parseInt(arg);
              debugger.setCurrentState(stateNum);
              AnsiConsole.out.println(debugger.printState(debugger.getCurrentState()));
            } catch (NumberFormatException e) {
              Error.silentReport("Argument to select must bean integer.");
            } catch (IllegalArgumentException e) {
              System.out.println(
                  "A node with the specified state number could not be found in the"
                      + K.lineSeparator
                      + "search graph");
            }
          }
          if (cmd.hasOption("show-search-graph")) {
            System.out.println(K.lineSeparator + "The search graph is:" + K.lineSeparator);
            System.out.println(debugger.getGraph());
          }
          if (cmd.hasOption("show-node")) {
            String nodeId = cmd.getOptionValue("show-node").trim();
            try {
              int stateNum = Integer.parseInt(nodeId);
              AnsiConsole.out.println(debugger.printState(stateNum));
            } catch (NumberFormatException e) {
              Error.silentReport("Argument to select node to show must be an integer.");
            } catch (IllegalArgumentException e) {
              System.out.println(
                  "A node with the specified state number could not be found in the"
                      + K.lineSeparator
                      + "search graph");
            }
          }
          if (cmd.hasOption("show-edge")) {
            String[] vals = cmd.getOptionValues("show-edge");
            vals = vals[0].split("=", 2);
            try {
              int state1 = Integer.parseInt(vals[0].trim());
              int state2 = Integer.parseInt(vals[1].trim());
              AnsiConsole.out.println(debugger.printEdge(state1, state2));
            } catch (ArrayIndexOutOfBoundsException e) {
              Error.silentReport("Must specify two nodes with an edge between them.");
            } catch (NumberFormatException e) {
              Error.silentReport("Arguments to select edge to show must be integers.");
            } catch (IllegalArgumentException e) {
              System.out.println(
                  "An edge with the specified endpoints could not be found in the"
                      + K.lineSeparator
                      + "search graph");
            }
          }

          DirectedGraph<KRunState, Transition> savedGraph = null;
          if (cmd.hasOption("save")) {
            BinaryLoader.save(
                new File(cmd.getOptionValue("save")).getCanonicalPath(), debugger.getGraph());
            System.out.println("File successfully saved.");
          }
          if (cmd.hasOption("load")) {
            savedGraph =
                (DirectedGraph<KRunState, Transition>)
                    BinaryLoader.load(cmd.getOptionValue("load"));
            krun = new MaudeKRun(context);
            debugger = krun.debug(savedGraph);
            debugger.setCurrentState(1);
            System.out.println("File successfully loaded.");
          }
          if (cmd.hasOption("read")) {
            try {
              debugger.readFromStdin(
                  StringBuiltin.valueOf("\"" + cmd.getOptionValue("read") + "\"").stringValue());
              AnsiConsole.out.println(debugger.printState(debugger.getCurrentState()));
            } catch (IllegalStateException e) {
              Error.silentReport(e.getMessage());
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  }
  /**
   * @param args the command line arguments
   * @throws Exception
   */
  @SuppressWarnings("unchecked")
  public static void main(String[] args) throws Exception {

    if (args.length == 0) {
      fail();
    }
    OpenCVJNILoader.load(); // Loads the OpenCV JNI (java native interface)
    //		File servo =
    // ScriptingEngine.fileFromGit("https://github.com/NeuronRobotics/BowlerStudioVitamins.git",
    //							"BowlerStudioVitamins/stl/servo/smallservo.stl");
    //
    //		ArrayList<CSG>  cad = (ArrayList<CSG>
    // )ScriptingEngine.inlineGistScriptRun("4814b39ee72e9f590757", "javaCad.groovy" , null);
    //		System.out.println(servo.exists()+" exists: "+servo);

    boolean startLoadingScripts = false;
    Object ret = null;
    for (String s : args) {
      if (startLoadingScripts) {
        try {

          ret = ScriptingEngine.inlineFileScriptRun(new File(s), null);
        } catch (Error e) {
          e.printStackTrace();
          fail();
        }
      }
      if (s.contains("script") || s.contains("-s")) {
        startLoadingScripts = true;
      }
    }
    startLoadingScripts = false;

    for (String s : args) {

      if (startLoadingScripts) {
        try {
          ret = ScriptingEngine.inlineFileScriptRun(new File(s), (ArrayList<Object>) ret);
        } catch (Error e) {
          e.printStackTrace();
          fail();
        }
      }
      if (s.contains("pipe") || s.contains("-p")) {
        startLoadingScripts = true;
      }
    }
    boolean runShell = false;
    ShellType shellTypeStorage = ShellType.GROOVY;
    for (String s : args) {

      if (runShell) {
        try {
          shellTypeStorage = ShellType.getFromSlug(s);
        } catch (Exception e) {
          shellTypeStorage = ShellType.GROOVY;
        }
        break;
      }
      if (s.contains("repl") || s.contains("-r")) {
        runShell = true;
      }
    }

    System.out.println("Starting Bowler REPL in langauge: " + shellTypeStorage.getNameOfShell());
    // sample from
    // http://jline.sourceforge.net/testapidocs/src-html/jline/example/Example.html

    if (!Terminal.getTerminal().isSupported()) {
      System.out.println("Terminal not supported " + Terminal.getTerminal());
    }
    // Terminal.getTerminal().initializeTerminal();

    ConsoleReader reader = new ConsoleReader();
    reader.addTriggeredAction(
        Terminal.CTRL_C,
        e -> {
          System.exit(0);
        });

    if (!historyFile.exists()) {
      historyFile.createNewFile();
      reader.getHistory().addToHistory("println SDKBuildInfo.getVersion()");
      reader.getHistory().addToHistory("for(int i=0;i<100;i++) { println dyio.getValue(0) }");
      reader.getHistory().addToHistory("dyio.setValue(0,128)");
      reader.getHistory().addToHistory("println dyio.getValue(0)");
      reader
          .getHistory()
          .addToHistory(
              "ScriptingEngine.inlineGistScriptRun(\"d4312a0787456ec27a2a\", \"helloWorld.groovy\" , null)");
      reader
          .getHistory()
          .addToHistory(
              "DeviceManager.addConnection(new DyIO(ConnectionDialog.promptConnection()),\"dyio\")");
      reader
          .getHistory()
          .addToHistory(
              "DeviceManager.addConnection(new DyIO(new SerialConnection(\"/dev/DyIO0\")),\"dyio\")");
      reader.getHistory().addToHistory("BowlerKernel.speak(\"Text to speech works like this\")");
      reader.getHistory().addToHistory("println \"Hello world!\"");
      writeHistory(reader.getHistory().getHistoryList());
    } else {
      List<String> history = loadHistory();
      for (String h : history) {
        reader.getHistory().addToHistory(h);
      }
    }
    reader.setBellEnabled(false);
    reader.setDebug(new PrintWriter(new FileWriter("writer.debug", true)));

    Runtime.getRuntime()
        .addShutdownHook(
            new Thread() {
              @Override
              public void run() {

                writeHistory(reader.getHistory().getHistoryList());
              }
            });

    // SpringApplication.run(SpringBowlerUI.class, new String[]{});

    String line;
    try {
      while ((line = reader.readLine("Bowler " + shellTypeStorage.getNameOfShell() + "> "))
          != null) {
        if (line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("exit")) {
          break;
        }
        if (line.equalsIgnoreCase("history") || line.equalsIgnoreCase("h")) {
          List<String> h = reader.getHistory().getHistoryList();
          for (String s : h) {
            System.out.println(s);
          }
          continue;
        }
        if (line.startsWith("shellType")) {
          try {
            shellTypeStorage = ShellType.getFromSlug(line.split(" ")[1]);
          } catch (Exception e) {
            shellTypeStorage = ShellType.GROOVY;
          }
          continue;
        }
        try {
          ret = ScriptingEngine.inlineScriptStringRun(line, null, shellTypeStorage);
          if (ret != null) System.out.println(ret);
        } catch (Error e) {
          e.printStackTrace();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Beispiel #14
0
  public static void main(String[] args) throws Exception {
    OptionsProcessor oproc = new OptionsProcessor();
    if (!oproc.process_stage1(args)) {
      System.exit(1);
    }

    SessionState.initHiveLog4j();

    CliSessionState ss = new CliSessionState(new HiveConf(SessionState.class));
    ss.in = System.in;
    try {
      ss.out = new PrintStream(System.out, true, "UTF-8");
      ss.err = new PrintStream(System.err, true, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      System.exit(3);
    }

    if (!oproc.process_stage2(ss)) {
      System.exit(2);
    }

    HiveConf conf = ss.getConf();
    for (Map.Entry<Object, Object> item : ss.cmdProperties.entrySet()) {
      conf.set((String) item.getKey(), (String) item.getValue());
    }

    if (!ShimLoader.getHadoopShims().usesJobShell()) {
      ClassLoader loader = conf.getClassLoader();
      String auxJars = HiveConf.getVar(conf, HiveConf.ConfVars.HIVEAUXJARS);
      if (StringUtils.isNotBlank(auxJars)) {
        loader = Utilities.addToClassPath(loader, StringUtils.split(auxJars, ","));
      }
      conf.setClassLoader(loader);
      Thread.currentThread().setContextClassLoader(loader);
    }

    SessionState.start(ss);

    CliDriver cli = new CliDriver();

    Hive hive = Hive.get();

    String username = ss.getUserName();
    String passwd = ss.passwd;
    if (!hive.isAUser(username, passwd)) {
      System.out.println("User or passwd is wrong!");
      System.exit(0);
    } else {
      System.out.println("Connect to TDW successfully!");
    }

    if (ss.getDbName() == null) {
      ss.setDbName(MetaStoreUtils.DEFAULT_DATABASE_NAME);
    }
    if (ss.execString != null) {
      System.exit(cli.processLine(ss.execString));
    }

    try {
      if (ss.fileName != null) {
        System.exit(cli.processReader(new BufferedReader(new FileReader(ss.fileName))));
      }
    } catch (FileNotFoundException e) {
      System.err.println("Could not open input file for reading. (" + e.getMessage() + ")");
      System.exit(3);
    }

    ConsoleReader reader = new ConsoleReader();
    reader.setBellEnabled(false);

    List<SimpleCompletor> completors = new LinkedList<SimpleCompletor>();
    completors.add(
        new SimpleCompletor(
            new String[] {"set", "from", "create", "load", "describe", "quit", "exit"}));
    reader.addCompletor(new ArgumentCompletor(completors));

    String line;
    final String HISTORYFILE = ".hivehistory";
    String historyFile = System.getProperty("user.home") + File.separator + HISTORYFILE;
    reader.setHistory(new History(new File(historyFile)));
    int ret = 0;

    String prefix = "";
    String curPrompt = prompt;
    while ((line = reader.readLine(curPrompt + "> ")) != null) {
      if (!prefix.equals("")) {
        prefix += '\n';
      }
      if (line.trim().endsWith(";") && !line.trim().endsWith("\\;")) {
        line = prefix + line;
        ret = cli.processLine(line);
        prefix = "";
        curPrompt = prompt;
      } else {
        prefix = prefix + line;
        curPrompt = prompt2;
        continue;
      }
    }

    System.exit(ret);
  }
Beispiel #15
0
  public static void main(String[] args) {
    KeyBinding bind;
    if ("-tsv".equals(args[0])) {
      bind = loadKeyBindingFromTsv(args[1], args[2], args[3]);
      bind.save(args[4]);
      return;
    } else {
      try {
        bind = KeyBinding.load(args[0]);
      } catch (LoadingException ex) {
        throw new RuntimeException(ex);
      }
    }

    if ("-value".equals(args[1])) {
      KeyBinding ret = KeyBinding.newLike(bind);
      Output out = new OutputKeyBinding(ret);

      Pusher p = new Pusher();
      for (int i = 2; i < args.length; i++) {
        p.onlyIf(new IfEquals(bind.getColumnName(1), args[i]), out);
      }
      p.push(new InputKeyBinding(bind));
      ret.dumpTsv(System.out);
    } else if ("-count".equals(args[1])) {
      Pusher p = new Pusher();
      for (int i = 2; i < args.length; i++) {
        p.onlyIf(new IfEquals(bind.getColumnName(1), args[i]), new BreakAfter(args[i]));
      }
      p.push(new InputKeyBinding(bind));
    } else if ("-dump".equals(args[1])) {
      bind.dumpTsv(System.out);
    } else if ("-prompt".equals(args[1])) {
      try {
        jline.ConsoleReader reader = new jline.ConsoleReader();
        reader.setBellEnabled(false);
        reader.getHistory().setHistoryFile(new File(".KeyBinding.history"));
        String line = null;
        System.err.println("=== Enter Keys ===");
        while ((line = reader.readLine("keys> ")) != null) {
          if (!line.trim().isEmpty()) {
            for (String k : line.split("\\s+")) {
              System.out.print(k);
              System.out.print('\t');
              if (bind.contains(k)) {
                System.out.print(bind.get(k));
              } else {
                System.out.print("false");
              }
              System.out.println();
            }
          }
        }
      } catch (IOException ioex) {
        ioex.printStackTrace();
      }
    } else {
      int i = 1;
      if ("-key".equals(args[1])) {
        i = 2;
      }
      for (; i < args.length; i++) {
        System.out.print(args[i]);
        System.out.print('\t');
        if (bind.contains(args[i])) {
          System.out.print(bind.get(args[i]));
        } else {
          System.out.print("false");
        }
        System.out.println();
      }
    }
  }
  public static void main(String[] args) throws Exception {

    final jline.ConsoleReader console = initConsoleReader();

    final CommandContextImpl cmdCtx = new CommandContextImpl(console);
    SecurityActions.addShutdownHook(
        new Thread(
            new Runnable() {
              @Override
              public void run() {
                cmdCtx.disconnectController();
              }
            }));
    console.addCompletor(cmdCtx.cmdCompleter);

    String[] commands = null;
    String fileName = null;
    boolean connect = false;
    for (String arg : args) {
      if (arg.startsWith("controller=")) {
        String value = arg.substring(11);
        String portStr = null;
        int colonIndex = value.indexOf(':');
        if (colonIndex < 0) {
          // default port
          cmdCtx.defaultControllerHost = value;
        } else if (colonIndex == 0) {
          // default host
          portStr = value.substring(1);
        } else {
          cmdCtx.defaultControllerHost = value.substring(0, colonIndex);
          portStr = value.substring(colonIndex + 1);
        }

        if (portStr != null) {
          int port = -1;
          try {
            port = Integer.parseInt(portStr);
            if (port < 0) {
              cmdCtx.printLine("The port must be a valid non-negative integer: '" + args + "'");
            } else {
              cmdCtx.defaultControllerPort = port;
            }
          } catch (NumberFormatException e) {
            cmdCtx.printLine("The port must be a valid non-negative integer: '" + arg + "'");
          }
        }
      } else if ("--connect".equals(arg)) {
        connect = true;
      } else if (arg.startsWith("file=")) {
        fileName = arg.substring(5);
      } else if (arg.startsWith("commands=")) {
        commands = arg.substring(9).split(",+");
      } else if (arg.startsWith("command=")) {
        commands = new String[] {arg.substring(8)};
      }
    }

    if (connect) {
      cmdCtx.connectController(null, -1);
    } else {
      cmdCtx.printLine(
          "You are disconnected at the moment."
              + " Type 'connect' to connect to the server or"
              + " 'help' for the list of supported commands.");
    }

    if (fileName != null && !fileName.isEmpty()) {
      File f = new File(fileName);
      if (!f.exists()) {
        cmdCtx.printLine("File " + f.getAbsolutePath() + " doesn't exist.");
      } else {
        BufferedReader reader = new BufferedReader(new FileReader(f));
        try {
          String line = reader.readLine();
          while (!cmdCtx.terminate && line != null) {
            processLine(cmdCtx, line.trim());
            line = reader.readLine();
          }
        } finally {
          StreamUtils.safeClose(reader);
          if (!cmdCtx.terminate) {
            cmdCtx.terminateSession();
          }
          cmdCtx.disconnectController();
        }
        return;
      }
    }

    if (commands != null) {
      for (int i = 0; i < commands.length && !cmdCtx.terminate; ++i) {
        processLine(cmdCtx, commands[i]);
      }
      if (!cmdCtx.terminate) {
        cmdCtx.terminateSession();
      }
      cmdCtx.disconnectController();
      return;
    }

    try {
      while (!cmdCtx.terminate) {
        String line = console.readLine(cmdCtx.getPrompt()).trim();
        processLine(cmdCtx, line);
      }
    } finally {
      cmdCtx.disconnectController();
    }
  }
Beispiel #17
0
 public static void main(String[] args) throws Exception {
   Config.hadoop_mode = false;
   if (args.length == 2 && args[0].equals("args")) // needed for mrql.flink script
   args = args[1].substring(1).split("!");
   for (String arg : args) {
     Config.hadoop_mode |= arg.equals("-local") || arg.equals("-dist");
     Config.bsp_mode |= arg.equals("-bsp");
     Config.spark_mode |= arg.equals("-spark");
     Config.flink_mode |= arg.equals("-flink");
   }
   ;
   Config.map_reduce_mode = !Config.bsp_mode && !Config.spark_mode && !Config.flink_mode;
   initialize_evaluator();
   if (Config.hadoop_mode) {
     conf = Evaluator.evaluator.new_configuration();
     GenericOptionsParser gop = new GenericOptionsParser(conf, args);
     conf = gop.getConfiguration();
     args = gop.getRemainingArgs();
   }
   ;
   Config.parse_args(args, conf);
   Config.hadoop_mode = Config.local_mode || Config.distributed_mode;
   if (!Config.info) {
     for (Enumeration en = LogManager.getCurrentLoggers(); en.hasMoreElements(); )
       ((Logger) en.nextElement()).setLevel(Level.WARN);
     LogManager.getRootLogger().setLevel(Level.WARN);
   }
   ;
   Evaluator.evaluator.init(conf);
   new TopLevel();
   System.out.print("Apache MRQL version " + version + " (");
   if (Config.compile_functional_arguments) System.out.print("compiled ");
   else System.out.print("interpreted ");
   if (Config.hadoop_mode) {
     if (Config.local_mode) System.out.print("local ");
     else if (Config.distributed_mode) System.out.print("distributed ");
     if (Config.spark_mode) System.out.println("Spark mode using " + Config.nodes + " tasks)");
     else if (Config.flink_mode)
       System.out.println("Flink mode using " + Config.nodes + " tasks)");
     else if (Config.bsp_mode)
       System.out.println("Hama BSP mode over " + Config.nodes + " BSP tasks)");
     else if (Config.nodes > 0)
       System.out.println("Hadoop MapReduce mode with " + Config.nodes + " reducers)");
     else if (!Config.local_mode)
       System.out.println("Hadoop MapReduce mode with 1 reducer, use -nodes to change it)");
     else System.out.println("Hadoop MapReduce mode)");
   } else if (Config.bsp_mode) System.out.println("in-memory BSP mode)");
   else System.out.println("in-memory Java mode)");
   if (Config.interactive) {
     System.out.println("Type quit to exit");
     ConsoleReader reader = new ConsoleReader();
     reader.setBellEnabled(false);
     History history = new History(new File(System.getProperty("user.home") + "/.mrqlhistory"));
     reader.setHistory(history);
     reader.setUseHistory(false);
     try {
       loop:
       while (true) {
         String line = "";
         String s = "";
         try {
           if (Config.hadoop_mode && Config.bsp_mode) Config.write(Plan.conf);
           do {
             s = reader.readLine("> ");
             if (s != null && (s.equals("quit") || s.equals("exit"))) break loop;
             if (s != null) line += " " + s;
           } while (s == null || s.indexOf(";") <= 0);
           line = line.substring(1);
           history.addToHistory(line);
           parser = new MRQLParser(new MRQLLex(new StringReader(line)));
           MRQLLex.reset();
           parser.parse();
         } catch (EOFException x) {
           break;
         } catch (Exception x) {
           if (x.getMessage() != null) System.out.println(x);
         } catch (Error x) {
           System.out.println(x);
         }
       }
     } finally {
       if (Config.hadoop_mode) {
         Plan.clean();
         Evaluator.evaluator.shutdown(Plan.conf);
       }
       ;
       if (Config.compile_functional_arguments) Compiler.clean();
     }
   } else
     try {
       if (Config.hadoop_mode && Config.bsp_mode) Config.write(Plan.conf);
       try {
         parser = new MRQLParser(new MRQLLex(new FileInputStream(query_file)));
       } catch (Exception e) {
         // when the query file is in HDFS
         Path path = new Path(query_file);
         FileSystem fs = path.getFileSystem(conf);
         parser = new MRQLParser(new MRQLLex(fs.open(path)));
       }
       ;
       parser.parse();
     } finally {
       if (Config.hadoop_mode) {
         Plan.clean();
         Evaluator.evaluator.shutdown(Plan.conf);
       }
       ;
       if (Config.compile_functional_arguments) Compiler.clean();
     }
 }
Beispiel #18
0
  /**
   * The Main-Class for the Pig Jar that will provide a shell and setup a classpath appropriate for
   * executing Jar files.
   *
   * @param args -jar can be used to add additional jar files (colon separated). - will start a
   *     shell. -e will execute the rest of the command line as if it was input to the shell.
   * @throws IOException
   */
  public static void main(String args[]) {
    int rc = 1;
    Properties properties = new Properties();
    PropertiesUtil.loadPropertiesFromFile(properties);

    boolean verbose = false;
    boolean gruntCalled = false;
    String logFileName = null;

    try {
      BufferedReader pin = null;
      boolean debug = false;
      boolean dryrun = false;
      ArrayList<String> params = new ArrayList<String>();
      ArrayList<String> paramFiles = new ArrayList<String>();
      HashSet<String> optimizerRules = new HashSet<String>();

      CmdLineParser opts = new CmdLineParser(args);
      opts.registerOpt('4', "log4jconf", CmdLineParser.ValueExpected.REQUIRED);
      opts.registerOpt('b', "brief", CmdLineParser.ValueExpected.NOT_ACCEPTED);
      opts.registerOpt('c', "cluster", CmdLineParser.ValueExpected.REQUIRED);
      opts.registerOpt('d', "debug", CmdLineParser.ValueExpected.REQUIRED);
      opts.registerOpt('e', "execute", CmdLineParser.ValueExpected.NOT_ACCEPTED);
      opts.registerOpt('f', "file", CmdLineParser.ValueExpected.REQUIRED);
      opts.registerOpt('h', "help", CmdLineParser.ValueExpected.NOT_ACCEPTED);
      opts.registerOpt('i', "version", CmdLineParser.ValueExpected.OPTIONAL);
      opts.registerOpt('j', "jar", CmdLineParser.ValueExpected.REQUIRED);
      opts.registerOpt('l', "logfile", CmdLineParser.ValueExpected.REQUIRED);
      opts.registerOpt('m', "param_file", CmdLineParser.ValueExpected.OPTIONAL);
      opts.registerOpt('o', "hod", CmdLineParser.ValueExpected.NOT_ACCEPTED);
      opts.registerOpt('p', "param", CmdLineParser.ValueExpected.OPTIONAL);
      opts.registerOpt('r', "dryrun", CmdLineParser.ValueExpected.NOT_ACCEPTED);
      opts.registerOpt('t', "optimizer_off", CmdLineParser.ValueExpected.REQUIRED);
      opts.registerOpt('v', "verbose", CmdLineParser.ValueExpected.NOT_ACCEPTED);
      opts.registerOpt('w', "warning", CmdLineParser.ValueExpected.NOT_ACCEPTED);
      opts.registerOpt('x', "exectype", CmdLineParser.ValueExpected.REQUIRED);
      opts.registerOpt('F', "stop_on_failure", CmdLineParser.ValueExpected.NOT_ACCEPTED);
      opts.registerOpt('M', "no_multiquery", CmdLineParser.ValueExpected.NOT_ACCEPTED);

      ExecMode mode = ExecMode.UNKNOWN;
      String file = null;
      ExecType execType = ExecType.MAPREDUCE;
      String execTypeString = properties.getProperty("exectype");
      if (execTypeString != null && execTypeString.length() > 0) {
        execType = PigServer.parseExecType(execTypeString);
      }
      String cluster = "local";
      String clusterConfigured = properties.getProperty("cluster");
      if (clusterConfigured != null && clusterConfigured.length() > 0) {
        cluster = clusterConfigured;
      }

      // by default warning aggregation is on
      properties.setProperty("aggregate.warning", "" + true);

      // by default multiquery optimization is on
      properties.setProperty("opt.multiquery", "" + true);

      // by default we keep going on error on the backend
      properties.setProperty("stop.on.failure", "" + false);

      char opt;
      while ((opt = opts.getNextOpt()) != CmdLineParser.EndOfOpts) {
        switch (opt) {
          case '4':
            String log4jconf = opts.getValStr();
            if (log4jconf != null) {
              properties.setProperty(LOG4J_CONF, log4jconf);
            }
            break;

          case 'b':
            properties.setProperty(BRIEF, "true");
            break;

          case 'c':
            // Needed away to specify the cluster to run the MR job on
            // Bug 831708 - fixed
            String clusterParameter = opts.getValStr();
            if (clusterParameter != null && clusterParameter.length() > 0) {
              cluster = clusterParameter;
            }
            break;

          case 'd':
            String logLevel = opts.getValStr();
            if (logLevel != null) {
              properties.setProperty(DEBUG, logLevel);
            }
            debug = true;
            break;

          case 'e':
            mode = ExecMode.STRING;
            break;

          case 'f':
            mode = ExecMode.FILE;
            file = opts.getValStr();
            break;

          case 'F':
            properties.setProperty("stop.on.failure", "" + true);
            break;

          case 'h':
            usage();
            return;

          case 'i':
            System.out.println(getVersionString());
            return;

          case 'j':
            String jarsString = opts.getValStr();
            if (jarsString != null) {
              properties.setProperty(JAR, jarsString);
            }
            break;

          case 'l':
            // call to method that validates the path to the log file
            // and sets up the file to store the client side log file
            String logFileParameter = opts.getValStr();
            if (logFileParameter != null && logFileParameter.length() > 0) {
              logFileName = validateLogFile(logFileParameter, null);
            } else {
              logFileName = validateLogFile(logFileName, null);
            }
            properties.setProperty("pig.logfile", logFileName);
            break;

          case 'm':
            paramFiles.add(opts.getValStr());
            break;

          case 'M':
            // turns off multiquery optimization
            properties.setProperty("opt.multiquery", "" + false);
            break;

          case 'o':
            // TODO sgroschupf using system properties is always a very bad idea
            String gateway = System.getProperty("ssh.gateway");
            if (gateway == null || gateway.length() == 0) {
              properties.setProperty("hod.server", "local");
            } else {
              properties.setProperty("hod.server", System.getProperty("ssh.gateway"));
            }
            break;

          case 'p':
            String val = opts.getValStr();
            params.add(opts.getValStr());
            break;

          case 'r':
            // currently only used for parameter substitution
            // will be extended in the future
            dryrun = true;
            break;

          case 't':
            optimizerRules.add(opts.getValStr());
            break;

          case 'v':
            properties.setProperty(VERBOSE, "" + true);
            verbose = true;
            break;

          case 'w':
            properties.setProperty("aggregate.warning", "" + false);
            break;

          case 'x':
            try {
              execType = PigServer.parseExecType(opts.getValStr());
            } catch (IOException e) {
              throw new RuntimeException("ERROR: Unrecognized exectype.", e);
            }
            break;
          default:
            {
              Character cc = new Character(opt);
              throw new AssertionError("Unhandled option " + cc.toString());
            }
        }
      }
      // configure logging
      configureLog4J(properties);
      // create the context with the parameter
      PigContext pigContext = new PigContext(execType, properties);

      if (logFileName == null) {
        logFileName = validateLogFile(null, null);
      }

      pigContext.getProperties().setProperty("pig.logfile", logFileName);

      if (optimizerRules.size() > 0) {
        pigContext
            .getProperties()
            .setProperty("pig.optimizer.rules", ObjectSerializer.serialize(optimizerRules));
      }

      LogicalPlanBuilder.classloader = pigContext.createCl(null);

      // construct the parameter substitution preprocessor
      Grunt grunt = null;
      BufferedReader in;
      String substFile = null;
      switch (mode) {
        case FILE:
          {
            // Run, using the provided file as a pig file
            in = new BufferedReader(new FileReader(file));

            // run parameter substitution preprocessor first
            substFile = file + ".substituted";
            pin = runParamPreprocessor(in, params, paramFiles, substFile, debug || dryrun);
            if (dryrun) {
              log.info("Dry run completed. Substituted pig script is at " + substFile);
              return;
            }

            logFileName = validateLogFile(logFileName, file);
            pigContext.getProperties().setProperty("pig.logfile", logFileName);

            // Set job name based on name of the script
            pigContext
                .getProperties()
                .setProperty(PigContext.JOB_NAME, "PigLatin:" + new File(file).getName());

            if (!debug) {
              new File(substFile).deleteOnExit();
            }

            grunt = new Grunt(pin, pigContext);
            gruntCalled = true;
            int results[] = grunt.exec();
            rc = getReturnCodeForStats(results);
            return;
          }

        case STRING:
          {
            // Gather up all the remaining arguments into a string and pass them into
            // grunt.
            StringBuffer sb = new StringBuffer();
            String remainders[] = opts.getRemainingArgs();
            for (int i = 0; i < remainders.length; i++) {
              if (i != 0) sb.append(' ');
              sb.append(remainders[i]);
            }
            in = new BufferedReader(new StringReader(sb.toString()));
            grunt = new Grunt(in, pigContext);
            gruntCalled = true;
            int results[] = grunt.exec();
            rc = getReturnCodeForStats(results);
            return;
          }

        default:
          break;
      }

      // If we're here, we don't know yet what they want.  They may have just
      // given us a jar to execute, they might have given us a pig script to
      // execute, or they might have given us a dash (or nothing) which means to
      // run grunt interactive.
      String remainders[] = opts.getRemainingArgs();
      if (remainders == null) {
        // Interactive
        mode = ExecMode.SHELL;
        ConsoleReader reader = new ConsoleReader(System.in, new OutputStreamWriter(System.out));
        reader.setDefaultPrompt("grunt> ");
        final String HISTORYFILE = ".pig_history";
        String historyFile = System.getProperty("user.home") + File.separator + HISTORYFILE;
        reader.setHistory(new History(new File(historyFile)));
        ConsoleReaderInputStream inputStream = new ConsoleReaderInputStream(reader);
        grunt = new Grunt(new BufferedReader(new InputStreamReader(inputStream)), pigContext);
        grunt.setConsoleReader(reader);
        gruntCalled = true;
        grunt.run();
        rc = 0;
        return;
      } else {
        // They have a pig script they want us to run.
        if (remainders.length > 1) {
          throw new RuntimeException(
              "You can only run one pig script " + "at a time from the command line.");
        }
        mode = ExecMode.FILE;
        in = new BufferedReader(new FileReader(remainders[0]));

        // run parameter substitution preprocessor first
        substFile = remainders[0] + ".substituted";
        pin = runParamPreprocessor(in, params, paramFiles, substFile, debug || dryrun);
        if (dryrun) {
          log.info("Dry run completed. Substituted pig script is at " + substFile);
          return;
        }

        logFileName = validateLogFile(logFileName, remainders[0]);
        pigContext.getProperties().setProperty("pig.logfile", logFileName);

        if (!debug) {
          new File(substFile).deleteOnExit();
        }

        // Set job name based on name of the script
        pigContext
            .getProperties()
            .setProperty(PigContext.JOB_NAME, "PigLatin:" + new File(remainders[0]).getName());

        grunt = new Grunt(pin, pigContext);
        gruntCalled = true;
        int[] results = grunt.exec();
        rc = getReturnCodeForStats(results);
        return;
      }

      // Per Utkarsh and Chris invocation of jar file via pig depricated.
    } catch (ParseException e) {
      usage();
      rc = 2;
    } catch (NumberFormatException e) {
      usage();
      rc = 2;
    } catch (PigException pe) {
      if (pe.retriable()) {
        rc = 1;
      } else {
        rc = 2;
      }

      if (!gruntCalled) {
        LogUtils.writeLog(pe, logFileName, log, verbose);
      }
    } catch (Throwable e) {
      rc = 2;
      if (!gruntCalled) {
        LogUtils.writeLog(e, logFileName, log, verbose);
      }
    } finally {
      // clear temp files
      FileLocalizer.deleteTempFiles();
      PerformanceTimerFactory.getPerfTimerFactory().dumpTimers();
      System.exit(rc);
    }
  }