/usr/lib/python2.7/dist-packages/kiwi/dist.py is in python-kiwi 1.9.22-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | #
# Kiwi: a Framework and Enhanced Widgets for Python
#
# Copyright (C) 2005-2006 Async Open Source
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
#
# Author(s): Johan Dahlin <jdahlin@async.com.br>
#
"""Distutils extensions and utilities"""
from distutils.command.install_data import install_data
from distutils.command.install_lib import install_lib
#from distutils.core import setup as DS_setup
from setuptools import setup as DS_setup
from distutils.dep_util import newer
from distutils.log import info, warn
from distutils.sysconfig import get_python_lib
from fnmatch import fnmatch
import os
import new
import sys
class _VariableExtender:
def __init__(self, distribution):
install = distribution.get_command_obj('install')
name = distribution.get_name()
prefix = install.prefix
if not prefix:
prefix = sys.prefix
# Remove trailing /
if prefix[-1] == '/':
prefix = prefix[:-1]
self.prefix = prefix
self.datadir = os.path.join('share', name)
if self.prefix == '/usr':
self.sysconfdir = '/etc'
else:
self.sysconfdir = os.path.join('etc')
def extend(self, string, relative=False):
"""
Expand a variable.
@param string: string to replace.
@param relative: if True, assume the content of all variables
to be relative to the prefix.
"""
for name, var in [('sysconfdir', self.sysconfdir),
('datadir', self.datadir),
('prefix', self.prefix)]:
if not relative and name != 'prefix':
var = os.path.join(self.prefix, var)
string = string.replace('$' + name, var)
return string
class KiwiInstallLib(install_lib):
# Overridable by subclass
resources = {}
global_resources = {}
def _get_template(self):
return os.path.join(self.install_dir,
self.distribution.get_name(),
'__installed__.py')
def generate_template(self):
if 'bdist_wininst' in sys.argv:
prefix = 'import sys\nprefix = sys.prefix'
else:
install = self.distribution.get_command_obj('install')
# Use raw strings so UNC paths which has \\ works
prefix = 'prefix = r"%s"' % install.prefix or sys.prefix
filename = self._get_template()
self.mkpath(os.path.dirname(filename))
fp = open(filename, 'w')
fp.write('# Generated by setup.py do not modify\n')
fp.write('import os\n')
fp.write('%s\n' % prefix)
self._write_dictionary(fp, 'resources', self.resources)
self._write_dictionary(fp, 'global_resources', self.global_resources)
fp.close()
return filename
def _write_dictionary(self, fp, name, dictionary):
fp.write('%s = {}\n' % name)
for key, value in dictionary.items():
value = value.replace('/', os.sep)
value = self.varext.extend(value)
value = value.replace(self.varext.prefix, '$prefix')
parts = []
for part in value.split(os.sep):
if part == "":
part = os.sep
if part == '$prefix':
part = 'prefix'
else:
part = '"%s"' % part
parts.append(part)
fp.write("%s['%s'] = %s\n" % (
name, key, 'os.path.join(%s)' % ', '.join(parts)))
def get_outputs(self):
filename = self._get_template()
files = [filename] + self._bytecode_filenames([filename])
return install_lib.get_outputs(self) + files
def install(self):
self.varext = _VariableExtender(self.distribution)
return install_lib.install(self) + [self.generate_template()]
# Backwards compat
TemplateInstallLib = KiwiInstallLib
class KiwiInstallData(install_data):
def run(self):
self.varext = _VariableExtender(self.distribution)
# Extend variables in all data files
data_files = []
for target, files in self.data_files[:]:
data_files.append((self.varext.extend(target, True), files))
self.data_files = data_files
return install_data.run(self)
def get_site_packages_dir(*dirs):
"""
Gets the relative path of the site-packages directory
This is mainly useful for setup.py usage:
>>> setup(...
data_files=[(get_site_packages_dir('foo'),
files..)])
where files is a list of files to be installed in
a directory called foo created in your site-packages directory
@param dirs: directory names to be appended
"""
libdir = get_python_lib(plat_specific=False,
standard_lib=True, prefix='')
return os.path.join(libdir, 'site-packages', *dirs)
def listfiles(*dirs):
"""
Lists all files in directories and optionally uses basic shell
matching, example:
>>> listfiles('data', 'glade', '*.glade')
['data/glade/Foo.glade', 'data/glade/Bar.glade', ...]
@param dirs: directory parts
"""
dir, pattern = os.path.split(os.path.join(*dirs))
abspath = os.path.abspath(dir)
if not os.path.exists(abspath):
# TODO: Print a warning here?
return []
return [os.path.join(dir, filename)
for filename in os.listdir(abspath)
if filename[0] != '.' and fnmatch(filename, pattern)]
def compile_po_files(domain, dirname='locale'):
"""
Compiles po files to mo files.
Note. this function depends on gettext utilities being installed
@param domain: gettext domain
@param dirname: base directory
@returns: a list of po files
"""
if os.system('msgfmt 2> /dev/null') != 256:
warn('msgfmt is missing, not installing translations')
return []
data_files = []
for po in listfiles('po', '*.po'):
lang = os.path.basename(po[:-3])
mo = os.path.join(dirname, lang, 'LC_MESSAGES', domain + '.mo')
if not os.path.exists(mo) or newer(po, mo):
directory = os.path.dirname(mo)
if not os.path.exists(directory):
info("creating %s" % directory)
os.makedirs(directory)
cmd = 'msgfmt -o %s %s' % (mo, po)
info('compiling %s -> %s' % (po, mo))
if os.system(cmd) != 0:
raise SystemExit("Error while running msgfmt")
dest = os.path.dirname(os.path.join('share', mo))
data_files.append((dest, [mo]))
return data_files
def listpackages(root, exclude=None):
"""Recursivly list all packages in directory root
Optionally exclude can be specified which is a string
like foo/bar.
@param root: directory
@param exclude: optional packages to be skipped
"""
packages = []
if not os.path.exists(root):
raise ValueError("%s does not exists" % (root,))
if not os.path.isdir(root):
raise ValueError("%s must be a directory" % (root,))
if os.path.exists(os.path.join(root, '__init__.py')):
packages.append(root.replace('/', '.'))
for filename in os.listdir(root):
full = os.path.join(root, filename)
if os.path.isdir(full):
packages.extend(listpackages(full))
if exclude:
for package in packages[:]:
if package.startswith(exclude):
packages.remove(package)
return packages
def setup(**kwargs):
"""
A drop in replacement for distutils.core.setup which
integrates nicely with kiwi.environ
@var resources:
@var global_resources:
@var templates: List of templates to install
"""
resources = {}
global_resources = {}
templates = []
if 'resources' in kwargs:
resources = kwargs.pop('resources')
if 'global_resources' in kwargs:
global_resources = kwargs.pop('global_resources')
if 'templates' in kwargs:
templates = kwargs.pop('templates')
def run_install(self):
name = kwargs.get('name')
if name:
self.data_files.extend(compile_po_files(name))
KiwiInstallData.run(self)
varext = _VariableExtender(self.distribution)
for path, files in templates:
install = self.distribution.get_command_obj('install')
target = os.path.join(install.prefix, path)
if install.root:
if target[0] == '/':
target = target[1:]
target = os.path.join(install.root, target)
if not os.path.exists(target):
info("creating %s" % target)
os.makedirs(target)
for filename in files:
data = open(filename).read()
data = varext.extend(data)
target_file = os.path.join(target, filename)
info('installing template %s' % target_file)
open(target_file, 'w').write(data)
# Copied from gazpacho, needs to be tested
#if 'bdist_wininst' in sys.argv:
# prefix_code = 'import sys; prefix = sys.prefix'
#else:
# install = self.distribution.get_command_obj('install')
# prefix = install.prefix
# prefix_code = 'prefix = r"%s"' % prefix
# distutils uses old style classes
InstallData = new.classobj('InstallData', (KiwiInstallData,),
dict(run=run_install))
InstallLib = new.classobj('InstallLib', (KiwiInstallLib,),
dict(resources=resources,
global_resources=global_resources))
kwargs['cmdclass'] = dict(install_data=InstallData,
install_lib=InstallLib)
DS_setup(**kwargs)
|