Package _setup :: Module data
[hide private]
[frames] | no frames]

Source Code for Module _setup.data

  1  # -*- coding: ascii -*- 
  2  # 
  3  # Copyright 2007, 2008, 2009, 2010 
  4  # Andr\xe9 Malo or his licensors, as applicable 
  5  # 
  6  # Licensed under the Apache License, Version 2.0 (the "License"); 
  7  # you may not use this file except in compliance with the License. 
  8  # You may obtain a copy of the License at 
  9  # 
 10  #     http://www.apache.org/licenses/LICENSE-2.0 
 11  # 
 12  # Unless required by applicable law or agreed to in writing, software 
 13  # distributed under the License is distributed on an "AS IS" BASIS, 
 14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 15  # See the License for the specific language governing permissions and 
 16  # limitations under the License. 
 17  """ 
 18  =================== 
 19   Data distribution 
 20  =================== 
 21   
 22  This module provides tools to simplify data distribution. 
 23  """ 
 24  __author__ = u"Andr\xe9 Malo" 
 25  __docformat__ = "restructuredtext en" 
 26   
 27  from distutils import filelist as _filelist 
 28  from distutils.command import install_data as _install_data 
 29  from distutils.command import install as _install 
 30  import os as _os 
 31  import posixpath as _posixpath 
 32  import sys as _sys 
 33   
 34   
35 -def splitpath(path):
36 """ Split a path """ 37 drive, path = '', _os.path.normpath(path) 38 try: 39 splitunc = _os.path.splitunc 40 except AttributeError: 41 pass 42 else: 43 drive, path = splitunc(path) 44 if not drive: 45 drive, path = _os.path.splitdrive(path) 46 elems = [] 47 try: 48 sep = _os.path.sep 49 except AttributeError: 50 sep = _os.path.join('1', '2')[1:-1] 51 while 1: 52 prefix, path = _os.path.split(path) 53 elems.append(path) 54 if prefix in ('', sep): 55 drive = _os.path.join(drive, prefix) 56 break 57 path = prefix 58 elems.reverse() 59 return drive, elems
60 61
62 -class Install(_install.install):
63 """ Extended installer to reflect the additional data options """ 64 user_options = _install.install.user_options + [ 65 ('no-install-docs', None, 66 "do not install the documentation files and samples"), 67 ('no-replace', None, 68 "do not replace the placeholders in the sample files"), 69 ] 70 boolean_options = _install.install.boolean_options + [ 71 'no-install-docs', 'no-replace', 72 ] 73
74 - def initialize_options(self):
75 """ Prepare for new options """ 76 # pylint: disable = W0201 77 _install.install.initialize_options(self) 78 self.no_install_docs = 0 79 self.no_replace = 0
80 81
82 -class InstallData(_install_data.install_data):
83 """ Extended data installer """ 84
85 - def finalize_options(self):
86 """ Flatten Data objects to plain files """ 87 _install_data.install_data.finalize_options(self) 88 data_files = [] 89 for item in self.data_files: 90 if not isinstance(item, Data): 91 data_files.append(item) 92 continue 93 data_files.extend(item.flatten(self)) 94 self.data_files = data_files
95 96
97 -class Data(object):
98 """ File list container """ 99 __BASES = ("base", "platbase", "purelib", "platlib", "headers", "lib", 100 "scripts", "data") 101
102 - def __init__(self, files, target=None, preserve=0, strip=0, 103 prefix=None):
104 """ Initialization """ 105 self._files = files 106 self._target = target 107 self._preserve = preserve 108 self._strip = strip 109 self._prefix = prefix
110
111 - def from_templates(cls, *templates, **kwargs):
112 """ Initialize from template """ 113 files = _filelist.FileList() 114 for tpl in templates: 115 for line in tpl.split(';'): 116 files.process_template_line(line.strip()) 117 files.sort() 118 files.remove_duplicates() 119 result = [] 120 for filename in files.files: 121 _, elems = splitpath(filename) 122 if '.svn' in elems: 123 continue 124 result.append(filename) 125 return cls(result, **kwargs)
126 from_templates = classmethod(from_templates) 127
128 - def flatten(self, installer):
129 """ Flatten the file list to (target, file) tuples """ 130 # pylint: disable = W0613 131 if self._prefix: 132 _, prefix = splitpath(self._prefix) 133 telems = prefix 134 else: 135 telems = [] 136 137 tmap = {} 138 for fname in self._files: 139 (_, name), target = splitpath(fname), telems 140 if self._preserve: 141 if self._strip: 142 name = name[max(0, min(self._strip, len(name) - 1)):] 143 if len(name) > 1: 144 target = telems + name[:-1] 145 tmap.setdefault(_posixpath.join(*target), []).append(fname) 146 return tmap.items()
147 148
149 -class Documentation(Data):
150 """ Documentation container """ 151
152 - def flatten(self, installer):
153 """ Check if docs should be installed at all """ 154 install = installer.get_finalized_command('install') 155 if install.no_install_docs: 156 return [] 157 return Data.flatten(self, installer)
158 159
160 -class Manpages(Documentation):
161 """ Manpages container """ 162
163 - def dispatch(cls, files):
164 """ Automatically dispatch manpages to their target directories """ 165 mpmap = {} 166 for manpage in files: 167 normalized = _os.path.normpath(manpage) 168 _, ext = _os.path.splitext(normalized) 169 if ext.startswith(_os.path.extsep): 170 ext = ext[len(_os.path.extsep):] 171 mpmap.setdefault(ext, []).append(manpage) 172 return [cls(manpages, prefix=_posixpath.join( 173 'share', 'man', 'man%s' % section, 174 )) for section, manpages in mpmap.items()]
175 dispatch = classmethod(dispatch) 176
177 - def flatten(self, installer):
178 """ Check if manpages are suitable """ 179 if _sys.platform == 'win32': 180 return [] 181 return Documentation.flatten(self, installer)
182