aboutsummaryrefslogtreecommitdiff
blob: 6aca5588a1a10978bb5806ea9620bf3e9606899b (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
"""
A package source module to import packages from URLs
"""

from os import path

from pbraw import grab

from pomu.package import Package
from pomu.source import dispatcher
from pomu.source.base import PackageBase, BaseSource
from pomu.util.query import query, QueryContext
from pomu.util.result import Result

class URLEbuild(PackageBase):
    """A class to represent an ebuild fetched from a url"""
    __cname__ = 'url'
    
    def __init__(self, url, contents, category, name, version, slot):
        self.url = url
        self.contents = contents
        self.category = category
        self.name = name
        self.version = version
        self.slot = slot

    def fetch(self):
        if self.contents:
            if isinstance(self.contents, str):
                self.content = self.contents.encode('utf-8')
        else:
            fs = grab(self.url)
            self.content = fs[0][1].encode('utf-8')
        return Package(self.name, '/', self, self.category, self.version,
                filemap = {
                    path.join(
                        self.category,
                        self.name,
                        '{}-{}.ebuild'.format(self.name, self.version)
                    ) : self.content})
    
    @staticmethod
    def from_data_dir(pkgdir):
        pkg = PackageBase.from_data_dir(pkgdir)
        if pkg.is_err():
            return pkg
        pkg = pkg.unwrap()

        with QueryContext(category=pkg.category, name=pkg.name, version=pkg.version, slot=pkg.slot):
            with open(path.join(pkgdir, 'ORIG_URL'), 'r') as f:
                return URLGrabberSource.parse_link(f.readline()).unwrap()

    def write_meta(self, pkgdir):
        super().write_meta(pkgdir)
        with open(path.join(pkgdir, 'ORIG_URL'), 'w') as f:
            f.write(self.url + '\n')

    def __str__(self):
        return super().__str__() + ' (from {})'.format(self.url)

@dispatcher.source
class URLGrabberSource(BaseSource):
    """
    The source module responsible for grabbing modules from URLs,
    including pastebins
    """
    __cname__ = 'url'

    @dispatcher.handler(priority=5)
    def parse_link(uri):
        if not (uri.startswith('http://') or uri.startswith('https://')):
            return Result.Err()

        name = query('name', 'Please specify package name').expect()
        category, _, name = name.rpartition('/')
        ver = query('version', 'Please specify package version for {}'.format(name)).expect()
        if not category:
            category = query('category', 'Please enter category for {}'.format(name)).expect()
        files = grab(uri)
        if not files:
            return Result.Err()
        slot = query('slot', 'Please specify package slot', '0').expect()
        return Result.Ok(URLEbuild(uri, files[0][1], category, name, ver, slot))

    @dispatcher.handler()
    def parse_full(url):
        if not url.startswith('url:'):
            return Result.Err()
        return URLGrabberSource.parse_link(url[4:])

    @classmethod
    def fetch_package(self, pkg):
        return pkg.fetch()

    @classmethod
    def from_meta_dir(cls, metadir):
        return URLEbuild.from_data_dir(metadir)