Skip to content

Commit 5a007c0

Browse files
ahornbyfacebook-github-bot
authored andcommitted
getdeps: add env subcommand
Summary: X-link: facebookincubator/zstrong#1025 Add getdeps.py `env` subcommand to get the environment from getdeps in a sourcable form so that its easier to run/test/debug the binaries There is an existing `debug` subcommand, but it starts an interactive shell inside getdeps, which isn't so useful for programatic use. To get the output clean enough to source I switched getdeps print() logging to go to stderr. example usage: ``` $ (source <(./build/fbcode_builder/getdeps.py --allow-system-packages env mononoke_integration); which mononoke) ``` Reviewed By: jdelliot Differential Revision: D64982397 fbshipit-source-id: 65212936d42185e4d395557b56d3dba499caa128
1 parent ee3c033 commit 5a007c0

File tree

5 files changed

+72
-16
lines changed

5 files changed

+72
-16
lines changed

build/fbcode_builder/getdeps.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,27 @@ def run_project_cmd(self, args, loader, manifest):
923923
self.create_builder(loader, manifest).debug(reconfigure=False)
924924

925925

926+
@cmd(
927+
"env",
928+
"print the environment in a shell sourceable format",
929+
)
930+
class EnvCmd(ProjectCmdBase):
931+
def setup_project_cmd_parser(self, parser):
932+
parser.add_argument(
933+
"--os-type",
934+
help="Filter to just this OS type to run",
935+
choices=["linux", "darwin", "windows"],
936+
action="store",
937+
dest="ostype",
938+
default=None,
939+
)
940+
941+
def run_project_cmd(self, args, loader, manifest):
942+
if args.ostype:
943+
loader.build_opts.host_type.ostype = args.ostype
944+
self.create_builder(loader, manifest).printenv(reconfigure=False)
945+
946+
926947
@cmd("generate-github-actions", "generate a GitHub actions configuration")
927948
class GenerateGitHubActionsCmd(ProjectCmdBase):
928949
RUN_ON_ALL = """ [push, pull_request]"""

build/fbcode_builder/getdeps/builder.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import subprocess
1616
import sys
1717
import typing
18+
from shlex import quote as shellquote
1819
from typing import Optional
1920

2021
from .dyndeps import create_dyn_dep_munger
@@ -157,6 +158,29 @@ def debug(self, reconfigure: bool) -> None:
157158
shell = ["powershell.exe"] if sys.platform == "win32" else ["/bin/sh", "-i"]
158159
self._run_cmd(shell, cwd=self.build_dir, env=env)
159160

161+
def printenv(self, reconfigure: bool) -> None:
162+
"""print the environment in a shell sourcable format"""
163+
reconfigure = self._reconfigure(reconfigure)
164+
self._apply_patchfile()
165+
self._prepare(reconfigure=reconfigure)
166+
env = self._compute_env(env=Env(src={}))
167+
prefix = "export "
168+
sep = ":"
169+
expand = "$"
170+
expandpost = ""
171+
if self.build_opts.is_windows():
172+
prefix = "SET "
173+
sep = ";"
174+
expand = "%"
175+
expandpost = "%"
176+
for k, v in sorted(env.items()):
177+
existing = os.environ.get(k, None)
178+
if k.endswith("PATH") and existing:
179+
v = shellquote(v) + sep + f"{expand}{k}{expandpost}"
180+
else:
181+
v = shellquote(v)
182+
print("%s%s=%s" % (prefix, k, v))
183+
160184
def build(self, reconfigure: bool) -> None:
161185
print("Building %s..." % self.manifest.name)
162186
reconfigure = self._reconfigure(reconfigure)
@@ -225,14 +249,16 @@ def _build(self, reconfigure) -> None:
225249
system needs to regenerate its rules."""
226250
pass
227251

228-
def _compute_env(self):
252+
def _compute_env(self, env=None) -> Env:
253+
if env is None:
254+
env = self.env
229255
# CMAKE_PREFIX_PATH is only respected when passed through the
230256
# environment, so we construct an appropriate path to pass down
231257
return self.build_opts.compute_env_for_install_dirs(
232258
self.loader,
233259
self.dep_manifests,
234260
self.ctx,
235-
env=self.env,
261+
env=env,
236262
manifest=self.manifest,
237263
)
238264

build/fbcode_builder/getdeps/cargo.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import re
1111
import shutil
12+
import sys
1213
import typing
1314

1415
from .builder import BuilderBase
@@ -97,7 +98,7 @@ def _create_cargo_config(self):
9798

9899
if os.path.isfile(cargo_config_file):
99100
with open(cargo_config_file, "r") as f:
100-
print(f"Reading {cargo_config_file}")
101+
print(f"Reading {cargo_config_file}", file=sys.stderr)
101102
cargo_content = f.read()
102103
else:
103104
cargo_content = ""
@@ -142,7 +143,8 @@ def _create_cargo_config(self):
142143
if new_content != cargo_content:
143144
with open(cargo_config_file, "w") as f:
144145
print(
145-
f"Writing cargo config for {self.manifest.name} to {cargo_config_file}"
146+
f"Writing cargo config for {self.manifest.name} to {cargo_config_file}",
147+
file=sys.stderr,
146148
)
147149
f.write(new_content)
148150

@@ -270,7 +272,10 @@ def _patchup_workspace(self, dep_to_git) -> None:
270272
new_content += "\n".join(config)
271273
if new_content != manifest_content:
272274
with open(patch_cargo, "w") as f:
273-
print(f"writing patch to {patch_cargo}")
275+
print(
276+
f"writing patch to {patch_cargo}",
277+
file=sys.stderr,
278+
)
274279
f.write(new_content)
275280

276281
def _resolve_config(self, dep_to_git) -> typing.Dict[str, typing.Dict[str, str]]:
@@ -296,7 +301,8 @@ def _resolve_config(self, dep_to_git) -> typing.Dict[str, typing.Dict[str, str]]
296301
if c in crate_source_map and c not in crates_to_patch_path:
297302
crates_to_patch_path[c] = crate_source_map[c]
298303
print(
299-
f"{self.manifest.name}: Patching crate {c} via virtual manifest in {self.workspace_dir()}"
304+
f"{self.manifest.name}: Patching crate {c} via virtual manifest in {self.workspace_dir()}",
305+
file=sys.stderr,
300306
)
301307
if crates_to_patch_path:
302308
git_url_to_crates_and_paths[git_url] = crates_to_patch_path
@@ -352,7 +358,8 @@ def _resolve_dep_to_git(self):
352358
subpath = subpath.replace("/", "\\")
353359
crate_path = os.path.join(dep_source_dir, subpath)
354360
print(
355-
f"{self.manifest.name}: Mapped crate {crate} to dep {dep} dir {crate_path}"
361+
f"{self.manifest.name}: Mapped crate {crate} to dep {dep} dir {crate_path}",
362+
file=sys.stderr,
356363
)
357364
crate_source_map[crate] = crate_path
358365
elif dep_cargo_conf:
@@ -367,7 +374,8 @@ def _resolve_dep_to_git(self):
367374
crate = match.group(1)
368375
if crate:
369376
print(
370-
f"{self.manifest.name}: Discovered crate {crate} in dep {dep} dir {crate_root}"
377+
f"{self.manifest.name}: Discovered crate {crate} in dep {dep} dir {crate_root}",
378+
file=sys.stderr,
371379
)
372380
crate_source_map[crate] = crate_root
373381

@@ -414,7 +422,8 @@ def _resolve_dep_to_crates(self, build_source_dir, dep_to_git):
414422
for c in crates:
415423
if c not in existing_crates:
416424
print(
417-
f"Patch {self.manifest.name} uses {dep_name} crate {crates}"
425+
f"Patch {self.manifest.name} uses {dep_name} crate {crates}",
426+
file=sys.stderr,
418427
)
419428
existing_crates.add(c)
420429
dep_to_crates.setdefault(name, set()).update(existing_crates)

build/fbcode_builder/getdeps/manifest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import configparser
99
import io
1010
import os
11+
import sys
1112
from typing import List
1213

1314
from .builder import (
@@ -391,7 +392,10 @@ def _is_satisfied_by_preinstalled_environment(self, ctx):
391392
return False
392393
for key in envs:
393394
val = os.environ.get(key, None)
394-
print(f"Testing ENV[{key}]: {repr(val)}")
395+
print(
396+
f"Testing ENV[{key}]: {repr(val)}",
397+
file=sys.stderr,
398+
)
395399
if val is None:
396400
return False
397401
if len(val) == 0:

build/fbcode_builder/getdeps/runcmd.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@
1010
import subprocess
1111
import sys
1212

13+
from shlex import quote as shellquote
14+
1315
from .envfuncs import Env
1416
from .platform import is_windows
1517

1618

17-
try:
18-
from shlex import quote as shellquote
19-
except ImportError:
20-
from pipes import quote as shellquote
21-
22-
2319
class RunCommandError(Exception):
2420
pass
2521

0 commit comments

Comments
 (0)