Ejemplo n.º 1
0
 public BeeLineOpts(BeeLine beeLine, Properties props) {
   this.beeLine = beeLine;
   if (terminal.getWidth() > 0) {
     maxWidth = terminal.getWidth();
   }
   if (terminal.getHeight() > 0) {
     maxHeight = terminal.getHeight();
   }
   loadProperties(props);
 }
Ejemplo n.º 2
0
  /**
   * Hedwig Console
   *
   * @param args arguments
   * @throws IOException
   * @throws InterruptedException
   */
  public HedwigConsole(String[] args) throws IOException, InterruptedException {
    // Setup Terminal
    terminal = Terminal.setupTerminal();
    HedwigCommands.init();
    cl.parseOptions(args);

    if (cl.getCommand() == null) {
      inConsole = true;
    } else {
      inConsole = false;
    }

    org.apache.bookkeeper.conf.ClientConfiguration bkClientConf =
        new org.apache.bookkeeper.conf.ClientConfiguration();
    ServerConfiguration hubServerConf = new ServerConfiguration();
    String serverCfgFile = cl.getOption("server-cfg");
    if (serverCfgFile != null) {
      try {
        hubServerConf.loadConf(new File(serverCfgFile).toURI().toURL());
      } catch (ConfigurationException e) {
        throw new IOException(e);
      }
      try {
        bkClientConf.loadConf(new File(serverCfgFile).toURI().toURL());
      } catch (ConfigurationException e) {
        throw new IOException(e);
      }
    }

    ClientConfiguration hubClientCfg = new ClientConfiguration();
    String clientCfgFile = cl.getOption("client-cfg");
    if (clientCfgFile != null) {
      try {
        hubClientCfg.loadConf(new File(clientCfgFile).toURI().toURL());
      } catch (ConfigurationException e) {
        throw new IOException(e);
      }
    }

    printMessage("Connecting to zookeeper/bookkeeper using HedwigAdmin");
    try {
      admin = new HedwigAdmin(bkClientConf, hubServerConf);
      admin.getZkHandle().register(new MyWatcher());
    } catch (Exception e) {
      throw new IOException(e);
    }

    printMessage("Connecting to default hub server " + hubClientCfg.getDefaultServerHost());
    hubClient = new HedwigClient(hubClientCfg);
    publisher = hubClient.getPublisher();
    subscriber = hubClient.getSubscriber();
    subscriber.addSubscriptionListener(new ConsoleSubscriptionListener());

    // other parameters
    myRegion = hubServerConf.getMyRegion();
  }
Ejemplo n.º 3
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;
 }
Ejemplo n.º 4
0
 public BaseRascalREPL(
     InputStream stdin,
     OutputStream stdout,
     boolean prettyPrompt,
     boolean allowColors,
     File persistentHistory,
     Terminal terminal)
     throws IOException {
   super(stdin, stdout, prettyPrompt, allowColors, persistentHistory, terminal);
   if (terminal.isAnsiSupported() && allowColors) {
     indentedPrettyPrinter = new ReplTextWriter();
     singleLinePrettyPrinter = new ReplTextWriter(false);
   } else {
     indentedPrettyPrinter = new StandardTextWriter();
     singleLinePrettyPrinter = new StandardTextWriter(false);
   }
 }
  /**
   * @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();
    }
  }
Ejemplo n.º 6
0
  protected void printUsageDelegate(
      CommandSession session,
      Object action,
      Map<Option, Field> optionsMap,
      Map<Argument, Field> argsMap,
      PrintStream out) {
    Command command = action.getClass().getAnnotation(Command.class);
    Terminal term = session != null ? (Terminal) session.get(".jline.terminal") : null;
    List<Argument> arguments = new ArrayList<Argument>(argsMap.keySet());
    Collections.sort(
        arguments,
        new Comparator<Argument>() {
          public int compare(Argument o1, Argument o2) {
            return Integer.valueOf(o1.index()).compareTo(Integer.valueOf(o2.index()));
          }
        });

    String commandName = command != null ? command.name() : name;
    String commandScope = command != null ? command.scope() : scope;
    String commandDescription =
        command != null ? command.description() : "No description available for " + name + ".";
    String commandDetailedDescription = command != null ? command.detailedDescription() : "";

    Set<Option> options = new TreeSet<Option>(CommandArguments.OPTION_COMPARATOR);
    options.addAll(optionsMap.keySet());
    options.add(HELP);
    boolean globalScope = NameScoping.isGlobalScope(session, scope);
    out.println(
        Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("DESCRIPTION").a(Ansi.Attribute.RESET));
    out.print("        ");
    if (commandName != null) {
      if (globalScope) {
        out.println(
            Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(commandName).a(Ansi.Attribute.RESET));
      } else {
        out.println(
            Ansi.ansi()
                .a(commandScope)
                .a(":")
                .a(Ansi.Attribute.INTENSITY_BOLD)
                .a(commandName)
                .a(Ansi.Attribute.RESET));
      }
      out.println();
    }
    String description = commandDescription;
    if (isBlank(description)) {
      description = "No description available for " + name + ".";
    }
    out.print("\t");
    out.println(description);
    out.println();
    out.println("\tLoaded from");
    out.println("\t" + scriptFile);
    out.println();

    StringBuffer syntax = new StringBuffer();
    if (globalScope) {
      syntax.append(commandName);
    } else {
      syntax.append(String.format("%s:%s", commandScope, commandName));
    }

    if (options.size() > 0) {
      syntax.append(" [options]");
    }
    if (arguments.size() > 0) {
      syntax.append(' ');
      for (Argument argument : arguments) {
        if (!argument.required()) {
          syntax.append(String.format("[%s] ", argument.name()));
        } else {
          syntax.append(String.format("%s ", argument.name()));
        }
      }
    }

    out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("SYNTAX").a(Ansi.Attribute.RESET));
    out.print("        ");
    out.println(syntax.toString());
    out.println();
    if (arguments.size() > 0) {
      out.println(
          Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("ARGUMENTS").a(Ansi.Attribute.RESET));
      for (Argument argument : arguments) {
        out.print("        ");
        out.println(
            Ansi.ansi()
                .a(Ansi.Attribute.INTENSITY_BOLD)
                .a(argument.name())
                .a(Ansi.Attribute.RESET));
        printFormatted(
            "                ", argument.description(), term != null ? term.getWidth() : 80, out);
        if (!argument.required()) {
          if (argument.valueToShowInHelp() != null && argument.valueToShowInHelp().length() != 0) {
            try {
              if (Argument.DEFAULT_STRING.equals(argument.valueToShowInHelp())) {
                argsMap.get(argument).setAccessible(true);
                Object o = argsMap.get(argument).get(action);
                printObjectDefaultsTo(out, o);
              } else {
                printDefaultsTo(out, argument.valueToShowInHelp());
              }
            } catch (Throwable t) {
              // Ignore
            }
          }
        }
      }
      out.println();
    }
    if (options.size() > 0) {
      out.println(
          Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("OPTIONS").a(Ansi.Attribute.RESET));
      for (Option option : options) {
        String opt = option.name();
        for (String alias : option.aliases()) {
          opt += ", " + alias;
        }
        out.print("        ");
        out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(opt).a(Ansi.Attribute.RESET));
        printFormatted(
            "                ", option.description(), term != null ? term.getWidth() : 80, out);
        if (option.valueToShowInHelp() != null && option.valueToShowInHelp().length() != 0) {
          try {
            if (Option.DEFAULT_STRING.equals(option.valueToShowInHelp())) {
              optionsMap.get(option).setAccessible(true);
              Object o = optionsMap.get(option).get(action);
              printObjectDefaultsTo(out, o);
            } else {
              printDefaultsTo(out, option.valueToShowInHelp());
            }
          } catch (Throwable t) {
            // Ignore
          }
        }
      }
      out.println();
    }
    if (commandDetailedDescription.length() > 0) {
      out.println(
          Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("DETAILS").a(Ansi.Attribute.RESET));
      String desc = loadDescription(action.getClass(), commandDetailedDescription);
      printFormattedFixed("        ", desc, term != null ? term.getWidth() : 80, out);
    }
  }
 public TestRunner() {
   // whatever you might want to do in your constructor, but stay away from Domino objects
   VIRTUAL_CONSOLE = Terminal.getTerminal().getTerminalWidth() < 10;
 }
Ejemplo n.º 8
0
  /** Hadoop {@link Tool} implementation */
  @Override
  public int run(String[] args) throws Exception {

    Options options = new Options();

    configureOptions(options);

    CommandLineParser parser = new GnuParser();

    try {
      CommandLine commandLine = parser.parse(options, args);

      if (commandLine.hasOption(VERBOSE)) {
        Logger.getGlobal().setLevel(Level.FINEST);
      }

      if (commandLine.hasOption(QUIET)) {
        Logger.getGlobal().setLevel(Level.OFF);
      }

      String transformationLocation = commandLine.getOptionValue(TRANSFORMATION);
      String sourcemmLocation = commandLine.getOptionValue(SOURCE_PACKAGE);
      String targetmmLocation = commandLine.getOptionValue(TARGET_PACKAGE);
      String recordsLocation = commandLine.getOptionValue(RECORDS_FILE);
      String inputLocation = commandLine.getOptionValue(INPUT_MODEL);
      String outputLocation =
          commandLine.getOptionValue(
              OUTPUT_MODEL, new Path(inputLocation).suffix(".out.xmi").toString());

      int recommendedMappers = 1;
      if (commandLine.hasOption(RECOMMENDED_MAPPERS)) {
        recommendedMappers =
            ((Number) commandLine.getParsedOptionValue(RECOMMENDED_MAPPERS)).intValue();
      }

      Configuration conf = this.getConf();
      Job job = Job.getInstance(conf, JOB_NAME);

      // Configure classes
      job.setJarByClass(ATLMRMaster.class);
      job.setMapperClass(ATLMRMapper.class);
      job.setReducerClass(ATLMRReducer.class);
      job.setInputFormatClass(NLineInputFormat.class);
      job.setOutputFormatClass(SequenceFileOutputFormat.class);
      job.setMapOutputKeyClass(LongWritable.class);
      job.setMapOutputValueClass(Text.class);
      job.setNumReduceTasks(1);

      // Configure MapReduce input/outputs
      Path recordsPath = new Path(recordsLocation);
      FileInputFormat.setInputPaths(job, recordsPath);
      String timestamp = new SimpleDateFormat("yyyyMMddhhmm").format(new Date());
      String outDirName = "atlmr-out-" + timestamp + "-" + UUID.randomUUID();
      FileOutputFormat.setOutputPath(
          job, new Path(job.getWorkingDirectory().suffix(Path.SEPARATOR + outDirName).toUri()));

      // Configure records per map
      FileSystem fileSystem = FileSystem.get(recordsPath.toUri(), conf);
      InputStream inputStream = fileSystem.open(recordsPath);
      long linesPerMap =
          (long) Math.ceil((double) countLines(inputStream) / (double) recommendedMappers);
      job.getConfiguration().setLong(NLineInputFormat.LINES_PER_MAP, linesPerMap);

      // Configure ATL related inputs/outputs
      job.getConfiguration().set(TRANSFORMATION, transformationLocation);
      job.getConfiguration().set(SOURCE_PACKAGE, sourcemmLocation);
      job.getConfiguration().set(TARGET_PACKAGE, targetmmLocation);
      job.getConfiguration().set(INPUT_MODEL, inputLocation);
      job.getConfiguration().set(OUTPUT_MODEL, outputLocation);

      Logger.getGlobal().log(Level.INFO, "Starting Job execution");
      long begin = System.currentTimeMillis();
      int returnValue = job.waitForCompletion(true) ? STATUS_OK : STATUS_ERROR;
      long end = System.currentTimeMillis();
      Logger.getGlobal()
          .log(
              Level.INFO,
              MessageFormat.format(
                  "Job execution ended in {0}s with status code {1}",
                  (end - begin) / 1000, returnValue));

      return returnValue;

    } catch (ParseException e) {
      System.err.println(e.getLocalizedMessage());
      HelpFormatter formatter = new HelpFormatter();
      formatter.setOptionComparator(new OptionComarator<>());
      try {
        formatter.setWidth(Math.max(Terminal.getTerminal().getTerminalWidth(), 80));
      } catch (Throwable t) {
        // Nothing to do...
      }
      ;
      formatter.printHelp("yarn jar <this-file.jar>", options, true);
      return STATUS_ERROR;
    }
  }
Ejemplo n.º 9
0
  /**
   * Executes the log command using the provided options.
   *
   * @param cli
   * @throws IOException
   * @see org.geogit.cli.AbstractCommand#runInternal(org.geogit.cli.GeogitCLI)
   */
  @Override
  public void runInternal(GeogitCLI cli) throws Exception {
    final Platform platform = cli.getPlatform();
    Preconditions.checkState(
        cli.getGeogit() != null, "Not a geogit repository: " + platform.pwd().getAbsolutePath());

    Preconditions.checkArgument(
        !(args.summary && args.oneline), "--summary and --oneline cannot be used together");
    Preconditions.checkArgument(
        !(args.stats && args.oneline), "--stats and --oneline cannot be used together");
    Preconditions.checkArgument(
        !(args.stats && args.oneline), "--name-only and --oneline cannot be used together");

    geogit = cli.getGeogit();

    LogOp op =
        geogit.command(LogOp.class).setTopoOrder(args.topo).setFirstParentOnly(args.firstParent);

    refs = Maps.newHashMap();
    if (args.decoration) {
      Optional<Ref> head = geogit.command(RefParse.class).setName(Ref.HEAD).call();
      refs.put(head.get().getObjectId(), Ref.HEAD);
      ImmutableSet<Ref> set = geogit.command(ForEachRef.class).call();
      for (Ref ref : set) {
        ObjectId id = ref.getObjectId();
        if (refs.containsKey(id)) {
          refs.put(id, refs.get(id) + ", " + ref.getName());
        } else {
          refs.put(id, ref.getName());
        }
      }
    }
    if (args.all) {
      ImmutableSet<Ref> refs = geogit.command(ForEachRef.class).call();
      List<ObjectId> list = Lists.newArrayList();
      for (Ref ref : refs) {
        list.add(ref.getObjectId());
      }
      Optional<Ref> head = geogit.command(RefParse.class).setName(Ref.HEAD).call();
      if (head.isPresent()) {
        Ref ref = head.get();
        if (ref instanceof SymRef) {
          ObjectId id = ref.getObjectId();
          list.remove(id);
          list.add(id); // put the HEAD ref in the last position, to give it preference
        }
      }
      for (ObjectId id : list) {
        op.addCommit(id);
      }
    } else if (args.branch != null) {
      Optional<Ref> obj = geogit.command(RefParse.class).setName(args.branch).call();
      Preconditions.checkArgument(obj.isPresent(), "Wrong branch name: " + args.branch);
      op.addCommit(obj.get().getObjectId());
    }

    if (args.author != null && !args.author.isEmpty()) {
      op.setAuthor(args.author);
    }
    if (args.committer != null && !args.committer.isEmpty()) {
      op.setCommiter(args.committer);
    }
    if (args.skip != null) {
      op.setSkip(args.skip.intValue());
    }
    if (args.limit != null) {
      op.setLimit(args.limit.intValue());
    }
    if (args.since != null || args.until != null) {
      Date since = new Date(0);
      Date until = new Date();
      if (args.since != null) {
        since = new Date(geogit.command(ParseTimestamp.class).setString(args.since).call());
      }
      if (args.until != null) {
        until = new Date(geogit.command(ParseTimestamp.class).setString(args.until).call());
        if (args.all) {
          throw new IllegalStateException(
              "Cannot specify 'until' commit when listing all branches");
        }
      }
      op.setTimeRange(new Range<Date>(Date.class, since, until));
    }
    if (!args.sinceUntilPaths.isEmpty()) {
      List<String> sinceUntil =
          ImmutableList.copyOf((Splitter.on("..").split(args.sinceUntilPaths.get(0))));
      Preconditions.checkArgument(
          sinceUntil.size() == 1 || sinceUntil.size() == 2,
          "Invalid refSpec format, expected [<until>]|[<since>..<until>]: %s",
          args.sinceUntilPaths.get(0));

      String sinceRefSpec;
      String untilRefSpec;
      if (sinceUntil.size() == 1) {
        // just until was given
        sinceRefSpec = null;
        untilRefSpec = sinceUntil.get(0);
      } else {
        sinceRefSpec = sinceUntil.get(0);
        untilRefSpec = sinceUntil.get(1);
      }
      if (sinceRefSpec != null) {
        Optional<ObjectId> since;
        since = geogit.command(RevParse.class).setRefSpec(sinceRefSpec).call();
        Preconditions.checkArgument(since.isPresent(), "Object not found '%s'", sinceRefSpec);
        op.setSince(since.get());
      }
      if (untilRefSpec != null) {
        if (args.all) {
          throw new IllegalStateException(
              "Cannot specify 'until' commit when listing all branches");
        }
        Optional<ObjectId> until;
        until = geogit.command(RevParse.class).setRefSpec(untilRefSpec).call();
        Preconditions.checkArgument(until.isPresent(), "Object not found '%s'", sinceRefSpec);
        op.setUntil(until.get());
      }
    }
    if (!args.pathNames.isEmpty()) {
      for (String s : args.pathNames) {
        op.addPath(s);
      }
    }
    Iterator<RevCommit> log = op.call();
    console = cli.getConsole();
    Terminal terminal = console.getTerminal();
    switch (args.color) {
      case never:
        useColor = false;
        break;
      case always:
        useColor = true;
        break;
      default:
        useColor = terminal.isAnsiSupported();
    }

    if (!log.hasNext()) {
      console.println("No commits to show");
      console.flush();
      return;
    }

    LogEntryPrinter printer;
    if (args.oneline) {
      printer = new OneLineConverter();
    } else {
      LOG_DETAIL detail;
      if (args.summary) {
        detail = LOG_DETAIL.SUMMARY;
      } else if (args.names) {
        detail = LOG_DETAIL.NAMES_ONLY;
      } else if (args.stats) {
        detail = LOG_DETAIL.STATS;
      } else {
        detail = LOG_DETAIL.NOTHING;
      }

      printer = new StandardConverter(detail, geogit.getPlatform());
    }

    while (log.hasNext()) {
      printer.print(log.next());
      console.flush();
    }
  }