annotate data_generation/transformations/pycaptcha/setup/my_install_data.py @ 612:21d53fd07f6e

reviews AISTATS
author Yoshua Bengio <bengioy@iro.umontreal.ca>
date Mon, 20 Dec 2010 11:54:35 -0500
parents 1f5937e9e530
children
rev   line source
87
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
1 """my_install_data.py
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
2
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
3 Provides a more sophisticated facility to install data files
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
4 than distutils' install_data does.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
5 You can specify your files as a template like in MANIFEST.in
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
6 and you have more control over the copy process.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
7
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
8 Copyright 2000 by Rene Liebscher, Germany.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
9
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
10 Permission is hereby granted, free of charge, to any person obtaining
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
11 a copy of this software and associated documentation files (the
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
12 "Software"), to deal in the Software without restriction, including
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
13 without limitation the rights to use, copy, modify, merge, publish,
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
14 distribute, sublicense, and/or sell copies of the Software, and to
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
15 permit persons to whom the Software is furnished to do so, subject to
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
16 the following conditions:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
17
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
18 The above copyright notice and this permission notice shall be included
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
19 in all copies or substantial portions of the Software.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
20
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
25 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
26 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
27 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
28
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
29 Note:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
30 This licence is only for this file.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
31 PyOpenGL has its own licence. (it is almost identical.)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
32 """
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
33
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
34 # created 2000/08/01, Rene Liebscher <R.Liebscher@gmx.de>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
35
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
36 ###########################################################################
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
37 # import some modules we need
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
38
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
39 import os,sys,string
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
40 from types import StringType,TupleType,ListType
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
41 from distutils.util import change_root
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
42 from distutils.filelist import FileList
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
43 from distutils.command.install_data import install_data
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
44
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
45 ###########################################################################
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
46 # a container class for our more sophisticated install mechanism
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
47
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
48 class Data_Files:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
49 """ container for list of data files.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
50 supports alternate base_dirs e.g. 'install_lib','install_header',...
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
51 supports a directory where to copy files
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
52 supports templates as in MANIFEST.in
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
53 supports preserving of paths in filenames
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
54 eg. foo/xyz is copied to base_dir/foo/xyz
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
55 supports stripping of leading dirs of source paths
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
56 eg. foo/bar1/xyz, foo/bar2/abc can be copied to bar1/xyz, bar2/abc
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
57 """
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
58
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
59 def __init__(self,base_dir=None,files=None,copy_to=None,template=None,preserve_path=0,strip_dirs=0):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
60 self.base_dir = base_dir
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
61 self.files = files
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
62 self.copy_to = copy_to
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
63 self.template = template
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
64 self.preserve_path = preserve_path
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
65 self.strip_dirs = strip_dirs
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
66 self.finalized = 0
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
67
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
68 def warn (self, msg):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
69 sys.stderr.write ("warning: %s: %s\n" %
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
70 ("install_data", msg))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
71
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
72 def debug_print (self, msg):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
73 """Print 'msg' to stdout if the global DEBUG (taken from the
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
74 DISTUTILS_DEBUG environment variable) flag is true.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
75 """
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
76 from distutils.core import DEBUG
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
77 if DEBUG:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
78 print msg
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
79
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
80
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
81 def finalize(self):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
82 """ complete the files list by processing the given template """
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
83 if self.finalized:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
84 return
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
85 if self.files == None:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
86 self.files = []
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
87 if self.template != None:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
88 if type(self.template) == StringType:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
89 self.template = string.split(self.template,";")
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
90 filelist = FileList(self.warn,self.debug_print)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
91 for line in self.template:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
92 filelist.process_template_line(string.strip(line))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
93 filelist.sort()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
94 filelist.remove_duplicates()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
95 self.files.extend(filelist.files)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
96 self.finalized = 1
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
97
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
98 # end class Data_Files
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
99
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
100 ###########################################################################
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
101 # a more sophisticated install routine than distutils install_data
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
102
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
103 class my_install_data (install_data):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
104
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
105 def check_data(self,d):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
106 """ check if data are in new format, if not create a suitable object.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
107 returns finalized data object
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
108 """
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
109 if not isinstance(d, Data_Files):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
110 self.warn(("old-style data files list found "
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
111 "-- please convert to Data_Files instance"))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
112 if type(d) is TupleType:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
113 if len(d) != 2 or not (type(d[1]) is ListType):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
114 raise DistutilsSetupError, \
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
115 ("each element of 'data_files' option must be an "
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
116 "Data File instance, a string or 2-tuple (string,[strings])")
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
117 d = Data_Files(copy_to=d[0],files=d[1])
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
118 else:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
119 if not (type(d) is StringType):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
120 raise DistutilsSetupError, \
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
121 ("each element of 'data_files' option must be an "
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
122 "Data File instance, a string or 2-tuple (string,[strings])")
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
123 d = Data_Files(files=[d])
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
124 d.finalize()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
125 return d
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
126
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
127 def run(self):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
128 self.outfiles = []
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
129 install_cmd = self.get_finalized_command('install')
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
130
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
131 for d in self.data_files:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
132 d = self.check_data(d)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
133
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
134 install_dir = self.install_dir
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
135 # alternative base dir given => overwrite install_dir
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
136 if d.base_dir != None:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
137 install_dir = getattr(install_cmd,d.base_dir)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
138
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
139 # copy to an other directory
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
140 if d.copy_to != None:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
141 if not os.path.isabs(d.copy_to):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
142 # relatiev path to install_dir
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
143 dir = os.path.join(install_dir, d.copy_to)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
144 elif install_cmd.root:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
145 # absolute path and alternative root set
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
146 dir = change_root(self.root,d.copy_to)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
147 else:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
148 # absolute path
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
149 dir = d.copy_to
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
150 else:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
151 # simply copy to install_dir
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
152 dir = install_dir
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
153 # warn if necceassary
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
154 self.warn("setup script did not provide a directory to copy files to "
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
155 " -- installing right in '%s'" % install_dir)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
156
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
157 dir=os.path.normpath(dir)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
158 # create path
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
159 self.mkpath(dir)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
160
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
161 # copy all files
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
162 for src in d.files:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
163 if d.strip_dirs > 0:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
164 dst = string.join(string.split(src,os.sep)[d.strip_dirs:],os.sep)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
165 else:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
166 dst = src
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
167 if d.preserve_path:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
168 # preserve path in filename
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
169 self.mkpath(os.path.dirname(os.path.join(dir,dst)))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
170 out = self.copy_file(src, os.path.join(dir,dst))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
171 else:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
172 out = self.copy_file(src, dir)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
173 if type(out) is TupleType:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
174 out = out[0]
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
175 self.outfiles.append(out)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
176
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
177 return self.outfiles
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
178
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
179 def get_inputs (self):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
180 inputs = []
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
181 for d in self.data_files:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
182 d = self.check_data(d)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
183 inputs.append(d.files)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
184 return inputs
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
185
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
186 def get_outputs (self):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
187 return self.outfiles
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
188
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
189
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
190 ###########################################################################
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
191