summaryrefslogtreecommitdiff
path: root/baserockimport/exts/cpan.to_lorry
blob: bcca022377fad45b12328038727e7331c2e748bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/python
#
# Create a Baserock .lorry file for a given RubyGem
#
# Copyright (C) 2014  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import requests
import requests_cache
import yaml
import json
import logging
import os
import sys
import urlparse

from importer_base import ImportException, ImportExtension


class GenerateLorryException(ImportException):
    pass


class CPANWebServiceClient(object):
    def __init__(self):
        # Save hammering the cpanmetadb.plackperl.org API: 'requests' API calls are
        # transparently cached in an SQLite database, instead.
        requests_cache.install_cache('cpan_api_cache')

    def _request(self, url):
        r = requests.get(url)
        if r.ok:
            return yaml.load(r.text)
        else:
            raise GenerateLorryException(
                'Request to %s failed: %s' % (r.url, r.reason))

    def get_cpan_info(self, cpan_name):
        info = self._request(
            'http://cpanmetadb.plackperl.org/v1.0/package/%s' % cpan_name)

        # TODO: Sanity check?  Check name appears in distfile string?
        # TODO: Use the history/ API, rather than package/ to get version list?
        return info


class CPANLorryGenerator(ImportExtension):
    def __init__(self):
        super(CPANLorryGenerator, self).__init__()

    def process_args(self, args):
        if len(args) not in [1, 2]:
            raise ImportException(
                'Please call me with the name of a CPAN module as an argument'
                ' and optionally its version number (format: NAME [VERSION])')

        cpan_name = args[0]
        # TODO: Version handling?

        lorry = self.generate_lorry_for_module(cpan_name)
        self.write_lorry(sys.stdout, lorry)

    def find_tarball_uri_for_module(self, cpan_name, cpan_info):
        return 'http://cpan.metacpan.org/authors/id/' + cpan_info['distfile']

    def generate_lorry_for_module(self, cpan_name):
        cpan_client = CPANWebServiceClient()

        cpan_info = cpan_client.get_cpan_info(cpan_name)

        cpan_source_url = self.find_tarball_uri_for_module(cpan_name, cpan_info)
        logging.info('Got URL <%s> for %s', cpan_source_url, cpan_name)

        lorry_name = 'cpan/' + cpan_name.replace('::', '-')

        lorry = {
            lorry_name: {
                'type': 'tarball',
                'url': cpan_source_url,
                'x-products-cpan': [cpan_name]
            }
        }

        return lorry

    def write_lorry(self, stream, lorry):
        json.dump(lorry, stream, indent=4)
        # Needed so the morphlib.extensions code will pick up the last line.
        stream.write('\n')


if __name__ == '__main__':
    CPANLorryGenerator().run()