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 312 313 314 315 316 317
|
#!/usr/bin/env python
import errno
import sys
import os
import os.path
import re
from stat import *
import distutils.command
import distutils.command.install
import distutils.core
import distutils.cmd
import distutils.errors
from distutils import log
from distutils.command.install import install
APP_NAME = "ndiff"
# The name of the file used to record the list of installed files, so that the
# uninstall command can remove them.
INSTALLED_FILES_NAME = "INSTALLED_FILES"
# path_startswith and path_strip_prefix are used to deal with the installation
# root (--root option, also known as DESTDIR).
def path_startswith(path, prefix):
"""Returns True if path starts with prefix. It's a little more intelligent
than str.startswith because it normalizes the paths to remove multiple
directory separators and down-up traversals."""
path = os.path.normpath(path)
prefix = os.path.normpath(prefix)
return path.startswith(prefix)
def path_strip_prefix(path, prefix):
"""Return path stripped of its directory prefix if it starts with prefix,
otherwise return path unmodified. This only works correctly with Unix
paths; for example it will not replace the drive letter on a Windows path.
Examples:
>>> path_strip_prefix('/tmp/destdir/usr/bin', '/tmp/destdir')
'/usr/bin'
>>> path_strip_prefix('/tmp/../tmp/destdir/usr/bin', '/tmp///destdir')
'/usr/bin'
>>> path_strip_prefix('/etc', '/tmp/destdir')
'/etc'
>>> path_strip_prefix('/etc', '/')
'/etc'
>>> path_strip_prefix('/etc', '')
'/etc'
"""
absolute = os.path.isabs(path)
path = os.path.normpath(path)
prefix = os.path.normpath(prefix)
if path.startswith(prefix) and prefix != os.sep:
path = path[len(prefix):]
# Absolute paths must remain absolute and relative paths must remain
# relative.
assert os.path.isabs(path) == absolute
return path
###############################################################################
# Distutils subclasses
class null_command(distutils.cmd.Command):
"""This is a dummy distutils command that does nothing. We use it to
replace the install_egg_info and avoid installing a .egg-info file, because
there's no option to disable that."""
def initialize_options(self):
pass
def finalize_options(self):
pass
def get_outputs(self):
return ()
def run(self):
pass
class checked_install(distutils.command.install.install):
"""This is a wrapper around the install command that checks for an error
caused by not having the python-dev package installed. By default,
distutils gives a misleading error message: "invalid Python installation."
"""
def finalize_options(self):
# Ubuntu's python2.6-2.6.4-0ubuntu3 package changes sys.prefix in
# install.finalize_options when sys.prefix is "/usr/local" (our
# default). Because we need the unchanged value later, remember it
# here.
self.saved_prefix = sys.prefix
try:
distutils.command.install.install.finalize_options(self)
except distutils.errors.DistutilsPlatformError as e:
raise distutils.errors.DistutilsPlatformError(str(e) + """
Installing your distribution's python-dev package may solve this problem.""")
def set_modules_path(self):
app_file_name = os.path.join(self.install_scripts, APP_NAME)
# Find where the modules are installed. distutils will put them in
# self.install_lib, but that path can contain the root (DESTDIR), so we
# must strip it off if necessary.
modules_dir = self.install_lib
if self.root is not None:
modules_dir = path_strip_prefix(modules_dir, self.root)
app_file = open(app_file_name, "r")
lines = app_file.readlines()
app_file.close()
for i in range(len(lines)):
if re.match(r'^INSTALL_LIB =', lines[i]):
lines[i] = "INSTALL_LIB = %s\n" % repr(modules_dir)
break
else:
raise ValueError(
"INSTALL_LIB replacement not found in %s" % app_file_name)
app_file = open(app_file_name, "w")
app_file.writelines(lines)
app_file.close()
def run(self):
install.run(self)
# These below are from Zenmap. We're only using set_modules_path right now, but
# we might consider whether the others would be useful (or, if not, whether we
# should remove them from Zenmap).
# self.set_perms()
self.set_modules_path()
# self.fix_paths()
self.create_uninstaller()
self.write_installed_files()
def get_installed_files(self):
"""Return a list of installed files and directories, each prefixed with
the installation root if given. The list of installed directories
doesn't come from distutils so it may be incomplete."""
installed_files = self.get_outputs()
for package in self.distribution.py_modules:
dir = package.replace(".", "/")
installed_files.append(os.path.join(self.install_lib, dir))
installed_files.append(
os.path.join(self.install_scripts, "uninstall_" + APP_NAME))
return installed_files
def create_uninstaller(self):
uninstaller_filename = os.path.join(
self.install_scripts, "uninstall_" + APP_NAME)
uninstaller = """\
#!/usr/bin/env python
import errno, os, os.path, sys
print('Uninstall %(name)s')
answer = raw_input('Are you sure that you want to uninstall '
'%(name)s (yes/no) ')
if answer != 'yes' and answer != 'y':
print('Not uninstalling.')
sys.exit(0)
""" % {'name': APP_NAME}
installed_files = []
for output in self.get_installed_files():
if self.root is not None:
# If we have a root (DESTDIR), we need to strip it off the
# front of paths so the uninstaller runs on the target host.
# The path manipulations are tricky, but made easier because
# the uninstaller only has to run on Unix.
if not path_startswith(output, self.root):
# This should never happen (everything gets installed
# inside the root), but if it does, be safe and don't
# delete anything.
uninstaller += ("print('%s was not installed inside "
"the root %s; skipping.')\n" % (output, self.root))
continue
output = path_strip_prefix(output, self.root)
assert os.path.isabs(output)
installed_files.append(output)
uninstaller += """\
INSTALLED_FILES = (
"""
for file in installed_files:
uninstaller += " %s,\n" % repr(file)
uninstaller += """\
)
# Split the list into lists of files and directories.
files = []
dirs = []
for path in INSTALLED_FILES:
if os.path.isfile(path) or os.path.islink(path):
files.append(path)
elif os.path.isdir(path):
dirs.append(path)
# Delete the files.
for file in files:
print("Removing '%s'." % file)
try:
os.remove(file)
except OSError as e:
print(' Error: %s.' % str(e), file=sys.stderr)
# Delete the directories. First reverse-sort the normalized paths by
# length so that child directories are deleted before their parents.
dirs = [os.path.normpath(dir) for dir in dirs]
dirs.sort(key = len, reverse = True)
for dir in dirs:
try:
print("Removing the directory '%s'." % dir)
os.rmdir(dir)
except OSError as e:
if e.errno == errno.ENOTEMPTY:
print("Directory '%s' not empty; not removing." % dir)
else:
print(str(e), file=sys.stderr)
"""
uninstaller_file = open(uninstaller_filename, 'w')
uninstaller_file.write(uninstaller)
uninstaller_file.close()
# Set exec bit for uninstaller
mode = ((os.stat(uninstaller_filename)[ST_MODE]) | 0o555) & 0o7777
os.chmod(uninstaller_filename, mode)
def write_installed_files(self):
"""Write a list of installed files for use by the uninstall command.
This is similar to what happens with the --record option except that it
doesn't strip off the installation root, if any. File names containing
newline characters are not handled."""
if INSTALLED_FILES_NAME == self.record:
distutils.log.warn("warning: installation record is overwriting "
"--record file '%s'." % self.record)
f = open(INSTALLED_FILES_NAME, "w")
try:
for output in self.get_installed_files():
assert "\n" not in output
print(output, file=f)
finally:
f.close()
class my_uninstall(distutils.cmd.Command):
"""A distutils command that performs uninstallation. It reads the list of
installed files written by the install command."""
command_name = "uninstall"
description = "uninstall installed files recorded in '%s'" % (
INSTALLED_FILES_NAME)
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
# Read the list of installed files.
try:
f = open(INSTALLED_FILES_NAME, "r")
except IOError as e:
if e.errno == errno.ENOENT:
log.error("Couldn't open the installation record '%s'. "
"Have you installed yet?" % INSTALLED_FILES_NAME)
return
installed_files = [file.rstrip("\n") for file in f.readlines()]
f.close()
# Delete the installation record too.
installed_files.append(INSTALLED_FILES_NAME)
# Split the list into lists of files and directories.
files = []
dirs = []
for path in installed_files:
if os.path.isfile(path) or os.path.islink(path):
files.append(path)
elif os.path.isdir(path):
dirs.append(path)
# Delete the files.
for file in files:
log.info("Removing '%s'." % file)
try:
if not self.dry_run:
os.remove(file)
except OSError as e:
log.error(str(e))
# Delete the directories. First reverse-sort the normalized paths by
# length so that child directories are deleted before their parents.
dirs = [os.path.normpath(dir) for dir in dirs]
dirs.sort(key=len, reverse=True)
for dir in dirs:
try:
log.info("Removing the directory '%s'." % dir)
if not self.dry_run:
os.rmdir(dir)
except OSError as e:
if e.errno == errno.ENOTEMPTY:
log.info("Directory '%s' not empty; not removing." % dir)
else:
log.error(str(e))
distutils.core.setup(name=u"ndiff", scripts=[u"scripts/ndiff"],
py_modules=[u"ndiff"],
data_files=[(u"share/man/man1", [u"docs/ndiff.1"])],
cmdclass={
"install_egg_info": null_command,
"install": checked_install,
"uninstall": my_uninstall
})
|