gpt4 book ai didi

python - 在 gulp 中运行带参数的命令

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:27 26 4
gpt4 key购买 nike

我正在 gulp 中重写一些 bash 代码,这些代码为受 ublockorigin 启发的不同浏览器生成了多个附加组件/扩展。 GitHub 上的项目。

对于 Firefox,有一行应该运行 python 脚本,该脚本将目标目录作为参数。在 gulp 中,我很难运行这个 python 脚本。

我尝试了 gulp-rungulp-shellchild_process,但它们都没有给我正确的输出。

当我从命令行运行 python ./tools/make-firefox-meta.py ../firefox_debug/ 时,我得到了我想要的结果和 firefox_debug 目录已创建。

这是我的 gulp-run 代码:

gulp.task("python-bsff", function(){
return run("python ./tools/make-firefox-meta.py ../firefox_debug/").exec();
});

这是在没有实际做任何事情的情况下给了我这个:

$ gulp python-bsff
[14:15:53] Using gulpfile ~\dev\gulpfile.js
[14:15:53] Starting 'python-bsff'...
[14:15:54] Finished 'python-bsff' after 629 ms
$ python ./tools/make-firefox-meta.py ../firefox_debug/

这是我的 gulp-shell 代码:

gulp.task("python-bsff", function(){
return shell.task(["./tools/make-firefox-meta.py ../firefox_debug/""]);
});

这给了我这个但没有实际结果:

$ gulp python-bsff
[14:18:54] Using gulpfile ~\dev\gulpfile.js
[14:18:54] Starting 'python-bsff'...
[14:18:54] Finished 'python-bsff' after 168 μs

这是我的 child_process 代码:这是最有前途的代码,因为我在命令行上看到了 python 的一些输出。

gulp.task("python-bsff", function(){
var spawn = process.spawn;
console.info('Starting python');
var PIPE = {stdio: 'inherit'};
spawn('python', ["./tools/make-firefox-meta.py `../firefox_debug/`"], PIPE);
});

它给我这个输出:

[14:08:59] Using gulpfile ~\dev\gulpfile.js
[14:08:59] Starting 'python-bsff'...
Starting python
[14:08:59] Finished 'python-bsff' after 172 ms
python: can't open file './tools/make-firefox-meta.py ../firefox_debug/`': [Errno 2] No such file or directory

谁能告诉我,我应该做些什么改变才能让它发挥作用?

最佳答案

最后一个使用 child_process.spawn() 的方法确实是我推荐的方法,但是您将参数传递给 python 可执行文件的方式是错误的。

每个参数都必须作为数组的一个单独元素传递。你不能只传递一个字符串。 spawn() 会将字符串解释为单个参数,python 将查找字面上名为./tools/的文件make-firefox-meta.py `../firefox_debug/`。这当然不存在。

所以不是这个:

spawn('python', ["./tools/make-firefox-meta.py `../firefox_debug/`"], PIPE);

你需要这样做:

spawn('python', ["./tools/make-firefox-meta.py", "../firefox_debug/"], PIPE);

您还需要正确发出信号 async completion :

gulp.task("python-bsff", function(cb) {
var spawn = process.spawn;
console.info('Starting python');
var PIPE = {stdio: 'inherit'};
spawn('python', ["./tools/make-firefox-meta.py", "../firefox_debug/"], PIPE).on('close', cb);
});

关于python - 在 gulp 中运行带参数的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41270405/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com