MCPcopy Index your code
hub / github.com/pre-commit/pre-commit / install_environment

Function install_environment

pre_commit/languages/rust.py:113–160  ·  view source on GitHub ↗
(
        prefix: Prefix,
        version: str,
        additional_dependencies: Sequence[str],
)

Source from the content-addressed store, hash-verified

111
112
113def install_environment(
114 prefix: Prefix,
115 version: str,
116 additional_dependencies: Sequence[str],
117) -> None:
118 envdir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)
119
120 # There are two cases where we might want to specify more dependencies:
121 # as dependencies for the library being built, and as binary packages
122 # to be `cargo install`'d.
123 #
124 # Unlike e.g. Python, if we just `cargo install` a library, it won't be
125 # used for compilation. And if we add a crate providing a binary to the
126 # `Cargo.toml`, the binary won't be built.
127 #
128 # Because of this, we allow specifying "cli" dependencies by prefixing
129 # with 'cli:'.
130 cli_deps = {
131 dep for dep in additional_dependencies if dep.startswith('cli:')
132 }
133 lib_deps = set(additional_dependencies) - cli_deps
134
135 packages_to_install: set[tuple[str, ...]] = {('--path', '.')}
136 for cli_dep in cli_deps:
137 cli_dep = cli_dep.removeprefix('cli:')
138 package, _, crate_version = cli_dep.partition(':')
139 if crate_version != '':
140 packages_to_install.add((package, '--version', crate_version))
141 else:
142 packages_to_install.add((package,))
143
144 with contextlib.ExitStack() as ctx:
145 ctx.enter_context(in_env(prefix, version))
146
147 if version != 'system':
148 install_rust_with_toolchain(_rust_toolchain(version), envdir)
149
150 tmpdir = ctx.enter_context(tempfile.TemporaryDirectory())
151 ctx.enter_context(envcontext((('RUSTUP_HOME', tmpdir),)))
152
153 if len(lib_deps) > 0:
154 _add_dependencies(prefix, lib_deps)
155
156 for args in packages_to_install:
157 cmd_output_b(
158 'cargo', 'install', '--bins', '--root', envdir, *args,
159 cwd=prefix.prefix_dir,
160 )

Callers

nothing calls this directly

Calls 6

envcontextFunction · 0.90
cmd_output_bFunction · 0.90
_rust_toolchainFunction · 0.85
_add_dependenciesFunction · 0.85
in_envFunction · 0.70

Tested by

no test coverage detected