Skip to content

Commit d9cf01d

Browse files
committed
Change Python API to match subprocess API
1 parent 0744106 commit d9cf01d

File tree

6 files changed

+193
-147
lines changed

6 files changed

+193
-147
lines changed

DEVELOPMENT.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Node.js PyPI distribution
2+
=====================
3+
4+
This repository contains the script used to repackage the [releases][nodejsdl] of [Node.js][nodejs] as [Python binary wheels][wheel]. This document is intended for maintainers; see the [package README][pkgreadme] for rationale and usage instructions.
5+
6+
The repackaged artifacts are published as the [node-js PyPI package][pypi].
7+
8+
[nodejs]: https://nodejs.org/
9+
[nodejsdl]: https://nodejs.org/en/download/
10+
[wheel]: https://github.com/pypa/wheel
11+
[pkgreadme]: README.pypi.md
12+
[pypi]: https://pypi.org/project/nodejs-bin/
13+
14+
This tool is based on the work of the creators of the [Zig language][ziglang], see [the original][basedon]. Thank you!
15+
16+
[ziglang]: https://ziglang.org
17+
[basedon]: https://github.com/ziglang/zig-pypi
18+
19+
Preparation
20+
-----------
21+
22+
The script requires Python 3.5 or later.
23+
24+
Install the dependencies:
25+
26+
```shell
27+
pip install -r requirements.txt
28+
```
29+
30+
The `libarchive-c` Python library requires the native [libarchive][] library to be available.
31+
32+
[libarchive]: https://libarchive.org/
33+
34+
Building wheels
35+
---------------
36+
37+
Run the repackaging script:
38+
39+
```shell
40+
python make_wheels.py
41+
```
42+
43+
This command will download the Node.js release archives for every supported platform and convert them to binary wheels, which are placed under `dist/`. The Node.js version and platforms are configured in the script source.
44+
45+
The process of converting release archives to binary wheels is deterministic, and the output of the script should be bit-for-bit identical regardless of the environment and platform it runs under. To this end, it prints the SHA256 hashes of inputs and outputs; the hashes of the inputs will match the ones on the [Node.js downloads page][nodejsdl], and the hashes of the outputs will match the ones on the [PyPI downloads page][pypidl].
46+
47+
[pypidl]: https://pypi.org/project/node-js/#files
48+
49+
Uploading wheels
50+
----------------
51+
52+
Run the publishing utility:
53+
54+
```shell
55+
twine dist/*
56+
```
57+
58+
This command will upload the binary wheels built in the previous step to PyPI.
59+
60+
License
61+
-------
62+
63+
This script is distributed under the terms of the [MIT (Expat) license](LICENSE.txt).

README.md

Lines changed: 86 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,119 @@
11
Node.js PyPI distribution
22
=====================
33

4-
This repository contains the script used to repackage the [releases][nodejsdl] of [Node.js][nodejs] as [Python binary wheels][wheel]. This document is intended for maintainers; see the [package README][pkgreadme] for rationale and usage instructions.
4+
[Node.js][nodejs] is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser.
55

6-
The repackaged artifacts are published as the [node-js PyPI package][pypi].
6+
The [nodejs-bin][pypi] Python package redistributes Node.js so that it can be used as a dependency of Python projects. With `nodejs-bin` you can call `nodejs`, `npm` and `npx` from both the [command line](#command-line-usage) and a [Python API](#python-api-usage).
7+
8+
**Note: this is an unofficial Node.js distribution.**
9+
10+
**This is intended for use within Python virtual environments and containers, it should probably not be used for global instillation.**
11+
12+
This PyPI distribution is provided by <https://github.com/samwillis/nodejs-pypi>.
713

814
[nodejs]: https://nodejs.org/
9-
[nodejsdl]: https://nodejs.org/en/download/
10-
[wheel]: https://github.com/pypa/wheel
11-
[pkgreadme]: README.pypi.md
1215
[pypi]: https://pypi.org/project/nodejs-bin/
1316

14-
This tool is based on the work of the creators of the [Zig language][ziglang], see [the original][basedon]. Thank you!
17+
Install
18+
-------
19+
20+
To install:
21+
22+
```shell
23+
pip install nodejs-bin
24+
```
25+
26+
You can specify the Node.js version to install with:
1527

16-
[ziglang]: https://ziglang.org
17-
[basedon]: https://github.com/ziglang/zig-pypi
28+
```shell
29+
pip install nodejs-bin==<version>
1830

19-
Preparation
20-
-----------
31+
# Example:
32+
pip install nodejs-bin==16.15.1
33+
```
2134

22-
The script requires Python 3.5 or later.
35+
Command Line Usage
36+
------------------
2337

24-
Install the dependencies:
38+
To run Node.js from the command line, use:
2539

2640
```shell
27-
pip install wheel twine libarchive-c
41+
python -m nodejs
42+
# or (see below)
43+
node
2844
```
2945

30-
The `libarchive-c` Python library requires the native [libarchive][] library to be available.
46+
`npm` and `npx` are also available as `python -m nodejs.npm` and `python -m nodejs.npx`.
47+
48+
Additionally, the standard `node`, `npm` and `npx` commands are also added to your Python environment's `bin` directory. This is usually on your `PATH` and so they should be available in your shell environment.
3149

32-
[libarchive]: https://libarchive.org/
50+
Python API Usage
51+
----------------
3352

34-
Building wheels
35-
---------------
53+
`node-bin` has a simple Python API that wraps the Node.js command line with the [Python `subprocess`](https://docs.python.org/3/library/subprocess.html).
3654

37-
Run the repackaging script:
55+
For `node`, `npm` and `npx` there are `.call()`, `.run()` and `.Popen()` methods that match the equivalent `subprocess` methods.
3856

39-
```shell
40-
python make_wheels.py
57+
To run Node.js from a Python program and return the exit code:
58+
59+
```python
60+
from nodejs import node, npm, npx
61+
62+
# Run Node.js and return the exit code.
63+
node.call(['script.js', 'arg1', ...], **kwargs)
64+
65+
# Run npm and return the exit code.
66+
npm.call(['command', 'arg1', ...], **kwargs)
67+
68+
# Run npx and return the exit code.
69+
npx.call(['command', 'arg1', ...], **kwargs)
4170
```
4271

43-
This command will download the Node.js release archives for every supported platform and convert them to binary wheels, which are placed under `dist/`. The Node.js version and platforms are configured in the script source.
72+
The `call(args, **kwargs)` functions wrap [`subprocess.call()`](https://docs.python.org/3/library/subprocess.html#subprocess.call), passes though all `kwargs` and returns the exit code of the process.
4473

45-
The process of converting release archives to binary wheels is deterministic, and the output of the script should be bit-for-bit identical regardless of the environment and platform it runs under. To this end, it prints the SHA256 hashes of inputs and outputs; the hashes of the inputs will match the ones on the [Node.js downloads page][nodejsdl], and the hashes of the outputs will match the ones on the [PyPI downloads page][pypidl].
74+
To run Node.js from a Python program and return a [CompletedProcess](https://docs.python.org/3/library/subprocess.html#subprocess.CompletedProcess) object:
4675

47-
[pypidl]: https://pypi.org/project/node-js/#files
76+
```python
77+
from nodejs import node, npm, npx
4878

49-
Uploading wheels
50-
----------------
79+
# Run Node.js and return the exit code.
80+
node.run(['script.js', 'arg1', ...], **kwargs)
5181

52-
Run the publishing utility:
82+
# Run npm and return the exit code.
83+
npm.run(['command', 'arg1', ...], **kwargs)
5384

54-
```shell
55-
twine dist/*
85+
# Run npx and return the exit code.
86+
npx.run(['command', 'arg1', ...], **kwargs)
5687
```
5788

58-
This command will upload the binary wheels built in the previous step to PyPI.
89+
The `run(args, **kwargs)` functions wrap [`subprocess.run()`](https://docs.python.org/3/library/subprocess.html#subprocess.run), passes though all `kwargs` and returns a `CompletedProcess`.
90+
91+
Additionally, to start a Node.js process and return a `subprocess.Popen` object, you can use the `Popen(args, **kwargs)` functions:
92+
93+
```python
94+
from nodejs import node, npm, npx
95+
96+
# Start Node.js and return the Popen object.
97+
node_process = node.Popen(['script.js', 'arg1', ...], **kwargs)
98+
99+
# Start npm and return the Popen object.
100+
npm_process = npm.Popen(['command', 'arg1', ...], **kwargs)
101+
102+
# Start npx and return the Popen object.
103+
npx_process = npx.Popen(['command', 'arg1', ...], **kwargs)
104+
```
105+
106+
The `Popen(args, **kwargs)` functions wrap [`subprocess.Popen()`](https://docs.python.org/3/library/subprocess.html#subprocess.Popen), passes though all `kwargs` and returns a [`Popen` object](https://docs.python.org/3/library/subprocess.html#popen-objects).
107+
108+
The `nodejs.node` api is also available as `nodejs.run` and `nodejs.call` and `nodejs.Popen`.
109+
110+
Finally, there are a number of convenient attributes on the `nodejs` module:
111+
112+
* `nodejs.node_version`: the version of Node.js that is installed.
113+
* `nodejs.path`: the path to the Node.js executable.
114+
59115

60116
License
61117
-------
62118

63-
This script is distributed under the terms of the [MIT (Expat) license](LICENSE.txt).
119+
The [Node.js license](https://raw.githubusercontent.com/nodejs/node/master/LICENSE).

README.pypi.md

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)