Cache the file only if not yet cached !
[confparser-old] / install
1 #!/bin/env python
2 # -*- coding: iso-8859-1 -*-
3
4 #
5 # TuxeeNet installer
6 # Copyright (C) 2005  Frédéric Jolliton <frederic@jolliton.com>
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 #
22
23 #--[ Configuration ]----------------------------------------------------------
24
25 from install_conf import *
26
27 #--[ Global variables ]-------------------------------------------------------
28
29 g_verbose = False
30 g_pretend = False
31
32 #-----------------------------------------------------------------------------
33
34 import sys
35 import os
36 import errno
37 import py_compile
38
39 from getopt import GetoptError
40 try :
41         from getopt import gnu_getopt as getopt
42 except :
43         from getopt import getopt
44
45 from distutils.sysconfig import get_python_lib
46
47 def touch( filename ) :
48
49         open( filename , 'a+' ).close()
50
51 def createFile( filename , contents ) :
52
53         f = open( filename , 'w' )
54         f.write( contents )
55         f.close()
56
57 def byteCompile( filename ) :
58
59         if g_verbose :
60                 print 'Byte-compiling %s' % filename
61         py_compile.compile( filename )
62
63 def installPath() :
64
65         '''Install Python path.'''
66
67         path = get_python_lib()
68         path += '/' + g_globalModule + '.pth'
69         target = g_targetPrefix
70         if g_verbose or g_pretend :
71                 print 'Creating Python path from %s to %s.' %  ( path , target )
72         if not g_pretend :
73                 try :
74                         createFile( path , target )
75                 except IOError , e :
76                         print '** Error creating Python path into %s' % path
77                         print e
78
79 def installModule() :
80
81         '''Install modules.'''
82
83         myPrefix = g_targetPrefix + '/' + g_globalModule
84
85         if g_verbose or g_pretend :
86                 print 'Creating directory %s' % myPrefix
87         if not g_pretend :
88                 try :
89                         os.makedirs( myPrefix )
90                 except OSError , e :
91                         if e[ 0 ] == errno.EEXIST :
92                                 pass
93                         else :
94                                 raise
95
96         if g_verbose or g_pretend :
97                 print 'Creating module %s' % g_globalModule
98         if not g_pretend :
99                 p = myPrefix + '/__init__.py'
100                 try :
101                         touch( p )
102                 except IOError :
103                         print '** Error touching %s' % p
104                 else :
105                         byteCompile( p )
106         for module in g_modules :
107                 if g_verbose or g_pretend :
108                         print 'Copying module %s -> %s' % ( module , myPrefix + '/' )
109                 if not g_pretend :
110                         mod = open( module ).read()
111                         try :
112                                 createFile( myPrefix + '/' + module , mod )
113                         except IOError , e :
114                                 print '** Error copying %s -> %s' % ( module , myPrefix + '/' )
115                                 print e
116                         else :
117                                 byteCompile( myPrefix + '/' + module )
118
119 def usage() :
120
121         print '''Usage: install [OPTIONS]
122
123  -h, --help     Print this help.
124  -n, --pretend  Display operations, but do nothing.
125  -v, --verbose  Verbose output.
126      --pth      Install Python path (.pth) only.
127
128 Report bugs to <fj@tuxee.net>.'''
129
130 def main() :
131
132         global g_verbose
133         global g_pretend
134
135         try :
136                 options , paramaters = getopt( sys.argv[ 1 : ] ,
137                         'hnv' , ( 'help' , 'pth' , 'pretend' , 'verbose' ) )
138         except GetoptError , e :
139                 print 'install:' , e
140                 print 'Try `install --help\' for more information.'
141                 sys.exit( 1 )
142
143         pathOnly = False
144
145         for option , argument in options :
146                 if option in [ '-h' , '--help' ] :
147                         usage()
148                         sys.exit( 0 )
149                 elif option in [ '-v' , '--verbose' ] :
150                         g_verbose = True
151                 elif option in [ '-n' , '--pretend' ] :
152                         g_pretend = True
153                 elif option in [ '--pth' ] :
154                         pathOnly = True
155
156         installPath()
157         if not pathOnly :
158                 installModule()
159
160 if __name__ == '__main__' :
161         main()