Beispiel #1
0
  private IStatus getReposStatus(
      final ISelectedRepos repos, final ISelectedRepos current, final int confirm) {
    if (repos.getRepos().isEmpty()) {
      return createStatus(
          IStatus.ERROR,
          "No repository is selected. Select the repositories where to install R packages from.");
    }

    final boolean requireCRAN = RVarRepo.requireCRANMirror(repos.getRepos());
    if (requireCRAN && repos.getCRANMirror() == null) {
      return createStatus(IStatus.ERROR, "No CRAN mirror is selected. Selected a mirror for CRAN.");
    }
    final boolean requireBioC = RVarRepo.requireBioCMirror(repos.getRepos());
    if (requireBioC && repos.getBioCMirror() == null) {
      return createStatus(
          IStatus.ERROR, "No BioC mirror is selected. Selected a mirror for Bioconductor.");
    }

    if ((requireCRAN && (confirm & REQUIRE_CRAN) != 0)
        || (requireBioC && (confirm & REQUIRE_BIOC) != 0)
        || (repos != current && !repos.equals(current))) {
      return createStatus(
          IStatus.INFO,
          "Check the repository settings and confirm with 'Apply' to show the available R packages.");
    }

    return Status.OK_STATUS;
  }
Beispiel #2
0
  private void checkRepos(final Change event) {
    if ((fRequireLoad & (REQUIRE_CRAN | REQUIRE_BIOC | REQUIRE_REPOS)) != 0) {
      return;
    }

    SelectedRepos selected = fSelectedRepos;

    fAllRepos = new ArrayList<>(fCustomRepos.size() + fAddRepos.size() + fRRepos.size());
    fAllRepos.addAll(fCustomRepos);
    fAllRepos.addAll(fAddRepos);
    for (final RRepo repo : fAllRepos) {
      if (repo instanceof RVarRepo) {
        ((RVarRepo) repo).updateURL(selected);
      }
    }
    for (final RRepo repo : fRRepos) {
      if (repo instanceof RVarRepo) {
        ((RVarRepo) repo).updateURL(selected);
      }
    }
    for (final RRepo repo : fRRepos) {
      if (!repo.getId().isEmpty()) {
        if (RPkgUtil.getRepoById(fAllRepos, repo.getId()) == null) {
          fAllRepos.add(repo);
        }
      } else {
        if (Util.getRepoByURL(fAllRepos, repo) == null) {
          fAllRepos.add(
              RVarRepo.create(RRepo.R_PREFIX + repo.getURL(), repo.getName(), repo.getURL(), null));
        }
      }
    }

    {
      final Collection<RRepo> selectedRepos = selected.getRepos();
      final Collection<RRepo> previous =
          (fFirstTime && selectedRepos.isEmpty()) ? fSelectedReposInR : selectedRepos;
      final List<RRepo> repos = new ArrayList<>(previous.size());
      for (RRepo repo : previous) {
        repo = Util.findRepo(fAllRepos, repo);
        if (repo != null) {
          repos.add(repo);
        }
      }
      selected =
          new SelectedRepos(
              repos, selected.getCRANMirror(), selected.getBioCVersion(), selected.getBioCMirror());
      fSelectedRepos = selected;
    }

    fRequireLoad |= REQUIRE_REPO_PKGS;

    event.fRepos = 1;
  }
Beispiel #3
0
  private void checkMirrors(final Change event) {
    if ((fRequireLoad & (REQUIRE_CRAN | REQUIRE_BIOC)) != 0) {
      return;
    }

    SelectedRepos selected = fSelectedRepos;
    fAllCRAN = ConstArrayList.concat(fCustomCRAN, fRCRAN);

    RRepo selectedCRAN = selected.getCRANMirror();
    if (selected.getCRANMirror() != null) {
      selectedCRAN = Util.findRepo(fAllCRAN, selectedCRAN);
    } else if (fFirstTime && fSelectedCRANInR != null) {
      selectedCRAN = Util.getRepoByURL(fAllCRAN, fSelectedCRANInR);
    }
    if (selectedCRAN == null) {
      fRequireConfirm |= REQUIRE_CRAN;
      selectedCRAN = PreferencesUtil.getInstancePrefs().getPreferenceValue(LAST_CRAN_PREF);
      if (selectedCRAN != null) {
        selectedCRAN = Util.findRepo(fAllCRAN, selectedCRAN);
      }
      if (!fCustomCRAN.isEmpty()
          && (selectedCRAN == null || !selectedCRAN.getId().startsWith(RRepo.CUSTOM_PREFIX))) {
        selectedCRAN = fCustomCRAN.get(0);
      }
      if (fFirstTime && selectedCRAN == null) {
        selectedCRAN = fRCRANByCountry;
      }
    }

    RRepo selectedBioC = selected.getBioCMirror();
    fAllBioC = ConstArrayList.concat(fCustomBioC, fRBioC);
    if (selectedBioC != null) {
      selectedBioC = Util.findRepo(fAllBioC, selectedBioC);
    } else if (fFirstTime && fSelectedBioCInR != null) {
      selectedBioC = RPkgUtil.getRepoByURL(fAllBioC, fSelectedBioCInR);
    }
    if (selectedBioC == null) {
      fRequireConfirm |= REQUIRE_BIOC;
      selectedBioC = PreferencesUtil.getInstancePrefs().getPreferenceValue(LAST_BIOC_PREF);
      if (!fCustomBioC.isEmpty()
          && (selectedBioC == null || !selectedBioC.getId().startsWith(RRepo.CUSTOM_PREFIX))) {
        selectedBioC = fCustomBioC.get(0);
      }
      if (fFirstTime && selectedBioC == null) {
        selectedBioC = Util.getRepoByURL(fAllBioC, "http://www.bioconductor.org"); // $NON-NLS-1$
      }
    }

    selected = new SelectedRepos(selected.getRepos(), selectedCRAN, fBioCVersion, selectedBioC);
    if ((fRequireLoad & (REQUIRE_REPOS)) == 0) {
      for (final RRepo repo : fAllRepos) {
        if (repo instanceof RVarRepo) {
          ((RVarRepo) repo).updateURL(selected);
        }
      }
    }
    fSelectedRepos = selected;

    event.fPkgs = 1;
  }
Beispiel #4
0
  @Override
  public void setSelectedRepos(final ISelectedRepos repos) {
    List<RRepo> selectedRepos;
    {
      final Collection<RRepo> selected = repos.getRepos();
      selectedRepos = new ArrayList<>(selected.size());
      for (final RRepo repo : fAllRepos) {
        if (selected.contains(repo)) {
          selectedRepos.add(repo);
        }
      }
    }
    RRepo selectedCRAN;
    {
      final RRepo repo = repos.getCRANMirror();
      selectedCRAN = (repo != null) ? Util.findRepo(fAllCRAN, repo) : null;
      fRequireConfirm &= ~REQUIRE_CRAN;
    }
    RRepo selectedBioC;
    {
      final RRepo repo = repos.getBioCMirror();
      selectedBioC = (repo != null) ? Util.findRepo(fAllBioC, repo) : null;
      fRequireConfirm &= ~REQUIRE_BIOC;
    }

    final SelectedRepos previousSettings = fSelectedRepos;
    final SelectedRepos newSettings =
        new SelectedRepos(
            selectedRepos, selectedCRAN, previousSettings.getBioCVersion(), selectedBioC);
    for (final RRepo repo : fAllRepos) {
      if (repo instanceof RVarRepo) {
        ((RVarRepo) repo).updateURL(newSettings);
      }
    }
    fSelectedRepos = newSettings;
    savePrefs(newSettings);

    if (!newSettings.equals(previousSettings)) {
      fRequireLoad |= (REQUIRE_REPO_PKGS);

      final Change event = new Change(fREnv);
      event.fRepos = 1;
      fireUpdate(event);
    }
  }
Beispiel #5
0
 public static RRepo createRepoFromR(String id, final String name, String url) {
   if (id == null) {
     id = ""; // $NON-NLS-1$
   }
   if ("@CRAN@".equals(url)) { // $NON-NLS-1$
     url = "%cran"; // $NON-NLS-1$
     if (id.isEmpty()) {
       id = "CRAN"; // $NON-NLS-1$
     }
   } else {
     url = checkURL(url);
     if (url.isEmpty()) {
       return null;
     }
   }
   if (!id.isEmpty()
       && !id.startsWith(RRepo.CUSTOM_PREFIX)
       && !id.startsWith(RRepo.SPECIAL_PREFIX)) {
     id = RRepo.R_PREFIX + id;
   }
   return RVarRepo.create(id, name, url, null);
 }
Beispiel #6
0
  private ISelectedRepos runLoadRepos(final RService r, final IProgressMonitor monitor)
      throws CoreException {
    try {
      String bioCVersion;
      {
        final RObject data =
            r.evalData("as.character(tools:::.BioC_version_associated_with_R_version)", monitor);
        bioCVersion = RDataUtil.checkSingleCharValue(data);
      }

      final boolean loadRMirrors = ((fRequireLoad & (REQUIRE_CRAN | REQUIRE_BIOC)) != 0);
      final boolean loadRRepos = ((fRequireLoad & (REQUIRE_REPOS)) != 0);

      List<RRepo> rCRAN = null;
      List<RRepo> rBioC = null;
      String selectedCRAN = null;
      RRepo rCRANByCountry = null;
      String selectedBioC = null;
      if (loadRMirrors) {
        monitor.subTask("Fetching available mirrors...");
        final String region = Locale.getDefault().getCountry().toLowerCase();
        {
          final RObject data =
              r.evalData(
                  "getCRANmirrors()[c('Name', 'URL', 'CountryCode')]", monitor); // $NON-NLS-1$
          final RDataFrame df = RDataUtil.checkRDataFrame(data);
          final RCharacterStore names =
              RDataUtil.checkRCharVector(df.get("Name")).getData(); // $NON-NLS-1$
          final RCharacterStore urls =
              RDataUtil.checkRCharVector(df.get("URL")).getData(); // $NON-NLS-1$
          final RCharacterStore regions =
              RDataUtil.checkRCharVector(df.get("CountryCode")).getData(); // $NON-NLS-1$

          final int l = RDataUtil.checkIntLength(names);
          rCRAN = new ArrayList<>(l);
          for (int i = 0; i < l; i++) {
            final String url = Util.checkURL(urls.getChar(i));
            if (!url.isEmpty()) {
              final RRepo repo = new RRepo(RRepo.R_PREFIX + url, names.getChar(i), url, null);
              rCRAN.add(repo);
              if (rCRANByCountry == null
                  && !region.isEmpty()
                  && region.equals(regions.getChar(i))) {
                rCRANByCountry = repo;
              }
            }
          }
        }

        {
          final String[][] fix =
              new String[][] {
                {"Seattle (USA)", "http://www.bioconductor.org"},
                {"Bethesda (USA)", "http://watson.nci.nih.gov/bioc_mirror"},
                {"Dortmund (Germany)", "http://bioconductor.statistik.tu-dortmund.de"},
                {"Bergen (Norway)", "http://bioconductor.uib.no"},
                {"Cambridge (UK)", "http://mirrors.ebi.ac.uk/bioconductor"}
              };
          rBioC = new ArrayList<>(fix.length);
          for (int i = 0; i < fix.length; i++) {
            final String url = Util.checkURL(fix[i][1]);
            if (!url.isEmpty()) {
              rBioC.add(new RRepo(RRepo.R_PREFIX + url, fix[i][0], url, null));
            }
          }
        }
      }

      List<RRepo> rrepos = null;
      List<RRepo> selected = null;
      if (loadRRepos) {
        monitor.subTask("Fetching available repositories...");
        {
          final RObject data = r.evalData("options('repos')[[1L]]", monitor); // $NON-NLS-1$
          if (data.getRObjectType() != RObject.TYPE_NULL) {
            final RCharacterStore urls = RDataUtil.checkRCharVector(data).getData();
            final RStore ids = ((RVector<?>) data).getNames();

            final int l = RDataUtil.checkIntLength(urls);
            selected = new ArrayList<>(l);
            for (int i = 0; i < l; i++) {
              final String id = (ids != null) ? ids.getChar(i) : null;
              final String url = urls.getChar(i);

              final RRepo repo = Util.createRepoFromR(id, null, url);
              if (repo != null) {
                selected.add(repo);
              }
            }
          } else {
            selected = new ArrayList<>(4);
          }
        }

        final RObject data =
            r.evalData(
                "local({"
                    + //$NON-NLS-1$
                    "p <- file.path(Sys.getenv('HOME'), '.R', 'repositories')\n"
                    + //$NON-NLS-1$
                    "if (!file.exists(p)) p <- file.path(R.home('etc'), 'repositories')\n"
                    + //$NON-NLS-1$
                    "r <- utils::read.delim(p, header = TRUE, comment.char = '#', colClasses = c(rep.int('character', 3L), rep.int('logical', 4L)))\n"
                    + //$NON-NLS-1$
                    "r[c(names(r)[1L], 'URL', 'default')]\n"
                    + //$NON-NLS-1$
                    "})",
                monitor); //$NON-NLS-1$
        final RDataFrame df = RDataUtil.checkRDataFrame(data);
        final RStore ids = df.getRowNames();
        final RCharacterStore labels = RDataUtil.checkRCharVector(df.get(0)).getData();
        final RCharacterStore urls =
            RDataUtil.checkRCharVector(df.get("URL")).getData(); // $NON-NLS-1$
        final RLogicalStore isDefault =
            (selected.isEmpty())
                ? RDataUtil.checkRLogiVector(df.get("default")).getData()
                : null; //$NON-NLS-1$

        {
          final int l = RDataUtil.checkIntLength(labels);
          rrepos = new ArrayList<>(l + 4);
          for (int i = 0; i < l; i++) {
            final String id = (ids != null) ? ids.getChar(i) : null;
            final String url = urls.getChar(i);

            final RRepo repo = Util.createRepoFromR(id, labels.getChar(i), url);
            if (repo != null) {
              rrepos.add(repo);
              if (isDefault != null && isDefault.getLogi(i)) {
                selected.add(repo);
              }
            }
          }
        }

        for (int i = 0; i < selected.size(); i++) {
          final RRepo repo = selected.get(i);
          RRepo rrepo = null;
          if (!repo.getURL().isEmpty()) {
            rrepo = RPkgUtil.getRepoByURL(rrepos, repo.getURL());
          }
          if (rrepo != null) {
            selected.set(i, rrepo);
            continue;
          }
          if (!repo.getId().isEmpty()) {
            final int j = rrepos.indexOf(repo); // by id
            if (j >= 0) {
              rrepo = rrepos.get(j);
              if (!RVarRepo.hasVars(rrepo.getURL())) {
                rrepo.setURL(repo.getURL());
              }
              selected.set(i, rrepo);
              continue;
            }
          }
          repo.setName(RRepo.hintName(repo));
          continue;
        }
      }

      if (loadRMirrors) {
        if (loadRRepos) {
          final RRepo repo = RPkgUtil.getRepoById(rrepos, RRepo.CRAN_ID);
          if (repo != null && !repo.getURL().isEmpty() && !RVarRepo.hasVars(repo.getURL())) {
            selectedCRAN = repo.getURL();
          }
        } else {
          final RObject data = r.evalData("options('repos')[[1L]]['CRAN']", monitor); // $NON-NLS-1$
          if (data.getRObjectType() != RObject.TYPE_NULL) {
            final String url = Util.checkURL(RDataUtil.checkSingleChar(data));
            if (!url.isEmpty() && !RVarRepo.hasVars(url)) {
              selectedCRAN = url;
            }
          }
        }
        {
          final RObject data = r.evalData("options('BioC_mirror')[[1L]]", monitor); // $NON-NLS-1$
          if (data.getRObjectType() != RObject.TYPE_NULL) {
            selectedBioC = RDataUtil.checkSingleChar(data);
          }
        }
      }

      getWriteLock().lock();
      try {
        fBioCVersion = bioCVersion;

        if (loadRMirrors) {
          fRequireLoad &= ~(REQUIRE_CRAN | REQUIRE_BIOC);
          fRCRAN = rCRAN;
          fRCRANByCountry = rCRANByCountry;
          fSelectedCRANInR = selectedCRAN;
          fRBioC = rBioC;
          fSelectedBioCInR = selectedBioC;
          fMirrorsStamp = fRTaskEvent.fStamp;

          checkMirrors(fRTaskEvent);
        }
        if (loadRRepos) {
          fRequireLoad &= ~(REQUIRE_REPOS);
          fRRepos = rrepos;
          fSelectedReposInR = selected;

          checkRepos(fRTaskEvent);
        }
        fFirstTime = false;

        if (getReposStatus(fSelectedRepos, fSelectedRepos, fRequireConfirm).isOK()) {
          return fSelectedRepos;
        }
        return null;
      } finally {
        getWriteLock().unlock();
      }
    } catch (final Exception e) {
      throw new CoreException(
          new Status(
              IStatus.ERROR,
              RCore.PLUGIN_ID,
              0,
              "An error occurred when loading data for package manager.",
              e));
    }
  }