4

I'm rewriting some bash code in gulp that produces several add-ons/extensions for different browsers inspired by the ublockorigin project on GitHub.

For Firefox there's a line that is supposed to run a python script which takes a destination directory as an argument. In gulp, I am having a hard time to run this python script.

I tried gulp-run, gulp-shell, child_process, but none of them are giving me the correct output.

When I run python ./tools/make-firefox-meta.py ../firefox_debug/ from the command line I get my desired result and the firefox_debug directory is created.

Here is my code for gulp-run:

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

This is giving me this without actually doing anything:

$ 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/

Here is my code for gulp-shell:

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

This is giving me this without actual result:

$ 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

Here is my code for child_process: this was the most promising one, since I saw some output from python on the command line.

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

It's giving me this output:

[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

Can somebody tell me please, what change should I do to make it work?

Sven Schoenung
  • 30,224
  • 8
  • 65
  • 70
Ali Saberi
  • 864
  • 1
  • 10
  • 33

1 Answers1

3

The last one using child_process.spawn() is indeed the approach I would recommend, but the way you're passing the arguments to the python executable is wrong.

Each argument has to be passed as a separate element of the array. You can't just pass a single string. spawn() will interpret the string as a single argument and python will look for a file that is literally named ./tools/make-firefox-meta.py `../firefox_debug/`. Which, of course, doesn't exist.

So instead of this:

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

You need to do this:

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

You also need to properly signal 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);
});
Sven Schoenung
  • 30,224
  • 8
  • 65
  • 70
  • sound better than before. the gulp is Finished 'python-bsff' after 13 ms, but the changes that they python file is supposed to do is not there. Anything directory addressing related can cause that? – Ali Saberi Dec 21 '16 at 19:57
  • It finishes after 13ms because you're not properly signalling async completion (see my edit). Also try running `python ./tools/make-firefox-meta.py ../firefox_debug/` on the command line (in the directory where your `gulpfile.js` is located). If it works there, it will work from gulp. – Sven Schoenung Dec 21 '16 at 20:21
  • now, it is working. I learned signaling on asyc tasks from you. thanks – Ali Saberi Dec 21 '16 at 20:37