diff options
author | Jingwen Chen <jingwen@google.com> | 2022-07-12 13:43:06 +0000 |
---|---|---|
committer | Jingwen Chen <jingwen@google.com> | 2022-07-18 13:44:21 +0000 |
commit | 1aa240aa8b5c6d3e5732dd74b72558194a1e496b (patch) | |
tree | 28fa55bc117435d8ce97c91000ba0f42e8615967 | |
parent | b6266eb321e8d676a9f3a45cf302cf2972a17fec (diff) | |
download | bazel_common_rules-1aa240aa8b5c6d3e5732dd74b72558194a1e496b.tar.gz |
Add --strip_components support to dist rule.
This supports disting files up to a specific shared package level.
e.g. for the following files to dist:
a/b/b.txt
a/c/c.txt
with --strip_components=1 and --dist_dir=/dist, the resulting output is:
/dist/b/b.txt
/dist/c/c.txt
This works the same as tar(1)'s --strip.
Bug: 238723069
Change-Id: Ibf0ac467048dff92e6f34d3f70222f7a4f342d27
-rw-r--r-- | dist/dist.bzl | 8 | ||||
-rw-r--r-- | dist/dist.py | 13 |
2 files changed, 19 insertions, 2 deletions
diff --git a/dist/dist.bzl b/dist/dist.bzl index 2ecc8c2..6f333c7 100644 --- a/dist/dist.bzl +++ b/dist/dist.bzl @@ -62,6 +62,7 @@ def copy_to_dist_dir( archives = None, flat = None, prefix = None, + strip_components = 0, archive_prefix = None, dist_dir = None, log = None, @@ -83,6 +84,9 @@ def copy_to_dist_dir( extracted to `--dist_dir`. flat: If true, `--flat` is provided to the script by default. Flatten the distribution directory. + strip_components: If specified, `--strip_components <prefix>` is provided to the script. Strip + leading components from the existing copied file paths before applying --prefix + (if specified). prefix: If specified, `--prefix <prefix>` is provided to the script by default. Path prefix to apply within dist_dir for copied files. archive_prefix: If specified, `--archive_prefix <prefix>` is provided to the script by @@ -105,6 +109,10 @@ def copy_to_dist_dir( default_args = [] if flat: default_args.append("--flat") + if strip_components != None: + if strip_components < 0: + fail("strip_components must greater than 0, but is %s" % strip_components) + default_args += ["--strip_components", str(strip_components)] if prefix != None: default_args += ["--prefix", prefix] if archive_prefix != None: diff --git a/dist/dist.py b/dist/dist.py index c9538c4..fe64961 100644 --- a/dist/dist.py +++ b/dist/dist.py @@ -56,11 +56,17 @@ def files_to_dist(pattern): def copy_files_to_dist_dir(files, archives, dist_dir, flat, prefix, - archive_prefix, **ignored): + strip_components, archive_prefix, **ignored): logging.info("Copying to %s", dist_dir) for src in files: - src_relpath = os.path.basename(src) if flat else src + if flat: + src_relpath = os.path.basename(src) + elif strip_components > 0: + src_relpath = src.split('/', strip_components)[-1] + else: + src_relpath = src + src_relpath = os.path.join(prefix, src_relpath) src_abspath = os.path.abspath(src) @@ -114,6 +120,9 @@ def main(): action="store_true", help="ignore subdirectories in the manifest") parser.add_argument( + "--strip_components", type=int, default=0, + help="number of leading components to strip from paths before applying --prefix") + parser.add_argument( "--prefix", default="", help="path prefix to apply within dist_dir for copied files") parser.add_argument( |