在gulp

时间:2016-12-21 19:21:14

标签: python gulp firefox-addon child-process gulp-shell

我在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();
});

这让我没有做任何事情:

$ 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

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

1 个答案:

答案 0 :(得分:3)

使用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);
});