Example #1
0
  @Internal("Sys.setenv")
  public static LogicalVector setEnvironment(
      @Current Context context, StringVector names, StringVector values) {

    Map<String, String> map = context.getSession().getSystemEnvironment();

    LogicalArrayVector.Builder result = new LogicalArrayVector.Builder();
    for (int i = 0; i != names.length(); ++i) {
      map.put(names.getElementAsString(i), values.getElementAsString(i));
      result.add(true);
    }
    return result.build();
  }
Example #2
0
  /**
   * Utility function to extract information about files on the user's file systems.
   *
   * @param context current call Context
   * @param paths the list of files for which to return information
   * @return list column-oriented table of file information
   * @throws FileSystemException
   */
  @Internal("file.info")
  public static ListVector fileInfo(@Current Context context, StringVector paths)
      throws FileSystemException {

    DoubleArrayVector.Builder size = new DoubleArrayVector.Builder();
    LogicalArrayVector.Builder isdir = new LogicalArrayVector.Builder();
    IntArrayVector.Builder mode =
        (IntArrayVector.Builder)
            new IntArrayVector.Builder()
                .setAttribute(Symbols.CLASS, StringVector.valueOf("octmode"));
    DoubleArrayVector.Builder mtime = new DoubleArrayVector.Builder();
    StringVector.Builder exe = new StringVector.Builder();

    for (String path : paths) {
      if (StringVector.isNA(path)) {
        throw new EvalException("invalid filename argument");
      }
      FileObject file = context.resolveFile(path);
      if (file.exists()) {
        if (file.getType() == FileType.FILE) {
          size.add((int) file.getContent().getSize());
        } else {
          size.add(0);
        }
        isdir.add(file.getType() == FileType.FOLDER);
        mode.add(mode(file));
        try {
          mtime.add(file.getContent().getLastModifiedTime());
        } catch (Exception e) {
          mtime.add(0);
        }
        exe.add(file.getName().getBaseName().endsWith(".exe") ? "yes" : "no");
      } else {
        size.addNA();
        isdir.addNA();
        mode.addNA();
        mtime.addNA();
        exe.addNA();
      }
    }

    return ListVector.newNamedBuilder()
        .add("size", size)
        .add("isdir", isdir)
        .add("mode", mode)
        .add("mtime", mtime)
        .add("ctime", mtime)
        .add("atime", mtime)
        .add("exe", exe)
        .build();
  }
Example #3
0
  /**
   * Returns object of class ‘"proc_time"’ which is a numeric vector of length 5, containing the
   * user, system, and total elapsed times for the currently running R process, and the cumulative
   * sum of user and system times of any child processes spawned by it on which it has waited.
   *
   * <p>_The ‘user time’ is the CPU time charged for the execution of user instructions of the
   * calling process. The ‘system time’ is the CPU time charged for execution by the system on
   * behalf of the calling process._
   */
  @Builtin("proc.time")
  public static DoubleVector procTime() {

    DoubleArrayVector.Builder result = new DoubleArrayVector.Builder();
    StringVector.Builder names = new StringVector.Builder();

    long totalCPUTime;
    long userCPUTime;
    long elapsedTime;

    // There doesn't seem to be any platform-independent way of accessing
    // CPU use for the whole JVM process, so we'll have to make do
    // with the timings for the thread we're running on.
    //
    // Additionally, the MX Beans may not be available in all environments,
    // so we need to fallback to app
    try {
      ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
      totalCPUTime = threadMXBean.getCurrentThreadCpuTime();
      userCPUTime = threadMXBean.getCurrentThreadUserTime();

      RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
      elapsedTime = runtimeMXBean.getUptime();
    } catch (Error e) {
      // ThreadMXBean is not available in all environments
      // Specifically, AppEngine will throw variously SecurityErrors or
      // ClassNotFoundErrors if we try to access these classes
      userCPUTime = totalCPUTime = java.lang.System.nanoTime();
      elapsedTime = new Date().getTime();
    }

    // user.self
    names.add("user.self");
    result.add(userCPUTime / NANOSECONDS_PER_SECOND);

    // sys.self
    names.add("sys.self");
    result.add((totalCPUTime - userCPUTime) / NANOSECONDS_PER_SECOND);

    // elapsed
    // (wall clock time)
    names.add("elapsed");
    result.add(elapsedTime);

    // AFAIK, we don't have any platform independent way of accessing
    // this info.

    // user.child
    names.add("user.child");
    result.add(0);

    // sys.child
    names.add("sys.child");
    result.add(0);

    result.setAttribute(Symbols.NAMES, names.build());
    result.setAttribute(Symbols.CLASS, StringVector.valueOf("proc_time"));
    return result.build();
  }
Example #4
0
 @Internal("file.access")
 public static IntVector fileAccess(@Current Context context, StringVector names, int mode)
     throws FileSystemException {
   IntArrayVector.Builder result = new IntArrayVector.Builder();
   for (String name : names) {
     FileObject file = context.resolveFile(pathExpand(name));
     result.add(checkAccess(file, mode));
   }
   result.setAttribute(Symbols.NAMES, new StringArrayVector(names.toArray()));
   return result.build();
 }
Example #5
0
 /**
  * Unlink deletes the file(s) or directories specified by {@code paths}.
  *
  * @param context the current call Context
  * @param paths list of paths to delete
  * @param recursive Should directories be deleted recursively?
  * @return 0 for success, 1 for failure. Not deleting a non-existent file is not a failure, nor is
  *     being unable to delete a directory if recursive = FALSE. However, missing values in x are
  *     regarded as failures.
  * @throws FileSystemException
  */
 @Internal
 public static IntVector unlink(
     @Current Context context, StringVector paths, boolean recursive, boolean force)
     throws FileSystemException {
   IntArrayVector.Builder result = new IntArrayVector.Builder();
   for (String path : paths) {
     if (StringVector.isNA(path)) {
       result.add(0);
     } else {
       FileObject file = context.resolveFile(path);
       delete(file, recursive);
       result.add(1);
     }
   }
   return result.build();
 }
Example #6
0
  @Internal("Sys.getenv")
  public static StringVector getEnvironment(
      @Current Context context, StringVector names, String unset) {
    StringVector.Builder result = new StringArrayVector.Builder();

    Map<String, String> map = context.getSession().getSystemEnvironment();
    if (names.length() == 0) {
      for (Map.Entry<String, String> entry : map.entrySet()) {
        result.add(entry.getKey() + "=" + entry.getValue());
      }
    } else {
      for (String name : names) {
        String value = map.get(name);
        result.add(value == null ? unset : value);
      }
    }
    return result.build();
  }