install_data.py
7.46 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
196
197
# -*- coding: utf-8 -*-
"""install_data.py
Provides a more sophisticated facility to install data files
than distutils' install_data does.
You can specify your files as a template like in MANIFEST.in
and you have more control over the copy process.
Copyright 2000 by Rene Liebscher, Germany.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
# created 2000/08/01, Rene Liebscher <R.Liebscher@gmx.de>
# modified 2000/12/18, Martin v. Löwis <loewis@informatik.hu-berlin.de>
###########################################################################
# import some modules we need
import os,sys,string
from types import StringType,TupleType,ListType
from distutils.util import change_root
from distutils.filelist import FileList
from distutils.command.install_data import install_data
###########################################################################
# a container class for our more sophisticated install mechanism
class Data_Files:
""" container for list of data files.
supports alternate base_dirs e.g. 'install_lib','install_header',...
supports a directory where to copy files
supports templates as in MANIFEST.in
supports preserving of paths in filenames
eg. foo/xyz is copied to base_dir/foo/xyz
supports stripping of leading dirs of source paths
eg. foo/bar1/xyz, foo/bar2/abc can be copied to bar1/xyz, bar2/abc
"""
def __init__(self,base_dir=None,files=None,copy_to=None,template=None,preserve_path=0,strip_dirs=0):
self.base_dir = base_dir
self.files = files
self.copy_to = copy_to
if template is not None:
t = []
for item in template:
item = string.strip(item)
if not item:continue
t.append(item)
template = t
self.template = template
self.preserve_path = preserve_path
self.strip_dirs = strip_dirs
self.finalized = 0
def warn (self, msg):
sys.stderr.write ("warning: %s: %s\n" %
("install_data", msg))
def debug_print (self, msg):
"""Print 'msg' to stdout if the global DEBUG (taken from the
DISTUTILS_DEBUG environment variable) flag is true.
"""
from distutils.core import DEBUG
if DEBUG:
print msg
def finalize(self):
""" complete the files list by processing the given template """
if self.finalized:
return
if self.files == None:
self.files = []
if self.template != None:
if type(self.template) == StringType:
self.template = string.split(self.template,";")
filelist = FileList(self.warn,self.debug_print)
for line in self.template:
filelist.process_template_line(string.strip(line))
filelist.sort()
filelist.remove_duplicates()
self.files.extend(filelist.files)
self.finalized = 1
# end class Data_Files
###########################################################################
# a more sophisticated install routine than distutils install_data
class install_Data_Files (install_data):
def check_data(self,d):
""" check if data are in new format, if not create a suitable object.
returns finalized data object
"""
if not isinstance(d, Data_Files):
self.warn(("old-style data files list found "
"-- please convert to Data_Files instance"))
if type(d) is TupleType:
if len(d) != 2 or not (type(d[1]) is ListType):
raise DistutilsSetupError, \
("each element of 'data_files' option must be an "
"Data File instance, a string or 2-tuple (string,[strings])")
d = Data_Files(copy_to=d[0],files=d[1])
else:
if not (type(d) is StringType):
raise DistutilsSetupError, \
("each element of 'data_files' option must be an "
"Data File instance, a string or 2-tuple (string,[strings])")
d = Data_Files(files=[d])
d.finalize()
return d
def run(self):
self.outfiles = []
install_cmd = self.get_finalized_command('install')
for d in self.data_files:
d = self.check_data(d)
install_dir = self.install_dir
# alternative base dir given => overwrite install_dir
if d.base_dir != None:
install_dir = getattr(install_cmd,d.base_dir)
# copy to an other directory
if d.copy_to != None:
if not os.path.isabs(d.copy_to):
# relatiev path to install_dir
dir = os.path.join(install_dir, d.copy_to)
elif install_cmd.root:
# absolute path and alternative root set
dir = change_root(self.root,d.copy_to)
else:
# absolute path
dir = d.copy_to
else:
# simply copy to install_dir
dir = install_dir
# warn if necceassary
self.warn("setup script did not provide a directory to copy files to "
" -- installing right in '%s'" % install_dir)
dir=os.path.normpath(dir)
# create path
self.mkpath(dir)
# copy all files
for src in d.files:
if d.strip_dirs > 0:
dst = string.join(string.split(os.path.normcase(src),os.sep)[d.strip_dirs:],os.sep)
else:
dst = src
if d.preserve_path:
# preserve path in filename
self.mkpath(os.path.dirname(os.path.join(dir,dst)))
out = self.copy_file(src, os.path.join(dir,dst))
else:
out = self.copy_file(src, dir)
if type(out) is TupleType:
out = out[0]
self.outfiles.append(out)
return self.outfiles
def get_inputs (self):
inputs = []
for d in self.data_files:
d = self.check_data(d)
inputs.append(d.files)
return inputs
def get_outputs (self):
return self.outfiles
###########################################################################