aboutsummaryrefslogtreecommitdiff
blob: 850e69f56192ea69fb480a76f56f25bb28e31744 (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
"""
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
from pomu.util.result import Result

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

    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):
        with open(path.join(pkgdir, 'ORIG_URL'), 'r') as f:
            return URLGrabberSource.parse_link(f.readline()).unwrap()

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

    def __str__(self):
        return '{}/{}-{} (from {})'.format(self.category, self.name, self.version, self.path)

@dispatcher.source
class URLGrabberSource(BaseSource):
    """
    The source module responsible for grabbing modules from URLs,
    including pastebins
    """
    @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()
        return Result.Ok(URLEbuild(category, name, ver, uri, files[0][1]))

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

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

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