Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 1 | """Building blocks for installers. |
| 2 | |
| 3 | When used as a script, this module installs a release thanks to info |
| 4 | obtained from an index (e.g. PyPI), with dependencies. |
| 5 | |
| 6 | This is a higher-level module built on packaging.database and |
| 7 | packaging.pypi. |
| 8 | """ |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 9 | import os |
| 10 | import sys |
| 11 | import stat |
| 12 | import errno |
| 13 | import shutil |
| 14 | import logging |
| 15 | import tempfile |
Éric Araujo | fa6cfbc | 2011-06-10 18:31:40 +0200 | [diff] [blame] | 16 | from sysconfig import get_config_var, get_path, is_python_build |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 17 | |
| 18 | from packaging import logger |
| 19 | from packaging.dist import Distribution |
| 20 | from packaging.util import (_is_archive_file, ask, get_install_method, |
| 21 | egginfo_to_distinfo) |
| 22 | from packaging.pypi import wrapper |
| 23 | from packaging.version import get_version_predicate |
| 24 | from packaging.database import get_distributions, get_distribution |
| 25 | from packaging.depgraph import generate_graph |
| 26 | |
| 27 | from packaging.errors import (PackagingError, InstallationException, |
| 28 | InstallationConflict, CCompilerError) |
| 29 | from packaging.pypi.errors import ProjectNotFound, ReleaseNotFound |
Tarek Ziade | 5a5ce38 | 2011-05-31 12:09:34 +0200 | [diff] [blame] | 30 | from packaging import database |
| 31 | |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 32 | |
| 33 | __all__ = ['install_dists', 'install_from_infos', 'get_infos', 'remove', |
| 34 | 'install', 'install_local_project'] |
| 35 | |
| 36 | |
| 37 | def _move_files(files, destination): |
| 38 | """Move the list of files in the destination folder, keeping the same |
| 39 | structure. |
| 40 | |
| 41 | Return a list of tuple (old, new) emplacement of files |
| 42 | |
| 43 | :param files: a list of files to move. |
| 44 | :param destination: the destination directory to put on the files. |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 45 | """ |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 46 | |
| 47 | for old in files: |
Tarek Ziade | 4bdd9f3 | 2011-05-21 15:12:10 +0200 | [diff] [blame] | 48 | filename = os.path.split(old)[-1] |
| 49 | new = os.path.join(destination, filename) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 50 | # try to make the paths. |
| 51 | try: |
| 52 | os.makedirs(os.path.dirname(new)) |
| 53 | except OSError as e: |
Éric Araujo | 6f67765 | 2011-06-16 23:43:15 +0200 | [diff] [blame] | 54 | if e.errno != errno.EEXIST: |
| 55 | raise |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 56 | os.rename(old, new) |
| 57 | yield old, new |
| 58 | |
| 59 | |
| 60 | def _run_distutils_install(path): |
| 61 | # backward compat: using setuptools or plain-distutils |
| 62 | cmd = '%s setup.py install --record=%s' |
| 63 | record_file = os.path.join(path, 'RECORD') |
| 64 | os.system(cmd % (sys.executable, record_file)) |
| 65 | if not os.path.exists(record_file): |
| 66 | raise ValueError('failed to install') |
| 67 | else: |
| 68 | egginfo_to_distinfo(record_file, remove_egginfo=True) |
| 69 | |
| 70 | |
| 71 | def _run_setuptools_install(path): |
| 72 | cmd = '%s setup.py install --record=%s --single-version-externally-managed' |
| 73 | record_file = os.path.join(path, 'RECORD') |
Tarek Ziade | 5a5ce38 | 2011-05-31 12:09:34 +0200 | [diff] [blame] | 74 | |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 75 | os.system(cmd % (sys.executable, record_file)) |
| 76 | if not os.path.exists(record_file): |
| 77 | raise ValueError('failed to install') |
| 78 | else: |
| 79 | egginfo_to_distinfo(record_file, remove_egginfo=True) |
| 80 | |
| 81 | |
| 82 | def _run_packaging_install(path): |
| 83 | # XXX check for a valid setup.cfg? |
| 84 | dist = Distribution() |
| 85 | dist.parse_config_files() |
| 86 | try: |
| 87 | dist.run_command('install_dist') |
Éric Araujo | bab50cb | 2011-07-29 02:37:21 +0200 | [diff] [blame] | 88 | name = dist.metadata['Name'] |
Tarek Ziade | 5a5ce38 | 2011-05-31 12:09:34 +0200 | [diff] [blame] | 89 | return database.get_distribution(name) is not None |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 90 | except (IOError, os.error, PackagingError, CCompilerError) as msg: |
Tarek Ziade | 5a5ce38 | 2011-05-31 12:09:34 +0200 | [diff] [blame] | 91 | raise ValueError("Failed to install, " + str(msg)) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 92 | |
| 93 | |
| 94 | def _install_dist(dist, path): |
| 95 | """Install a distribution into a path. |
| 96 | |
| 97 | This: |
| 98 | |
| 99 | * unpack the distribution |
| 100 | * copy the files in "path" |
| 101 | * determine if the distribution is packaging or distutils1. |
| 102 | """ |
| 103 | where = dist.unpack() |
| 104 | |
| 105 | if where is None: |
| 106 | raise ValueError('Cannot locate the unpacked archive') |
| 107 | |
| 108 | return _run_install_from_archive(where) |
| 109 | |
| 110 | |
| 111 | def install_local_project(path): |
| 112 | """Install a distribution from a source directory. |
| 113 | |
| 114 | If the source directory contains a setup.py install using distutils1. |
| 115 | If a setup.cfg is found, install using the install_dist command. |
| 116 | |
Tarek Ziade | 5a5ce38 | 2011-05-31 12:09:34 +0200 | [diff] [blame] | 117 | Returns True on success, False on Failure. |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 118 | """ |
| 119 | path = os.path.abspath(path) |
| 120 | if os.path.isdir(path): |
Éric Araujo | bab50cb | 2011-07-29 02:37:21 +0200 | [diff] [blame] | 121 | logger.info('Installing from source directory: %r', path) |
Tarek Ziade | 5a5ce38 | 2011-05-31 12:09:34 +0200 | [diff] [blame] | 122 | return _run_install_from_dir(path) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 123 | elif _is_archive_file(path): |
Éric Araujo | bab50cb | 2011-07-29 02:37:21 +0200 | [diff] [blame] | 124 | logger.info('Installing from archive: %r', path) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 125 | _unpacked_dir = tempfile.mkdtemp() |
Vinay Sajip | 0bec35d | 2011-07-07 12:59:31 +0100 | [diff] [blame] | 126 | try: |
| 127 | shutil.unpack_archive(path, _unpacked_dir) |
| 128 | return _run_install_from_archive(_unpacked_dir) |
| 129 | finally: |
| 130 | shutil.rmtree(_unpacked_dir) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 131 | else: |
Éric Araujo | bab50cb | 2011-07-29 02:37:21 +0200 | [diff] [blame] | 132 | logger.warning('No project to install.') |
Tarek Ziade | 5a5ce38 | 2011-05-31 12:09:34 +0200 | [diff] [blame] | 133 | return False |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 134 | |
| 135 | |
| 136 | def _run_install_from_archive(source_dir): |
| 137 | # XXX need a better way |
| 138 | for item in os.listdir(source_dir): |
| 139 | fullpath = os.path.join(source_dir, item) |
| 140 | if os.path.isdir(fullpath): |
| 141 | source_dir = fullpath |
| 142 | break |
| 143 | return _run_install_from_dir(source_dir) |
| 144 | |
| 145 | |
| 146 | install_methods = { |
| 147 | 'packaging': _run_packaging_install, |
| 148 | 'setuptools': _run_setuptools_install, |
| 149 | 'distutils': _run_distutils_install} |
| 150 | |
| 151 | |
| 152 | def _run_install_from_dir(source_dir): |
| 153 | old_dir = os.getcwd() |
| 154 | os.chdir(source_dir) |
| 155 | install_method = get_install_method(source_dir) |
| 156 | func = install_methods[install_method] |
| 157 | try: |
| 158 | func = install_methods[install_method] |
Tarek Ziade | 5a5ce38 | 2011-05-31 12:09:34 +0200 | [diff] [blame] | 159 | try: |
| 160 | func(source_dir) |
| 161 | return True |
| 162 | except ValueError as err: |
| 163 | # failed to install |
| 164 | logger.info(str(err)) |
| 165 | return False |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 166 | finally: |
| 167 | os.chdir(old_dir) |
| 168 | |
| 169 | |
Éric Araujo | 6f67765 | 2011-06-16 23:43:15 +0200 | [diff] [blame] | 170 | def install_dists(dists, path, paths=None): |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 171 | """Install all distributions provided in dists, with the given prefix. |
| 172 | |
| 173 | If an error occurs while installing one of the distributions, uninstall all |
| 174 | the installed distribution (in the context if this function). |
| 175 | |
| 176 | Return a list of installed dists. |
| 177 | |
| 178 | :param dists: distributions to install |
| 179 | :param path: base path to install distribution in |
| 180 | :param paths: list of paths (defaults to sys.path) to look for info |
| 181 | """ |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 182 | |
| 183 | installed_dists = [] |
| 184 | for dist in dists: |
Tarek Ziade | b1b6e13 | 2011-05-30 12:07:49 +0200 | [diff] [blame] | 185 | logger.info('Installing %r %s...', dist.name, dist.version) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 186 | try: |
| 187 | _install_dist(dist, path) |
| 188 | installed_dists.append(dist) |
| 189 | except Exception as e: |
Tarek Ziade | b1b6e13 | 2011-05-30 12:07:49 +0200 | [diff] [blame] | 190 | logger.info('Failed: %s', e) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 191 | |
| 192 | # reverting |
| 193 | for installed_dist in installed_dists: |
Éric Araujo | bab50cb | 2011-07-29 02:37:21 +0200 | [diff] [blame] | 194 | logger.info('Reverting %r', installed_dist) |
Éric Araujo | 6f67765 | 2011-06-16 23:43:15 +0200 | [diff] [blame] | 195 | remove(installed_dist.name, paths) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 196 | raise e |
| 197 | return installed_dists |
| 198 | |
| 199 | |
| 200 | def install_from_infos(install_path=None, install=[], remove=[], conflicts=[], |
Éric Araujo | 6f67765 | 2011-06-16 23:43:15 +0200 | [diff] [blame] | 201 | paths=None): |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 202 | """Install and remove the given distributions. |
| 203 | |
| 204 | The function signature is made to be compatible with the one of get_infos. |
| 205 | The aim of this script is to povide a way to install/remove what's asked, |
| 206 | and to rollback if needed. |
| 207 | |
| 208 | So, it's not possible to be in an inconsistant state, it could be either |
| 209 | installed, either uninstalled, not half-installed. |
| 210 | |
| 211 | The process follow those steps: |
| 212 | |
| 213 | 1. Move all distributions that will be removed in a temporary location |
| 214 | 2. Install all the distributions that will be installed in a temp. loc. |
| 215 | 3. If the installation fails, rollback (eg. move back) those |
| 216 | distributions, or remove what have been installed. |
| 217 | 4. Else, move the distributions to the right locations, and remove for |
| 218 | real the distributions thats need to be removed. |
| 219 | |
| 220 | :param install_path: the installation path where we want to install the |
| 221 | distributions. |
| 222 | :param install: list of distributions that will be installed; install_path |
| 223 | must be provided if this list is not empty. |
| 224 | :param remove: list of distributions that will be removed. |
| 225 | :param conflicts: list of conflicting distributions, eg. that will be in |
| 226 | conflict once the install and remove distribution will be |
| 227 | processed. |
| 228 | :param paths: list of paths (defaults to sys.path) to look for info |
| 229 | """ |
| 230 | # first of all, if we have conflicts, stop here. |
| 231 | if conflicts: |
| 232 | raise InstallationConflict(conflicts) |
| 233 | |
| 234 | if install and not install_path: |
| 235 | raise ValueError("Distributions are to be installed but `install_path`" |
| 236 | " is not provided.") |
| 237 | |
| 238 | # before removing the files, we will start by moving them away |
| 239 | # then, if any error occurs, we could replace them in the good place. |
| 240 | temp_files = {} # contains lists of {dist: (old, new)} paths |
| 241 | temp_dir = None |
| 242 | if remove: |
| 243 | temp_dir = tempfile.mkdtemp() |
| 244 | for dist in remove: |
| 245 | files = dist.list_installed_files() |
| 246 | temp_files[dist] = _move_files(files, temp_dir) |
| 247 | try: |
| 248 | if install: |
| 249 | install_dists(install, install_path, paths) |
| 250 | except: |
| 251 | # if an error occurs, put back the files in the right place. |
| 252 | for files in temp_files.values(): |
| 253 | for old, new in files: |
| 254 | shutil.move(new, old) |
| 255 | if temp_dir: |
| 256 | shutil.rmtree(temp_dir) |
| 257 | # now re-raising |
| 258 | raise |
| 259 | |
| 260 | # we can remove them for good |
| 261 | for files in temp_files.values(): |
| 262 | for old, new in files: |
| 263 | os.remove(new) |
| 264 | if temp_dir: |
| 265 | shutil.rmtree(temp_dir) |
| 266 | |
| 267 | |
| 268 | def _get_setuptools_deps(release): |
| 269 | # NotImplementedError |
| 270 | pass |
| 271 | |
| 272 | |
| 273 | def get_infos(requirements, index=None, installed=None, prefer_final=True): |
| 274 | """Return the informations on what's going to be installed and upgraded. |
| 275 | |
| 276 | :param requirements: is a *string* containing the requirements for this |
| 277 | project (for instance "FooBar 1.1" or "BarBaz (<1.2)") |
| 278 | :param index: If an index is specified, use this one, otherwise, use |
| 279 | :class index.ClientWrapper: to get project metadatas. |
| 280 | :param installed: a list of already installed distributions. |
| 281 | :param prefer_final: when picking up the releases, prefer a "final" one |
| 282 | over a beta/alpha/etc one. |
| 283 | |
| 284 | The results are returned in a dict, containing all the operations |
| 285 | needed to install the given requirements:: |
| 286 | |
| 287 | >>> get_install_info("FooBar (<=1.2)") |
| 288 | {'install': [<FooBar 1.1>], 'remove': [], 'conflict': []} |
| 289 | |
| 290 | Conflict contains all the conflicting distributions, if there is a |
| 291 | conflict. |
| 292 | """ |
| 293 | # this function does several things: |
| 294 | # 1. get a release specified by the requirements |
| 295 | # 2. gather its metadata, using setuptools compatibility if needed |
| 296 | # 3. compare this tree with what is currently installed on the system, |
| 297 | # return the requirements of what is missing |
| 298 | # 4. do that recursively and merge back the results |
| 299 | # 5. return a dict containing information about what is needed to install |
| 300 | # or remove |
| 301 | |
| 302 | if not installed: |
Tarek Ziade | b1b6e13 | 2011-05-30 12:07:49 +0200 | [diff] [blame] | 303 | logger.debug('Reading installed distributions') |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 304 | installed = list(get_distributions(use_egg_info=True)) |
| 305 | |
| 306 | infos = {'install': [], 'remove': [], 'conflict': []} |
| 307 | # Is a compatible version of the project already installed ? |
| 308 | predicate = get_version_predicate(requirements) |
| 309 | found = False |
| 310 | |
| 311 | # check that the project isn't already installed |
| 312 | for installed_project in installed: |
| 313 | # is it a compatible project ? |
| 314 | if predicate.name.lower() != installed_project.name.lower(): |
| 315 | continue |
| 316 | found = True |
Éric Araujo | bab50cb | 2011-07-29 02:37:21 +0200 | [diff] [blame] | 317 | logger.info('Found %r %s', installed_project.name, |
| 318 | installed_project.version) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 319 | |
| 320 | # if we already have something installed, check it matches the |
| 321 | # requirements |
Éric Araujo | bab50cb | 2011-07-29 02:37:21 +0200 | [diff] [blame] | 322 | if predicate.match(installed_project.version): |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 323 | return infos |
| 324 | break |
| 325 | |
| 326 | if not found: |
Tarek Ziade | b1b6e13 | 2011-05-30 12:07:49 +0200 | [diff] [blame] | 327 | logger.debug('Project not installed') |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 328 | |
| 329 | if not index: |
| 330 | index = wrapper.ClientWrapper() |
| 331 | |
| 332 | if not installed: |
| 333 | installed = get_distributions(use_egg_info=True) |
| 334 | |
| 335 | # Get all the releases that match the requirements |
| 336 | try: |
| 337 | release = index.get_release(requirements) |
| 338 | except (ReleaseNotFound, ProjectNotFound): |
Éric Araujo | bab50cb | 2011-07-29 02:37:21 +0200 | [diff] [blame] | 339 | raise InstallationException('Release not found: %r' % requirements) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 340 | |
| 341 | if release is None: |
Tarek Ziade | b1b6e13 | 2011-05-30 12:07:49 +0200 | [diff] [blame] | 342 | logger.info('Could not find a matching project') |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 343 | return infos |
| 344 | |
| 345 | metadata = release.fetch_metadata() |
| 346 | |
| 347 | # we need to build setuptools deps if any |
| 348 | if 'requires_dist' not in metadata: |
| 349 | metadata['requires_dist'] = _get_setuptools_deps(release) |
| 350 | |
| 351 | # build the dependency graph with local and required dependencies |
| 352 | dists = list(installed) |
| 353 | dists.append(release) |
| 354 | depgraph = generate_graph(dists) |
| 355 | |
| 356 | # Get what the missing deps are |
| 357 | dists = depgraph.missing[release] |
| 358 | if dists: |
Tarek Ziade | b1b6e13 | 2011-05-30 12:07:49 +0200 | [diff] [blame] | 359 | logger.info("Missing dependencies found, retrieving metadata") |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 360 | # we have missing deps |
| 361 | for dist in dists: |
| 362 | _update_infos(infos, get_infos(dist, index, installed)) |
| 363 | |
| 364 | # Fill in the infos |
| 365 | existing = [d for d in installed if d.name == release.name] |
| 366 | if existing: |
| 367 | infos['remove'].append(existing[0]) |
| 368 | infos['conflict'].extend(depgraph.reverse_list[existing[0]]) |
| 369 | infos['install'].append(release) |
| 370 | return infos |
| 371 | |
| 372 | |
| 373 | def _update_infos(infos, new_infos): |
| 374 | """extends the lists contained in the `info` dict with those contained |
| 375 | in the `new_info` one |
| 376 | """ |
| 377 | for key, value in infos.items(): |
| 378 | if key in new_infos: |
| 379 | infos[key].extend(new_infos[key]) |
| 380 | |
| 381 | |
Éric Araujo | 6f67765 | 2011-06-16 23:43:15 +0200 | [diff] [blame] | 382 | def remove(project_name, paths=None, auto_confirm=True): |
Tarek Ziade | f47fa58 | 2011-05-30 23:26:51 +0200 | [diff] [blame] | 383 | """Removes a single project from the installation. |
| 384 | |
| 385 | Returns True on success |
| 386 | """ |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 387 | dist = get_distribution(project_name, use_egg_info=True, paths=paths) |
| 388 | if dist is None: |
Éric Araujo | bab50cb | 2011-07-29 02:37:21 +0200 | [diff] [blame] | 389 | raise PackagingError('Distribution %r not found' % project_name) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 390 | files = dist.list_installed_files(local=True) |
| 391 | rmdirs = [] |
| 392 | rmfiles = [] |
| 393 | tmp = tempfile.mkdtemp(prefix=project_name + '-uninstall') |
Tarek Ziade | f47fa58 | 2011-05-30 23:26:51 +0200 | [diff] [blame] | 394 | |
| 395 | def _move_file(source, target): |
| 396 | try: |
| 397 | os.rename(source, target) |
| 398 | except OSError as err: |
| 399 | return err |
| 400 | return None |
| 401 | |
| 402 | success = True |
| 403 | error = None |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 404 | try: |
| 405 | for file_, md5, size in files: |
| 406 | if os.path.isfile(file_): |
| 407 | dirname, filename = os.path.split(file_) |
| 408 | tmpfile = os.path.join(tmp, filename) |
| 409 | try: |
Tarek Ziade | f47fa58 | 2011-05-30 23:26:51 +0200 | [diff] [blame] | 410 | error = _move_file(file_, tmpfile) |
| 411 | if error is not None: |
| 412 | success = False |
| 413 | break |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 414 | finally: |
| 415 | if not os.path.isfile(file_): |
| 416 | os.rename(tmpfile, file_) |
| 417 | if file_ not in rmfiles: |
| 418 | rmfiles.append(file_) |
| 419 | if dirname not in rmdirs: |
| 420 | rmdirs.append(dirname) |
| 421 | finally: |
| 422 | shutil.rmtree(tmp) |
| 423 | |
Tarek Ziade | f47fa58 | 2011-05-30 23:26:51 +0200 | [diff] [blame] | 424 | if not success: |
| 425 | logger.info('%r cannot be removed.', project_name) |
Éric Araujo | bab50cb | 2011-07-29 02:37:21 +0200 | [diff] [blame] | 426 | logger.info('Error: %s', error) |
Tarek Ziade | f47fa58 | 2011-05-30 23:26:51 +0200 | [diff] [blame] | 427 | return False |
| 428 | |
Tarek Ziade | b1b6e13 | 2011-05-30 12:07:49 +0200 | [diff] [blame] | 429 | logger.info('Removing %r: ', project_name) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 430 | |
| 431 | for file_ in rmfiles: |
| 432 | logger.info(' %s', file_) |
| 433 | |
| 434 | # Taken from the pip project |
| 435 | if auto_confirm: |
| 436 | response = 'y' |
| 437 | else: |
| 438 | response = ask('Proceed (y/n)? ', ('y', 'n')) |
| 439 | |
| 440 | if response == 'y': |
| 441 | file_count = 0 |
| 442 | for file_ in rmfiles: |
| 443 | os.remove(file_) |
| 444 | file_count += 1 |
| 445 | |
| 446 | dir_count = 0 |
| 447 | for dirname in rmdirs: |
| 448 | if not os.path.exists(dirname): |
| 449 | # could |
| 450 | continue |
| 451 | |
| 452 | files_count = 0 |
| 453 | for root, dir, files in os.walk(dirname): |
| 454 | files_count += len(files) |
| 455 | |
| 456 | if files_count > 0: |
| 457 | # XXX Warning |
| 458 | continue |
| 459 | |
| 460 | # empty dirs with only empty dirs |
| 461 | if os.stat(dirname).st_mode & stat.S_IWUSR: |
| 462 | # XXX Add a callable in shutil.rmtree to count |
| 463 | # the number of deleted elements |
| 464 | shutil.rmtree(dirname) |
| 465 | dir_count += 1 |
| 466 | |
| 467 | # removing the top path |
| 468 | # XXX count it ? |
| 469 | if os.path.exists(dist.path): |
| 470 | shutil.rmtree(dist.path) |
| 471 | |
Tarek Ziade | b1b6e13 | 2011-05-30 12:07:49 +0200 | [diff] [blame] | 472 | logger.info('Success: removed %d files and %d dirs', |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 473 | file_count, dir_count) |
| 474 | |
Tarek Ziade | f47fa58 | 2011-05-30 23:26:51 +0200 | [diff] [blame] | 475 | return True |
| 476 | |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 477 | |
| 478 | def install(project): |
Tarek Ziade | 5a5ce38 | 2011-05-31 12:09:34 +0200 | [diff] [blame] | 479 | """Installs a project. |
| 480 | |
| 481 | Returns True on success, False on failure |
| 482 | """ |
Éric Araujo | fa6cfbc | 2011-06-10 18:31:40 +0200 | [diff] [blame] | 483 | if is_python_build(): |
| 484 | # Python would try to install into the site-packages directory under |
| 485 | # $PREFIX, but when running from an uninstalled code checkout we don't |
| 486 | # want to create directories under the installation root |
| 487 | message = ('installing third-party projects from an uninstalled ' |
| 488 | 'Python is not supported') |
| 489 | logger.error(message) |
| 490 | return False |
| 491 | |
Tarek Ziade | 5a5ce38 | 2011-05-31 12:09:34 +0200 | [diff] [blame] | 492 | logger.info('Checking the installation location...') |
| 493 | purelib_path = get_path('purelib') |
Éric Araujo | fa6cfbc | 2011-06-10 18:31:40 +0200 | [diff] [blame] | 494 | |
Tarek Ziade | 5a5ce38 | 2011-05-31 12:09:34 +0200 | [diff] [blame] | 495 | # trying to write a file there |
| 496 | try: |
| 497 | with tempfile.NamedTemporaryFile(suffix=project, |
| 498 | dir=purelib_path) as testfile: |
| 499 | testfile.write(b'test') |
| 500 | except OSError: |
Éric Araujo | fa6cfbc | 2011-06-10 18:31:40 +0200 | [diff] [blame] | 501 | # FIXME this should check the errno, or be removed altogether (race |
| 502 | # condition: the directory permissions could be changed between here |
| 503 | # and the actual install) |
Tarek Ziade | 5a5ce38 | 2011-05-31 12:09:34 +0200 | [diff] [blame] | 504 | logger.info('Unable to write in "%s". Do you have the permissions ?' |
| 505 | % purelib_path) |
| 506 | return False |
| 507 | |
Tarek Ziade | b1b6e13 | 2011-05-30 12:07:49 +0200 | [diff] [blame] | 508 | logger.info('Getting information about %r...', project) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 509 | try: |
| 510 | info = get_infos(project) |
| 511 | except InstallationException: |
Tarek Ziade | b1b6e13 | 2011-05-30 12:07:49 +0200 | [diff] [blame] | 512 | logger.info('Cound not find %r', project) |
Tarek Ziade | 5a5ce38 | 2011-05-31 12:09:34 +0200 | [diff] [blame] | 513 | return False |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 514 | |
| 515 | if info['install'] == []: |
Tarek Ziade | b1b6e13 | 2011-05-30 12:07:49 +0200 | [diff] [blame] | 516 | logger.info('Nothing to install') |
Tarek Ziade | 5a5ce38 | 2011-05-31 12:09:34 +0200 | [diff] [blame] | 517 | return False |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 518 | |
| 519 | install_path = get_config_var('base') |
| 520 | try: |
| 521 | install_from_infos(install_path, |
| 522 | info['install'], info['remove'], info['conflict']) |
| 523 | |
| 524 | except InstallationConflict as e: |
| 525 | if logger.isEnabledFor(logging.INFO): |
Éric Araujo | bab50cb | 2011-07-29 02:37:21 +0200 | [diff] [blame] | 526 | projects = ('%r %s' % (p.name, p.version) for p in e.args[0]) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 527 | logger.info('%r conflicts with %s', project, ','.join(projects)) |
| 528 | |
Tarek Ziade | 5a5ce38 | 2011-05-31 12:09:34 +0200 | [diff] [blame] | 529 | return True |