Document _cargo_toml

This commit is contained in:
Alberto Planas 2021-03-24 17:46:19 +01:00
parent b8c292f0aa
commit 9bcfa374ec

View file

@ -16,20 +16,37 @@ def _get_binaries(cargo_toml):
def _cargo_toml(source_path, path, exclude_vendor=True):
"""Find a Cargo.toml related with a binary."""
# If path is aready a Cargo.toml file, we are done
if path.endswith('Cargo.toml'):
# Finding the Cargo.toml is a bit tricky, and the general idea is
# like this:
#
# - `source_path`: is set but the dependecy generator call
# (cargo_bundled.attr) to point to `%{_builddir}`, so where
# the sources are (`.../rpmbuild/BUILD`)
#
# - `path`: can point to an executable when called by
# cargo_bundler.attr, that lives in `%{_buildroot}`, for
# example `.../rpmbuild/BUILDROOT/<PKGNAME>/usr/bin/<BINNAME>`
#
# - `path`: can also point to a Cargo.toml when called from
# command line
#
# - If `path` is a binary, we search the Cargo.toml from
# `source_path` that generates this specific binary
# If path is already a Cargo.toml file, we are done
binary_or_cargo_toml = os.path.basename(path)
if binary_or_cargo_toml == 'Cargo.toml':
return os.path.join(source_path, path)
binary = os.path.basename(path)
cargo_tomls = glob.glob(os.path.join(source_path, '**/Cargo.toml'),
recursive=True)
for cargo_toml in cargo_tomls:
if exclude_vendor and 'vendor' in cargo_toml.split(os.sep):
continue
if binary in _get_binaries(cargo_toml):
if binary_or_cargo_toml in _get_binaries(cargo_toml):
return cargo_toml
raise Exception(f'Cargo.toml not found for binary {binary}')
raise Exception(f'Cargo.toml not found for binary {binary_or_cargo_toml}')
def main():