Skip to content

Commit

Permalink
pw_package: Forbid some packages in downstream
Browse files Browse the repository at this point in the history
Some packages are only installed as packages in upstream Pigweed because
upstream Pigweed doesn't have submodules. Projects using Pigweed should
be using submodules to include these packages. Make it a little harder
to use these packages in downstream projects.

Bug: b/249829950
Change-Id: I6eda3dbfb7488bd277df96bd73a00e466bbfe7d3
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/117834
Pigweed-Auto-Submit: Rob Mohr <[email protected]>
Reviewed-by: Alexei Frolov <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
  • Loading branch information
mohrr authored and CQ Bot Account committed Jan 4, 2023
1 parent 10e1ce7 commit 0bf36e5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
3 changes: 2 additions & 1 deletion pw_package/docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ In general, these should not be used by projects using Pigweed. Pigweed uses
these packages to avoid using submodules so downstream projects don't have
multiple copies of a given repository in their source tree. Projects using
Pigweed should use submodules instead of packages because submodules are
supported by much more mature tooling: git.
supported by much more mature tooling: git. To install these packages anyway,
use ``--force`` on the command line or ``force=True`` in Python code.

-----
Usage
Expand Down
1 change: 1 addition & 0 deletions pw_package/py/pw_package/git_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(
self._commit = commit
self._tag = tag
self._sparse_list = sparse_list
self._allow_use_in_downstream = False

def status(self, path: pathlib.Path) -> bool:
# TODO(tonymd): Check the correct SHA is checked out here.
Expand Down
29 changes: 29 additions & 0 deletions pw_package/py/pw_package/package_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ class Package:

def __init__(self, name):
self._name = name
self._allow_use_in_downstream = True

@property
def name(self):
return self._name

@property
def allow_use_in_downstream(self):
return self._allow_use_in_downstream

def install(
self, path: pathlib.Path
) -> None: # pylint: disable=no-self-use
Expand Down Expand Up @@ -83,6 +88,14 @@ class Packages:
available: Tuple[str, ...]


class UpstreamOnlyPackageError(Exception):
def __init__(self, pkg_name):
super().__init__(
f'Package {pkg_name} is an upstream-only package--it should be '
'imported as a submodule and not a package'
)


class PackageManager:
"""Install and remove optional packages."""

Expand All @@ -91,7 +104,23 @@ def __init__(self, root: pathlib.Path):
os.makedirs(root, exist_ok=True)

def install(self, package: str, force: bool = False) -> None:
"""Install the named package.
Args:
package: The name of the package to install.
force: Install the package regardless of whether it's already
installed or if it's not "allowed" to be installed on this
project.
"""

pkg = _PACKAGES[package]
if not pkg.allow_use_in_downstream:
if os.environ.get('PW_ROOT') != os.environ.get('PW_PROJECT_ROOT'):
if force:
_LOG.warning(str(UpstreamOnlyPackageError(pkg.name)))
else:
raise UpstreamOnlyPackageError(pkg.name)

if force:
self.remove(package)
pkg.install(self._pkg_root / pkg.name)
Expand Down
1 change: 1 addition & 0 deletions pw_package/py/pw_package/packages/boringssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, *args, **kwargs):
),
commit='9f55d972854d0b34dae39c7cd3679d6ada3dfd5b',
)
self._allow_use_in_downstream = False

def status(self, path: pathlib.Path) -> bool:
if not self._boringssl.status(boringssl_repo_path(path)):
Expand Down
8 changes: 5 additions & 3 deletions pw_presubmit/py/pw_presubmit/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def bazel(ctx: PresubmitContext, cmd: str, *args: str) -> None:
raise exc


def install_package(ctx: PresubmitContext, name: str) -> None:
def install_package(
ctx: PresubmitContext, name: str, force: bool = False
) -> None:
"""Install package with given name in given path."""
root = ctx.package_root
mgr = package_manager.PackageManager(root)
Expand All @@ -114,8 +116,8 @@ def install_package(ctx: PresubmitContext, name: str) -> None:
'configuration module'
)

if not mgr.status(name):
mgr.install(name)
if not mgr.status(name) or force:
mgr.install(name, force=force)


def _gn_value(value) -> str:
Expand Down

0 comments on commit 0bf36e5

Please sign in to comment.