diff --git a/CHANGELOG.md b/CHANGELOG.md index 45adb51d..d551066e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,21 @@ We [keep a changelog.](http://keepachangelog.com/) +## 1.12.2 + +### Tickets closed + +* AC-192 - SPIKE: Remove unnecessary /user calls +* AC-194 - Check token before calling /user endpoint +* AC-200 - Replacing vendored appdirs with platformdirs + +### Pull requests merged + +* [PR 693](https://github.com/Anaconda-Platform/anaconda-client/pull/693) - AC-200: replace appdirs with platformdirs +* [PR 691](https://github.com/Anaconda-Platform/anaconda-client/pull/691) - Support Python 3.12 +* [PR 688](https://github.com/Anaconda-Platform/anaconda-client/pull/688) - AC-194: check token before making /user call +* [PR 680](https://github.com/Anaconda-Platform/anaconda-client/pull/680) - use conda.gateways.anaconda_client for tokens possible + ## 1.12.1 - 2023-09-13 ### Tickets closed diff --git a/binstar_client/__about__.py b/binstar_client/__about__.py index c83d8d27..7c187431 100644 --- a/binstar_client/__about__.py +++ b/binstar_client/__about__.py @@ -4,4 +4,4 @@ __all__ = ['__version__'] -__version__ = '1.12.1' +__version__ = '1.12.2' diff --git a/binstar_client/__init__.py b/binstar_client/__init__.py index 4b4c68f2..363f08a6 100644 --- a/binstar_client/__init__.py +++ b/binstar_client/__init__.py @@ -236,9 +236,12 @@ def user(self, login=None): this method will return the information of the authenticated user. """ if login: - url = '%s/user/%s' % (self.domain, login) + url = f'{self.domain}/user/{login}' + elif self.token: + url = f'{self.domain}/user' else: - url = '%s/user' % (self.domain) + raise errors.Unauthorized( + 'Authentication token is missing. Please, use `anaconda login` to reauthenticate.', 401) res = self.session.get(url, verify=self.session.verify) self._check_response(res) diff --git a/binstar_client/errors.py b/binstar_client/errors.py index aa881f1c..aa1819ab 100644 --- a/binstar_client/errors.py +++ b/binstar_client/errors.py @@ -1,9 +1,6 @@ # pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring -from clyent.errors import ClyentError - - -class BinstarError(ClyentError): +class BinstarError(Exception): def __init__(self, *args): super().__init__(*args) diff --git a/binstar_client/scripts/cli.py b/binstar_client/scripts/cli.py index dd5fc518..c3471b43 100644 --- a/binstar_client/scripts/cli.py +++ b/binstar_client/scripts/cli.py @@ -8,14 +8,14 @@ import argparse from importlib import metadata +import json import logging import os +import pkgutil import sys import types import typing -from clyent import add_subparser_modules - from binstar_client import __version__ from binstar_client import commands from binstar_client import errors @@ -26,6 +26,13 @@ logger = logging.getLogger('binstar') +def _get_entry_points(group: str) -> typing.List[metadata.EntryPoint]: + # The API was changed in Python 3.10, see https://docs.python.org/3/library/importlib.metadata.html#entry-points + if sys.version_info.major == 3 and sys.version_info.minor < 10: + return metadata.entry_points().get(group, []) + return metadata.entry_points().select(group=group) # type: ignore + + def file_or_token(value: str) -> str: """ Retrieve a token from input. @@ -49,6 +56,99 @@ def file_or_token(value: str) -> str: return value +def _json_action(action): + # pylint: disable=protected-access # intentional access of argparse object members + a_data = dict(action._get_kwargs()) + + if a_data.get('help'): + a_data['help'] = a_data['help'] % a_data + + if isinstance(action, argparse._SubParsersAction): + a_data.pop('choices', None) + choices = {} + for choice in action._get_subactions(): + choices[choice.dest] = choice.help + a_data['choices'] = choices + + reg = {value: key for key, value in action.container._registries['action'].items()} + a_data['action'] = reg.get(type(action), type(action).__name__) + if a_data['action'] == 'store' and not a_data.get('metavar'): + a_data['metavar'] = action.dest.upper() + + a_data.pop('type', None) + a_data.pop('default', None) + + return a_data + + +def _json_group(group): + # pylint: disable=protected-access # intentional access of argparse object members + grp_data = { + 'description': group.description, + 'title': group.title, + 'actions': [_json_action(action) for action in group._group_actions if action.help != argparse.SUPPRESS], + } + + if group._action_groups: + grp_data['groups'] = [_json_group(group) for group in group._action_groups] + + return grp_data + + +class _JSONHelp(argparse.Action): + # pylint: disable-next=redefined-builtin + def __init__(self, option_strings, dest, nargs=0, help=argparse.SUPPRESS, **kwargs): + argparse.Action.__init__(self, option_strings, dest, nargs=nargs, help=help, **kwargs) + + def __call__(self, parser, namespace, values, option_string=None): + # pylint: disable=protected-access # intentional access of argparse object members + self.nargs = 0 + docs = { + 'prog': parser.prog, + 'usage': parser.format_usage()[7:], + 'description': parser.description, + 'epilog': parser.epilog, + } + + docs['groups'] = [] + for group in parser._action_groups: + if group._group_actions: + docs['groups'].append(_json_group(group)) + + json.dump(docs, sys.stdout, indent=2) + raise SystemExit(0) + + +def _get_sub_command_names(module): + return [name for _, name, _ in pkgutil.iter_modules([os.path.dirname(module.__file__)]) if not name.startswith('_')] + + +def _get_sub_commands(module): + names = _get_sub_command_names(module) + this_module = __import__(module.__package__ or module.__name__, fromlist=names) + + for name in names: + yield getattr(this_module, name) + + +def _add_subparser_modules(parser, module=None, entry_point_name=None): + + subparsers = parser.add_subparsers(title='Commands', metavar='') + + if module: # LOAD sub parsers from module + for command_module in _get_sub_commands(module): + command_module.add_parser(subparsers) + + if entry_point_name: # LOAD sub parsers from setup.py entry_point + for entry_point in _get_entry_points(entry_point_name): + add_parser = entry_point.load() + add_parser(subparsers) + + for key, sub_parser in subparsers.choices.items(): + sub_parser.set_defaults(sub_command_name=key) + sub_parser.add_argument('--json-help', action=_JSONHelp) + + def binstar_main( sub_command_module: types.ModuleType, args: typing.Optional[typing.Sequence[str]] = None, @@ -89,7 +189,7 @@ def binstar_main( '-V', '--version', action='version', version=f'%(prog)s Command line client (version {__version__})', ) - add_subparser_modules(parser, sub_command_module, 'conda_server.subcommand') + _add_subparser_modules(parser, sub_command_module, 'conda_server.subcommand') arguments: argparse.Namespace = parser.parse_args(args) @@ -123,12 +223,7 @@ def _load_main_plugin() -> typing.Optional[typing.Callable[[], typing.Any]]: """Allow loading a new CLI main entrypoint via plugin mechanisms. There can only be one.""" plugin_group_name: typing.Final[str] = 'anaconda_cli.main' - # The API was changed in Python 3.10, see https://docs.python.org/3/library/importlib.metadata.html#entry-points - plugin_mains: typing.List[metadata.EntryPoint] - if sys.version_info.major == 3 and sys.version_info.minor < 10: - plugin_mains = metadata.entry_points().get(plugin_group_name, []) - else: - plugin_mains = metadata.entry_points().select(group=plugin_group_name) # type: ignore + plugin_mains: typing.List[metadata.EntryPoint] = _get_entry_points(plugin_group_name) if len(plugin_mains) > 1: raise EnvironmentError( diff --git a/binstar_client/utils/appdirs.py b/binstar_client/utils/appdirs.py index 5ba4e664..cbc17d1e 100644 --- a/binstar_client/utils/appdirs.py +++ b/binstar_client/utils/appdirs.py @@ -2,210 +2,15 @@ # pylint: disable=redefined-outer-name,import-outside-toplevel,line-too-long # pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring -# Copyright (c) 2005-2010 ActiveState Software Inc. -"""Utilities for determining application-specific dirs. +"""Utilities for determining application-specific dirs.""" -See for details and usage. -""" -# Dev Notes: -# - MSDN on where to store app data files: -# http://support.microsoft.com/default.aspx?scid=kb;en-us;310294#XSLTH3194121123120121120120 -# - Mac OS X: http://developer.apple.com/documentation/MacOSX/Conceptual/BPFileSystem/index.html -# - XDG spec for Un*x: http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html - -__version_info__ = (1, 2, 0) -__version__ = '.'.join(map(str, __version_info__)) - - -import sys -import os - - -class AppDirsError(Exception): - pass - - -def user_data_dir(appname, appauthor=None, version=None, roaming=False): - r"""Return full path to the user-specific data dir for this application. - - "appname" is the name of application. - "appauthor" (only required and used on Windows) is the name of the - appauthor or distributing body for this application. Typically - it is the owning company name. - "version" is an optional version path element to append to the - path. You might want to use this if you want multiple versions - of your app to be able to run independently. If used, this - would typically be ".". - "roaming" (boolean, default False) can be set True to use the Windows - roaming appdata directory. That means that for users on a Windows - network setup for roaming profiles, this user data will be - sync'd on login. See - - for a discussion of issues. - - Typical user data directories are: - Mac OS X: ~/Library/Application Support/ - Unix: ~/.config/ # or in $XDG_CONFIG_HOME if defined - Win XP (not roaming): C:\Documents and Settings\\Application Data\\ - Win XP (roaming): - C:\Documents and Settings\\Local Settings\Application Data\\ - Win 7 (not roaming): C:\Users\\AppData\Local\\ - Win 7 (roaming): C:\Users\\AppData\Roaming\\ - - For Unix, we follow the XDG spec and support $XDG_CONFIG_HOME. We don't - use $XDG_DATA_HOME as that data dir is mostly used at the time of - installation, instead of the application adding data during runtime. - Also, in practice, Linux apps tend to store their data in - "~/.config/" instead of "~/.local/share/". - """ - if sys.platform.startswith('win'): - if appauthor is None: - raise AppDirsError('must specify \'appauthor\' on Windows') - const = roaming and 'CSIDL_APPDATA' or 'CSIDL_LOCAL_APPDATA' # pylint: disable=consider-using-ternary - path = os.path.join(_get_win_folder(const), appauthor, appname) - elif sys.platform == 'darwin': - path = os.path.join(os.path.expanduser('~/Library/Application Support/'), appname) - else: - path = os.path.join(os.getenv('XDG_CONFIG_HOME', os.path.expanduser('~/.config')), appname.lower()) - if version: - path = os.path.join(path, version) - return path - - -def site_data_dir(appname, appauthor=None, version=None): - """Return full path to the user-shared data dir for this application. - - "appname" is the name of application. - "appauthor" (only required and used on Windows) is the name of the - appauthor or distributing body for this application. Typically - it is the owning company name. - "version" is an optional version path element to append to the - path. You might want to use this if you want multiple versions - of your app to be able to run independently. If used, this - would typically be ".". - - Typical user data directories are: - Mac OS X: /Library/Application Support/ - Unix: /etc/xdg/ - Win XP: C:\\Documents and Settings\\All Users\\Application Data\\\\ - Vista: (Fail! "C:\\ProgramData" is a hidden *system* directory on Vista.) - Win 7: C:\\ProgramData\\\\ # Hidden, but writeable on Win 7. - - For Unix, this is using the $XDG_CONFIG_DIRS[0] default. - - WARNING: Do not use this on Windows. See the Vista-Fail note above for why. - """ - if sys.platform.startswith('win'): - if appauthor is None: - raise AppDirsError('must specify \'appauthor\' on Windows') - path = os.path.join(_get_win_folder('CSIDL_COMMON_APPDATA'), appauthor, appname) - elif sys.platform == 'darwin': - path = os.path.join(os.path.expanduser('/Library/Application Support'), appname) - else: - # XDG default for $XDG_CONFIG_DIRS[0]. Perhaps should actually *use* that envvar, if defined. - path = '/etc/xdg/' + appname.lower() - if version: - path = os.path.join(path, version) - return path - - -def user_cache_dir(appname, appauthor=None, version=None, opinion=True): - r"""Return full path to the user-specific cache dir for this application. - - "appname" is the name of application. - "appauthor" (only required and used on Windows) is the name of the - appauthor or distributing body for this application. Typically - it is the owning company name. - "version" is an optional version path element to append to the - path. You might want to use this if you want multiple versions - of your app to be able to run independently. If used, this - would typically be ".". - "opinion" (boolean) can be False to disable the appending of - "Cache" to the base app data dir for Windows. See - discussion below. - - Typical user cache directories are: - Mac OS X: ~/Library/Caches/ - Unix: ~/.cache/ (XDG default) - Win XP: C:\Documents and Settings\\Local Settings\Application Data\\\Cache - Vista: C:\Users\\AppData\Local\\\Cache - - On Windows the only suggestion in the MSDN docs is that local settings go in - the `CSIDL_LOCAL_APPDATA` directory. This is identical to the non-roaming - app data dir (the default returned by `user_data_dir` above). Apps typically - put cache data somewhere *under* the given dir here. Some examples: - ...\Mozilla\Firefox\Profiles\\Cache - ...\Acme\SuperApp\Cache\1.0 - OPINION: This function appends "Cache" to the `CSIDL_LOCAL_APPDATA` value. - This can be disabled with the `opinion=False` option. - """ - if sys.platform.startswith('win'): - if appauthor is None: - raise AppDirsError('must specify \'appauthor\' on Windows') - path = os.path.join(_get_win_folder('CSIDL_LOCAL_APPDATA'), appauthor, appname) - if opinion: - path = os.path.join(path, 'Cache') - elif sys.platform == 'darwin': - path = os.path.join(os.path.expanduser('~/Library/Caches'), appname) - else: - path = os.path.join(os.getenv('XDG_CACHE_HOME', os.path.expanduser('~/.cache')), appname.lower()) - if version: - path = os.path.join(path, version) - return path - - -def user_log_dir(appname, appauthor=None, version=None, opinion=True): - r"""Return full path to the user-specific log dir for this application. - - "appname" is the name of application. - "appauthor" (only required and used on Windows) is the name of the - appauthor or distributing body for this application. Typically - it is the owning company name. - "version" is an optional version path element to append to the - path. You might want to use this if you want multiple versions - of your app to be able to run independently. If used, this - would typically be ".". - "opinion" (boolean) can be False to disable the appending of - "Logs" to the base app data dir for Windows, and "log" to the - base cache dir for Unix. See discussion below. - - Typical user cache directories are: - Mac OS X: ~/Library/Logs/ - Unix: ~/.cache//log # or under $XDG_CACHE_HOME if defined - Win XP: C:\Documents and Settings\\Local Settings\Application Data\\\Logs - Vista: C:\Users\\AppData\Local\\\Logs - - On Windows the only suggestion in the MSDN docs is that local settings - go in the `CSIDL_LOCAL_APPDATA` directory. (Note: I'm interested in - examples of what some windows apps use for a logs dir.) - - OPINION: This function appends "Logs" to the `CSIDL_LOCAL_APPDATA` - value for Windows and appends "log" to the user cache dir for Unix. - This can be disabled with the `opinion=False` option. - """ - if sys.platform == 'darwin': - path = os.path.join(os.path.expanduser('~/Library/Logs'), appname) - elif sys.platform == 'win32': - path = user_data_dir(appname, appauthor, version) - version = False - if opinion: - path = os.path.join(path, 'Logs') - else: - path = user_cache_dir(appname, appauthor, version) - version = False - if opinion: - path = os.path.join(path, 'log') - if version: - path = os.path.join(path, version) - return path +import os.path class EnvAppDirs: - def __init__(self, appname, appauthor, root_path): - self.appname = appname - self.appauthor = appauthor + def __init__(self, root_path): self.root_path = root_path @property @@ -223,138 +28,3 @@ def user_cache_dir(self): @property def user_log_dir(self): return os.path.join(self.root_path, 'log') - - -class AppDirs: - """Convenience wrapper for getting application dirs.""" - - def __init__(self, appname, appauthor, version=None, roaming=False): - self.appname = appname - self.appauthor = appauthor - self.version = version - self.roaming = roaming - - @property - def user_data_dir(self): - return user_data_dir(self.appname, self.appauthor, version=self.version, roaming=self.roaming) - - @property - def site_data_dir(self): - return site_data_dir(self.appname, self.appauthor, version=self.version) - - @property - def user_cache_dir(self): - return user_cache_dir(self.appname, self.appauthor, version=self.version) - - @property - def user_log_dir(self): - return user_log_dir(self.appname, self.appauthor, version=self.version) - - -# ---- internal support stuff - -def _get_win_folder_from_registry(csidl_name): - """ - This is a fallback technique at best. I'm not sure if using the registry for this guarantees us the correct answer - for all CSIDL_* names. - """ - import _winreg # pylint: disable=import-error - - shell_folder_name = { - 'CSIDL_APPDATA': 'AppData', - 'CSIDL_COMMON_APPDATA': 'Common AppData', - 'CSIDL_LOCAL_APPDATA': 'Local AppData', - }[csidl_name] - - key = _winreg.OpenKey( - _winreg.HKEY_CURRENT_USER, - r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders' - ) - directory, item_type = _winreg.QueryValueEx(key, shell_folder_name) # pylint: disable=unused-variable - return directory - - -def _get_win_folder_with_pywin32(csidl_name): - from win32com.shell import shellcon, shell # pylint: disable=import-error - directory = shell.SHGetFolderPath(0, getattr(shellcon, csidl_name), 0, 0) - - # Try to make this a unicode path because SHGetFolderPath does not return unicode strings when there is unicode data - # in the path. - try: - directory = str(directory) - - # Downgrade to short path name if have highbit chars. See - # . - has_high_char = False - for char in directory: - if ord(char) > 255: - has_high_char = True - break - if has_high_char: - try: - import win32api - directory = win32api.GetShortPathName(directory) - except ImportError: - pass - except UnicodeError: - pass - return directory - - -def _get_win_folder_with_ctypes(csidl_name): - import ctypes - - csidl_const = { - 'CSIDL_APPDATA': 26, - 'CSIDL_COMMON_APPDATA': 35, - 'CSIDL_LOCAL_APPDATA': 28, - }[csidl_name] - - buf = ctypes.create_unicode_buffer(1024) - ctypes.windll.shell32.SHGetFolderPathW(None, csidl_const, None, 0, buf) - - # Downgrade to short path name if have highbit chars. See - # . - has_high_char = False - for char in buf: - if ord(char) > 255: - has_high_char = True - break - if has_high_char: - buf2 = ctypes.create_unicode_buffer(1024) - if ctypes.windll.kernel32.GetShortPathNameW(buf.value, buf2, 1024): - buf = buf2 - - return buf.value - - -if sys.platform == 'win32': - try: - import win32com.shell # pylint: disable=unused-import - _get_win_folder = _get_win_folder_with_pywin32 - except ImportError: - try: - import ctypes # pylint: disable=unused-import - _get_win_folder = _get_win_folder_with_ctypes - except ImportError: - _get_win_folder = _get_win_folder_from_registry - - -# ---- self test code - -if __name__ == '__main__': - APP_NAME = 'MyApp' - APP_AUTHOR = 'MyCompany' - - props = ('user_data_dir', 'site_data_dir', 'user_cache_dir', - 'user_log_dir') - - print("-- app dirs (without optional 'version')") - dirs = AppDirs(APP_NAME, APP_AUTHOR, version='1.0') - for prop in props: - print('%s: %s' % (prop, getattr(dirs, prop))) - - print("\n-- app dirs (with optional 'version')") - dirs = AppDirs(APP_NAME, APP_AUTHOR) - for prop in props: - print('%s: %s' % (prop, getattr(dirs, prop))) diff --git a/binstar_client/utils/config.py b/binstar_client/utils/config.py index 181e1d5a..31761d9b 100644 --- a/binstar_client/utils/config.py +++ b/binstar_client/utils/config.py @@ -15,6 +15,12 @@ from urllib.parse import quote_plus import yaml +from platformdirs import PlatformDirs + +try: + from conda.gateways import anaconda_client as c_client +except ImportError: + c_client = None try: from conda.gateways import anaconda_client as c_client @@ -22,20 +28,17 @@ c_client = None from binstar_client.errors import BinstarError -from binstar_client.utils.appdirs import AppDirs, EnvAppDirs -from binstar_client.utils import conda -from binstar_client.utils import paths +from binstar_client.utils import conda, paths +from binstar_client.utils.appdirs import EnvAppDirs from .yaml import yaml_load, yaml_dump - logger = logging.getLogger('binstar') - if 'BINSTAR_CONFIG_DIR' in os.environ: - dirs = EnvAppDirs('binstar', 'ContinuumIO', os.environ['BINSTAR_CONFIG_DIR']) + dirs = EnvAppDirs(os.environ['BINSTAR_CONFIG_DIR']) USER_CONFIG = os.path.join(dirs.user_data_dir, 'config.yaml') else: - dirs = AppDirs('binstar', 'ContinuumIO') # type: ignore + dirs = PlatformDirs('binstar', 'ContinuumIO') # type: ignore USER_CONFIG = os.path.join(os.path.expanduser('~'), '.continuum', 'anaconda-client', 'config.yaml') @@ -276,7 +279,7 @@ def load_config(config_file): def load_file_configs(search_path): def _file_yaml_loader(fullpath): - assert (fullpath.endswith('.yml') # nosec + assert (fullpath.endswith('.yml') # nosec or fullpath.endswith('.yaml') or fullpath.endswith('anacondarc')), fullpath yield fullpath, load_config(fullpath) diff --git a/conda.recipe/meta.yaml b/conda.recipe/meta.yaml index f3761a91..eacc5775 100644 --- a/conda.recipe/meta.yaml +++ b/conda.recipe/meta.yaml @@ -23,13 +23,13 @@ requirements: run: - python - anaconda-anon-usage >=0.4.0 - - clyent >=1.2.0 - conda-package-handling >=1.7.3 - defusedxml >=0.7.1 - nbformat >=4.4.0 - python-dateutil >=2.6.1 - pytz >=2021.3 - pyyaml >=3.12 + - platformdirs >=3.10.0,<5.0 - requests >=2.20.0 - requests-toolbelt >=0.9.1 - setuptools >=58.0.4 diff --git a/requirements.txt b/requirements.txt index c49fa1d7..5ebf561c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,13 +1,13 @@ # Requirements to run the application # anaconda-anon-usage>=0.4.0 -clyent>=1.2.0 conda-package-handling>=1.7.3 defusedxml>=0.7.1 nbformat>=4.4.0 python-dateutil>=2.6.1 pytz>=2021.3 pyyaml>=3.12 +platformdirs>=3.10.0,<5.0 requests>=2.20.0 requests-toolbelt>=0.9.1 setuptools>=58.0.4 diff --git a/tests/test_whoami.py b/tests/test_whoami.py index 0ae21f47..60b1ed64 100644 --- a/tests/test_whoami.py +++ b/tests/test_whoami.py @@ -51,4 +51,7 @@ def test_netrc_ignored(self, urls): user = urls.register(path='/user', status=401) main(['--show-traceback', 'whoami']) - self.assertNotIn('Authorization', user.req.headers) + + # Not called because token is missing. + user.assertNotCalled() + self.assertIsNone(user.req)