ENH Use dist-info + `importlib.metadata` to store and retrieve package metadata. This switches to using the file system to store the information about packages
and using `importlib.metadata` to retrieve it.
This has two related benefits:

1. We don't have to separately maintain our own separate state about what
we've installed.

2. We are guaranteed to agree with the Python stdlib about whether or not a
package is installed, and if so which version is installed. Since the state is 
maintained in only one place, there is no chance of it diverging.

According to the packaging specs, if the package is from a url we should put 
that into a file here:
https://packaging.python.org/en/latest/specifications/direct-url/#direct-url
Other than that, we should set the `INSTALLER` to `pyodide.loadPackage` 
if the package was loaded with `pyodide.loadPackage` either directly or 
indirectly via `micropip`. Otherwise set `INSTALLER` to `micropip`. That
way we can maintain the current behavior of `micropip.list`:
1. if `direct_url.json` is present, then `source` is the url in `direct_url.json`
2. otherwise, set `source` to `pyodide` if `INSTALLER` is `pyodide.loadPackage`
and set it to `micropip` if `INSTALLER` is `micropip`.

Oddly enough, the packaging specs suggest no place to put the source
index, so it seems that according to the specs if a package was installed
by name from some custom index, this info should not be recorded.

### Checklists

- [x] Add a [CHANGELOG](https://github.com/pyodide/pyodide/blob/main/docs/project/changelog.md) entry
- [x] Add / update tests

import { expectType, expectAssignable } from "tsd";
import {
  loadPyodide,
  PyProxy,
  PyProxyWithLength,
  PyProxyWithGet,
  PyProxyWithSet,
  PyProxyWithHas,
  PyProxyIterable,
  PyProxyIterator,
  PyProxyAwaitable,
  PyProxyBuffer,
  PyProxyCallable,
  PyBuffer,
  TypedArray,
} from "pyodide";

async function main() {
  let pyodide = await loadPyodide();
  expectType<Promise<typeof pyodide>>(loadPyodide({ indexURL: "blah" }));
  expectType<Promise<typeof pyodide>>(loadPyodide({ fullStdLib: true }));

  expectType<Promise<typeof pyodide>>(
    loadPyodide({
      indexURL: "blah",
      fullStdLib: true,
      stdin: () => "a string",
      stdout: (x: string) => {},
      stderr: (err: string) => {},
    })
  );

  expectType<PyProxy>(pyodide.globals);

  let x: any;
  expectType<boolean>(pyodide.isPyProxy(x));
  if (pyodide.isPyProxy(x)) {
    expectType<PyProxy>(x);
  } else {
    expectType<any>(x);
  }

  let px: PyProxy = <PyProxy>{};

  expectType<any>(pyodide.runPython("1+1"));
  expectType<any>(pyodide.runPython("1+1", { globals: px }));
  expectType<Promise<any>>(pyodide.runPythonAsync("1+1"));
  expectType<Promise<any>>(pyodide.runPythonAsync("1+1", { globals: px }));

  expectType<Promise<void>>(pyodide.loadPackagesFromImports("import some_pkg"));
  expectType<Promise<void>>(
    pyodide.loadPackagesFromImports("import some_pkg", (x: any) =>
      console.log(x)
    )
  );
  expectType<Promise<void>>(
    pyodide.loadPackagesFromImports(
      "import some_pkg",
      (x: any) => console.log(x),
      (x: any) => console.warn(x)
    )
  );

  expectType<Promise<void>>(pyodide.loadPackage("blah"));
  expectType<Promise<void>>(pyodide.loadPackage(["blah", "blah2"]));
  expectType<Promise<void>>(
    pyodide.loadPackage("blah", (x: any) => console.log(x))
  );
  expectType<Promise<void>>(
    pyodide.loadPackage(
      ["blah", "blah2"],
      (x: any) => console.log(x),
      (x: any) => console.warn(x)
    )
  );
  expectType<Promise<void>>(pyodide.loadPackage(px));

  expectType<PyProxy>(pyodide.pyodide_py);
  expectType<void>(pyodide.registerJsModule("blah", { a: 7 }));
  expectType<void>(pyodide.unregisterJsModule("blah"));

  pyodide.setInterruptBuffer(new Int32Array(1));
  expectType<any>(pyodide.toPy({}));
  expectType<string>(pyodide.version);

  expectType<any>(px.x);
  expectType<PyProxy>(px.copy());
  expectType<void>(px.destroy("blah"));
  expectType<void>(px.destroy());
  expectType<any>(px.toJs());
  expectType<any>(px.toJs({}));
  expectType<any>(px.toJs({ depth: 10 }));
  expectType<any>(px.toJs({ create_pyproxies: false }));
  expectType<any>(px.toJs({ create_pyproxies: true }));
  expectType<any>(px.toJs({ pyproxies: [] }));
  expectType<string>(px.toString());
  expectType<string>(px.type);

  if (px.supportsGet()) {
    expectType<PyProxyWithGet>(px);
    expectType<(x: any) => any>(px.get);
  }

  if (px.supportsHas()) {
    expectType<PyProxyWithHas>(px);
    expectType<(x: any) => boolean>(px.has);
  }

  if (px.supportsLength()) {
    expectType<PyProxyWithLength>(px);
    expectType<number>(px.length);
  }

  if (px.supportsSet()) {
    expectType<PyProxyWithSet>(px);
    expectType<(x: any, y: any) => void>(px.set);
  }

  if (px.isAwaitable()) {
    expectType<PyProxyAwaitable>(px);
    expectType<any>(await px);
  }

  if (px.isBuffer()) {
    expectType<PyProxyBuffer>(px);
    let buf = px.getBuffer();
    expectType<PyBuffer>(buf);
    expectType<boolean>(buf.c_contiguous);
    expectType<TypedArray>(buf.data);
    expectType<boolean>(buf.f_contiguous);
    expectType<string>(buf.format);
    expectType<number>(buf.itemsize);
    expectType<number>(buf.nbytes);
    expectType<number>(buf.ndim);
    expectType<number>(buf.offset);
    expectType<boolean>(buf.readonly);
    expectType<() => void>(buf.release);
    expectType<number[]>(buf.shape);
    expectType<number[]>(buf.strides);
  }

  if (px.isCallable()) {
    expectType<PyProxyCallable>(px);
    expectType<any>(px(1, 2, 3));
    expectAssignable<(...args: any[]) => any>(px);
  }

  if (px.isIterable()) {
    expectType<PyProxyIterable>(px);
    for (let x of px) {
      expectType<any>(x);
    }
    let it = px[Symbol.iterator]();
    expectAssignable<{ done?: any; value: any }>(it.next());
  }

  if (px.isIterator()) {
    expectType<PyProxyIterator>(px);
    expectAssignable<{ done?: any; value: any }>(px.next());
    expectAssignable<{ done?: any; value: any }>(px.next(22));
  }

  pyodide.unpackArchive(new Uint8Array(40), "tar");
  pyodide.unpackArchive(new Uint8Array(40), "tar", {
    extractDir: "/some/path",
  });
}

import { expectType, expectAssignable } from "tsd";
import {
  loadPyodide,
  PyProxy,
  PyProxyWithLength,
  PyProxyWithGet,
  PyProxyWithSet,
  PyProxyWithHas,
  PyProxyIterable,
  PyProxyIterator,
  PyProxyAwaitable,
  PyProxyBuffer,
  PyProxyCallable,
  PyBuffer,
  TypedArray,
} from "pyodide";

async function main() {
  let pyodide = await loadPyodide();
  expectType<Promise<typeof pyodide>>(loadPyodide({ indexURL: "blah" }));
  expectType<Promise<typeof pyodide>>(loadPyodide({ fullStdLib: true }));

  expectType<Promise<typeof pyodide>>(
    loadPyodide({
      indexURL: "blah",
      fullStdLib: true,
      stdin: () => "a string",
      stdout: (x: string) => {},
      stderr: (err: string) => {},
    })
  );

  expectType<PyProxy>(pyodide.globals);

  let x: any;
  expectType<boolean>(pyodide.isPyProxy(x));
  if (pyodide.isPyProxy(x)) {
    expectType<PyProxy>(x);
  } else {
    expectType<any>(x);
  }

  let px: PyProxy = <PyProxy>{};

  expectType<any>(pyodide.runPython("1+1"));
  expectType<any>(pyodide.runPython("1+1", { globals: px }));
  expectType<Promise<any>>(pyodide.runPythonAsync("1+1"));
  expectType<Promise<any>>(pyodide.runPythonAsync("1+1", { globals: px }));

  expectType<Promise<void>>(pyodide.loadPackagesFromImports("import some_pkg"));
  expectType<Promise<void>>(
    pyodide.loadPackagesFromImports("import some_pkg", (x: any) =>
      console.log(x)
    )
  );
  expectType<Promise<void>>(
    pyodide.loadPackagesFromImports(
      "import some_pkg",
      (x: any) => console.log(x),
      (x: any) => console.warn(x)
    )
  );

  expectType<Promise<void>>(pyodide.loadPackage("blah"));
  expectType<Promise<void>>(pyodide.loadPackage(["blah", "blah2"]));
  expectType<Promise<void>>(
    pyodide.loadPackage("blah", (x: any) => console.log(x))
  );
  expectType<Promise<void>>(
    pyodide.loadPackage(
      ["blah", "blah2"],
      (x: any) => console.log(x),
      (x: any) => console.warn(x)
    )
  );
  expectType<Promise<void>>(pyodide.loadPackage(px));

  expectType<PyProxy>(pyodide.pyodide_py);
  expectType<void>(pyodide.registerJsModule("blah", { a: 7 }));
  expectType<void>(pyodide.unregisterJsModule("blah"));

  pyodide.setInterruptBuffer(new Int32Array(1));
  expectType<any>(pyodide.toPy({}));
  expectType<string>(pyodide.version);

  expectType<any>(px.x);
  expectType<PyProxy>(px.copy());
  expectType<void>(px.destroy("blah"));
  expectType<void>(px.destroy());
  expectType<any>(px.toJs());
  expectType<any>(px.toJs({}));
  expectType<any>(px.toJs({ depth: 10 }));
  expectType<any>(px.toJs({ create_pyproxies: false }));
  expectType<any>(px.toJs({ create_pyproxies: true }));
  expectType<any>(px.toJs({ pyproxies: [] }));
  expectType<string>(px.toString());
  expectType<string>(px.type);

  if (px.supportsGet()) {
    expectType<PyProxyWithGet>(px);
    expectType<(x: any) => any>(px.get);
  }

  if (px.supportsHas()) {
    expectType<PyProxyWithHas>(px);
    expectType<(x: any) => boolean>(px.has);
  }

  if (px.supportsLength()) {
    expectType<PyProxyWithLength>(px);
    expectType<number>(px.length);
  }

  if (px.supportsSet()) {
    expectType<PyProxyWithSet>(px);
    expectType<(x: any, y: any) => void>(px.set);
  }

  if (px.isAwaitable()) {
    expectType<PyProxyAwaitable>(px);
    expectType<any>(await px);
  }

  if (px.isBuffer()) {
    expectType<PyProxyBuffer>(px);
    let buf = px.getBuffer();
    expectType<PyBuffer>(buf);
    expectType<boolean>(buf.c_contiguous);
    expectType<TypedArray>(buf.data);
    expectType<boolean>(buf.f_contiguous);
    expectType<string>(buf.format);
    expectType<number>(buf.itemsize);
    expectType<number>(buf.nbytes);
    expectType<number>(buf.ndim);
    expectType<number>(buf.offset);
    expectType<boolean>(buf.readonly);
    expectType<() => void>(buf.release);
    expectType<number[]>(buf.shape);
    expectType<number[]>(buf.strides);
  }

  if (px.isCallable()) {
    expectType<PyProxyCallable>(px);
    expectType<any>(px(1, 2, 3));
    expectAssignable<(...args: any[]) => any>(px);
  }

  if (px.isIterable()) {
    expectType<PyProxyIterable>(px);
    for (let x of px) {
      expectType<any>(x);
    }
    let it = px[Symbol.iterator]();
    expectAssignable<{ done?: any; value: any }>(it.next());
  }

  if (px.isIterator()) {
    expectType<PyProxyIterator>(px);
    expectAssignable<{ done?: any; value: any }>(px.next());
    expectAssignable<{ done?: any; value: any }>(px.next(22));
  }

  pyodide.unpackArchive(new Uint8Array(40), "tar");
  pyodide.unpackArchive(new Uint8Array(40), "tar", {
    extractDir: "/some/path",
  });
}
