Skip to content

Commit

Permalink
Don't load binstar_client until needed. Part 2. (conda#10692)
Browse files Browse the repository at this point in the history
* Don't load binstar_client until needed.

* Fix flake8 issues.

* Use importlib for programmatic loading of binstar.

* Actually call get_server_api.

Co-authored-by: Christopher Granade <[email protected]>
  • Loading branch information
jezdez and cgranade authored Jun 7, 2021
1 parent ca6094c commit e14d589
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions conda_env/specs/binstar.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
import importlib
import re

from conda.models.version import normalized_version
from .. import env
from ..exceptions import EnvironmentFileNotDownloaded

try:
from binstar_client import errors
from binstar_client.utils import get_server_api
except ImportError:
get_server_api = None

ENVIRONMENT_TYPE = 'env'
# TODO: isolate binstar related code into conda_env.utils.binstar
Expand All @@ -31,15 +27,12 @@ class BinstarSpec(object):
_packagename = None
_package = None
_file_data = None
_binstar = None
msg = None

def __init__(self, name=None, **kwargs):
self.name = name
self.quiet = False
if get_server_api is not None:
self.binstar = get_server_api()
else:
self.binstar = None

def can_handle(self):
result = self._can_handle()
Expand Down Expand Up @@ -80,6 +73,16 @@ def valid_package(self):
"""
return len(self.file_data) > 0

@property
def binstar(self):
if self._binstar is None:
try:
binstar_utils = importlib.import_module("binstar_client.utils")
self._binstar = binstar_utils.get_server_api()
except (AttributeError, ModuleNotFoundError):
pass
return self._binstar

@property
def file_data(self):
if self._file_data is None:
Expand Down Expand Up @@ -112,7 +115,7 @@ def package(self):
if self._package is None:
try:
self._package = self.binstar.package(self.username, self.packagename)
except errors.NotFound:
except IndexError:
self.msg = "{} was not found on anaconda.org.\n"\
"You may need to be logged in. Try running:\n"\
" anaconda login".format(self.name)
Expand Down

0 comments on commit e14d589

Please sign in to comment.