Merge pull request #2346 from primalmotion/fix-popen

FIXED: Closes all streams after a OS.popen()
This commit is contained in:
Alexandre Wilhelm committed 2015-05-27 15:25:36 -07:00
commit 5bba109873
13 files changed
+188 -111

No files matched your search

-2
View File
@@ -1,8 +1,6 @@
require("../common.jake");
checkUlimit();
var framework = require("objective-j/jake").framework,
BundleTask = require("objective-j/jake").BundleTask;
+25 -10
View File
@@ -366,23 +366,38 @@ function pressEnvironment(rootPath, outputFiles, environment, options) {
return {executable:includedBytes, data:dataBytes, mhtml:mhtmlBytes};
}
function pngcrushDirectory(directory) {
var directoryPath = FILE.path(directory);
var pngs = directoryPath.glob("**/*.png");
function pngcrushDirectory(directory)
{
var directoryPath = FILE.path(directory),
pngs = directoryPath.glob("**/*.png");
system.stderr.print("Running pngcrush on " + pngs.length + " pngs:");
pngs.forEach(function(dstPath) {
pngs.forEach(function(dstPath)
{
var tmpPath = FILE.path(dstPath+".tmp");
var p = OS.popen(["pngcrush", "-rem", "alla", "-reduce", /*"-brute",*/ dstPath, tmpPath]);
if (p.wait()) {
CPLog.warn("pngcrush failed. Ensure it's installed and on your PATH.");
try
{
var p = OS.popen(["pngcrush", "-rem", "alla", "-reduce", /*"-brute",*/ dstPath, tmpPath]);
if (p.wait())
{
CPLog.warn("pngcrush failed. Ensure it's installed and on your PATH.");
}
else
{
FILE.move(tmpPath, dstPath);
system.stderr.write(".").flush();
}
}
else {
FILE.move(tmpPath, dstPath);
system.stderr.write(".").flush();
finally
{
p.stdin.close();
p.stdout.close();
p.stderr.close();
}
});
system.stderr.print("");
}
+15 -5
View File
@@ -3,10 +3,20 @@ var OS = require("os");
exports.fontinfo = function(name, size)
{
var p = OS.popen(["fontinfo", "-n", name, size || 12]);
var result;
if (p.wait() === 0)
return JSON.parse(p.stdout.read());
else
return null;
try
{
var p = OS.popen(["fontinfo", "-n", name, size || 12]);
if (p.wait() === 0)
result = p.stdout.read();
}
finally
{
p.stdin.close();
p.stdout.close();
p.stderr.close();
}
return result ? JSON.parse(result) : null;
};
+15 -5
View File
@@ -3,10 +3,20 @@ var OS = require("os");
exports.imagesize = function(path)
{
var p = OS.popen(["imagesize", "-n", path]);
var result;
if (p.wait() === 0)
return JSON.parse(p.stdout.read());
else
return null;
try
{
var p = OS.popen(["imagesize", "-n", path]);
if (p.wait() === 0)
result = p.stdout.read();
}
finally
{
p.stdin.close();
p.stdout.close();
p.stderr.close();
}
return result ? JSON.parse(result) : null;
};
+14 -5
View File
@@ -128,12 +128,21 @@ function generateDocs(/* boolean */ noFrame)
// If the Doxygen application is installed on Mac OS X, use that
if (!doxygen && executableExists("mdfind"))
{
var p = OS.popen(["mdfind", "kMDItemContentType == 'com.apple.application-bundle' && kMDItemCFBundleIdentifier == 'org.doxygen'"]);
if (p.wait() === 0)
try
{
var doxygenApps = p.stdout.read().split("\n");
if (doxygenApps[0])
doxygen = FILE.join(doxygenApps[0], "Contents/Resources/doxygen");
var p = OS.popen(["mdfind", "kMDItemContentType == 'com.apple.application-bundle' && kMDItemCFBundleIdentifier == 'org.doxygen'"]);
if (p.wait() === 0)
{
var doxygenApps = p.stdout.read().split("\n");
if (doxygenApps[0])
doxygen = FILE.join(doxygenApps[0], "Contents/Resources/doxygen");
}
}
finally
{
p.stdin.close();
p.stdout.close();
p.stderr.close();
}
}
+14 -5
View File
@@ -277,12 +277,12 @@ CFHTTPRequest.prototype.removeEventListener = function(/*String*/ anEventName, /
this._eventDispatcher.removeEventListener(anEventName, anEventListener);
};
CFHTTPRequest.prototype.setWithCredentials = function(/*Boolean*/ willSendWithCredentials)
CFHTTPRequest.prototype.setWithCredentials = function(/*Boolean*/ willSendWithCredentials)
{
this._nativeRequest.withCredentials = willSendWithCredentials;
};
CFHTTPRequest.prototype.withCredentials = function()
CFHTTPRequest.prototype.withCredentials = function()
{
return this._nativeRequest.withCredentials;
};
@@ -323,12 +323,21 @@ function FileRequest(/*CFURL*/ aURL, onsuccess, onfailure, onprogress)
var aFilePath = aURL.toString().substring(5),
OS = require("os"),
gccFlags = require("objective-j").currentCompilerFlags(),
gcc = OS.popen("gcc -E -x c -P " + (gccFlags ? gccFlags : "") + " " + OS.enquote(aFilePath), { charset:"UTF-8" }),
chunk,
fileContents = "";
while (chunk = gcc.stdout.read())
fileContents += chunk;
try
{
var gcc = OS.popen("gcc -E -x c -P " + (gccFlags ? gccFlags : "") + " " + OS.enquote(aFilePath), { charset:"UTF-8" });
while (chunk = gcc.stdout.read())
fileContents += chunk;
}
finally
{
gcc.stdin.close();
gcc.stdout.close();
gcc.stderr.close();
}
if (fileContents.length > 0)
{
@@ -46,18 +46,39 @@ function compileWithResolvedFlags(aFilePath, objjcFlags, gccFlags, asPlainJavasc
if (!shouldObjjPreprocess)
{
if (OS.popen("which gcc").stdout.read().length === 0)
fileContents = FILE.read(aFilePath, { charset:"UTF-8" });
else
try
{
// GCC preprocess the file.
var gcc = OS.popen("gcc -E -x c -P " + (gccFlags ? gccFlags.join(" ") : "") + " " + OS.enquote(aFilePath), { charset:"UTF-8" }),
chunk = "";
var p = OS.popen("which gcc");
while (chunk = gcc.stdout.read())
fileContents += chunk;
if (p.stdout.read().length === 0)
{
fileContents = FILE.read(aFilePath, { charset:"UTF-8" });
}
else
{
try
{
var gcc = OS.popen("gcc -E -x c -P " + (gccFlags ? gccFlags.join(" ") : "") + " " + OS.enquote(aFilePath), { charset:"UTF-8" }),
chunk = "";
while (chunk = gcc.stdout.read())
fileContents += chunk;
}
finally
{
gcc.stdin.close();
gcc.stdout.close();
gcc.stderr.close();
}
}
}
finally
{
p.stdin.close();
p.stdout.close();
p.stderr.close();
}
return fileContents;
}
@@ -22,7 +22,6 @@
var FILE = require("file");
var BUNDLE_TASK = require("objective-j/jake/bundletask");
exports.BundleTask = BUNDLE_TASK.BundleTask;
+15 -6
View File
@@ -40,8 +40,8 @@ $BROWSER_FILES = new FileList($BROWSER_FILE).include($OBJECTIVEJ_FILES);
filedir($BUILD_BROWSER_FILE, $BROWSER_FILES, function(aTask)
{
gcc($BROWSER_FILE,
$BUILD_BROWSER_FILE,
gcc($BROWSER_FILE,
$BUILD_BROWSER_FILE,
environmentFlags("Browser", "ObjJ").concat($INCLUDE_FLAGS, $DEBUG_FLAGS), $CONFIGURATION !== "Debug");
});
@@ -119,11 +119,20 @@ function gcc(inputFilePath, outputFilePath, flags, compress)
stream.print("Building... \0green(" + outputFilePath +"\0)");
// GCC preprocess the file.
var cmd = ["gcc", "-E", "-x", "c", "-P"].concat(flags, inputFilePath).join(" ");
var gcc = OS.popen(cmd, { charset:"UTF-8" });
var cmd = ["gcc", "-E", "-x", "c", "-P"].concat(flags, inputFilePath).join(" "),
contents = FILE.read("header.txt", { charset : "UTF-8" });
var contents = FILE.read("header.txt", { charset : "UTF-8" });
contents += gcc.stdout.read();
try
{
var gcc = OS.popen(cmd, { charset:"UTF-8" });
contents += gcc.stdout.read();
}
finally
{
gcc.stdin.close();
gcc.stdout.close();
gcc.stderr.close();
}
if (FILE.extension(inputFilePath) === ".js" && compress)
contents = compressor(contents);
+22 -10
View File
@@ -13,22 +13,34 @@ task ("build", function()
OS.exit(0);
// No building on 10.6
var p = OS.popen(["sw_vers", "-productVersion"]);
if (p.wait() === 0)
try
{
var versions = p.stdout.read().split("."),
majorVersion = parseInt(versions[0], 10),
minorVersion = parseInt(versions[1], 10),
buildVersion = parseInt(versions[2], 10);
var p = OS.popen(["sw_vers", "-productVersion"]);
if (majorVersion < 10 || minorVersion < 7)
if (p.wait() === 0)
{
colorPrint("XcodeCapp can only be built on Mac OS X 10.7+. You can download the binary here: https://www.dropbox.com/sh/gxdgm356gyb9tqc/rFNyl8hcVG", "red");
var versions = p.stdout.read().split("."),
majorVersion = parseInt(versions[0], 10),
minorVersion = parseInt(versions[1], 10),
buildVersion = parseInt(versions[2], 10);
OS.exit(0);
if (majorVersion < 10 || minorVersion < 7)
{
colorPrint("XcodeCapp can only be built on Mac OS X 10.7+. You can download the binary here: https://www.dropbox.com/sh/gxdgm356gyb9tqc/rFNyl8hcVG", "red");
p.stdin.close();
p.stdout.close();
p.stderr.close();
OS.exit(0);
}
}
}
finally
{
p.stdin.close();
p.stdout.close();
p.stderr.close();
}
if (executableExists("xcodebuild"))
{
+25 -4
View File
@@ -119,8 +119,19 @@ ConverterConversionException = @"ConverterConversionException";
// Compile xib or nib to make sure we have a non-new format nib.
temporaryNibFilePath = FILE.join("/tmp", FILE.basename(aFilePath) + ".tmp.nib");
if (OS.popen(["/usr/bin/ibtool", aFilePath, "--compile", temporaryNibFilePath]).wait() === 1)
[CPException raise:ConverterConversionException reason:@"Could not compile file: " + aFilePath];
try
{
var p = OS.popen(["/usr/bin/ibtool", aFilePath, "--compile", temporaryNibFilePath]);
if (p.wait() === 1)
[CPException raise:ConverterConversionException reason:@"Could not compile file: " + aFilePath];
}
finally
{
p.stdin.close();
p.stdout.close();
p.stderr.close();
}
}
else
{
@@ -130,8 +141,18 @@ ConverterConversionException = @"ConverterConversionException";
// Convert from binary plist to XML plist
var temporaryPlistFilePath = FILE.join("/tmp", FILE.basename(aFilePath) + ".tmp.plist");
if (OS.popen(["/usr/bin/plutil", "-convert", "xml1", temporaryNibFilePath, "-o", temporaryPlistFilePath]).wait() === 1)
[CPException raise:ConverterConversionException reason:@"Could not convert to xml plist for file: " + aFilePath];
try
{
var p = OS.popen(["/usr/bin/plutil", "-convert", "xml1", temporaryNibFilePath, "-o", temporaryPlistFilePath]);
if (p.wait() === 1)
[CPException raise:ConverterConversionException reason:@"Could not convert to xml plist for file: " + aFilePath];
}
finally
{
p.stdin.close();
p.stdout.close();
p.stderr.close();
}
if (!FILE.isReadable(temporaryPlistFilePath))
[CPException raise:ConverterConversionException reason:@"Unable to convert nib file."];
-20
View File
@@ -28,27 +28,7 @@ var OS = require("os"),
function main(args)
{
checkUlimit();
var nib2cib = [[Nib2Cib alloc] initWithArgs:args];
[nib2cib run];
}
function checkUlimit()
{
var minUlimit = 1024,
p = OS.popen(["ulimit", "-n"]);
if (p.wait() === 0)
{
var limit = p.stdout.read().split("\n")[0];
if (Number(limit) < minUlimit)
{
stream.print("\0red(\0bold(WARNING:\0)\0) nib2cib may need to open more files than this terminal session currently allows (" + limit + "). Add the following line to your login configuration file (.bash_profile, .bashrc, etc.), start a new terminal session, then try again:\n");
stream.print("ulimit -n " + minUlimit);
OS.exit(1);
}
}
}
+14 -30
View File
@@ -378,11 +378,20 @@ global.setPackageMetadata = function(packagePath)
{
var pkg = JSON.parse(FILE.read(packagePath, { charset : "UTF-8" }));
var p = OS.popen(["git", "rev-parse", "--verify", "HEAD"]);
if (p.wait() === 0) {
var sha = p.stdout.read().split("\n")[0];
if (sha.length === 40)
pkg["cappuccino-revision"] = sha;
try
{
var p = OS.popen(["git", "rev-parse", "--verify", "HEAD"]);
if (p.wait() === 0) {
var sha = p.stdout.read().split("\n")[0];
if (sha.length === 40)
pkg["cappuccino-revision"] = sha;
}
}
finally
{
p.stdin.close();
p.stdout.close();
p.stderr.close();
}
pkg["cappuccino-timestamp"] = new Date().getTime();
@@ -577,31 +586,6 @@ global.colorPrint = function(/* String */ message, /* String */ color)
stream.print(colorize(message, color));
};
var minUlimit = 1024;
global.checkUlimit = function()
{
var ulimitPath = executableExists("ulimit");
if (!ulimitPath)
return;
var p = OS.popen([ulimitPath, "-n"]);
if (p.wait() === 0)
{
var limit = p.stdout.read().split("\n")[0];
if (Number(limit) < minUlimit)
{
stream.print("\0red(\0bold(ERROR:\0)\0) Cappuccino may need to open more files than this terminal session currently allows (" + limit + "). Add the following line to your login configuration file (.bash_profile, .bashrc, etc.), start a new terminal session, then try again:\n");
stream.print("ulimit -n " + minUlimit);
OS.exit(1);
}
}
}
// built in tasks
task ("build");