Use try/finally to ensure popen streams are always closed

This commit is contained in:
Antoine Mercadal
2015-05-09 10:22:13 -07:00
parent 9bcaef192c
commit c603326e58
10 changed files with 172 additions and 112 deletions
+18 -12
View File
@@ -374,24 +374,30 @@ function pngcrushDirectory(directory)
system.stderr.print("Running pngcrush on " + pngs.length + " pngs:"); system.stderr.print("Running pngcrush on " + pngs.length + " pngs:");
pngs.forEach(function(dstPath) pngs.forEach(function(dstPath)
{ {
var tmpPath = FILE.path(dstPath+".tmp"), var tmpPath = FILE.path(dstPath+".tmp");
p = OS.popen(["pngcrush", "-rem", "alla", "-reduce", /*"-brute",*/ dstPath, tmpPath]);
if (p.wait()) try
{ {
CPLog.warn("pngcrush failed. Ensure it's installed and on your PATH."); 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 finally
{ {
FILE.move(tmpPath, dstPath); p.stdin.close();
system.stderr.write(".").flush(); p.stdout.close();
p.stderr.close();
} }
p.stdin.close();
p.stdout.close();
p.stderr.close();
}); });
system.stderr.print(""); system.stderr.print("");
} }
+14 -12
View File
@@ -3,18 +3,20 @@ var OS = require("os");
exports.fontinfo = function(name, size) exports.fontinfo = function(name, size)
{ {
var p = OS.popen(["fontinfo", "-n", name, size || 12]), var result;
result;
if (p.wait() === 0) try
result = p.stdout.read(); {
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();
}
p.stdin.close(); return result ? JSON.parse(result) : null;
p.stdout.close();
p.stderr.close();
if (result)
return JSON.parse(result);
else
return null;
}; };
+14 -12
View File
@@ -3,18 +3,20 @@ var OS = require("os");
exports.imagesize = function(path) exports.imagesize = function(path)
{ {
var p = OS.popen(["imagesize", "-n", path]), var result;
result;
if (p.wait() === 0) try
result = p.stdout.read(); {
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();
}
p.stdin.close(); return result ? JSON.parse(result) : null;
p.stdout.close();
p.stderr.close();
if (result)
return JSON.parse(result);
else
return null;
}; };
+14 -9
View File
@@ -128,17 +128,22 @@ function generateDocs(/* boolean */ noFrame)
// If the Doxygen application is installed on Mac OS X, use that // If the Doxygen application is installed on Mac OS X, use that
if (!doxygen && executableExists("mdfind")) if (!doxygen && executableExists("mdfind"))
{ {
var p = OS.popen(["mdfind", "kMDItemContentType == 'com.apple.application-bundle' && kMDItemCFBundleIdentifier == 'org.doxygen'"]); try
if (p.wait() === 0)
{ {
var doxygenApps = p.stdout.read().split("\n"); var p = OS.popen(["mdfind", "kMDItemContentType == 'com.apple.application-bundle' && kMDItemCFBundleIdentifier == 'org.doxygen'"]);
if (doxygenApps[0]) if (p.wait() === 0)
doxygen = FILE.join(doxygenApps[0], "Contents/Resources/doxygen"); {
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();
} }
p.stdin.close();
p.stdout.close();
p.stderr.close();
} }
if (!doxygen || !FILE.exists(doxygen)) if (!doxygen || !FILE.exists(doxygen))
+12 -7
View File
@@ -323,16 +323,21 @@ function FileRequest(/*CFURL*/ aURL, onsuccess, onfailure, onprogress)
var aFilePath = aURL.toString().substring(5), var aFilePath = aURL.toString().substring(5),
OS = require("os"), OS = require("os"),
gccFlags = require("objective-j").currentCompilerFlags(), gccFlags = require("objective-j").currentCompilerFlags(),
gcc = OS.popen("gcc -E -x c -P " + (gccFlags ? gccFlags : "") + " " + OS.enquote(aFilePath), { charset:"UTF-8" }),
chunk, chunk,
fileContents = ""; fileContents = "";
while (chunk = gcc.stdout.read()) try
fileContents += chunk; {
var gcc = OS.popen("gcc -E -x c -P " + (gccFlags ? gccFlags : "") + " " + OS.enquote(aFilePath), { charset:"UTF-8" });
gcc.stdin.close(); while (chunk = gcc.stdout.read())
gcc.stdout.close(); fileContents += chunk;
gcc.stderr.close(); }
finally
{
gcc.stdin.close();
gcc.stdout.close();
gcc.stderr.close();
}
if (fileContents.length > 0) if (fileContents.length > 0)
{ {
@@ -46,27 +46,39 @@ function compileWithResolvedFlags(aFilePath, objjcFlags, gccFlags, asPlainJavasc
if (!shouldObjjPreprocess) if (!shouldObjjPreprocess)
{ {
var p = OS.popen("which gcc"); try
if (p.stdout.read().length === 0)
fileContents = FILE.read(aFilePath, { charset:"UTF-8" });
else
{ {
// GCC preprocess the file. var p = OS.popen("which gcc");
var gcc = OS.popen("gcc -E -x c -P " + (gccFlags ? gccFlags.join(" ") : "") + " " + OS.enquote(aFilePath), { charset:"UTF-8" }),
chunk = "";
while (chunk = gcc.stdout.read()) if (p.stdout.read().length === 0)
fileContents += chunk; {
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 = "";
gcc.stdin.close(); while (chunk = gcc.stdout.read())
gcc.stdout.close(); fileContents += chunk;
gcc.stderr.close(); }
finally
{
gcc.stdin.close();
gcc.stdout.close();
gcc.stderr.close();
}
}
}
finally
{
p.stdin.close();
p.stdout.close();
p.stderr.close();
} }
p.stdin.close();
p.stdout.close();
p.stderr.close();
return fileContents; return fileContents;
} }
+11 -6
View File
@@ -120,14 +120,19 @@ function gcc(inputFilePath, outputFilePath, flags, compress)
// GCC preprocess the file. // GCC preprocess the file.
var cmd = ["gcc", "-E", "-x", "c", "-P"].concat(flags, inputFilePath).join(" "), var cmd = ["gcc", "-E", "-x", "c", "-P"].concat(flags, inputFilePath).join(" "),
gcc = OS.popen(cmd, { charset:"UTF-8" }),
contents = FILE.read("header.txt", { charset : "UTF-8" }); contents = FILE.read("header.txt", { charset : "UTF-8" });
contents += gcc.stdout.read(); try
{
gcc.stdin.close(); var gcc = OS.popen(cmd, { charset:"UTF-8" });
gcc.stdout.close(); contents += gcc.stdout.read();
gcc.stderr.close(); }
finally
{
gcc.stdin.close();
gcc.stdout.close();
gcc.stderr.close();
}
if (FILE.extension(inputFilePath) === ".js" && compress) if (FILE.extension(inputFilePath) === ".js" && compress)
contents = compressor(contents); contents = compressor(contents);
+23 -14
View File
@@ -13,25 +13,34 @@ task ("build", function()
OS.exit(0); OS.exit(0);
// No building on 10.6 // No building on 10.6
var p = OS.popen(["sw_vers", "-productVersion"]); try
if (p.wait() === 0)
{ {
var versions = p.stdout.read().split("."), var p = OS.popen(["sw_vers", "-productVersion"]);
majorVersion = parseInt(versions[0], 10),
minorVersion = parseInt(versions[1], 10),
buildVersion = parseInt(versions[2], 10);
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("."),
OS.exit(0); majorVersion = parseInt(versions[0], 10),
minorVersion = parseInt(versions[1], 10),
buildVersion = parseInt(versions[2], 10);
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.stdin.close();
p.stderr.close(); p.stdout.close();
p.stderr.close();
}
if (executableExists("xcodebuild")) if (executableExists("xcodebuild"))
{ {
+24 -15
View File
@@ -119,14 +119,19 @@ ConverterConversionException = @"ConverterConversionException";
// Compile xib or nib to make sure we have a non-new format nib. // Compile xib or nib to make sure we have a non-new format nib.
temporaryNibFilePath = FILE.join("/tmp", FILE.basename(aFilePath) + ".tmp.nib"); temporaryNibFilePath = FILE.join("/tmp", FILE.basename(aFilePath) + ".tmp.nib");
var p = OS.popen(["/usr/bin/ibtool", aFilePath, "--compile", temporaryNibFilePath]); 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();
}
if (p.wait() === 1)
[CPException raise:ConverterConversionException reason:@"Could not compile file: " + aFilePath];
p.stdin.close();
p.stdout.close();
p.stderr.close();
} }
else else
{ {
@@ -136,14 +141,18 @@ ConverterConversionException = @"ConverterConversionException";
// Convert from binary plist to XML plist // Convert from binary plist to XML plist
var temporaryPlistFilePath = FILE.join("/tmp", FILE.basename(aFilePath) + ".tmp.plist"); var temporaryPlistFilePath = FILE.join("/tmp", FILE.basename(aFilePath) + ".tmp.plist");
var p = OS.popen(["/usr/bin/plutil", "-convert", "xml1", temporaryNibFilePath, "-o", temporaryPlistFilePath]); try
{
if (p.wait() === 1) var p = OS.popen(["/usr/bin/plutil", "-convert", "xml1", temporaryNibFilePath, "-o", temporaryPlistFilePath]);
[CPException raise:ConverterConversionException reason:@"Could not convert to xml plist for file: " + aFilePath]; if (p.wait() === 1)
[CPException raise:ConverterConversionException reason:@"Could not convert to xml plist for file: " + aFilePath];
p.stdin.close(); }
p.stdout.close(); finally
p.stderr.close(); {
p.stdin.close();
p.stdout.close();
p.stderr.close();
}
if (!FILE.isReadable(temporaryPlistFilePath)) if (!FILE.isReadable(temporaryPlistFilePath))
[CPException raise:ConverterConversionException reason:@"Unable to convert nib file."]; [CPException raise:ConverterConversionException reason:@"Unable to convert nib file."];
+14 -9
View File
@@ -378,16 +378,21 @@ global.setPackageMetadata = function(packagePath)
{ {
var pkg = JSON.parse(FILE.read(packagePath, { charset : "UTF-8" })); var pkg = JSON.parse(FILE.read(packagePath, { charset : "UTF-8" }));
var p = OS.popen(["git", "rev-parse", "--verify", "HEAD"]); try
if (p.wait() === 0) { {
var sha = p.stdout.read().split("\n")[0]; var p = OS.popen(["git", "rev-parse", "--verify", "HEAD"]);
if (sha.length === 40) if (p.wait() === 0) {
pkg["cappuccino-revision"] = sha; 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();
} }
p.stdin.close();
p.stdout.close();
p.stderr.close();
pkg["cappuccino-timestamp"] = new Date().getTime(); pkg["cappuccino-timestamp"] = new Date().getTime();
pkg["version"] = getCappuccinoVersion(); pkg["version"] = getCappuccinoVersion();