PHP exec 抛出意外结果

发布于 2024-12-25 13:15:35 字数 2978 浏览 0 评论 0原文

我正在尝试使用 phing 自动化部署。当我使用 svnlastrevision 任务时出现以下错误

由于以下原因,目标“builddiff”的执行失败:/home/ramjee/Work/Projects/it/dev-stack/build.xml:88:1:无法解析“svn info --xml”的输出'.

在进一步调试问题时,我将其归结为以下内容:

以下是一个重现问题的小程序:

$cmd = "/usr/bin/svn info --non-interactive '/home/ramjee/Work/Projects/trunk/src' '--xml'";
exec("$cmd 2>&1",$out,$ret_var);

print_r($out);

当我执行上面的

i.使用 Bitnami lampstack.1.2-5 附带的 PHP (5.2.17)。我得到以下结果(不是预期的):

Array
(
    [0] => /usr/bin/svn: /home/ramjee/Work/lampstack-1.2-5/common/lib/libsasl2.so.2: no version information available (required by /usr/lib/libldap_r-2.4.so.2)
    [1] => /usr/bin/svn: /home/ramjee/Work/lampstack-1.2-5/common/lib/libsasl2.so.2: no version information available (required by /usr/lib/libsvn_ra_svn-1.so.1)
    [2] => <?xml version="1.0"?>
    [3] => <info>
    [4] => <entry
    [5] =>    kind="dir"
    [6] =>    path="/home/ramjee/Work/Projects/trunk/src"
    [7] =>    revision="818">
    [8] => <url>svn://abc.abc.abc.abc/data/repositories/src</url>
    [9] => <repository>
    [10] => <root>svn://abc.abc.abc.abc/data/repositories/</root>
    [11] => <uuid>f74a063e-5e8e-11e0-b400-13ff509e0209</uuid>
    [12] => </repository>
    [13] => <wc-info>
    [14] => <schedule>normal</schedule>
    [15] => <depth>infinity</depth>
    [16] => </wc-info>
    [17] => <commit
    [18] =>    revision="802">
    [19] => <author>shweta</author>
    [20] => <date>2012-01-03T12:07:46.427638Z</date>
    [21] => </commit>
    [22] => </entry>
    [23] => </info>
)

ii。 PHP (5.3.17) 是 lampp 设置的一部分。我得到以下结果(预期):

Array
(
    [0] => <?xml version="1.0"?>
    [1] => <info>
    [2] => <entry
    [3] =>    kind="dir"
    [4] =>    path="/home/ramjee/Work/Projects/trunk/src"
    [5] =>    revision="818">
    [6] => <url>svn://abc.abc.abc.abc/data/repositories/src</url>
    [7] => <repository>
    [8] => <root>svn://abc.abc.abc.abc/data/repositories/</root>
    [9] => <uuid>f74a063e-5e8e-11e0-b400-13ff509e0209</uuid>
    [10] => </repository>
    [11] => <wc-info>
    [12] => <schedule>normal</schedule>
    [13] => <depth>infinity</depth>
    [14] => </wc-info>
    [15] => <commit
    [16] =>    revision="802">
    [17] => <author>shweta</author>
    [18] => <date>2012-01-03T12:07:46.427638Z</date>
    [19] => </commit>
    [20] => </entry>
    [21] => </info>
)

在第一行中,我们有两行不需要的行,导致 phing 任务抛出错误。

我不知道如何解决这个问题?对此的任何帮助都将非常有价值。

I am trying to automate my deployment using phing. I get the below error when I use svnlastrevision task

Execution of target "builddiff" failed for the following reason: /home/ramjee/Work/Projects/it/dev-stack/build.xml:88:1: Failed to parse the output of 'svn info --xml'.

On debugging the issue further I zeroed it on to the following:

The following is a small program to recreate the issue:

$cmd = "/usr/bin/svn info --non-interactive '/home/ramjee/Work/Projects/trunk/src' '--xml'";
exec("$cmd 2>&1",$out,$ret_var);

print_r($out);

When I execute the above

i. With PHP (5.2.17) that is shipped with bitnami lampstack.1.2-5. I get the following result (not expected):

Array
(
    [0] => /usr/bin/svn: /home/ramjee/Work/lampstack-1.2-5/common/lib/libsasl2.so.2: no version information available (required by /usr/lib/libldap_r-2.4.so.2)
    [1] => /usr/bin/svn: /home/ramjee/Work/lampstack-1.2-5/common/lib/libsasl2.so.2: no version information available (required by /usr/lib/libsvn_ra_svn-1.so.1)
    [2] => <?xml version="1.0"?>
    [3] => <info>
    [4] => <entry
    [5] =>    kind="dir"
    [6] =>    path="/home/ramjee/Work/Projects/trunk/src"
    [7] =>    revision="818">
    [8] => <url>svn://abc.abc.abc.abc/data/repositories/src</url>
    [9] => <repository>
    [10] => <root>svn://abc.abc.abc.abc/data/repositories/</root>
    [11] => <uuid>f74a063e-5e8e-11e0-b400-13ff509e0209</uuid>
    [12] => </repository>
    [13] => <wc-info>
    [14] => <schedule>normal</schedule>
    [15] => <depth>infinity</depth>
    [16] => </wc-info>
    [17] => <commit
    [18] =>    revision="802">
    [19] => <author>shweta</author>
    [20] => <date>2012-01-03T12:07:46.427638Z</date>
    [21] => </commit>
    [22] => </entry>
    [23] => </info>
)

ii. With PHP (5.3.17) which was part of a lampp setup. I get the following result (expected):

Array
(
    [0] => <?xml version="1.0"?>
    [1] => <info>
    [2] => <entry
    [3] =>    kind="dir"
    [4] =>    path="/home/ramjee/Work/Projects/trunk/src"
    [5] =>    revision="818">
    [6] => <url>svn://abc.abc.abc.abc/data/repositories/src</url>
    [7] => <repository>
    [8] => <root>svn://abc.abc.abc.abc/data/repositories/</root>
    [9] => <uuid>f74a063e-5e8e-11e0-b400-13ff509e0209</uuid>
    [10] => </repository>
    [11] => <wc-info>
    [12] => <schedule>normal</schedule>
    [13] => <depth>infinity</depth>
    [14] => </wc-info>
    [15] => <commit
    [16] =>    revision="802">
    [17] => <author>shweta</author>
    [18] => <date>2012-01-03T12:07:46.427638Z</date>
    [19] => </commit>
    [20] => </entry>
    [21] => </info>
)

In the first line we have two unwanted lines which cause the phing task to throw error.

I don't know how to fix this? Any help on this will be very valuable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

oО清风挽发oО 2025-01-01 13:15:35

这不是一个很好的解决方案,因为它只是掩盖了问题(以及潜在的其他问题),但您可以从执行中删除 2>&1 部分:

exec($cmd, $out, $ret_var);

2>&1 在 bash 中用于将 STDERR(发送前 2 行的位置)重定向到 STDOUT(发送 XML 的位置) — 请参阅 此问题了解更多信息。

这样做的影响是您掩盖了该错误以及将来可能从该命令遇到的任何其他错误。这是一个更长的解决方案,仍然需要您修补库,但至少感觉不像黑客:

$cmd = "/usr/bin/svn info --non-interactive '/home/ramjee/Work/Projects/trunk/src' '--xml'";

$descriptors = array(
    1 => array('pipe', 'w'), // stdout
    2 => array('pipe', 'w')  // stderr
);

$process = proc_open($cmd, $descriptors, $pipes);

$out = $err = '';
while ($data = fgets($pipes[1])) { $out .= $data; } // contains your XML
while ($data = fgets($pipes[2])) { $err .= $data; } // contains any errors (which you can log)

This is not a great solution, as it simply masks the problem (and potentially other problems), but you can remove the 2>&1 part from the exec:

exec($cmd, $out, $ret_var);

2>&1 is used in bash to redirect STDERR (where the first 2 lines are being sent) to STDOUT (where the XML is sent) — see this question for more information.

The impact of this is that you're masking the error, and any other errors that you may encounter in the future from that command. Here's a longer solution that still involves you patching the library, but at least feels like less of a hack:

$cmd = "/usr/bin/svn info --non-interactive '/home/ramjee/Work/Projects/trunk/src' '--xml'";

$descriptors = array(
    1 => array('pipe', 'w'), // stdout
    2 => array('pipe', 'w')  // stderr
);

$process = proc_open($cmd, $descriptors, $pipes);

$out = $err = '';
while ($data = fgets($pipes[1])) { $out .= $data; } // contains your XML
while ($data = fgets($pipes[2])) { $err .= $data; } // contains any errors (which you can log)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文