-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DM-26688: Add command-line tool for Registry.associate #459
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ | |
|
||
from .arguments import * | ||
from .options import * | ||
from .optionGroups import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# This file is part of ctrl_mpexec. | ||
# | ||
# Developed for the LSST Data Management System. | ||
# This product includes software developed by the LSST Project | ||
# (https://www.lsst.org). | ||
# See the COPYRIGHT file at the top-level directory of this distribution | ||
# for details of code ownership. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import click | ||
|
||
from . import ( | ||
collections_option, | ||
dataset_type_option, | ||
glob_argument, | ||
repo_argument, | ||
where_option, | ||
) | ||
from ..utils import OptionGroup, unwrap, where_help | ||
|
||
|
||
class query_datasets_options(OptionGroup): # noqa: N801 | ||
|
||
def __init__(self, repo=True, showUri=True, useArguments=True): | ||
self.decorators = [] | ||
if repo: | ||
if not useArguments: | ||
raise RuntimeError("repo as an option is not currently supported.") | ||
self.decorators.append(repo_argument(required=True)) | ||
if useArguments: | ||
self.decorators.append(glob_argument( | ||
help=unwrap("""GLOB is one or more glob-style expressions that fully or partially identify the | ||
dataset type names to be queried."""))) | ||
else: | ||
self.decorators.append(dataset_type_option( | ||
help=unwrap("""One or more glob-style expressions that fully or partially identify the dataset | ||
type names to be queried."""))) | ||
self.decorators.extend([ | ||
collections_option(), | ||
where_option(help=where_help), | ||
click.option("--find-first", | ||
is_flag=True, | ||
help=unwrap("""For each result data ID, only yield one DatasetRef of each | ||
DatasetType, from the first collection in which a dataset of that dataset | ||
type appears (according to the order of 'collections' passed in). If | ||
used, 'collections' must specify at least one expression and must not | ||
contain wildcards."""))]) | ||
if showUri: | ||
self.decorators.append(click.option("--show-uri", | ||
is_flag=True, | ||
help="Show the dataset URI in results.")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# This file is part of daf_butler. | ||
# | ||
# Developed for the LSST Data Management System. | ||
# This product includes software developed by the LSST Project | ||
# (http://www.lsst.org). | ||
# See the COPYRIGHT file at the top-level directory of this distribution | ||
# for details of code ownership. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
from .. import Butler, CollectionType | ||
from ..script import QueryDatasets | ||
|
||
|
||
def associate(repo, collection, dataset_type, collections, where, find_first): | ||
"""Add existing datasets to a CHAINED collection. | ||
""" | ||
|
||
butler = Butler(repo, writeable=True) | ||
|
||
butler.registry.registerCollection(collection, CollectionType.TAGGED) | ||
|
||
results = QueryDatasets( | ||
butler=butler, | ||
glob=dataset_type, | ||
collections=collections, | ||
where=where, | ||
find_first=find_first, | ||
show_uri=False, | ||
repo=None | ||
) | ||
|
||
butler.registry.associate(collection, results.getDatasets()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# This file is part of daf_butler. | ||
# | ||
# Developed for the LSST Data Management System. | ||
# This product includes software developed by the LSST Project | ||
# (http://www.lsst.org). | ||
# See the COPYRIGHT file at the top-level directory of this distribution | ||
# for details of code ownership. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Unit tests for daf_butler CLI prune-datasets subcommand. | ||
""" | ||
|
||
import unittest | ||
from unittest.mock import patch | ||
|
||
from lsst.daf.butler.cli.butler import cli as butlerCli | ||
from lsst.daf.butler.cli.utils import clickResultMsg, LogCliRunner | ||
|
||
|
||
class AssociateTestCase(unittest.TestCase): | ||
"""Tests the ``associate`` ``butler`` subcommand. | ||
|
||
``script.associate`` contains no logic, so instead of mocking the | ||
internals, just mock the call to that function to test for expected inputs | ||
and input types. | ||
""" | ||
|
||
def setUp(self): | ||
self.runner = LogCliRunner() | ||
|
||
@patch("lsst.daf.butler.script.associate") | ||
def test_defaults(self, mockAssociate): | ||
"""Test the expected default values & types for optional options. | ||
""" | ||
result = self.runner.invoke( | ||
butlerCli, ["associate", "myRepo", "myCollection"]) | ||
self.assertEqual(result.exit_code, 0, clickResultMsg(result)) | ||
mockAssociate.assert_called_once_with( | ||
repo="myRepo", | ||
collection="myCollection", | ||
dataset_type=tuple(), | ||
collections=tuple(), | ||
where=None, | ||
find_first=False | ||
) | ||
|
||
@patch("lsst.daf.butler.script.associate") | ||
def test_values(self, mockAssociate): | ||
"""Test expected values & types when passing in options. | ||
""" | ||
result = self.runner.invoke( | ||
butlerCli, ["associate", "myRepo", "myCollection", | ||
"--dataset-type", "myDatasetType", | ||
"--collections", "myCollection,otherCollection", | ||
"--where", "'a=b'", | ||
"--find-first"]) | ||
self.assertEqual(result.exit_code, 0, clickResultMsg(result)) | ||
mockAssociate.assert_called_once_with( | ||
repo="myRepo", | ||
collection="myCollection", | ||
dataset_type=("myDatasetType",), | ||
collections=("myCollection", "otherCollection"), | ||
where="'a=b'", | ||
find_first=True | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of calling the script rather than the native method to share logic like transforming globs to regexes. Do we need to worry about the script also doing unnecessary things (like making astropy Tables) at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
QueryDatasets generates astropy tables on demand via the getter (
getTables
), so it's structured to not create these if they're not needed. (I do see a mistake in passing the QueryDatasets results to the butler without using the accessor, will fix)