Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 195 lines (156 sloc) 6.72 kb
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
import re, urllib2, os
from uploader import MoveFiles, TarFileExtract, HttpFileUploader, UploaderChain, GitUploader
from repository import Repository

class Library():
    '''
Base object for creating library specification.
Each library specification should contain information about full library name, vendor
and provide possibility to build necessary pathes (installation, temp, linking etc).
Library object is a container for building and retrieving Uploader object.
'''
    # Library main specification
    NAME = ''
    VENDOR = ''

    # Different pathes for resolving symlinking process
    PATHES = {
        'docs': '',
        'execute': '',
        'demos': '',
        'install': ''
    }

    # Information about uploading procedure
    UPLOADER_CONFIG = ('http', 'tar')

    # Initialize library version if necessary
    def __init__(self, version=None):
        if version != None:
            self.version = version

    # Return relative to object version number (full)
    def get_version(self):
        return self._version

    # Set new value for library version
    # @todo Validate version params and raise exception in all invalid cases
    def set_version(self, version):
        self._version = version

    # Full number for version of library which we are working with.
    # @todo Use for this @property && @property.setter notations
    version = property(get_version, set_version)

    # Return path for given operation type
    # For ex, you can use this to retrive installation path by calling
    # %> Library.get_path('install')
    def get_path(self, type):
        path = self.PATHES[type]
        return path % self.version if path.count('%s') == 1 else path

    def uploader(self):
        '''Should be implemented in child classess'''
        pass

    def find_latest_version(self):
        '''
Resolve in any way latest available version of library
Specific to each library, so should be implemented in inhereted classes
'''
        raise FeatureNotImplemented

    def init_latest_version(self):
        self.version = self.find_latest_version()
        return self.version

class Zf(Library):
    '''
Concrete library class (Zend Framework updating)
Download tar.gz archive with Zend Framework from official web-site and
move all extracted files to repository directory
'''
    NAME = 'Zend Framework'
    VENDOR = 'Zend Technologies Ltd.'

    # Different pathes for resolving symlinking process
    PATHES = {
        'docs': 'docs',
        'execute': 'library/Zend',
        'demos': 'demos',
        'install': 'Zf',
        'version': 'Zf/stable/%s'
    }

    UPLOADER_CONFIG = {
        'HttpFile' : 'http://framework.zend.com/releases/ZendFramework-%s/ZendFramework-%s.tar.gz',
        'HttpTemp' : '/tmp/zf-%s.tar.gz',
        'MoveTarget': 'ZendFramework-%s',
        'Unzip' : '/tmp/',
        # Configuration for resolving latest available version
        'latest' : { 'url': 'http://framework.zend.com/download/latest', 'format': r'Zend Framework (\S+) Full' }
    }

    def uploader(self):
        uploader = UploaderChain()

        # Upload file from Zend server
        dest = self.UPLOADER_CONFIG['HttpTemp'] % self.version
        uploader.add(HttpFileUploader(self.UPLOADER_CONFIG['HttpFile'] % (self.version, self.version), dest))

        # Unzipping all files in /tmp directory
        uploader.add(TarFileExtract(dest, self.UPLOADER_CONFIG['Unzip']))

        # Move all necessary files from temp directory to repository path
        uploader.add(MoveFiles(os.path.join(self.UPLOADER_CONFIG['Unzip'],
                                            self.UPLOADER_CONFIG['MoveTarget'] % self.version, '*'),
                               os.path.join(Repository.DIR, self.get_path('version'))))

        return uploader

    def find_latest_version(self):
        response = urllib2.urlopen(self.UPLOADER_CONFIG['latest']['url'])
        m = re.search(self.UPLOADER_CONFIG['latest']['format'], response.read())

        if not m.group(1):
            raise Exception('Can not resolve latest library version')

        return m.group(1)

class Sf(Library):
    '''
Concrete library class (Symfony 1.x updating)
Download tar.gz archive with Symfony from official web-site and
move all extracted files to repository directory
'''
    NAME = 'Symfony'
    VENDOR = 'Sensio Labs'

    # Different pathes for resolving symlinking process
    PATHES = {
        'execute': '',
        'install': 'Sf',
        'version': 'Sf/stable/%s'
    }

    UPLOADER_CONFIG = {
        'HttpFile' : 'http://www.symfony-project.org/get/symfony-%s.tgz',
        'HttpTemp' : '/tmp/sf-%s.tgz',
        'MoveTarget': 'symfony-%s',
        'Unzip' : '/tmp/',
        # Configuration for resolving latest available version
        'latest' : { 'url': 'http://www.symfony-project.org/installation',
                        'format': r'\/get\/symfony\-1\.(.+?)\.tgz' }
    }

    def uploader(self):
        uploader = UploaderChain()

        # Upload file from Zend server
        dest = self.UPLOADER_CONFIG['HttpTemp'] % self.version
        uploader.add(HttpFileUploader(self.UPLOADER_CONFIG['HttpFile'] % self.version, dest))

        # Unzipping all files in /tmp directory
        uploader.add(TarFileExtract(dest, self.UPLOADER_CONFIG['Unzip']))

        # Move all necessary files from temp directory to repository path
        uploader.add(MoveFiles(os.path.join(self.UPLOADER_CONFIG['Unzip'],
                                            self.UPLOADER_CONFIG['MoveTarget'] % self.version, '*'),
                               os.path.join(Repository.DIR, self.get_path('version'))))

        return uploader

    def find_latest_version(self):
        response = urllib2.urlopen(self.UPLOADER_CONFIG['latest']['url']).read()
        m = re.compile(self.UPLOADER_CONFIG['latest']['format']).findall(response)

        return '1.' + m[-1]

class Zfcompress(Library):
    NAME = 'ZFCompress View Helpers'
    VENDOR = 'Alexey S. Kachayev'

    # Different pathes for resolving symlinking process
    PATHES = {
        'execute': 'library/Dm',
        'install': 'Zfc',
        'version': 'Zfc/master/'
    }

    UPLOADER_CONFIG = {
        'repo' : 'git://github.com/kachayev/zend-fw-head-compressor.git'
    }

    def uploader(self):
        uploader = UploaderChain()
        uploader.add(GitUploader(self.UPLOADER_CONFIG['repo'],
                                 os.path.join(Repository.DIR, self.get_path('version'))))

        return uploader

    def find_latest_version(self):
        return 'master'

class FeatureNotImplemented(Exception):
    pass
Something went wrong with that request. Please try again.