diff options
Diffstat (limited to 'pomu/source')
-rw-r--r-- | pomu/source/__init__.py | 2 | ||||
-rw-r--r-- | pomu/source/url.py | 86 |
2 files changed, 88 insertions, 0 deletions
diff --git a/pomu/source/__init__.py b/pomu/source/__init__.py index 3e78150..c54447c 100644 --- a/pomu/source/__init__.py +++ b/pomu/source/__init__.py @@ -4,3 +4,5 @@ dispatcher = PackageDispatcher() import pomu.source.portage import pomu.source.file +# sealed until pbraw is released +#import pomu.source.url diff --git a/pomu/source/url.py b/pomu/source/url.py new file mode 100644 index 0000000..fe346a4 --- /dev/null +++ b/pomu/source/url.py @@ -0,0 +1,86 @@ +""" +A package source module to import packages from URLs +""" + +from os import path, close +from tempfile import mkstemp + +from pbraw import grab + +from pomu.package import Package +from pomu.source import dispatcher +from pomu.source.base import PackageBase, BaseSource +from pomu.util.pkg import cpv_split, ver_str +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): + fd, tfile = tempfile.mkstemp() + os.close(fd) + with open(tfile, 'w') as f: + if self.contents: + f.write(self.contents) + else: + fs = grab(self.url) + f.write(fs[0][1]) + return Package(self.name, '/', self, self.category, self.version, + filemap = { + path.join( + self.category, + self.name, + '{}/{}-{}.ebuild'.format(self.category, self.name, self.version) + ) : tfile}) + + @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), parent).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(uri[3:]) + + @classmethod + def from_meta_dir(cls, metadir): + return LocalEbuild.from_data_dir(cls, metadir) |