Skip to content

Commit

Permalink
Add --no-resolve-links argument to copy (#430)
Browse files Browse the repository at this point in the history
Useful for skipping over pulling external files to local storage
  • Loading branch information
jsignell authored Jun 1, 2023
1 parent fcf2800 commit e0fec5f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- `--no-resolve-links` to the copy command ([#430](https://github.com/stac-utils/stactools/pull/430))

### Changed

- Use `pyproject.toml` for project metadata ([#424](https://github.com/stac-utils/stactools/pull/424))
Expand Down
20 changes: 18 additions & 2 deletions src/stactools/cli/commands/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def create_copy_command(cli: click.Group) -> click.Command:
"--copy-assets",
"-a",
is_flag=True,
help=("Copy all item assets to " "be alongside the new item location."),
help="Copy all item assets to be alongside the new item location.",
)
@click.option(
"-l",
Expand All @@ -78,12 +78,21 @@ def create_copy_command(cli: click.Group) -> click.Command:
"instead of the destination folder."
),
)
@click.option(
"--resolve-links/--no-resolve-links",
default=True,
help=(
"Whether to resolve HREF links. Defaults to --resolve-links. "
"Use --no-resolve-links to avoid writing external child objects locally."
),
)
def copy_command(
src: str,
dst: str,
catalog_type: pystac.CatalogType,
copy_assets: bool,
publish_location: Optional[str],
resolve_links: bool,
) -> None:
"""Copy a STAC Catalog or Collection at SRC to the directory at DST.
Expand All @@ -92,6 +101,13 @@ def copy_command(
source_catalog = pystac.read_file(make_absolute_href(src))
if not isinstance(source_catalog, pystac.Catalog):
raise click.BadArgumentUsage(f"{src} is not a STAC Catalog")
copy_catalog(source_catalog, dst, catalog_type, copy_assets, publish_location)
copy_catalog(
source_catalog,
dst,
catalog_type,
copy_assets,
publish_location,
resolve_links,
)

return copy_command
12 changes: 9 additions & 3 deletions src/stactools/core/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,22 @@ def copy_catalog(
catalog_type: Optional[CatalogType] = None,
copy_assets: bool = False,
publish_location: Optional[str] = None,
resolve_links: bool = True,
) -> None:
catalog = source_catalog.full_copy()
if resolve_links:
catalog = source_catalog.full_copy()
else:
catalog = source_catalog.clone()
catalog.set_root(catalog)

dest_directory = make_absolute_href(dest_directory)

if copy_assets:
catalog = move_all_assets(catalog, copy=True, make_hrefs_relative=True)

if publish_location is not None:
catalog.normalize_hrefs(publish_location)
catalog.normalize_hrefs(publish_location, skip_unresolved=not resolve_links)
catalog.save(catalog_type, dest_directory)
else:
catalog.normalize_hrefs(dest_directory)
catalog.normalize_hrefs(dest_directory, skip_unresolved=not resolve_links)
catalog.save(catalog_type)
8 changes: 8 additions & 0 deletions tests/cli/commands/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from stactools.cli.commands.copy import create_copy_command, create_move_assets_command
from stactools.testing import CliTestCase

from tests import test_data

from .test_cases import TestCases


Expand Down Expand Up @@ -108,3 +110,9 @@ def test_move_assets(self):
)

self.assertEqual(common_path, os.path.dirname(item_href))

def test_copy_using_no_resolve_links(self) -> None:
path = test_data.get_path("data-files/catalogs/external-child/catalog.json")
with TemporaryDirectory() as tmp_dir:
self.run_command(["copy", path, tmp_dir, "--no-resolve-links"])
self.assertEqual(os.listdir(tmp_dir), ["catalog.json"])
15 changes: 15 additions & 0 deletions tests/data-files/catalogs/external-child/catalog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id": "test",
"type": "Catalog",
"stac_extensions": [],
"stac_version": "1.0.0",
"description": "test",
"title": "test",
"links": [
{
"rel": "child",
"href": "https://raw.githubusercontent.com/radiantearth/stac-spec/v1.0.0/examples/collection.json",
"type": "application/json"
}
]
}

0 comments on commit e0fec5f

Please sign in to comment.