Mwalker has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/148970

Change subject: Allow unzipped bundle
......................................................................

Allow unzipped bundle

Allow the renderer to consume a unzipped bundle file and to accept
a working directory. This saves CPU cycles when the bundle was
created on the same machine. A manually specified working directory
allows the caller to define a better working space than /tmp -- e.g.
a RAM disk.

Change-Id: If2be196ca29a4b1d93cda1b818491d64746e44a0
---
M bin/mw-ocg-latexer
M lib/index.js
M package.json
3 files changed, 42 insertions(+), 15 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Collection/OfflineContentGenerator/latex_renderer
 refs/changes/70/148970/1

diff --git a/bin/mw-ocg-latexer b/bin/mw-ocg-latexer
index 7e57a1c..5d4ba3c 100755
--- a/bin/mw-ocg-latexer
+++ b/bin/mw-ocg-latexer
@@ -13,7 +13,7 @@
 
 program
        .version(latexer.version)
-       .usage('[options] <bundle.zip>')
+       .usage('[options] <bundle>')
        .option('-o, --output <filename>',
                        'Save PDF to the given <filename>', null)
        .option('-s, --size <letter|a4>',
@@ -31,7 +31,10 @@
        .option('-D, --debug',
                        'Turn on debugging features (eg, full stack traces on 
exceptions)')
        .option('--syslog',
-                       'Log errors using syslog (for production deployments)');
+                       'Log errors using syslog (for production deployments)')
+       .option('--builddir <path>',
+                       'Manually specify a working directory to place 
temporary files. ' +
+                       'If specificed, this directory is not removed on 
failure.');
 
 program.parse(process.argv);
 
@@ -73,6 +76,7 @@
        latex: !!program.latex,
        debug: !!program.debug,
        output: program.output,
+       builddir: program.builddir,
        lang: program.lang,
        onecolumn: program.oneColumn,
        log: log
diff --git a/lib/index.js b/lib/index.js
index 49b9ba9..0b81bff 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -11,6 +11,7 @@
 var fs = require('fs');
 var gammalatex = require('gammalatex');
 var path = require('path');
+var rimraf = require('rimraf');
 var stream = require('stream');
 var tmp = require('tmp');
 var ubidi = require('icu-bidi');
@@ -1117,22 +1118,41 @@
        status.createStage(0, 'Unpacking content bundle');
 
        // first create a temporary directory
-       return P.call(tmp.dir, tmp, {
-               prefix: json.name,
-               unsafeCleanup: !(options.debug || options.latex)
+       return Promise.resolve().then(function() {
+               // Either take the directory at face value or create one
+               // Note: we do not get automatic clean up when we take the 
directory
+               if (options.builddir) {
+                       return P.call(fs.mkdir, fs, 
options.builddir).then(function() {
+                               return options.builddir;
+                       });
+               } else {
+                       return P.call(tmp.dir, tmp, {
+                               prefix: json.name,
+                               unsafeCleanup: !(options.debug || options.latex)
+                       });
+               }
        }).then(function(_builddir) {
                builddir = _builddir;
-               // make bundle and latex subdirs
+
+               // deal with the bundle and latex dirs. latex dir is easy, 
bundle..
+               // we will hardlink if the given bundle is a directory, 
otherwise
+               // we will extract it
+               var dealWithBundle = function() {
+                       return P.call(fs.stat, fs, 
options.bundle).then(function(stat) {
+                               if (stat.isDirectory()) {
+                                       return P.call(fs.symlink, fs, 
options.bundle, path.join(builddir, 'bundle'))
+                               } else {
+                                       return P.spawn('unzip', [ path.resolve( 
options.bundle ) ], {
+                                               cwd: path.join(builddir, 
'bundle')
+                                       });
+                               }
+                       })
+               };
+
                return Promise.join(
-                       P.call(fs.mkdir, fs, path.join(builddir, 'bundle')),
+                       dealWithBundle(),
                        P.call(fs.mkdir, fs, path.join(builddir, 'latex'))
                );
-       }).then(function() {
-               // now unpack the zip archive
-               var bundledir = path.join(builddir, 'bundle');
-               return P.spawn('unzip', [ path.resolve( options.bundle ) ], {
-                       cwd: bundledir
-               });
        }).then(function() {
                // now read in the main metabook.json file
                return P.call(
@@ -1597,10 +1617,12 @@
                // compile it to PDF
                return compileLatex(builddir, options);
        }).then(function() {
+               // Only delete things on success, if fail keep them around
+               return P.promisify(rimraf)(builddir);
+       }).then(function() {
                status.createStage(0, 'Done');
                return 0; // success!
        }, function(err) {
-               // xxx clean up?
                if (options.debug) {
                        throw err;
                }
diff --git a/package.json b/package.json
index f2495d7..813c6f5 100644
--- a/package.json
+++ b/package.json
@@ -30,7 +30,8 @@
     "prfun": "~1.0.0",
     "readable-stream": "~1.0.0",
     "sqlite3": "~2.2.3",
-    "tmp": "~0.0.24"
+    "tmp": "~0.0.24",
+       "rimraf": "~2.2.2"
   },
   "devDependencies": {
     "mocha": "~1.20.1"

-- 
To view, visit https://gerrit.wikimedia.org/r/148970
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If2be196ca29a4b1d93cda1b818491d64746e44a0
Gerrit-PatchSet: 1
Gerrit-Project: 
mediawiki/extensions/Collection/OfflineContentGenerator/latex_renderer
Gerrit-Branch: master
Gerrit-Owner: Mwalker <mwal...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to