Fix Windows file locking issue in `pkg_install` (#983)

`NamedTemporaryFile` turns out to lock the file on Windows, preventing
`os.replace` from moving it:
```
windows> bazel run --enable_runfiles //[redacted]:install -- --destdir=d:\est
[...]
INFO: Running command line: [redacted]/install.exe <args omitted>
INFO: Installing to [redacted]
Traceback (most recent call last):
  File "[redacted]\install_install_script.py", line 92, in _do_file_copy
    os.replace(tmp_file.name, dest)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: '[redacted]\\tmp4wd6gg1t' -> 'd:\est/filename.ext'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "[redacted]\install_install_script.py", line 307, in <module>
    sys.exit(main(sys.argv))
             ^^^^^^^^^^^^^^
  File "[redacted]\install_install_script.py", line 303, in main
    installer.do_the_thing()
  File "[redacted]\install_install_script.py", line 206, in do_the_thing
    self._install_file(entry)
  File "[redacted]\install_install_script.py", line 126, in _install_file
    self._do_file_copy(entry.src, entry.dest)
  File "[redacted]\install_install_script.py", line 94, in _do_file_copy
    pathlib.Path(tmp_file.name).unlink(missing_ok=True)
  File "[redacted]\rules_python++python+python_3_12_x86_64-pc-windows-msvc\Lib\pathlib.py", line 1342, in unlink
    os.unlink(self)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: '[redacted]\\tmp4wd6gg1t'

Intuition: _Any problem in computer science can be solved with another
level of indirection._ (https://bwlampson.site/Slides/TuringLecture.htm)

The change therefore proposes to use `TemporaryDirectory` instead of
`NamedTemporaryFile` as an indirection to avoid a file lock from being
held, which allows the file to be freely written and moved on all
platforms while maintaining the same atomic replace behavior for macOS
code-signed binaries introduced in commit
31cab20079c1f2919f569119272923dea2e679c8 (#941).
1 file changed
tree: 8c71e1b85c775ac560154a3c9cf4072c360f5492
  1. .bazelci/
  2. .bcr/
  3. .github/
  4. contrib/
  5. distro/
  6. doc_build/
  7. docs/
  8. examples/
  9. pkg/
  10. tests/
  11. toolchains/
  12. .bazelignore
  13. .bazelrc
  14. .gitignore
  15. .pre-commit-config.yaml
  16. .typos.toml
  17. AUTHORS
  18. BUILD
  19. CHANGELOG.md
  20. CODEOWNERS
  21. CONTRIBUTING.md
  22. CONTRIBUTORS
  23. deps.bzl
  24. developers.md
  25. LICENSE
  26. mappings.bzl
  27. MODULE.bazel
  28. patching.md
  29. pkg.bzl
  30. README.md
  31. version.bzl
  32. WORKSPACE
  33. WORKSPACE.bzlmod
README.md

Bazel package building

Bazel rules for building tar, zip, deb, and rpm for packages.

For the latest version, see Releases (with WORKSPACE setup) / Documentation

Use rules-pkg-discuss@googlegroups.com for discussion.

CI: Build status

Basic rules

Package building rules

  • pkg - Rules for building packages of various types.
  • examples - Cookbook examples for using the rules.

As of Bazel 4.x, Bazel uses this rule set for packaging its distribution. Bazel still contains a limited version of pkg_tar but its feature set is frozen. Any new capabilities will be added here.

WORKSPACE setup

Sample, but see releases for the current release.

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
    name = "rules_pkg",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.9.1/rules_pkg-0.9.1.tar.gz",
        "https://github.com/bazelbuild/rules_pkg/releases/download/0.9.1/rules_pkg-0.9.1.tar.gz",
    ],
    sha256 = "8f9ee2dc10c1ae514ee599a8b42ed99fa262b757058f65ad3c384289ff70c4b8",
)
load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
rules_pkg_dependencies()

To use pkg_rpm(), you must provide a copy of rpmbuild. You can use the system installed rpmbuild with this stanza.

load("@rules_pkg//toolchains/rpm:rpmbuild_configure.bzl", "find_system_rpmbuild")

find_system_rpmbuild(
    name = "rules_pkg_rpmbuild",
    verbose = False,
)

MODULE.bazel setup

bazel_dep(name = "rules_pkg", version = "0.0.10")

To use pkg_rpm(), you must provide a copy of rpmbuild. You can use the system installed rpmbuild with this stanza.

find_rpm = use_extension("//toolchains/rpm:rpmbuild_configure.bzl", "find_system_rpmbuild_bzlmod")
use_repo(find_rpm, "rules_pkg_rpmbuild")
register_toolchains("@rules_pkg_rpmbuild//:all")

For developers

We hold an engineering status meeting on the first Monday of every month at 10am USA East coast time. Add to calendar / meeting notes