| Total Complexity | 176 |
| Total Lines | 895 |
| Duplicated Lines | 5.92 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like _CondaAPI often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # -*- coding: utf-8 -*- |
||
| 224 | return self._process.state() == QProcess.NotRunning and self._fired |
||
| 225 | |||
| 226 | def start(self): |
||
| 227 | """Start process.""" |
||
| 228 | logger.debug(str(' '.join(self._cmd_list))) |
||
| 229 | |||
| 230 | if not self._fired: |
||
| 231 | self._partial_ouput = None |
||
| 232 | self._process.start(self._cmd_list[0], self._cmd_list[1:]) |
||
| 233 | self._timer.start() |
||
| 234 | else: |
||
| 235 | raise CondaProcessWorker('A Conda ProcessWorker can only run once ' |
||
| 236 | 'per method call.') |
||
| 237 | |||
| 238 | |||
| 239 | # --- API |
||
| 240 | # ----------------------------------------------------------------------------- |
||
| 241 | class _CondaAPI(QObject): |
||
| 242 | """Conda API to connect to conda in a non blocking way via QProcess.""" |
||
| 243 | |||
| 244 | ENCODING = 'ascii' |
||
| 245 | UTF8 = 'utf-8' |
||
| 246 | DEFAULT_CHANNELS = ['https://repo.continuum.io/pkgs/pro', |
||
| 247 | 'https://repo.continuum.io/pkgs/free'] |
||
| 248 | |||
| 249 | def __init__(self, parent=None): |
||
| 250 | """Conda API to connect to conda in a non blocking way via QProcess.""" |
||
| 251 | super(_CondaAPI, self).__init__() |
||
| 252 | self._parent = parent |
||
| 253 | self._queue = deque() |
||
| 254 | self._timer = QTimer() |
||
| 255 | self._current_worker = None |
||
| 256 | self._workers = [] |
||
| 257 | |||
| 258 | self._timer.setInterval(1000) |
||
| 259 | self._timer.timeout.connect(self._clean) |
||
| 260 | |||
| 261 | self.ROOT_PREFIX = None |
||
| 262 | self.set_root_prefix() |
||
| 263 | |||
| 264 | # Set config files path |
||
| 265 | self.user_rc_path = abspath(expanduser('~/.condarc')) |
||
| 266 | self.sys_rc_path = join(self.ROOT_PREFIX, '.condarc') |
||
| 267 | |||
| 268 | def _clean(self): |
||
| 269 | """Remove references of inactive workers periodically.""" |
||
| 270 | if self._workers: |
||
| 271 | for w in self._workers: |
||
| 272 | if w.is_finished(): |
||
| 273 | self._workers.remove(w) |
||
| 274 | else: |
||
| 275 | self._current_worker = None |
||
| 276 | self._timer.stop() |
||
| 277 | |||
| 278 | def _start(self): |
||
| 279 | if len(self._queue) == 1: |
||
| 280 | self._current_worker = self._queue.popleft() |
||
| 281 | self._workers.append(self._current_worker) |
||
| 282 | self._current_worker.start() |
||
| 283 | self._timer.start() |
||
| 284 | |||
| 285 | def is_active(self): |
||
| 286 | """Check if a worker is still active.""" |
||
| 287 | return len(self._workers) == 0 |
||
| 288 | |||
| 289 | def terminate_all_processes(self): |
||
| 290 | """Kill all working processes.""" |
||
| 291 | for worker in self._workers: |
||
| 292 | worker.close() |
||
| 293 | |||
| 294 | # --- Conda api |
||
| 295 | # ------------------------------------------------------------------------- |
||
| 296 | def _call_conda(self, extra_args, abspath=True, parse=False, |
||
| 297 | callback=None): |
||
| 298 | """ |
||
| 299 | Call conda with the list of extra arguments, and return the worker. |
||
| 300 | |||
| 301 | The result can be force by calling worker.communicate(), which returns |
||
| 302 | the tuple (stdout, stderr). |
||
| 303 | """ |
||
| 304 | if abspath: |
||
| 305 | if sys.platform == 'win32': |
||
| 306 | python = join(self.ROOT_PREFIX, 'python.exe') |
||
| 307 | conda = join(self.ROOT_PREFIX, 'Scripts', |
||
| 308 | 'conda-script.py') |
||
| 309 | else: |
||
| 310 | python = join(self.ROOT_PREFIX, 'bin/python') |
||
| 311 | conda = join(self.ROOT_PREFIX, 'bin/conda') |
||
| 312 | cmd_list = [python, conda] |
||
| 313 | else: |
||
| 314 | # Just use whatever conda is on the path |
||
| 315 | cmd_list = ['conda'] |
||
| 316 | |||
| 317 | cmd_list.extend(extra_args) |
||
| 318 | |||
| 319 | process_worker = ProcessWorker(cmd_list, parse=parse, |
||
| 320 | callback=callback) |
||
| 321 | process_worker.sig_finished.connect(self._start) |
||
| 322 | self._queue.append(process_worker) |
||
| 323 | self._start() |
||
| 324 | |||
| 325 | return process_worker |
||
| 326 | |||
| 327 | def _call_and_parse(self, extra_args, abspath=True, callback=None): |
||
| 328 | return self._call_conda(extra_args, abspath=abspath, parse=True, |
||
| 329 | callback=callback) |
||
| 330 | |||
| 331 | @staticmethod |
||
| 332 | def _setup_install_commands_from_kwargs(kwargs, keys=tuple()): |
||
| 333 | """Setup install commands for conda.""" |
||
| 334 | cmd_list = [] |
||
| 335 | if kwargs.get('override_channels', False) and 'channel' not in kwargs: |
||
| 336 | raise TypeError('conda search: override_channels requires channel') |
||
| 337 | |||
| 338 | if 'env' in kwargs: |
||
| 339 | cmd_list.extend(['--name', kwargs.pop('env')]) |
||
| 340 | if 'prefix' in kwargs: |
||
| 341 | cmd_list.extend(['--prefix', kwargs.pop('prefix')]) |
||
| 342 | if 'channel' in kwargs: |
||
| 343 | channel = kwargs.pop('channel') |
||
| 344 | if isinstance(channel, str): |
||
| 345 | cmd_list.extend(['--channel', channel]) |
||
| 346 | else: |
||
| 347 | cmd_list.append('--channel') |
||
| 348 | cmd_list.extend(channel) |
||
| 349 | |||
| 350 | for key in keys: |
||
| 351 | if key in kwargs and kwargs[key]: |
||
| 352 | cmd_list.append('--' + key.replace('_', '-')) |
||
| 353 | |||
| 354 | return cmd_list |
||
| 355 | |||
| 356 | def set_root_prefix(self, prefix=None): |
||
| 357 | """ |
||
| 358 | Set the prefix to the root environment (default is /opt/anaconda). |
||
| 359 | |||
| 360 | This function should only be called once (right after importing |
||
| 361 | conda_api). |
||
| 362 | """ |
||
| 363 | if prefix: |
||
| 364 | self.ROOT_PREFIX = prefix |
||
| 365 | else: |
||
| 366 | # Find some conda instance, and then use info to get 'root_prefix' |
||
| 367 | worker = self._call_and_parse(['info', '--json'], abspath=False) |
||
| 368 | info = worker.communicate()[0] |
||
| 369 | self.ROOT_PREFIX = info['root_prefix'] |
||
| 370 | |||
| 371 | def get_conda_version(self): |
||
| 372 | """Return the version of conda being used (invoked) as a string.""" |
||
| 373 | return self._call_conda(['--version'], |
||
| 374 | callback=self._get_conda_version) |
||
| 375 | |||
| 376 | @staticmethod |
||
| 377 | def _get_conda_version(stdout, stderr): |
||
| 378 | """Callback for get_conda_version.""" |
||
| 379 | # argparse outputs version to stderr in Python < 3.4. |
||
| 380 | # http://bugs.python.org/issue18920 |
||
| 381 | pat = re.compile(r'conda:?\s+(\d+\.\d\S+|unknown)') |
||
| 382 | m = pat.match(stderr.decode().strip()) |
||
| 383 | if m is None: |
||
| 384 | m = pat.match(stdout.decode().strip()) |
||
| 385 | |||
| 386 | if m is None: |
||
| 387 | raise Exception('output did not match: {0}'.format(stderr)) |
||
| 388 | |||
| 389 | return m.group(1) |
||
| 390 | |||
| 391 | def get_envs(self, log=True): |
||
| 392 | """Return environment list of absolute path to their prefixes.""" |
||
| 393 | if log: |
||
| 394 | logger.debug('') |
||
| 395 | # return self._call_and_parse(['info', '--json'], |
||
| 396 | # callback=lambda o, e: o['envs']) |
||
| 397 | envs = os.listdir(os.sep.join([self.ROOT_PREFIX, 'envs'])) |
||
| 398 | envs = [os.sep.join([self.ROOT_PREFIX, 'envs', i]) for i in envs] |
||
| 399 | |||
| 400 | valid_envs = [e for e in envs if os.path.isdir(e) and |
||
| 401 | self.environment_exists(prefix=e)] |
||
| 402 | |||
| 403 | return valid_envs |
||
| 404 | |||
| 405 | def get_prefix_envname(self, name): |
||
| 406 | """Return full prefix path of environment defined by `name`.""" |
||
| 407 | prefix = None |
||
| 408 | if name == 'root': |
||
| 409 | prefix = self.ROOT_PREFIX |
||
| 410 | |||
| 411 | # envs, error = self.get_envs().communicate() |
||
| 412 | envs = self.get_envs() |
||
| 413 | for p in envs: |
||
| 414 | if basename(p) == name: |
||
| 415 | prefix = p |
||
| 416 | |||
| 417 | return prefix |
||
| 418 | |||
| 419 | @staticmethod |
||
| 420 | def linked(prefix): |
||
| 421 | """Return set of canonical names of linked packages in `prefix`.""" |
||
| 422 | logger.debug(str(prefix)) |
||
| 423 | |||
| 424 | if not isdir(prefix): |
||
| 425 | return set() |
||
| 426 | |||
| 427 | meta_dir = join(prefix, 'conda-meta') |
||
| 428 | if not isdir(meta_dir): |
||
| 429 | # We might have nothing in linked (and no conda-meta directory) |
||
| 430 | return set() |
||
| 431 | |||
| 432 | return set(fn[:-5] for fn in os.listdir(meta_dir) |
||
| 433 | if fn.endswith('.json')) |
||
| 434 | |||
| 435 | @staticmethod |
||
| 436 | def split_canonical_name(cname): |
||
| 437 | """Split a canonical package name into name, version, build.""" |
||
| 438 | return tuple(cname.rsplit('-', 2)) |
||
| 439 | |||
| 440 | def info(self, abspath=True): |
||
| 441 | """ |
||
| 442 | Return a dictionary with configuration information. |
||
| 443 | |||
| 444 | No guarantee is made about which keys exist. Therefore this function |
||
| 445 | should only be used for testing and debugging. |
||
| 446 | """ |
||
| 447 | View Code Duplication | logger.debug(str('')) |
|
|
|
|||
| 448 | return self._call_and_parse(['info', '--json'], abspath=abspath) |
||
| 449 | |||
| 450 | def package_info(self, package, abspath=True): |
||
| 451 | """Return a dictionary with package information.""" |
||
| 452 | return self._call_and_parse(['info', package, '--json'], |
||
| 453 | abspath=abspath) |
||
| 454 | |||
| 455 | def search(self, regex=None, spec=None, **kwargs): |
||
| 456 | """Search for packages.""" |
||
| 457 | cmd_list = ['search', '--json'] |
||
| 458 | |||
| 459 | if regex and spec: |
||
| 460 | raise TypeError('conda search: only one of regex or spec allowed') |
||
| 461 | |||
| 462 | if regex: |
||
| 463 | cmd_list.append(regex) |
||
| 464 | |||
| 465 | if spec: |
||
| 466 | cmd_list.extend(['--spec', spec]) |
||
| 467 | |||
| 468 | if 'platform' in kwargs: |
||
| 469 | cmd_list.extend(['--platform', kwargs.pop('platform')]) |
||
| 470 | |||
| 471 | cmd_list.extend( |
||
| 472 | self._setup_install_commands_from_kwargs( |
||
| 473 | kwargs, |
||
| 474 | ('canonical', 'unknown', 'use_index_cache', 'outdated', |
||
| 475 | 'override_channels'))) |
||
| 476 | |||
| 477 | return self._call_and_parse(cmd_list, |
||
| 478 | abspath=kwargs.get('abspath', True)) |
||
| 479 | |||
| 480 | def create_from_yaml(self, name, yamlfile): |
||
| 481 | """ |
||
| 482 | Create new environment using conda-env via a yaml specification file. |
||
| 483 | |||
| 484 | Unlike other methods, this calls conda-env, and requires a named |
||
| 485 | environment and uses channels as defined in rcfiles. |
||
| 486 | |||
| 487 | Parameters |
||
| 488 | ---------- |
||
| 489 | name : string |
||
| 490 | Environment name |
||
| 491 | yamlfile : string |
||
| 492 | Path to yaml file with package spec (as created by conda env export |
||
| 493 | """ |
||
| 494 | logger.debug(str((name, yamlfile))) |
||
| 495 | cmd_list = ['env', 'create', '-n', name, '-f', yamlfile, '--json'] |
||
| 496 | return self._call_and_parse(cmd_list) |
||
| 497 | |||
| 498 | def create(self, name=None, prefix=None, pkgs=None, channels=None): |
||
| 499 | """Create an environment with a specified set of packages.""" |
||
| 500 | logger.debug(str((prefix, pkgs, channels))) |
||
| 501 | |||
| 502 | # TODO: Fix temporal hack |
||
| 503 | if (not pkgs or (not isinstance(pkgs, (list, tuple)) and |
||
| 504 | not is_text_string(pkgs))): |
||
| 505 | raise TypeError('must specify a list of one or more packages to ' |
||
| 506 | 'install into new environment') |
||
| 507 | |||
| 508 | cmd_list = ['create', '--yes', '--json', '--mkdir'] |
||
| 509 | if name: |
||
| 510 | ref = name |
||
| 511 | search = [os.path.join(d, name) for d in |
||
| 512 | self.info().communicate()[0]['envs_dirs']] |
||
| 513 | cmd_list.extend(['--name', name]) |
||
| 514 | elif prefix: |
||
| 515 | ref = prefix |
||
| 516 | search = [prefix] |
||
| 517 | cmd_list.extend(['--prefix', prefix]) |
||
| 518 | else: |
||
| 519 | raise TypeError('must specify either an environment name or a ' |
||
| 520 | 'path for new environment') |
||
| 521 | |||
| 522 | if any(os.path.exists(prefix) for prefix in search): |
||
| 523 | raise CondaEnvExistsError('Conda environment {0} already ' |
||
| 524 | 'exists'.format(ref)) |
||
| 525 | |||
| 526 | # TODO: Fix temporal hack |
||
| 527 | if isinstance(pkgs, (list, tuple)): |
||
| 528 | cmd_list.extend(pkgs) |
||
| 529 | elif is_text_string(pkgs): |
||
| 530 | cmd_list.extend(['--file', pkgs]) |
||
| 531 | |||
| 532 | # TODO: Check if correct |
||
| 533 | if channels: |
||
| 534 | cmd_list.extend(['--override-channels']) |
||
| 535 | |||
| 536 | for channel in channels: |
||
| 537 | cmd_list.extend(['--channel']) |
||
| 538 | cmd_list.extend([channel]) |
||
| 539 | |||
| 540 | return self._call_and_parse(cmd_list) |
||
| 541 | |||
| 542 | def parse_token_channel(self, channel, token): |
||
| 543 | """ |
||
| 544 | Adapt a channel to include token of the logged user. |
||
| 545 | |||
| 546 | Ignore default channels. |
||
| 547 | """ |
||
| 548 | if (token and channel not in self.DEFAULT_CHANNELS and |
||
| 549 | channel != 'defaults'): |
||
| 550 | url_parts = channel.split('/') |
||
| 551 | start = url_parts[:-1] |
||
| 552 | middle = 't/{0}'.format(token) |
||
| 553 | end = url_parts[-1] |
||
| 554 | token_channel = '{0}/{1}/{2}'.format('/'.join(start), middle, end) |
||
| 555 | return token_channel |
||
| 556 | else: |
||
| 557 | return channel |
||
| 558 | |||
| 559 | def install(self, name=None, prefix=None, pkgs=None, dep=True, |
||
| 560 | channels=None, token=None): |
||
| 561 | """ |
||
| 562 | Install a set of packages into an environment by name or path. |
||
| 563 | |||
| 564 | If token is specified, the channels different from the defaults will |
||
| 565 | get the token appended. |
||
| 566 | """ |
||
| 567 | logger.debug(str((prefix, pkgs, channels))) |
||
| 568 | |||
| 569 | # TODO: Fix temporal hack |
||
| 570 | if not pkgs or not isinstance(pkgs, (list, tuple, str)): |
||
| 571 | raise TypeError('must specify a list of one or more packages to ' |
||
| 572 | 'install into existing environment') |
||
| 573 | |||
| 574 | cmd_list = ['install', '--yes', '--json', '--force-pscheck'] |
||
| 575 | if name: |
||
| 576 | cmd_list.extend(['--name', name]) |
||
| 577 | elif prefix: |
||
| 578 | cmd_list.extend(['--prefix', prefix]) |
||
| 579 | else: |
||
| 580 | # Just install into the current environment, whatever that is |
||
| 581 | pass |
||
| 582 | |||
| 583 | # TODO: Check if correct |
||
| 584 | if channels: |
||
| 585 | cmd_list.extend(['--override-channels']) |
||
| 586 | |||
| 587 | for channel in channels: |
||
| 588 | cmd_list.extend(['--channel']) |
||
| 589 | channel = self.parse_token_channel(channel, token) |
||
| 590 | cmd_list.extend([channel]) |
||
| 591 | |||
| 592 | # TODO: Fix temporal hack |
||
| 593 | if isinstance(pkgs, (list, tuple)): |
||
| 594 | cmd_list.extend(pkgs) |
||
| 595 | elif isinstance(pkgs, str): |
||
| 596 | cmd_list.extend(['--file', pkgs]) |
||
| 597 | |||
| 598 | if not dep: |
||
| 599 | cmd_list.extend(['--no-deps']) |
||
| 600 | |||
| 601 | return self._call_and_parse(cmd_list) |
||
| 602 | |||
| 603 | def update(self, *pkgs, **kwargs): |
||
| 604 | """Update package(s) (in an environment) by name.""" |
||
| 605 | cmd_list = ['update', '--json', '--yes'] |
||
| 606 | |||
| 607 | if not pkgs and not kwargs.get('all'): |
||
| 608 | raise TypeError("Must specify at least one package to update, or " |
||
| 609 | "all=True.") |
||
| 610 | |||
| 611 | cmd_list.extend( |
||
| 612 | self._setup_install_commands_from_kwargs( |
||
| 613 | kwargs, |
||
| 614 | ('dry_run', 'no_deps', 'override_channels', |
||
| 615 | 'no_pin', 'force', 'all', 'use_index_cache', 'use_local', |
||
| 616 | 'alt_hint'))) |
||
| 617 | |||
| 618 | cmd_list.extend(pkgs) |
||
| 619 | |||
| 620 | return self._call_and_parse(cmd_list, abspath=kwargs.get('abspath', |
||
| 621 | True)) |
||
| 622 | |||
| 623 | def remove(self, name=None, prefix=None, pkgs=None, all_=False): |
||
| 624 | """ |
||
| 625 | Remove a package (from an environment) by name. |
||
| 626 | |||
| 627 | Returns { |
||
| 628 | success: bool, (this is always true), |
||
| 629 | (other information) |
||
| 630 | } |
||
| 631 | """ |
||
| 632 | logger.debug(str((prefix, pkgs))) |
||
| 633 | |||
| 634 | cmd_list = ['remove', '--json', '--yes'] |
||
| 635 | |||
| 636 | if not pkgs and not all_: |
||
| 637 | raise TypeError("Must specify at least one package to remove, or " |
||
| 638 | "all=True.") |
||
| 639 | |||
| 640 | if name: |
||
| 641 | cmd_list.extend(['--name', name]) |
||
| 642 | elif prefix: |
||
| 643 | cmd_list.extend(['--prefix', prefix]) |
||
| 644 | View Code Duplication | else: |
|
| 645 | raise TypeError('must specify either an environment name or a ' |
||
| 646 | 'path for package removal') |
||
| 647 | |||
| 648 | if all_: |
||
| 649 | cmd_list.extend(['--all']) |
||
| 650 | else: |
||
| 651 | cmd_list.extend(pkgs) |
||
| 652 | |||
| 653 | return self._call_and_parse(cmd_list) |
||
| 654 | |||
| 655 | def remove_environment(self, name=None, path=None, **kwargs): |
||
| 656 | """ |
||
| 657 | Remove an environment entirely. |
||
| 658 | |||
| 659 | See ``remove``. |
||
| 660 | """ |
||
| 661 | return self.remove(name=name, path=path, all=True, **kwargs) |
||
| 662 | |||
| 663 | def clone_environment(self, clone, name=None, prefix=None, **kwargs): |
||
| 664 | """Clone the environment `clone` into `name` or `prefix`.""" |
||
| 665 | cmd_list = ['create', '--json'] |
||
| 666 | |||
| 667 | if (name and prefix) or not (name or prefix): |
||
| 668 | raise TypeError("conda clone_environment: exactly one of `name` " |
||
| 669 | "or `path` required") |
||
| 670 | |||
| 671 | if name: |
||
| 672 | cmd_list.extend(['--name', name]) |
||
| 673 | |||
| 674 | if prefix: |
||
| 675 | cmd_list.extend(['--prefix', prefix]) |
||
| 676 | |||
| 677 | cmd_list.extend(['--clone', clone]) |
||
| 678 | |||
| 679 | cmd_list.extend( |
||
| 680 | self._setup_install_commands_from_kwargs( |
||
| 681 | kwargs, |
||
| 682 | ('dry_run', 'unknown', 'use_index_cache', 'use_local', |
||
| 683 | 'no_pin', 'force', 'all', 'channel', 'override_channels', |
||
| 684 | 'no_default_packages'))) |
||
| 685 | |||
| 686 | return self._call_and_parse(cmd_list, abspath=kwargs.get('abspath', |
||
| 687 | True)) |
||
| 688 | |||
| 689 | # FIXME: |
||
| 690 | # def process(self, name=None, prefix=None, cmd=None): |
||
| 691 | # """Create a Popen process for cmd inside specified conda env. |
||
| 692 | # |
||
| 693 | # The returned object will need to be invoked with p.communicate() or |
||
| 694 | # similar. |
||
| 695 | # """ |
||
| 696 | # if bool(name) == bool(prefix): |
||
| 697 | # raise TypeError('exactly one of name or prefix must be specified') |
||
| 698 | # |
||
| 699 | # if not cmd: |
||
| 700 | # raise TypeError('cmd to execute must be specified') |
||
| 701 | # |
||
| 702 | # if not args: |
||
| 703 | # args = [] |
||
| 704 | # |
||
| 705 | # if name: |
||
| 706 | # prefix = self.get_prefix_envname(name) |
||
| 707 | # |
||
| 708 | # conda_env = dict(os.environ) |
||
| 709 | # sep = os.pathsep |
||
| 710 | # |
||
| 711 | # if sys.platform == 'win32': |
||
| 712 | # conda_env['PATH'] = join(prefix, |
||
| 713 | # 'Scripts') + sep + conda_env['PATH'] |
||
| 714 | # else: |
||
| 715 | # # Unix |
||
| 716 | # conda_env['PATH'] = join(prefix, 'bin') + sep + conda_env['PATH'] |
||
| 717 | # |
||
| 718 | # conda_env['PATH'] = prefix + os.pathsep + conda_env['PATH'] |
||
| 719 | # |
||
| 720 | # cmd_list = [cmd] |
||
| 721 | # cmd_list.extend(args) |
||
| 722 | |||
| 723 | # = self.subprocess.process(cmd_list, env=conda_env, stdin=stdin, |
||
| 724 | # stdout=stdout, stderr=stderr) |
||
| 725 | |||
| 726 | @staticmethod |
||
| 727 | def _setup_config_from_kwargs(kwargs): |
||
| 728 | """Setup config commands for conda.""" |
||
| 729 | cmd_list = ['--json', '--force'] |
||
| 730 | |||
| 731 | if 'file' in kwargs: |
||
| 732 | cmd_list.extend(['--file', kwargs['file']]) |
||
| 733 | |||
| 734 | if 'system' in kwargs: |
||
| 735 | cmd_list.append('--system') |
||
| 736 | |||
| 737 | return cmd_list |
||
| 738 | |||
| 739 | # def config_path(self, **kwargs): |
||
| 740 | # """Get the path to the config file.""" |
||
| 741 | # cmd_list = ['config', '--get'] |
||
| 742 | # cmd_list.extend(self._setup_config_from_kwargs(kwargs)) |
||
| 743 | # |
||
| 744 | # return self._call_and_parse(cmd_list, |
||
| 745 | # abspath=kwargs.get('abspath', True), |
||
| 746 | # callback=lambda o, e: o['rc_path']) |
||
| 747 | |||
| 748 | # def config_get(self, *keys, **kwargs): |
||
| 749 | # """ |
||
| 750 | # Get the values of configuration keys. |
||
| 751 | # |
||
| 752 | # Returns a dictionary of values. Note, the key may not be in the |
||
| 753 | # dictionary if the key wasn't set in the configuration file. |
||
| 754 | # """ |
||
| 755 | # cmd_list = ['config', '--get'] |
||
| 756 | # cmd_list.extend(keys) |
||
| 757 | # cmd_list.extend(self._setup_config_from_kwargs(kwargs)) |
||
| 758 | # |
||
| 759 | # return self._call_and_parse(cmd_list, |
||
| 760 | # abspath=kwargs.get('abspath', True), |
||
| 761 | # callback=lambda o, e: o['get']) |
||
| 762 | |||
| 763 | # def config_set(self, key, value, **kwargs): |
||
| 764 | # """ |
||
| 765 | # Set a key to a (bool) value. |
||
| 766 | # |
||
| 767 | # Returns a list of warnings Conda may have emitted. |
||
| 768 | # """ |
||
| 769 | # cmd_list = ['config', '--set', key, str(value)] |
||
| 770 | # cmd_list.extend(self._setup_config_from_kwargs(kwargs)) |
||
| 771 | # |
||
| 772 | # return self._call_and_parse( |
||
| 773 | # cmd_list, |
||
| 774 | # abspath=kwargs.get('abspath', True), |
||
| 775 | # callback=lambda o, e: o.get('warnings', [])) |
||
| 776 | |||
| 777 | def config_add(self, key, value, **kwargs): |
||
| 778 | """ |
||
| 779 | Add a value to a key. |
||
| 780 | |||
| 781 | Returns a list of warnings Conda may have emitted. |
||
| 782 | """ |
||
| 783 | cmd_list = ['config', '--add', key, value] |
||
| 784 | cmd_list.extend(self._setup_config_from_kwargs(kwargs)) |
||
| 785 | |||
| 786 | return self._call_and_parse( |
||
| 787 | cmd_list, |
||
| 788 | abspath=kwargs.get('abspath', True), |
||
| 789 | callback=lambda o, e: o.get('warnings', [])) |
||
| 790 | |||
| 791 | def config_remove(self, key, value, **kwargs): |
||
| 792 | """ |
||
| 793 | Remove a value from a key. |
||
| 794 | |||
| 795 | Returns a list of warnings Conda may have emitted. |
||
| 796 | """ |
||
| 797 | cmd_list = ['config', '--remove', key, value] |
||
| 798 | cmd_list.extend(self._setup_config_from_kwargs(kwargs)) |
||
| 799 | |||
| 800 | return self._call_and_parse( |
||
| 801 | cmd_list, |
||
| 802 | abspath=kwargs.get('abspath', True), |
||
| 803 | callback=lambda o, e: o.get('warnings', [])) |
||
| 804 | |||
| 805 | # def config_delete(self, key, **kwargs): |
||
| 806 | # """ |
||
| 807 | # Remove a key entirely. |
||
| 808 | # |
||
| 809 | # Returns a list of warnings Conda may have emitted. |
||
| 810 | # """ |
||
| 811 | # cmd_list = ['config', '--remove-key', key] |
||
| 812 | # cmd_list.extend(self._setup_config_from_kwargs(kwargs)) |
||
| 813 | # |
||
| 814 | # return self._call_and_parse( |
||
| 815 | # cmd_list, |
||
| 816 | # abspath=kwargs.get('abspath', True), |
||
| 817 | # callback=lambda o, e: o.get('warnings', [])) |
||
| 818 | |||
| 819 | # def run(self, command, abspath=True): |
||
| 820 | # """ |
||
| 821 | # Launch the specified app by name or full package name. |
||
| 822 | # |
||
| 823 | # Returns a dictionary containing the key "fn", whose value is the full |
||
| 824 | # package (ending in ``.tar.bz2``) of the app. |
||
| 825 | # """ |
||
| 826 | # cmd_list = ['run', '--json', command] |
||
| 827 | # |
||
| 828 | # return self._call_and_parse(cmd_list, abspath=abspath) |
||
| 829 | |||
| 830 | # --- Additional methods |
||
| 831 | # ----------------------------------------------------------------------------- |
||
| 832 | def dependencies(self, name=None, prefix=None, pkgs=None, channels=None, |
||
| 833 | dep=True): |
||
| 834 | """Get dependenciy list for packages to be installed in an env.""" |
||
| 835 | if not pkgs or not isinstance(pkgs, (list, tuple)): |
||
| 836 | raise TypeError('must specify a list of one or more packages to ' |
||
| 837 | 'install into existing environment') |
||
| 838 | |||
| 839 | cmd_list = ['install', '--dry-run', '--json', '--force-pscheck'] |
||
| 840 | |||
| 841 | if not dep: |
||
| 842 | cmd_list.extend(['--no-deps']) |
||
| 843 | |||
| 844 | if name: |
||
| 845 | cmd_list.extend(['--name', name]) |
||
| 846 | elif prefix: |
||
| 847 | cmd_list.extend(['--prefix', prefix]) |
||
| 848 | else: |
||
| 849 | pass |
||
| 850 | |||
| 851 | cmd_list.extend(pkgs) |
||
| 852 | |||
| 853 | # TODO: Check if correct |
||
| 854 | if channels: |
||
| 855 | cmd_list.extend(['--override-channels']) |
||
| 856 | |||
| 857 | for channel in channels: |
||
| 858 | cmd_list.extend(['--channel']) |
||
| 859 | cmd_list.extend([channel]) |
||
| 860 | |||
| 861 | return self._call_and_parse(cmd_list) |
||
| 862 | |||
| 863 | def environment_exists(self, name=None, prefix=None, abspath=True, |
||
| 864 | log=True): |
||
| 865 | """Check if an environment exists by 'name' or by 'prefix'. |
||
| 866 | |||
| 867 | If query is by 'name' only the default conda environments directory is |
||
| 868 | searched. |
||
| 869 | """ |
||
| 870 | if log: |
||
| 871 | logger.debug(str((name, prefix))) |
||
| 872 | |||
| 873 | if name and prefix: |
||
| 874 | raise TypeError("Exactly one of 'name' or 'prefix' is required.") |
||
| 875 | |||
| 876 | if name: |
||
| 877 | prefix = self.get_prefix_envname(name, log=log) |
||
| 878 | |||
| 879 | if prefix is None: |
||
| 880 | prefix = self.ROOT_PREFIX |
||
| 881 | |||
| 882 | return os.path.isdir(os.path.join(prefix, 'conda-meta')) |
||
| 883 | |||
| 884 | def clear_lock(self, abspath=True): |
||
| 885 | """Clean any conda lock in the system.""" |
||
| 886 | cmd_list = ['clean', '--lock', '--json'] |
||
| 887 | return self._call_and_parse(cmd_list, abspath=abspath) |
||
| 888 | |||
| 889 | def package_version(self, prefix=None, name=None, pkg=None, build=False): |
||
| 890 | """Get installed package version in a given env.""" |
||
| 891 | package_versions = {} |
||
| 892 | |||
| 893 | if name and prefix: |
||
| 894 | raise TypeError("Exactly one of 'name' or 'prefix' is required.") |
||
| 895 | |||
| 896 | if name: |
||
| 897 | prefix = self.get_prefix_envname(name) |
||
| 898 | |||
| 899 | if self.environment_exists(prefix=prefix): |
||
| 900 | |||
| 901 | for package in self.linked(prefix): |
||
| 902 | if pkg in package: |
||
| 903 | n, v, b = self.split_canonical_name(package) |
||
| 904 | if build: |
||
| 905 | package_versions[n] = '{0}={1}'.format(v, b) |
||
| 906 | else: |
||
| 907 | package_versions[n] = v |
||
| 908 | |||
| 909 | return package_versions.get(pkg) |
||
| 910 | |||
| 911 | @staticmethod |
||
| 912 | def get_platform(): |
||
| 913 | """Get platform of current system (system and bitness).""" |
||
| 914 | _sys_map = {'linux2': 'linux', 'linux': 'linux', |
||
| 915 | 'darwin': 'osx', 'win32': 'win', 'openbsd5': 'openbsd'} |
||
| 916 | |||
| 917 | non_x86_linux_machines = {'armv6l', 'armv7l', 'ppc64le'} |
||
| 918 | sys_platform = _sys_map.get(sys.platform, 'unknown') |
||
| 919 | bits = 8 * tuple.__itemsize__ |
||
| 920 | |||
| 921 | if (sys_platform == 'linux' and |
||
| 922 | platform.machine() in non_x86_linux_machines): |
||
| 923 | arch_name = platform.machine() |
||
| 924 | subdir = 'linux-{0}'.format(arch_name) |
||
| 925 | else: |
||
| 926 | arch_name = {64: 'x86_64', 32: 'x86'}[bits] |
||
| 927 | subdir = '{0}-{1}'.format(sys_platform, bits) |
||
| 928 | |||
| 929 | return subdir |
||
| 930 | |||
| 931 | def load_rc(self, path=None, system=False): |
||
| 932 | """ |
||
| 933 | Load the conda configuration file. |
||
| 934 | |||
| 935 | If both user and system configuration exists, user will be used. |
||
| 936 | """ |
||
| 937 | if os.path.isfile(self.user_rc_path) and not system: |
||
| 938 | path = self.user_rc_path |
||
| 939 | elif os.path.isfile(self.sys_rc_path): |
||
| 940 | path = self.sys_rc_path |
||
| 941 | |||
| 942 | if not path or not os.path.isfile(path): |
||
| 943 | return {} |
||
| 944 | |||
| 945 | with open(path) as f: |
||
| 946 | return yaml.load(f) or {} |
||
| 947 | |||
| 948 | def get_condarc_channels(self, |
||
| 949 | normalize=False, |
||
| 950 | conda_url='https://conda.anaconda.org', |
||
| 951 | channels=None): |
||
| 952 | """Return all the channel urls defined in .condarc. |
||
| 953 | |||
| 954 | If no condarc file is found, use the default channels. |
||
| 955 | the `default_channel_alias` key is ignored and only the anaconda client |
||
| 956 | `url` key is used. |
||
| 957 | """ |
||
| 958 | # https://docs.continuum.io/anaconda-repository/configuration |
||
| 959 | # They can only exist on a system condarc |
||
| 960 | default_channels = self.load_rc(system=True).get('default_channels', |
||
| 961 | self.DEFAULT_CHANNELS) |
||
| 962 | |||
| 963 | normalized_channels = [] |
||
| 964 | if channels is None: |
||
| 965 | condarc = self.load_rc() |
||
| 966 | channels = condarc.get('channels') |
||
| 967 | |||
| 968 | if channels is None: |
||
| 969 | channels = ['defaults'] |
||
| 970 | |||
| 971 | if normalize: |
||
| 972 | template = '{0}/{1}' if conda_url[-1] != '/' else '{0}{1}' |
||
| 973 | for channel in channels: |
||
| 974 | if channel == 'defaults': |
||
| 975 | normalized_channels += default_channels |
||
| 976 | elif channel.startswith('http'): |
||
| 977 | normalized_channels.append(channel) |
||
| 978 | else: |
||
| 979 | # Append to the conda_url that comes from anaconda client |
||
| 980 | # default_channel_alias key is deliberately ignored |
||
| 981 | normalized_channels.append(template.format(conda_url, |
||
| 982 | channel)) |
||
| 983 | channels = normalized_channels |
||
| 984 | |||
| 985 | return channels |
||
| 986 | |||
| 987 | # --- Pip commands |
||
| 988 | # ------------------------------------------------------------------------- |
||
| 989 | def _call_pip(self, name=None, prefix=None, extra_args=None, |
||
| 990 | callback=None): |
||
| 991 | """Call pip in QProcess worker.""" |
||
| 992 | cmd_list = self._pip_cmd(name=name, prefix=prefix) |
||
| 993 | cmd_list.extend(extra_args) |
||
| 994 | |||
| 995 | process_worker = ProcessWorker(cmd_list, pip=True, callback=callback) |
||
| 996 | process_worker.sig_finished.connect(self._start) |
||
| 997 | self._queue.append(process_worker) |
||
| 998 | self._start() |
||
| 999 | |||
| 1000 | return process_worker |
||
| 1001 | |||
| 1002 | def _pip_cmd(self, name=None, prefix=None): |
||
| 1003 | """Get pip location based on environment `name` or `prefix`.""" |
||
| 1004 | if (name and prefix) or not (name or prefix): |
||
| 1005 | raise TypeError("conda pip: exactly one of 'name' ""or 'prefix' " |
||
| 1006 | "required.") |
||
| 1007 | |||
| 1008 | if name and self.environment_exists(name=name): |
||
| 1009 | prefix = self.get_prefix_envname(name) |
||
| 1010 | |||
| 1011 | if sys.platform == 'win32': |
||
| 1012 | python = join(prefix, 'python.exe') # FIXME: |
||
| 1013 | pip = join(prefix, 'pip.exe') # FIXME: |
||
| 1014 | else: |
||
| 1015 | python = join(prefix, 'bin/python') |
||
| 1016 | pip = join(prefix, 'bin/pip') |
||
| 1017 | |||
| 1018 | cmd_list = [python, pip] |
||
| 1019 | |||
| 1020 | return cmd_list |
||
| 1021 | |||
| 1022 | def pip_list(self, name=None, prefix=None, abspath=True): |
||
| 1023 | """Get list of pip installed packages.""" |
||
| 1024 | if (name and prefix) or not (name or prefix): |
||
| 1025 | raise TypeError("conda pip: exactly one of 'name' ""or 'prefix' " |
||
| 1026 | "required.") |
||
| 1027 | |||
| 1028 | if name: |
||
| 1029 | prefix = self.get_prefix_envname(name) |
||
| 1030 | |||
| 1031 | pip_command = os.sep.join([prefix, 'bin', 'python']) |
||
| 1032 | cmd_list = [pip_command, PIP_LIST_SCRIPT] |
||
| 1033 | process_worker = ProcessWorker(cmd_list, pip=True, parse=True, |
||
| 1034 | callback=self._pip_list, |
||
| 1035 | extra_kwargs={'prefix': prefix}) |
||
| 1036 | process_worker.sig_finished.connect(self._start) |
||
| 1037 | self._queue.append(process_worker) |
||
| 1038 | self._start() |
||
| 1039 | |||
| 1040 | return process_worker |
||
| 1041 | |||
| 1042 | def _pip_list(self, stdout, stderr, prefix=None): |
||
| 1043 | """Callback for `pip_list`.""" |
||
| 1044 | result = stdout # A dict |
||
| 1045 | linked = self.linked(prefix) |
||
| 1046 | |||
| 1047 | pip_only = [] |
||
| 1048 | |||
| 1049 | linked_names = [self.split_canonical_name(l)[0] for l in linked] |
||
| 1050 | |||
| 1051 | for pkg in result: |
||
| 1052 | name = self.split_canonical_name(pkg)[0] |
||
| 1053 | if name not in linked_names: |
||
| 1054 | pip_only.append(pkg) |
||
| 1055 | # FIXME: NEED A MORE ROBUST WAY! |
||
| 1056 | # if '<pip>' in line and '#' not in line: |
||
| 1057 | # temp = line.split()[:-1] + ['pip'] |
||
| 1058 | # temp = '-'.join(temp) |
||
| 1059 | # if '-(' in temp: |
||
| 1060 | # start = temp.find('-(') |
||
| 1061 | # end = temp.find(')') |
||
| 1062 | # substring = temp[start:end+1] |
||
| 1063 | # temp = temp.replace(substring, '') |
||
| 1064 | # result.append(temp) |
||
| 1065 | |||
| 1066 | return pip_only |
||
| 1067 | |||
| 1068 | def pip_remove(self, name=None, prefix=None, pkgs=None): |
||
| 1069 | """Remove a pip package in given environment by `name` or `prefix`.""" |
||
| 1070 | logger.debug(str((prefix, pkgs))) |
||
| 1071 | |||
| 1072 | if isinstance(pkgs, (list, tuple)): |
||
| 1073 | pkg = ' '.join(pkgs) |
||
| 1074 | else: |
||
| 1075 | pkg = pkgs |
||
| 1076 | |||
| 1077 | extra_args = ['uninstall', '--yes', pkg] |
||
| 1078 | |||
| 1079 | return self._call_pip(name=name, prefix=prefix, extra_args=extra_args) |
||
| 1080 | |||
| 1081 | def pip_search(self, search_string=None): |
||
| 1082 | """Search for pip packages in PyPI matching `search_string`.""" |
||
| 1083 | extra_args = ['search', search_string] |
||
| 1084 | return self._call_pip(name='root', extra_args=extra_args, |
||
| 1085 | callback=self._pip_search) |
||
| 1086 | |||
| 1087 | # if stderr: |
||
| 1088 | # raise PipError(stderr) |
||
| 1089 | # You are using pip version 7.1.2, however version 8.0.2 is available. |
||
| 1090 | # You should consider upgrading via the 'pip install --upgrade pip' |
||
| 1091 | # command. |
||
| 1092 | |||
| 1093 | @staticmethod |
||
| 1094 | def _pip_search(stdout, stderr): |
||
| 1095 | """Callback for pip search.""" |
||
| 1096 | result = {} |
||
| 1097 | lines = to_text_string(stdout).split('\n') |
||
| 1098 | while '' in lines: |
||
| 1099 | lines.remove('') |
||
| 1100 | |||
| 1101 | for line in lines: |
||
| 1102 | if ' - ' in line: |
||
| 1103 | parts = line.split(' - ') |
||
| 1104 | name = parts[0].strip() |
||
| 1105 | description = parts[1].strip() |
||
| 1106 | result[name] = description |
||
| 1107 | |||
| 1108 | return result |
||
| 1109 | |||
| 1110 | |||
| 1111 | CONDA_API = None |
||
| 1112 | |||
| 1113 | |||
| 1114 | def CondaAPI(): |
||
| 1115 | """Conda non blocking api.""" |
||
| 1116 | global CONDA_API |
||
| 1117 | |||
| 1118 | if CONDA_API is None: |
||
| 1119 | CONDA_API = _CondaAPI() |
||
| 1155 |