在gulp中运行带有参数的命令

2024-09-30 01:29:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我在gulp中重写了一些bash代码,这些代码为不同的浏览器生成了几个附加组件/扩展,这些插件/扩展的灵感来自GitHub上的ublockorigin项目。在

对于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();
});

这是在不做任何事的情况下给我这个:

^{pr2}$

以下是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

有人能告诉我,我应该做些什么改变才能使它生效?在


Tags: run代码pydebug脚本taskmakefirefox
1条回答
网友
1楼 · 发布于 2024-09-30 01:29:51

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

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

所以不是这样:

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

你需要这样做:

^{pr2}$

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

^{3}$

相关问题 更多 >

    热门问题