diff --git a/colcon_cargo/package_identification/cargo.py b/colcon_cargo/package_identification/cargo.py index f1125c0..6ee4ad4 100644 --- a/colcon_cargo/package_identification/cargo.py +++ b/colcon_cargo/package_identification/cargo.py @@ -1,6 +1,10 @@ # Copyright 2018 Easymov Robotics # Licensed under the Apache License, Version 2.0 +from collections.abc import MutableMapping +from typing import Optional, List +from pathlib import Path + from colcon_core.logging import colcon_logger from colcon_core.package_identification \ import PackageIdentificationExtensionPoint @@ -40,12 +44,11 @@ def identify(self, metadata): # noqa: D102 metadata.dependencies['run'] |= data['depends'] -def extract_data(cargo_toml): +def extract_data(cargo_toml: Path) -> Optional[dict]: """ Extract the project name and dependencies from a Cargo.toml file. - :param Path corgo_toml: The path of the Cargo.toml file - :rtype: dict + :param cargo_toml: The path of the Cargo.toml file """ content = {} try: @@ -68,13 +71,12 @@ def extract_data(cargo_toml): return data -def extract_project_name(content): +def extract_project_name(content: MutableMapping) -> Optional[str]: """ Extract the Cargo project name from the Cargo.toml file. - :param str content: The Cargo.toml parsed dictionnary + :param content: The Cargo.toml parsed dictionnary :returns: The project name, otherwise None - :rtype: str """ try: return content['package']['name'] @@ -82,12 +84,14 @@ def extract_project_name(content): return None -def extract_dependencies(content): +def extract_dependencies(content: MutableMapping) -> List[str]: """ Extract the dependencies from the Cargo.toml file. - :param str content: The Cargo.toml parsed dictionnary - :returns: The dependencies name - :rtype: list + :param content: The Cargo.toml parsed dictionnary + :returns: Packages listed in the dependencies section """ - return list(content['dependencies'].keys()) + try: + list(content['dependencies'].keys()) + except KeyError: + return [] diff --git a/colcon_cargo/task/cargo/__init__.py b/colcon_cargo/task/cargo/__init__.py index 5607f74..fb86e00 100644 --- a/colcon_cargo/task/cargo/__init__.py +++ b/colcon_cargo/task/cargo/__init__.py @@ -1,30 +1,36 @@ # Copyright 2018 Easymov Robotics # Licensed under the Apache License, Version 2.0 +from typing import Optional + import os import shutil from colcon_core.environment_variable import EnvironmentVariable -"""Environment variable to override the Cargo executable""" +# Environment variable to override the Cargo executable CARGO_COMMAND_ENVIRONMENT_VARIABLE = EnvironmentVariable( 'CARGO_COMMAND', 'The full path to the Cargo executable') -def which_executable(environment_variable, executable_name): +def which_executable(environment_variable: str, executable_name: str) \ + -> Optional[str]: """ Determine the path of an executable. An environment variable can be used to override the location instead of relying on searching the PATH. - :param str environment_variable: The name of the environment variable - :param str executable_name: The name of the executable - :rtype: str + + :param environment_variable: The name of the environment variable + :param executable_name: The name of the executable, or None if that's not + found """ - value = os.getenv(environment_variable) - if value: - return value - return shutil.which(executable_name) + if environment_variable in os.environ: + return os.environ[environment_variable] + + which = shutil.which(executable_name) + if which: + return which CARGO_EXECUTABLE = which_executable( diff --git a/setup.cfg b/setup.cfg index bbbaad2..1d88291 100644 --- a/setup.cfg +++ b/setup.cfg @@ -26,6 +26,7 @@ install_requires = # to set an environment variable when a package installs a library colcon-library-path toml + typing packages = find: tests_require = flake8