Skip to content

Commit 0c2358e

Browse files
committed
simplify env assembly and tests
1 parent 5e70c18 commit 0c2358e

5 files changed

Lines changed: 129 additions & 91 deletions

File tree

abx_pkg/binprovider_cargo.py

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -82,35 +82,12 @@ def setup(
8282
install_root = self.install_root
8383
assert install_root is not None
8484
self.cargo_home.mkdir(parents=True, exist_ok=True)
85-
self._cargo_target_dir().mkdir(parents=True, exist_ok=True)
85+
(install_root / "target").mkdir(parents=True, exist_ok=True)
8686
if install_root != self.cargo_home:
8787
bin_dir = self.bin_dir
8888
assert bin_dir is not None
8989
bin_dir.mkdir(parents=True, exist_ok=True)
9090

91-
def _cargo_target_dir(self) -> Path:
92-
install_root = self.install_root
93-
assert install_root is not None
94-
return install_root / "target"
95-
96-
def _cargo_env(self) -> dict[str, str]:
97-
install_root = self.install_root
98-
assert install_root is not None
99-
env = os.environ.copy()
100-
env["CARGO_HOME"] = str(self.cargo_home)
101-
env["CARGO_TARGET_DIR"] = str(self._cargo_target_dir())
102-
if install_root != self.cargo_home:
103-
env["CARGO_INSTALL_ROOT"] = str(install_root)
104-
return env
105-
106-
def _cargo_install_args(self) -> list[str]:
107-
install_root = self.install_root
108-
assert install_root is not None
109-
install_args = [*self.cargo_install_args]
110-
if install_root != self.cargo_home:
111-
install_args.extend(["--root", str(install_root)])
112-
return install_args
113-
11491
def _cargo_package_specs(
11592
self,
11693
bin_name: str,
@@ -171,11 +148,25 @@ def default_install_handler(
171148
if min_version and not any(arg.startswith("--version") for arg in install_args):
172149
install_args = ["--version", f">={min_version}", *install_args]
173150
installer_bin = self._require_installer_bin()
151+
install_root = self.install_root
152+
assert install_root is not None
153+
cargo_install_args = [*self.cargo_install_args]
154+
if install_root != self.cargo_home:
155+
cargo_install_args.extend(["--root", str(install_root)])
174156

175157
proc = self.exec(
176158
bin_name=installer_bin,
177-
cmd=["install", *self._cargo_install_args(), *install_args],
178-
env=self._cargo_env(),
159+
cmd=["install", *cargo_install_args, *install_args],
160+
env={
161+
**os.environ,
162+
"CARGO_HOME": str(self.cargo_home),
163+
"CARGO_TARGET_DIR": str(install_root / "target"),
164+
**(
165+
{"CARGO_INSTALL_ROOT": str(install_root)}
166+
if install_root != self.cargo_home
167+
else {}
168+
),
169+
},
179170
timeout=timeout,
180171
)
181172
if proc.returncode != 0:
@@ -202,16 +193,30 @@ def default_update_handler(
202193
if min_version and not any(arg.startswith("--version") for arg in install_args):
203194
install_args = ["--version", f">={min_version}", *install_args]
204195
installer_bin = self._require_installer_bin()
196+
install_root = self.install_root
197+
assert install_root is not None
198+
cargo_install_args = [*self.cargo_install_args]
199+
if install_root != self.cargo_home:
200+
cargo_install_args.extend(["--root", str(install_root)])
205201

206202
proc = self.exec(
207203
bin_name=installer_bin,
208204
cmd=[
209205
"install",
210206
"--force",
211-
*self._cargo_install_args(),
207+
*cargo_install_args,
212208
*install_args,
213209
],
214-
env=self._cargo_env(),
210+
env={
211+
**os.environ,
212+
"CARGO_HOME": str(self.cargo_home),
213+
"CARGO_TARGET_DIR": str(install_root / "target"),
214+
**(
215+
{"CARGO_INSTALL_ROOT": str(install_root)}
216+
if install_root != self.cargo_home
217+
else {}
218+
),
219+
},
215220
timeout=timeout,
216221
)
217222
if proc.returncode != 0:
@@ -234,20 +239,30 @@ def default_uninstall_handler(
234239
install_args=install_args,
235240
)
236241
installer_bin = self._require_installer_bin()
242+
install_root = self.install_root
243+
assert install_root is not None
237244

238245
proc = self.exec(
239246
bin_name=installer_bin,
240247
cmd=[
241248
"uninstall",
242249
*(
243-
["--root", str(self.install_root)]
244-
if self.install_root is not None
245-
and self.install_root != self.cargo_home
250+
["--root", str(install_root)]
251+
if install_root != self.cargo_home
246252
else []
247253
),
248254
*package_specs,
249255
],
250-
env=self._cargo_env(),
256+
env={
257+
**os.environ,
258+
"CARGO_HOME": str(self.cargo_home),
259+
"CARGO_TARGET_DIR": str(install_root / "target"),
260+
**(
261+
{"CARGO_INSTALL_ROOT": str(install_root)}
262+
if install_root != self.cargo_home
263+
else {}
264+
),
265+
},
251266
timeout=timeout,
252267
)
253268
if proc.returncode != 0 and "did not match any packages" not in proc.stderr:

abx_pkg/binprovider_gem.py

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -89,36 +89,6 @@ def setup(
8989
install_root.mkdir(parents=True, exist_ok=True)
9090
bin_dir.mkdir(parents=True, exist_ok=True)
9191

92-
def _gem_install_args(self) -> list[str]:
93-
install_root = self.install_root
94-
bin_dir = self.bin_dir
95-
assert install_root is not None
96-
assert bin_dir is not None
97-
return [
98-
"--install-dir",
99-
str(install_root),
100-
"--bindir",
101-
str(bin_dir),
102-
*self.gem_install_args,
103-
]
104-
105-
def _gem_scope_args(self) -> list[str]:
106-
install_root = self.install_root
107-
assert install_root is not None
108-
return [
109-
"-i",
110-
str(install_root),
111-
]
112-
113-
def _gem_env(self) -> dict[str, str]:
114-
install_root = self.install_root
115-
assert install_root is not None
116-
env = os.environ.copy()
117-
gem_home = str(install_root)
118-
env["GEM_HOME"] = gem_home
119-
env["GEM_PATH"] = gem_home
120-
return env
121-
12292
def _patch_generated_wrappers(self) -> None:
12393
install_root = self.install_root
12494
bin_dir = self.bin_dir
@@ -174,11 +144,28 @@ def default_install_handler(
174144
if min_version and not any(arg.startswith("--version") for arg in install_args):
175145
install_args = ["--version", f">={min_version}", *install_args]
176146
installer_bin = self._require_installer_bin()
147+
install_root = self.install_root
148+
bin_dir = self.bin_dir
149+
assert install_root is not None
150+
assert bin_dir is not None
151+
gem_home = str(install_root)
177152

178153
proc = self.exec(
179154
bin_name=installer_bin,
180-
cmd=["install", *self._gem_install_args(), *install_args],
181-
env=self._gem_env(),
155+
cmd=[
156+
"install",
157+
"--install-dir",
158+
gem_home,
159+
"--bindir",
160+
str(bin_dir),
161+
*self.gem_install_args,
162+
*install_args,
163+
],
164+
env={
165+
**os.environ,
166+
"GEM_HOME": gem_home,
167+
"GEM_PATH": gem_home,
168+
},
182169
timeout=timeout,
183170
)
184171
if proc.returncode != 0:
@@ -207,11 +194,28 @@ def default_update_handler(
207194
if min_version and not any(arg.startswith("--version") for arg in install_args):
208195
install_args = ["--version", f">={min_version}", *install_args]
209196
installer_bin = self._require_installer_bin()
197+
install_root = self.install_root
198+
bin_dir = self.bin_dir
199+
assert install_root is not None
200+
assert bin_dir is not None
201+
gem_home = str(install_root)
210202

211203
proc = self.exec(
212204
bin_name=installer_bin,
213-
cmd=["update", *self._gem_install_args(), *install_args],
214-
env=self._gem_env(),
205+
cmd=[
206+
"update",
207+
"--install-dir",
208+
gem_home,
209+
"--bindir",
210+
str(bin_dir),
211+
*self.gem_install_args,
212+
*install_args,
213+
],
214+
env={
215+
**os.environ,
216+
"GEM_HOME": gem_home,
217+
"GEM_PATH": gem_home,
218+
},
215219
timeout=timeout,
216220
)
217221
if proc.returncode != 0:
@@ -232,6 +236,9 @@ def default_uninstall_handler(
232236
) -> bool:
233237
install_args = install_args or self.get_install_args(bin_name)
234238
installer_bin = self._require_installer_bin()
239+
install_root = self.install_root
240+
assert install_root is not None
241+
gem_home = str(install_root)
235242

236243
proc = self.exec(
237244
bin_name=installer_bin,
@@ -241,10 +248,15 @@ def default_uninstall_handler(
241248
"--executables",
242249
"--ignore-dependencies",
243250
"--force",
244-
*self._gem_scope_args(),
251+
"-i",
252+
gem_home,
245253
*install_args,
246254
],
247-
env=self._gem_env(),
255+
env={
256+
**os.environ,
257+
"GEM_HOME": gem_home,
258+
"GEM_PATH": gem_home,
259+
},
248260
timeout=timeout,
249261
)
250262
if proc.returncode != 0 and "is not installed in GEM_HOME" not in proc.stderr:

abx_pkg/binprovider_goget.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,6 @@ def setup(
9090
install_root.mkdir(parents=True, exist_ok=True)
9191
bin_dir.mkdir(parents=True, exist_ok=True)
9292

93-
def _go_env(self) -> dict[str, str]:
94-
install_root = self.install_root
95-
bin_dir = self.bin_dir
96-
assert install_root is not None
97-
assert bin_dir is not None
98-
env = os.environ.copy()
99-
env["GOPATH"] = str(install_root)
100-
env["GOBIN"] = str(bin_dir)
101-
return env
102-
10393
def default_install_args_handler(self, bin_name: BinName, **context) -> InstallArgs:
10494
bin_name_str = str(bin_name)
10595
if not (
@@ -129,11 +119,19 @@ def default_install_handler(
129119

130120
install_args = install_args or self.get_install_args(bin_name)
131121
installer_bin = self._require_installer_bin()
122+
install_root = self.install_root
123+
bin_dir = self.bin_dir
124+
assert install_root is not None
125+
assert bin_dir is not None
132126

133127
proc = self.exec(
134128
bin_name=installer_bin,
135129
cmd=["install", *self.go_install_args, *install_args],
136-
env=self._go_env(),
130+
env={
131+
**os.environ,
132+
"GOPATH": str(install_root),
133+
"GOBIN": str(bin_dir),
134+
},
137135
timeout=timeout,
138136
)
139137
if proc.returncode != 0:
@@ -236,11 +234,19 @@ def default_version_handler(
236234
abspath = abspath or self.get_abspath(bin_name, quiet=True)
237235
if not abspath or not self.INSTALLER_BIN_ABSPATH:
238236
return None
237+
install_root = self.install_root
238+
bin_dir = self.bin_dir
239+
assert install_root is not None
240+
assert bin_dir is not None
239241

240242
proc = self.exec(
241243
bin_name=self.INSTALLER_BIN_ABSPATH,
242244
cmd=["version", "-m", abspath],
243-
env=self._go_env(),
245+
env={
246+
**os.environ,
247+
"GOPATH": str(install_root),
248+
"GOBIN": str(bin_dir),
249+
},
244250
timeout=timeout,
245251
quiet=True,
246252
)

abx_pkg/binprovider_nix.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,6 @@ def setup(
123123
if self.nix_state_dir:
124124
self.nix_state_dir.mkdir(parents=True, exist_ok=True)
125125

126-
def _nix_env(self) -> dict[str, str]:
127-
env = os.environ.copy()
128-
if self.nix_state_dir:
129-
env["XDG_STATE_HOME"] = str(self.nix_state_dir)
130-
env["XDG_CACHE_HOME"] = str(self.nix_state_dir / "cache")
131-
return env
132-
133126
def _profile_element_name(
134127
self,
135128
bin_name: str,
@@ -161,6 +154,10 @@ def default_install_handler(
161154

162155
install_args = install_args or self.get_install_args(bin_name)
163156
installer_bin = self._require_installer_bin()
157+
env = os.environ.copy()
158+
if self.nix_state_dir:
159+
env["XDG_STATE_HOME"] = str(self.nix_state_dir)
160+
env["XDG_CACHE_HOME"] = str(self.nix_state_dir / "cache")
164161

165162
proc = self.exec(
166163
bin_name=installer_bin,
@@ -172,7 +169,7 @@ def default_install_handler(
172169
str(self.install_root),
173170
*install_args,
174171
],
175-
env=self._nix_env(),
172+
env=env,
176173
timeout=timeout,
177174
)
178175
if proc.returncode != 0:
@@ -195,6 +192,10 @@ def default_update_handler(
195192
install_args=install_args,
196193
)
197194
installer_bin = self._require_installer_bin()
195+
env = os.environ.copy()
196+
if self.nix_state_dir:
197+
env["XDG_STATE_HOME"] = str(self.nix_state_dir)
198+
env["XDG_CACHE_HOME"] = str(self.nix_state_dir / "cache")
198199

199200
proc = self.exec(
200201
bin_name=installer_bin,
@@ -206,7 +207,7 @@ def default_update_handler(
206207
str(self.install_root),
207208
profile_element,
208209
],
209-
env=self._nix_env(),
210+
env=env,
210211
timeout=timeout,
211212
)
212213
if proc.returncode != 0:
@@ -229,6 +230,10 @@ def default_uninstall_handler(
229230
install_args=install_args,
230231
)
231232
installer_bin = self._require_installer_bin()
233+
env = os.environ.copy()
234+
if self.nix_state_dir:
235+
env["XDG_STATE_HOME"] = str(self.nix_state_dir)
236+
env["XDG_CACHE_HOME"] = str(self.nix_state_dir / "cache")
232237

233238
proc = self.exec(
234239
bin_name=installer_bin,
@@ -240,7 +245,7 @@ def default_uninstall_handler(
240245
str(self.install_root),
241246
profile_element,
242247
],
243-
env=self._nix_env(),
248+
env=env,
244249
timeout=timeout,
245250
)
246251
if proc.returncode not in (0, 1):

0 commit comments

Comments
 (0)