Skip to content
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 5 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 3 additions & 32 deletions bin.src/dumpButlerConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,8 @@
# 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 argparse
import sys
import logging
from lsst.daf.butler.script.dumpButlerConfig import main

from lsst.daf.butler import ButlerConfig

if __name__ == "__main__":
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.")

args = parser.parse_args()

if args.verbose:
logging.basicConfig(level=logging.DEBUG)

config = ButlerConfig(args.root, searchPaths=args.searchpath)

if args.subset is not None:
config = config[args.subset]

try:
config.dump(sys.stdout)
except AttributeError:
print(config)
if __name__ == '__main__':
sys.exit(main())
36 changes: 4 additions & 32 deletions bin.src/makeButlerRepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,8 @@
# 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 argparse
import logging
import sys
from lsst.daf.butler.script.makeButlerRepo import main

from lsst.daf.butler import Butler, Config


if __name__ == "__main__":
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.")
args = parser.parse_args()

if args.verbose:
logging.basicConfig(level=logging.DEBUG)

forceConfigRoot = not args.override

config = Config(args.config) if args.config is not None else None
Butler.makeRepo(args.root, config=config, standalone=args.standalone, forceConfigRoot=forceConfigRoot,
outfile=args.outfile)
if __name__ == '__main__':
sys.exit(main())
67 changes: 3 additions & 64 deletions bin.src/validateButlerConfiguration.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,69 +21,8 @@
# 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 argparse
import sys
from lsst.daf.butler.script.validateButlerConfiguration import main

from lsst.daf.butler import Butler, ValidationError


def processCommas(arg):
"""Given a list that might contain strings with commas, return expanded
list.

Parameters
----------
arg : iterable of `str`
Values read from command line.

Returns
-------
expanded : `list` of `str`
List where any items with commas are expanded into multiple entries.
"""
expanded = []
if arg is None:
return expanded
for item in arg:
expanded.extend(item.split(","))
return expanded


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Validate the configuration files for a "
"Gen3 Butler repository.")
parser.add_argument("root",
help="Filesystem path for an existing Butler repository.")
parser.add_argument("--collection", "-c", default="validate", type=str,
help="Collection to refer to in this repository.")
parser.add_argument("--quiet", "-q", action="store_true",
help="Do not report individual failures.")
parser.add_argument("--datasettype", "-d", action="append", type=str,
help="Specific DatasetType(s) to validate (can be comma-separated)")
parser.add_argument("--ignore", "-i", action="append", type=str,
help="DatasetType(s) to ignore for validation (can be comma-separated)")

args = parser.parse_args()

logFailures = True
if args.quiet:
logFailures = False

# Process any commas in dataset type or ignore list
ignore = processCommas(args.ignore)
datasetTypes = processCommas(args.datasettype)

exitStatus = 0

# The collection does not matter for validation but if a run is specified
# in the configuration then it must be consistent with this collection
butler = Butler(config=args.root, collection=args.collection)
try:
butler.validateConfiguration(logFailures=logFailures, datasetTypeNames=datasetTypes,
ignore=ignore)
except ValidationError:
exitStatus = 1
else:
print("No problems encountered with configuration.")

sys.exit(exitStatus)
if __name__ == '__main__':
sys.exit(main())
Empty file.
100 changes: 100 additions & 0 deletions python/lsst/daf/butler/script/dumpButlerConfig.py
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():
Copy link
Member

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 to parse_args() that would take sys.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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
98 changes: 98 additions & 0 deletions python/lsst/daf/butler/script/makeButlerRepo.py
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