Example #1
0
  static PyObject replacePathItem(PySystemState sys, int idx, PyList paths) {
    PyObject path = paths.__getitem__(idx);
    if (path instanceof SyspathArchive) {
      // already an archive
      return path;
    }

    try {
      // this has the side affect of adding the jar to the PackageManager during the
      // initialization of the SyspathArchive
      path = new SyspathArchive(sys.getPath(path.toString()));
    } catch (Exception e) {
      return path;
    }
    paths.__setitem__(idx, path);
    return path;
  }
Example #2
0
  @Override
  protected Enumeration<URL> findResources(String res) throws IOException {
    List<URL> resources = new ArrayList<URL>();

    PySystemState sys = Py.getSystemState();

    if (res.charAt(0) == SLASH_CHAR) {
      res = res.substring(1);
    }
    String entryRes = res;
    if (File.separatorChar != SLASH_CHAR) {
      res = res.replace(SLASH_CHAR, File.separatorChar);
      entryRes = entryRes.replace(File.separatorChar, SLASH_CHAR);
    }

    PyList path = sys.path;
    for (int i = 0; i < path.__len__(); i++) {
      PyObject entry = replacePathItem(sys, i, path);
      if (entry instanceof SyspathArchive) {
        SyspathArchive archive = (SyspathArchive) entry;
        ZipEntry ze = archive.getEntry(entryRes);
        if (ze != null) {
          try {
            resources.add(new URL("jar:file:" + archive.asUriCompatibleString() + "!/" + entryRes));
          } catch (MalformedURLException e) {
            throw new RuntimeException(e);
          }
        }
        continue;
      }
      if (!(entry instanceof PyUnicode)) {
        entry = entry.__str__();
      }
      String dir = sys.getPath(entry.toString());
      try {
        File resource = new File(dir, res);
        if (!resource.exists()) {
          continue;
        }
        resources.add(resource.toURI().toURL());
      } catch (MalformedURLException e) {
        throw new RuntimeException(e);
      }
    }
    return Collections.enumeration(resources);
  }