maybach-vm v1.8.2
maybach-vm
一个基于 Python 3.8.3
和 Conda
的跨平台 Python
运行时。主要服务西瓜的 Python客户端
。
下载
git
git clone https://gitlab.xiguacity.cn/fee/python/maybach-vm.git
npm
yarn add @xigua/maybach-vm
npm install @xigua/maybach-vm
使用
执行 Python 字符串代码
import { Python } from '@xigua/maybach-vm';
const p = new Python();
(async () => {
await p.init();
p.python$(
`
import time
print(time.time())
`,
cwd,
port
(res) => {
// { status: 0, type: 1, msg: ['1599186008.622693', ''], done: false }
// { status: 0, type: 1, msg: [], done: true }
}
);
})()
执行 Python 文件
import { Python } from '@xigua/maybach-vm';
const p = new Python();
(async () => {
await p.init();
p.python('test.py', cwd, port, (res) => {
// { status: 0, type: 0, msg: ['200', ''], done: false }
// { status: 0, type: 0, msh: [], done: true }
});
})();
# test.py
import requests
r = requests.get('https://xiguacity.cn')
print(r.status_code)
查看安装的库
import { Python } from '@xigua/maybach-vm';
const p = new Python();
(async () => {
await p.init();
p.list('numpy', (res) => {
/*
* {
* status: 0,
* type: 2,
* msg: [
* {
* base_url: 'https://repo.anaconda.com/pkgs/main',
* build_number: 0,
* build_string: 'py38_0',
* channel: 'pkgs/main',
* dist_name: 'requests-2.23.0-py38_0',
* name: 'requests',
* platform: 'osx-64',
* version: '2.23.0'
* }
* ],
* done: false
* }
* { statusL 0, type: 2, msg: [], done: true }
*/
});
})();
下载库
import { Python, prompt } from '@xigua/maybach-vm';
const p = new Python();
(async () => {
await init();
p.install(['pandas'], (res) => {
// prompt是实现用户可以主动向子进程写入流,从而达到执行过程当中进程与用户之间的交互
// res.done为false表示,进程没有执行完,在这个过程当中,最有可能是在等待用户的输入
if (!res.done) prompt('y\n');
/**
* {
type: 3,
status: 0,
msg: 'Collecting package metadata (current_repodata.json): ...working... ',
done: false
}
{ type: 3, status: 0, msg: 'done\n', done: false }
{
type: 3,
status: 0,
msg: 'Solving environment: ...working... ',
done: false
}
{ type: 3, status: 0, msg: 'done\n', done: false }
{
type: 3,
status: 0,
msg: '\n# All requested packages already installed.\n\n',
done: false
}
{ type: 3, status: 0, msg: '', done: true }
*/
});
})();
更新库
import { Python, prompt } from '@xigua/maybach-vm';
const p = new Python();
p.update('flask', (res) => {
// 跟install类似
});
查看镜像信息
import { Python } from '@xigua/maybach-vm';
const p = new Python();
p.mirrorInfo((res) => {
/**
* {
type: 5,
status: 0,
msg: [
'https://mirrors.ustc.edu.cn/anaconda/pkgs/free/osx-64',
'https://mirrors.ustc.edu.cn/anaconda/pkgs/free/noarch',
'https://mirrors.ustc.edu.cn/anaconda/pkgs/main/osx-64',
'https://mirrors.ustc.edu.cn/anaconda/pkgs/main/noarch',
'https://repo.anaconda.com/pkgs/main/osx-64',
'https://repo.anaconda.com/pkgs/main/noarch',
'https://repo.anaconda.com/pkgs/r/osx-64',
'https://repo.anaconda.com/pkgs/r/noarch'
],
done: false
}
{ type: 5, status: 0, msg: [], done: true }
*/
});
conda 本身的 cli
import { Python } from '@xigua/maybach-vm';
const p = new Python();
(async () => {
await p.init();
p.conda(['info', '--env', '--json'], cwd, (res) => {
// 查询conda的配置信息
});
})();
注意:conda 接口是为了提供扩展而产生的,你可以基于这个接口去封装自己要实现的逻辑,基于 conda
,Maybach-vm
内置的接口有:
addMirror
: 增加镜像源removeMirror
: 移除镜像源
API
python
: 执行python
文件python$
: 执行python
字符串代码install
: 下载包list
: 获取包信息mirrorInfo
: 获取镜像源信息addMirror
: 添加镜像源removeMirror
: 移除镜像源conda
: conda 自身 cliprompt
: 子进程输入流kill
: 关闭当前正在执行的进程
文档
maybach-vm
通过调用Node.js
的child_process
模块,为每一个接口开启一个独立的进程去执行相应的任务,例如:python
接口
import cp from 'child_process';
export function python() {
const child = cp.spawn(cmd, args, { stdio: 'pipe' });
child.stderr.on('data', (buf) => {
// 返回错误的输出流
});
child.stdout.on('data', (buf) => {
// 返回正常输出流
});
child.on('close', (code, signal) => {
// 返回输出流
});
}
协议
返回的流被转换为字符串,并以JSON
的形式返回:
enum handler {
python, // python文件接口
python$, // python字符串代码接口
list, // 包列表接口
install, // 安装包接口
update, // 更新包接口
conda, // conda自身cli接口
pip // pip自身cli接口
}
export IResult {
status: 0 | 1, // 0 - 执行正常 1 - 执行错误
type: handler,
msg: any,
done: boolean
}
历史
TODO
- conda 安装检查
- 32 位 windows 系统支持
- conda 镜像源检查
- 冷启动
- pip 下载接口
- 扩展接口
LICENSE
MIT License
Copyright (c) 2020 娄外科技
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4 years ago