-
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-22677: Relocate scripts to python module #216
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
90fea83
Relocate script logic to python module
timj ed09789
Add simple test for new script module
timj d108d2c
Add scripts to documentation
timj 838db10
Change Note to Notes in numpydoc
timj df495f0
For scripts do not catch exceptions
timj 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
Empty file.
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,100 @@ | ||
#!/usr/bin/env python | ||
|
||
# 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/>. | ||
|
||
__all__ = ("main",) | ||
|
||
import argparse | ||
import sys | ||
import logging | ||
|
||
from lsst.daf.butler import ButlerConfig | ||
|
||
|
||
def build_argparser(): | ||
"""Construct an argument parser for the ``dumpButlerConfig`` script. | ||
|
||
Returns | ||
------- | ||
argparser : `argparse.ArgumentParser` | ||
The argument parser that defines the script | ||
command-line interface. | ||
""" | ||
|
||
parser = argparse.ArgumentParser(description="Dump either a subset or full Butler configuration to " | ||
"standard output.") | ||
parser.add_argument("root", | ||
help="Filesystem path for an existing Butler repository or path to config file.") | ||
parser.add_argument("--subset", "-s", default=None, type=str, | ||
help="Subset of a configuration to report. This can be any key in the" | ||
" hierarchy such as '.datastore.root' where the leading '.' specified" | ||
" the delimiter for the hierarchy.") | ||
parser.add_argument("--searchpath", "-p", action="append", type=str, | ||
help="Additional search paths to use for configuration overrides") | ||
parser.add_argument("--verbose", "-v", action="store_true", | ||
help="Turn on debug reporting.") | ||
|
||
return parser | ||
|
||
|
||
def dumpButlerConfig(root, searchPath=None, subset=None, outfh=sys.stdout): | ||
"""Dump a config or subset to the specified file handle. | ||
|
||
Parameters | ||
---------- | ||
root : `str` | ||
Location of butler configuration. Can be a path to a YAML | ||
file or a directory containing the configuration file. | ||
searchPath : `list` of `str` | ||
Directory paths for resolving the locations of configuration | ||
file lookups. | ||
subset : `str` or `tuple` of `str` | ||
Key into a subset of the configuration. If a string it should use | ||
the standard notation of supplying the relevant delimiter in the | ||
first character. | ||
outfh | ||
File descriptor to use to write the configuration content. | ||
""" | ||
|
||
config = ButlerConfig(root, searchPaths=searchPath) | ||
|
||
if subset is not None: | ||
config = config[subset] | ||
|
||
try: | ||
config.dump(outfh) | ||
except AttributeError: | ||
print(config, file=outfh) | ||
|
||
|
||
def main(): | ||
args = build_argparser().parse_args() | ||
|
||
if args.verbose: | ||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
try: | ||
dumpButlerConfig(args.root, args.searchpath, args.subset, sys.stdout) | ||
except Exception as e: | ||
print(f"{e}", file=sys.stderr) | ||
timj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return 1 | ||
return 0 |
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,98 @@ | ||
#!/usr/bin/env python | ||
|
||
# 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/>. | ||
|
||
__all__ = ("main",) | ||
|
||
import sys | ||
import argparse | ||
import logging | ||
|
||
from lsst.daf.butler import Butler, Config | ||
|
||
|
||
def build_argparser(): | ||
"""Construct an argument parser for the ``makeButlerRepo`` script. | ||
|
||
Returns | ||
------- | ||
argparser : `argparse.ArgumentParser` | ||
The argument parser that defines the script | ||
command-line interface. | ||
""" | ||
parser = argparse.ArgumentParser(description="Create an empty Gen3 Butler repository.") | ||
parser.add_argument("root", | ||
help=("Filesystem path for the new repository. " | ||
"Will be created if it does not exist.")) | ||
parser.add_argument("-c", "--config", | ||
help=("Path to an existing YAML config file to apply (on top of defaults).")) | ||
parser.add_argument("--standalone", action="store_true", default=False, | ||
help=("Include all defaults in the config file in the repo, insulating " | ||
"the repo from changes in package defaults.")) | ||
parser.add_argument("--outfile", "-f", default=None, type=str, | ||
help="Name of output file to receive repository configuration." | ||
" Default is to write butler.yaml into the specified root.") | ||
parser.add_argument("--verbose", "-v", action="store_true", | ||
help="Turn on debug reporting.") | ||
parser.add_argument("--override", "-o", action="store_true", | ||
help="Allow values in the supplied config to override any root settings.") | ||
|
||
return parser | ||
|
||
|
||
def makeButlerRepo(root, config=None, standalone=False, override=False, outfile=None): | ||
"""Make a new Butler repository. | ||
|
||
Parameters | ||
---------- | ||
root : `str` | ||
Location to seed a butler repository. | ||
config : `str`, optional | ||
Configuration to seed the repository. | ||
standalone : `bool`, optional | ||
If `True` a fully expanded configuration will be written. | ||
override : `bool`, optional | ||
If `True` a root provided in the supplied config will not be | ||
overwritten. | ||
outfile : `str`, optional | ||
Path to output configuration. This can be left `None` | ||
if the configuration is to be written to ``root``. | ||
""" | ||
forceConfigRoot = not override | ||
config = Config(config) if config is not None else None | ||
Butler.makeRepo(root, config=config, standalone=standalone, forceConfigRoot=forceConfigRoot, | ||
outfile=outfile) | ||
|
||
|
||
def main(): | ||
args = build_argparser().parse_args() | ||
|
||
if args.verbose: | ||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
try: | ||
makeButlerRepo(args.root, args.config, args.standalone, args.override, args.outfile) | ||
except Exception as e: | ||
print(f"{e}", file=sys.stderr) | ||
return 1 | ||
|
||
return 0 |
Oops, something went wrong.
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.
Would it be worthwhile having this take an
args
to pass toparse_args()
that would takesys.argv[1:]
or similar? That seems like it's a missing piece of making this more usable, but I also don't really a scenario where we'd actually use that.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 believe I'm following standard practice here: https://developer.lsst.io/stack/argparse-script-topic-type.html?highlight=autoprogram