1 | """ |
---|
2 | Various utilities for scrambling. |
---|
3 | """ |
---|
4 | import os, sys, errno, re, distutils.util, glob, shutil, subprocess, tarfile, zipfile |
---|
5 | from distutils.sysconfig import get_config_var, get_config_vars |
---|
6 | |
---|
7 | try: |
---|
8 | import zlib |
---|
9 | except: |
---|
10 | raise Exception( 'Cannot import zlib, which must exist to build eggs. If your python interpreter is truly missing it, you will need to recompile or (on supported platforms) download a binary version from python.org.' ) |
---|
11 | |
---|
12 | def get_tag(): |
---|
13 | try: |
---|
14 | return open( '.galaxy_tag', 'r' ).read().strip() |
---|
15 | except: |
---|
16 | return None |
---|
17 | |
---|
18 | def get_ver(): |
---|
19 | try: |
---|
20 | return open( '.galaxy_ver', 'r' ).read().strip() |
---|
21 | except: |
---|
22 | return None |
---|
23 | |
---|
24 | def get_deps(): |
---|
25 | try: |
---|
26 | depf = open( '.galaxy_deps', 'r' ) |
---|
27 | except: |
---|
28 | return [] |
---|
29 | c = eggs.Crate() |
---|
30 | for dep in depf: |
---|
31 | c[dep.strip()].require() |
---|
32 | |
---|
33 | def clean( extra_dirs=[] ): |
---|
34 | for dir in [ 'build', 'dist' ] + extra_dirs: |
---|
35 | try: |
---|
36 | shutil.rmtree( dir ) |
---|
37 | except OSError, e: |
---|
38 | if e.errno != errno.ENOENT: |
---|
39 | raise |
---|
40 | |
---|
41 | def apply_patches(): |
---|
42 | name = os.path.basename( os.getcwd() ) |
---|
43 | for file in glob.glob( os.path.join( patches, name, '*' ) ): |
---|
44 | shutil.copy( file, os.path.basename( file ) ) |
---|
45 | |
---|
46 | def get_archive( base ): |
---|
47 | for arctype in ( 'tar.gz', 'tgz', 'zip' ): |
---|
48 | archive = '.'.join( ( base, arctype ) ) |
---|
49 | if os.path.exists( archive ): |
---|
50 | return archive |
---|
51 | else: |
---|
52 | raise Exception( "%s(): Couldn't find a suitable archive for %s in %s" % ( sys._getframe().f_code.co_name, os.path.basename( base ), archives ) ) |
---|
53 | |
---|
54 | def compress( path, *files ): |
---|
55 | tarcf( path, *files ) |
---|
56 | |
---|
57 | def uncompress( path ): |
---|
58 | if path.endswith( '.zip' ): |
---|
59 | unzip( path ) |
---|
60 | else: |
---|
61 | tarxf( path ) |
---|
62 | |
---|
63 | def unzip( zipf ): |
---|
64 | z = zipfile.ZipFile( zipf ) |
---|
65 | try: |
---|
66 | for info in z.infolist(): |
---|
67 | name = info.filename |
---|
68 | mode = (info.external_attr >> 16L) & 0777 |
---|
69 | # don't extract absolute paths or ones with .. in them |
---|
70 | if name.startswith('/') or '..' in name: |
---|
71 | continue |
---|
72 | if not name: |
---|
73 | continue |
---|
74 | if name.endswith('/'): |
---|
75 | # directory |
---|
76 | pkg_resources.ensure_directory(name) |
---|
77 | else: |
---|
78 | # file |
---|
79 | pkg_resources.ensure_directory(name) |
---|
80 | data = z.read(info.filename) |
---|
81 | f = open(name,'wb') |
---|
82 | try: |
---|
83 | f.write(data) |
---|
84 | finally: |
---|
85 | f.close() |
---|
86 | del data |
---|
87 | try: |
---|
88 | if not os.path.islink( name ): |
---|
89 | os.chmod(name,mode) |
---|
90 | except: |
---|
91 | pass |
---|
92 | finally: |
---|
93 | z.close() |
---|
94 | |
---|
95 | def tarxf( ball ): |
---|
96 | t = tarfile.open( ball, 'r' ) |
---|
97 | for fn in t.getnames(): |
---|
98 | t.extract( fn ) |
---|
99 | t.close() |
---|
100 | |
---|
101 | def tarcf( ball, *files ): |
---|
102 | if ball.endswith( '.gz' ): |
---|
103 | t = tarfile.open( ball, 'w:gz' ) |
---|
104 | elif ball.endswith( '.bz2' ): |
---|
105 | t = tarfile.open( ball, 'w:bz2' ) |
---|
106 | else: |
---|
107 | t = tarfile.open( ball, 'w' ) |
---|
108 | for file in files: |
---|
109 | t.add( file ) |
---|
110 | t.close() |
---|
111 | |
---|
112 | def run( cmd, d, txt ): |
---|
113 | p = subprocess.Popen( args = cmd, shell = True, cwd = d ) |
---|
114 | r = p.wait() |
---|
115 | if r != 0: |
---|
116 | print '%s(): %s failed' % ( sys._getframe().f_code.co_name, txt ) |
---|
117 | sys.exit( 1 ) |
---|
118 | |
---|
119 | def unpack_dep( source, prepped, builder, args={} ): |
---|
120 | if prepped is not None and os.path.exists( prepped ): |
---|
121 | print "%s(): Prepared dependency already exists at the following path, remove to force re-prep:" % sys._getframe().f_code.co_name |
---|
122 | print " ", prepped |
---|
123 | uncompress( prepped ) |
---|
124 | else: |
---|
125 | print "%s(): Prepared dependency does not exist, preparing now from source:" % sys._getframe().f_code.co_name |
---|
126 | print " ", source |
---|
127 | uncompress( source ) |
---|
128 | builder( prepped, args ) |
---|
129 | |
---|
130 | def get_solaris_compiler(): |
---|
131 | p = subprocess.Popen( '%s -V 2>&1' % get_config_var('CC'), shell = True, stdout = subprocess.PIPE ) |
---|
132 | out = p.stdout.read() |
---|
133 | p.wait() |
---|
134 | if 'Sun C' in out: |
---|
135 | return 'cc' |
---|
136 | else: |
---|
137 | return 'gcc' |
---|
138 | |
---|
139 | # get galaxy eggs lib |
---|
140 | galaxy_lib = os.path.abspath( os.path.join( os.path.dirname( __file__ ), '..', '..', '..', 'lib' ) ) |
---|
141 | sys.path.insert( 0, galaxy_lib ) |
---|
142 | from galaxy import eggs |
---|
143 | |
---|
144 | # get setuptools |
---|
145 | try: |
---|
146 | from setuptools import * |
---|
147 | import pkg_resources |
---|
148 | except ImportError: |
---|
149 | from ez_setup import use_setuptools |
---|
150 | use_setuptools( download_delay=8, to_dir=os.path.dirname( __file__ ) ) |
---|
151 | from setuptools import * |
---|
152 | import pkg_resources |
---|
153 | |
---|
154 | # some constants |
---|
155 | root = os.path.abspath( os.path.join( os.path.dirname( __file__ ), '..' ) ) |
---|
156 | archives = os.path.abspath( os.path.join( root, 'archives' ) ) |
---|
157 | patches = os.path.abspath( os.path.join( root, 'patches' ) ) |
---|
158 | platform_noucs = pkg_resources.get_platform().rsplit( '-', 1 )[0] |
---|