Removes HOME from the environment when running as root. This script is sometimes run as root in docker containers. buck2 doesn't allow running as root unless $HOME is owned by root or is not set. TODO(pytorch/test-infra#5091): Remove this once the CI jobs stop running as root.
| 648 | |
| 649 | |
| 650 | class Buck2EnvironmentFixer(contextlib.AbstractContextManager): |
| 651 | """Removes HOME from the environment when running as root. |
| 652 | |
| 653 | This script is sometimes run as root in docker containers. buck2 doesn't |
| 654 | allow running as root unless $HOME is owned by root or is not set. |
| 655 | |
| 656 | TODO(pytorch/test-infra#5091): Remove this once the CI jobs stop running as |
| 657 | root. |
| 658 | """ |
| 659 | |
| 660 | def __init__(self): |
| 661 | self.saved_env = {} |
| 662 | |
| 663 | def __enter__(self): |
| 664 | if os.name != "nt" and os.geteuid() == 0 and "HOME" in os.environ: |
| 665 | log.info("temporarily unsetting HOME while running as root") |
| 666 | self.saved_env["HOME"] = os.environ.pop("HOME") |
| 667 | return self |
| 668 | |
| 669 | def __exit__(self, *args, **kwargs): |
| 670 | if "HOME" in self.saved_env: |
| 671 | log.info("restored HOME") |
| 672 | os.environ["HOME"] = self.saved_env["HOME"] |
| 673 | |
| 674 | |
| 675 | # TODO(dbort): For editable wheels, may need to update get_source_files(), |