【发布时间】:2014-07-25 15:39:28
【问题描述】:
我正在尝试解析 Jenkins 作业“config.xml”文件。
这是我尝试使用 HTTP::Tiny 的一种方法:
use HTTP::Tiny;
my $page = "http://localhost:8080/job/Job_One_Name/config.xml";
system("start $page"); # this loads the xml file in browser successfully
my $wsdlResponse = HTTP::Tiny->new->get($page);
$wsdlResponse->{success} or die print $logger->error_die($!);
my $wsdlXML = XML::LibXML->new->parse_file($wsdlResponse->{content});
print $wsdlXML;
它因错误而死,“错误的文件描述符”。
如果我注释掉该行,脚本会在 parse_file 行中断 错误信息:
Could not create file parser context for file "<html><head><meta http-equiv='refresh' content='1;url=/login?from=%2Fjo
b%2FJob_One_Name/config.xml'/><script>window.location.replace('/login?from=%2Fjob%2FJob_One_Name/config.xml');</script></head><body style='background-color:white; color:white;'>
Authentication required
<!--
You are authenticated as: anonymous
Groups that you are in:
Permission you need to have (but didn't): hudson.model.Hudson.Read
... which is implied by: hudson.security.Permission.GenericRead
... which is implied by: hudson.model.Hudson.Administer
-->
</body></html>
": Result too large at Job.pm line 67.
我也尝试了 XML::Twig 和 LibXML->load_xml 和 load_html 来自网上不同的例子,但他们都死了类似于 “无法为文件创建文件解析器上下文”、“错误的文件描述符” 或 “指定位置、字符串或 IO”的错误 em>。
关于可能是什么问题的任何想法?提前致谢。
更新:
按照来自 Jenkins'Authenticating scripted clients 的 'Perl LWP example for a scripted client',我已成功通过身份验证:
my $uagent = LWP::UserAgent->new(cookie_jar => HTTP::Cookies->new());
my %options = {'myUserName' => 'myPassword'};
$req = HTTP::Request->new( GET => $serverHost );
$req->authorization_basic( 'myUserName', 'myPassword' );
my $res = $uagent->request($req);
# Check the outcome of the response
print "Result: " . $res->status_line . "\n";
print $res->headers->as_string;
print "\n";
if ( !$res->is_success ) {
print "Failed\n";
} else {
print "Success!\n";
# Parse XML
my $wsdlResponse = HTTP::Tiny->new->get($page, \%options);
#$dom = XML::LibXML->load_xml(location=>$page);
#print $dom . "\n";
my $wsdlXML = XML::LibXML->new->parse_file($wsdlResponse->{content});
print $wsdlXML;
#print $res->content, "\n";
}
它仍然给我同样的 'Authentication required' 错误。我不知道如何使用成功授权的请求来解析文件。
【问题讨论】:
-
“它死于错误,“错误的文件描述符”。” -- WHERE?
-
@JimGarrison 在“die”行,“$wsdlResponse->{success} or die print $logger->error_die($!);”
-
你想抢什么?
config.xml是一个结构良好的 XML。 Jenkins 中的大部分信息都有一个 RESTful API,包括作业和单个构建。您可以通过这种方式以 JSON、Python 或 XML 格式提取信息。单击 Jenkins 页面底部的 REST API 链接。这会有帮助吗? -
@DavidW。感谢您的回复。我只是在尝试编写自己的脚本,该脚本的功能与 Fetch 在 REST API 中的功能几乎相同。该脚本只是通过命令提示符或 shell(作业名称、节点等)提示所有内容,对其进行处理,并列出 HTML 表中的所有内容(它还包括每个作业的“可嵌入构建状态”图像)。我认为它可能会派上用场,或者允许我在这里和那里自定义一些小东西,但它最终可能没用......尽管如此,这只是我正在测试和工作的东西。
标签: xml perl parsing authentication jenkins