root/galaxy-central/scripts/egg_packager_template.py

リビジョン 2, 7.9 KB (コミッタ: hatakeyama, 14 年 前)

import galaxy-central

行番号 
1#!/usr/bin/env python
2
3# Configure stdout logging
4
5import os, sys, logging, glob, zipfile, shutil
6
7log = logging.getLogger()
8log.setLevel( 10 )
9log.addHandler( logging.StreamHandler( sys.stdout ) )
10
11# Fake pkg_resources
12
13import re
14
15macosVersionString = re.compile(r"macosx-(\d+)\.(\d+)-(.*)")
16darwinVersionString = re.compile(r"darwin-(\d+)\.(\d+)\.(\d+)-(.*)")
17solarisVersionString = re.compile(r"solaris-(\d)\.(\d+)-(.*)")
18
19def compatible_platforms(provided,required):
20    """Can code for the `provided` platform run on the `required` platform?
21
22    Returns true if either platform is ``None``, or the platforms are equal.
23
24    XXX Needs compatibility checks for Linux and other unixy OSes.
25    """
26    if provided is None or required is None or provided==required:
27        return True     # easy case
28
29    # Mac OS X special cases
30    reqMac = macosVersionString.match(required)
31    if reqMac:
32        provMac = macosVersionString.match(provided)
33
34        # is this a Mac package?
35        if not provMac:
36            # this is backwards compatibility for packages built before
37            # setuptools 0.6. All packages built after this point will
38            # use the new macosx designation.
39            provDarwin = darwinVersionString.match(provided)
40            if provDarwin:
41                dversion = int(provDarwin.group(1))
42                macosversion = "%s.%s" % (reqMac.group(1), reqMac.group(2))
43                if dversion == 7 and macosversion >= "10.3" or \
44                    dversion == 8 and macosversion >= "10.4":
45
46                    #import warnings
47                    #warnings.warn("Mac eggs should be rebuilt to "
48                    #    "use the macosx designation instead of darwin.",
49                    #    category=DeprecationWarning)
50                    return True
51            return False    # egg isn't macosx or legacy darwin
52
53        # are they the same major version and machine type?
54        if provMac.group(1) != reqMac.group(1) or \
55            provMac.group(3) != reqMac.group(3):
56            return False
57
58
59
60        # is the required OS major update >= the provided one?
61        if int(provMac.group(2)) > int(reqMac.group(2)):
62            return False
63
64        return True
65
66    # Solaris' special cases
67    reqSol = solarisVersionString.match(required)
68    if reqSol:
69        provSol = solarisVersionString.match(provided)
70        if not provSol:
71            return False
72        if provSol.group(1) != reqSol.group(1) or \
73            provSol.group(3) != reqSol.group(3):
74            return False
75        if int(provSol.group(2)) > int(reqSol.group(2)):
76            return False
77        return True
78
79    # XXX Linux and other platforms' special cases should go here
80    return False
81
82EGG_NAME = re.compile(
83    r"(?P<name>[^-]+)"
84    r"( -(?P<ver>[^-]+) (-py(?P<pyver>[^-]+) (-(?P<plat>.+))? )? )?",
85    re.VERBOSE | re.IGNORECASE
86).match
87
88class Distribution( object ):
89    def __init__( self, egg_name, project_name, version, py_version, platform ):
90        self._egg_name = egg_name
91        self.project_name = project_name
92        if project_name is not None:
93            self.project_name = project_name.replace( '-', '_' )
94        self.version = version
95        if version is not None:
96            self.version = version.replace( '-', '_' )
97        self.py_version = py_version
98        self.platform = platform
99        self.location = os.path.join( tmpd, egg_name ) + '.egg'
100    def egg_name( self ):
101        return self._egg_name
102    @classmethod
103    def from_filename( cls, basename ):
104        project_name, version, py_version, platform = [None]*4
105        basename, ext = os.path.splitext(basename)
106        if ext.lower() == '.egg':
107            match = EGG_NAME( basename )
108            if match:
109                project_name, version, py_version, platform = match.group( 'name','ver','pyver','plat' )
110        return cls( basename, project_name, version, py_version, platform )
111
112class pkg_resources( object ):
113    pass
114
115pkg_resources.Distribution = Distribution
116
117# Fake galaxy.eggs
118
119env = None
120def get_env():
121    return None
122
123import urllib, urllib2, HTMLParser
124class URLRetriever( urllib.FancyURLopener ):
125    def http_error_default( *args ):
126        urllib.URLopener.http_error_default( *args )
127
128class Egg( object ):
129    def __init__( self, distribution ):
130        self.url = url + '/' + distribution.project_name.replace( '-', '_' )
131        self.dir = tmpd
132        self.distribution = distribution
133    def set_distribution( self ):
134        pass
135    def unpack_if_needed( self ):
136        pass
137    def remove_doppelgangers( self ):
138        pass
139    def fetch( self, requirement ):
140        """
141        fetch() serves as the install method to pkg_resources.working_set.resolve()
142        """
143        def find_alternative():
144            """
145            Some platforms (e.g. Solaris) support eggs compiled on older platforms
146            """
147            class LinkParser( HTMLParser.HTMLParser ):
148                """
149                Finds links in what should be an Apache-style directory index
150                """
151                def __init__( self ):
152                    HTMLParser.HTMLParser.__init__( self )
153                    self.links = []
154                def handle_starttag( self, tag, attrs ):
155                    if tag == 'a' and 'href' in dict( attrs ):
156                        self.links.append( dict( attrs )['href'] )
157            parser = LinkParser()
158            try:
159                parser.feed( urllib2.urlopen( self.url + '/' ).read() )
160            except urllib2.HTTPError, e:
161                if e.code == 404:
162                    return None
163            parser.close()
164            for link in parser.links:
165                file = urllib.unquote( link ).rsplit( '/', 1 )[-1]
166                tmp_dist = pkg_resources.Distribution.from_filename( file )
167                if tmp_dist.platform is not None and \
168                        self.distribution.project_name == tmp_dist.project_name and \
169                        self.distribution.version == tmp_dist.version and \
170                        self.distribution.py_version == tmp_dist.py_version and \
171                        compatible_platforms( tmp_dist.platform, self.distribution.platform ):
172                    return file
173            return None
174        if self.url is None:
175            return None
176        alternative = None
177        try:
178            url = self.url + '/' + self.distribution.egg_name() + '.egg'
179            URLRetriever().retrieve( url, self.distribution.location )
180            log.debug( "Fetched %s" % url )
181        except IOError, e:
182            if e[1] == 404 and self.distribution.platform != py:
183                alternative = find_alternative()
184                if alternative is None:
185                    return None
186            else:
187                return None
188        if alternative is not None:
189            try:
190                url = '/'.join( ( self.url, alternative ) )
191                URLRetriever().retrieve( url, os.path.join( self.dir, alternative ) )
192                log.debug( "Fetched %s" % url )
193            except IOError, e:
194                return None
195            self.platform = alternative.split( '-', 2 )[-1].rsplit( '.egg', 1 )[0]
196            self.set_distribution()
197        self.unpack_if_needed()
198        self.remove_doppelgangers()
199        global env
200        env = get_env() # reset the global Environment object now that we've obtained a new egg
201        return self.distribution
202
203def create_zip():
204    fname = 'galaxy_eggs-%s.zip' % platform
205    z = zipfile.ZipFile( fname, 'w', zipfile.ZIP_STORED )
206    for egg in glob.glob( os.path.join( tmpd, '*.egg' ) ):
207        z.write( egg, 'eggs/' + os.path.basename( egg ) )
208    z.close()
209    print 'Egg package is in %s' % fname
210    print "To install the eggs, please copy this file to your Galaxy installation's root"
211    print "directory and unpack with:"
212    print "  unzip %s" % fname
213
214def clean():
215    shutil.rmtree( tmpd )
216
217import tempfile
218tmpd = tempfile.mkdtemp()
219
220failures = []
221
222# Automatically generated egg definitions follow
223
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。