Skip to content

Commit

Permalink
Install PIP deps (aiortc) locally #33
Browse files Browse the repository at this point in the history
  • Loading branch information
ibc authored Dec 19, 2023
2 parents 0d0b450 + c07653d commit e6dedf9
Show file tree
Hide file tree
Showing 12 changed files with 557 additions and 1,629 deletions.
23 changes: 20 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const eslintConfig =
lib : [ 'es2022', 'dom' ],
project : 'tsconfig.json'
},
globals :
{
NodeJS : 'readonly'
},
rules :
{
'array-bracket-spacing' : [ 2, 'always',
Expand All @@ -42,6 +46,14 @@ const eslintConfig =
'computed-property-spacing' : 2,
'constructor-super' : 2,
'curly' : [ 2, 'all' ],
// Unfortunatelly `curly` does not apply to blocks in `switch` cases so
// this is needed.
'no-restricted-syntax' : [ 2,
{
'selector' : 'SwitchCase > *.consequent[type!="BlockStatement"]',
'message' : 'Switch cases without blocks are disallowed'
}
],
'func-call-spacing' : 2,
'generator-star-spacing' : 2,
'guard-for-in' : 2,
Expand Down Expand Up @@ -72,10 +84,10 @@ const eslintConfig =
beforeLineComment : false
}
],
'max-len' : [ 2, 90,
'max-len' : [ 2, 100,
{
tabWidth : 2,
comments : 90,
comments : 88,
ignoreUrls : true,
ignoreStrings : true,
ignoreTemplateLiterals : true,
Expand Down Expand Up @@ -163,7 +175,12 @@ const eslintConfig =
'prefer-rest-params' : 2,
'prefer-spread' : 2,
'prefer-template' : 2,
'quotes' : [ 2, 'single', { avoidEscape: true } ],
'quotes' : [ 2, 'single',
{
avoidEscape : true,
allowTemplateLiterals : true
}
],
'semi' : [ 2, 'always' ],
'semi-spacing' : 2,
'space-before-blocks' : 2,
Expand Down
13 changes: 9 additions & 4 deletions .github/workflows/mediasoup-client-aiortc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ jobs:
key: ${{ matrix.ci.os }}-node-${{ hashFiles('**/package.json') }}
restore-keys: |
${{ matrix.ci.os }}-node-
- run: npm ci
- run: npm run install-python-dev-deps
- name: npm ci
run: npm ci --force --foreground-scripts

# NOTE: Avoid lint:python due to
# https://github.com/versatica/mediasoup-client-aiortc/issues/25
- run: npm run lint:node
- run: DEBUG="mediasoup-client-aiortc:WARN* mediasoup-client-aiortc:ERROR*" PYTHON_LOG_TO_STDOUT=true npm run test
- name: npm run lint:node
run: npm run lint:node

- name: npm run test
run: DEBUG="mediasoup-client-aiortc:WARN* mediasoup-client-aiortc:ERROR*" PYTHON_LOG_TO_STDOUT=true npm run test
18 changes: 5 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
# Node.
/node_modules
/lib
/coverage

# Vim temporal files.
*.swp
*.swo

# Mac Stuff.
.DS_Store

# Vistual Studio Code stuff.
/.vscode

# Cache.
# Others.
.mypy_cache
/.cache
/.mypy_cache
.cache
/worker/pip_deps/
/worker/pip_dev_deps/
/worker/__pycache__
/worker/build
/worker/mediasoup_client_aiortc.egg-info
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@

## Requirements

This module uses **aiortc** Python library, which requires Python 3. Check the installation requirements of **aiortc** in the [project site](https://github.com/aiortc/aiortc).
- Python 3.


## Installation

Once the requirements above are satisfied, install **mediasoup-client-aiortc** within your Node.js application:
Install **mediasoup-client-aiortc** within your Node.js application:

```bash
$ npm install --save mediasoup-client-aiortc
npm install mediasoup-client-aiortc
```

The "postinstall" script in `package.json` will install the Python libraries (including **aiortc**) by using `pip3` command. If such a command is not in the `PATH` or has a different name in your system, you can override its location by setting the `PIP` environment variable:
The "postinstall" script in `package.json` will install the Python libraries (including **aiortc**). You can override the path to `python` executable by setting the `PYTHON` environment variable:

```bash
$ PIP=/home/me/bin/pip3 npm install --save mediasoup-client-aiortc
PYTHON=/home/me/bin/python3.13 npm install mediasoup-client-aiortc
```

Once you run your Node.js application, **mediasoup-client-aiortc** will eventually spawn Python processes and communicate with them via `UnixSocket`. This module assumes that there is a `python3` executable in your `PATH` to spawn the Python executable. If not, you can override its location by setting the `PYTHON` environment variable:
Same once you run your Node.js application. **mediasoup-client-aiortc** will spawn Python processes and communicate with them via `UnixSocket`. You can override the `python` executable path by setting the `PYTHON` environment variable:

```bash
$ PYTHON=/home/me/bin/python3.12 node my_app.js
PYTHON=/home/me/bin/python3.13 node my_app.js
```


Expand Down Expand Up @@ -259,14 +259,25 @@ When sending, `dataChannel.send()` (and hence `dataProducer.send()`) allows pass
## Development
### Lint task
### Lint
In order to run `npm run lint` task, install Python dev dependencies:
```bash
npm run lint
```
### Test
```bash
npm run test
```
### Check release
```bash
$ npm run install-python-dev-deps
npm run release:check
```
### Make Python log to stdout/stderr while running tests
```bash
Expand Down
91 changes: 60 additions & 31 deletions npm-scripts.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import process from 'process';
import fs from 'fs';
import { execSync } from 'child_process';
import process from 'node:process';
import os from 'node:os';
import fs from 'node:fs';
import path from 'node:path';
import { execSync } from 'node:child_process';

const PKG = JSON.parse(fs.readFileSync('./package.json').toString());
const IS_WINDOWS = os.platform() === 'win32';
const MAYOR_VERSION = PKG.version.split('.')[0];
const PYTHON = getPython();
const PIP_DEPS_DIR = path.resolve('worker/pip_deps');
const PIP_DEV_DEPS_DIR = path.resolve('worker/pip_dev_deps');

const task = process.argv.slice(2).join(' ');

// Set PYTHONPATH env since we use custom locations for locally installed PIP
// deps.
if (IS_WINDOWS)
{
process.env.PYTHONPATH = `${PIP_DEPS_DIR};${PIP_DEV_DEPS_DIR};${process.env.PYTHONPATH}`;
}
else
{
process.env.PYTHONPATH = `${PIP_DEPS_DIR}:${PIP_DEV_DEPS_DIR}:${process.env.PYTHONPATH}`;
}

run();

async function run()
Expand All @@ -25,7 +42,7 @@ async function run()
// So here we compile TypeScript to JavaScript.
case 'prepare':
{
buildTypescript(/* force */ false);
buildTypescript({ force: false });

break;
}
Expand All @@ -40,7 +57,7 @@ async function run()
case 'typescript:build':
{
installNodeDeps();
buildTypescript(/* force */ true);
buildTypescript({ force: true });
replacePythonVersion();

break;
Expand Down Expand Up @@ -78,7 +95,7 @@ async function run()

case 'test':
{
buildTypescript(/* force */ false);
buildTypescript({ force: false });
replacePythonVersion();
test();

Expand All @@ -87,7 +104,7 @@ async function run()

case 'coverage':
{
buildTypescript(/* force */ false);
buildTypescript({ force: false });
replacePythonVersion();
executeCmd('jest --coverage');
executeCmd('open-cli coverage/lcov-report/index.html');
Expand All @@ -114,27 +131,33 @@ async function run()
break;
}

case 'install-python-deps':
default:
{
installPythonDeps();
logError('unknown task');

break;
exitWithError();
}
}
}

case 'install-python-dev-deps':
{
installPythonDevDeps();
function getPython()
{
let python = process.env.PYTHON;

break;
if (!python)
{
try
{
execSync('python3 --version', { stdio: [ 'ignore', 'ignore', 'ignore' ] });
python = 'python3';
}

default:
catch (error)
{
logError('unknown task');

exitWithError();
python = 'python';
}
}

return python;
}

function replacePythonVersion()
Expand All @@ -157,10 +180,10 @@ function deleteNodeLib()

logInfo('deleteNodeLib()');

executeCmd('rm -rf lib');
fs.rmSync('node/lib', { recursive: true, force: true });
}

function buildTypescript(force = false)
function buildTypescript({ force = false } = { force: false })
{
if (!force && fs.existsSync('lib'))
{
Expand All @@ -184,10 +207,10 @@ function lintPython()
{
logInfo('lintPython()');

const PYTHON = process.env.PYTHON || 'python3';
installPythonDevDeps();

executeCmd(`cd worker && ${PYTHON} -m flake8 && cd ..`);
executeCmd(`cd worker && ${PYTHON} -m mypy . && cd ..`);
executeCmd(`cd worker && "${PYTHON}" -m flake8 --filename *.py && cd ..`);
executeCmd(`cd worker && "${PYTHON}" -m mypy --exclude pip_deps --exclude pip_dev_deps . && cd ..`);
}

function test()
Expand All @@ -211,18 +234,24 @@ function installPythonDeps()
{
logInfo('installPythonDeps()');

const PIP = process.env.PIP || 'pip3';

executeCmd(`${PIP} install --user worker/`);
// Install PIP deps into custom location, so we don't depend on system-wide
// installation.
executeCmd(
`"${PYTHON}" -m pip install --upgrade --no-user --target="${PIP_DEPS_DIR}" worker/`,
/* exitOnError */ true
);
}

function installPythonDevDeps()
{
logInfo('installPythonDevDeps()');

const PIP = process.env.PIP || 'pip3';

executeCmd(`${PIP} install flake8 mypy`);
// Install PIP dev deps into custom location, so we don't depend on system-wide
// installation.
executeCmd(
`"${PYTHON}" -m pip install --upgrade --no-user --target="${PIP_DEV_DEPS_DIR}" flake8 mypy`,
/* exitOnError */ true
);
}

function checkRelease()
Expand All @@ -231,7 +260,7 @@ function checkRelease()

installNodeDeps();
installPythonDeps();
buildTypescript(/* force */ true);
buildTypescript({ force: true });
replacePythonVersion();
lintNode();
// TODO: Disabled due to
Expand Down
Loading

0 comments on commit e6dedf9

Please sign in to comment.