view paint/package.py @ 7:a7bf894c96c7

yet more stubbing
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 23 Feb 2012 17:41:54 -0800
parents 5b74792d46e4
children c1181e9c9ca2
line wrap: on
line source

"""
package model for python PAckage INTrospection
"""

import os
import tarfile
import tempfile
import urllib2
import utils

class Package(object):
    # XXX much of this is generic resource stuff and should be split off

    def __init__(self, src):
        self.src = src
        self._tmppath = None

    def path(self):
        """filesystem path to package"""

        # return cached copy if it exists
        if self._tmppath:
            return self._tmppath

        # fetch from the web if a URL
        tmpfile = None
        src = self.src
        if utils.isURL(self.src):
            tmpfile = self.fetch()
            return self._tmppath

        # unpack if an archive
        if self.is_archive(src):
            self.unpack(src)
            return self._tmppath

        return self.src

    def fetch(self):
        """fetch from remote source"""

        fd, filename = tmpfile.mkstemp()
        resource = urllib.urlopen(self.src)
        os.write(fd, resource.read())
        os.close(fd)
        return filename

    def unpack(self, archive):
        """unpack the archive to a temporary destination"""
        # TODO: should handle zipfile additionally at least
        # Ideally, this would be pluggable, etc
        assert tarfile.is_tarfile
        tarfile.TarFile.open(

    def is_archive(self, path):
        """returns if the filesystem path is an archive"""
        # TODO: should handle zipfile additionally at least
        # Ideally, this would be pluggable, etc
        return tarfile.is_tarfile(path)