【问题标题】:How can we identify all the modules belongs to a given distribution我们如何识别所有模块属于给定分布
【发布时间】:2014-04-28 16:45:53
【问题描述】:

我们如何识别所有模块属于给定分布。

例如XML::LibXML 发行版提供了一组以下模块 https://metacpan.org/release/XML-LibXML

我们如何通过 cpan/ppm 或通过每个包的任何标准获得此列表。

实际上,我们正在为我们用 Perl 编写的代码编写一个单元测试框架。为了验证模块,我们需要一种方法来找到给定模块名称的分发名称。

【问题讨论】:

    标签: perl


    【解决方案1】:

    MetaCPAN API 通过 JSON Web 服务 (http://api.metacpan.org) 提供了解决此问题的方法。

    在命令行上使用curl 或通过http://explorer.metacpan.org/ 的Web 表单尝试不同的查询很容易

    如果您知道要搜索的版本名称, 您可以执行这样的查询来获取模块名称列表:

    /module/_search
    {
      "query" : { "match_all" : {} },
      "size" : 1000,
      "fields" : [ "module.name" ],
      "filter" : {
        "and": [
          { "term" : { "module.authorized" : true } },
          { "term" : { "module.indexed" : true } },
          { "term" : { "release" : "XML-LibXML-1.95" } },
          { "term" : { "status" : "latest" } }
        ]
      }
    }
    

    您也可以将"release": "XML-LibXML-1.95" 替换为"distribution": "XML-LibXML"

    如果您以模块名称开头并且需要先确定发布名称,请尝试以下操作:

    /module/_search
    {
      "query" : { "match_all" : {} },
      "size" : 1000,
      "fields" : [ "release", "distribution" ],
      "filter" : {
        "and": [
          { "term" : { "module.name" : "XML::LibXML" } },
          { "term" : { "status" : "latest" } }
        ]
      }
    }
    

    该查询语法是 ElasticSearch DSL,因为 api 使用 ElasticSearch 来索引数据。

    要从 perl 进行查询,有一个 MetaCPAN::API 模块,虽然我自己没用过。

    由于它只是一个 Web 请求,您可以使用 LWP 或任何其他 HTTP 模块。

    您可能还想查看 ElasticSearchElasticSearch::SearchBuilder 提供更完整的 perl 接口来查询 ElasticSearch 数据库的模块。

    这是使用 LWP 在 perl 中的完整示例:

    use JSON qw( encode_json decode_json );
    use LWP::UserAgent;
    my $ua = LWP::UserAgent->new;
    my $res = $ua->post("http://api.metacpan.org/module/_search",
      Content => encode_json({
        query  => { match_all => {} },
        size   => 1000,
        # limit reponse text to just the module names since that's all we want
        fields => ['module.name'],
        filter => {
          and  => [
            { term => { "module.authorized" => 1 } },
            { term => { "module.indexed"    => 1 } },
            { term => { "distribution" => "XML-LibXML" } },
            { term => { "status" => "latest" } }
          ]
        }
      })
    );
    my @modules =
      # this can be an array (ref) of module names for multiple packages in one file
      map { ref $_ ? @$_ : $_ }
      # the pieces we want
      map { $_->{fields}{'module.name'} }
      # search results
      @{ decode_json($res->decoded_content)->{hits}{hits} };
    print join "\n", sort @modules;
    

    如需更多帮助,请访问#metacpanirc.perl.org, 或查看https://github.com/CPAN-API/cpan-api/wiki 的 wiki。

    如果您多解释一下您正在做什么和/或试图实现的目标,您可能会找到其他方法。

    【讨论】:

    • 这是伟大的蒂姆·邦斯(Tim Bunce)的一篇详尽的帖子,他想在更大范围内做到这一点,这导致他创建了 Dist::Surveyor:blog.timbunce.org/2011/11/16/…
    猜你喜欢
    • 2018-07-05
    • 2015-10-22
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    相关资源
    最近更新 更多