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
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 @@
archives = None,
flat = None,
prefix = None,
+ strip_components = 0,
archive_prefix = None,
dist_dir = None,
log = None,
@@ -83,6 +84,9 @@
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 @@
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 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 @@
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(