update.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python
  2. # Copyright 2022 The HuggingFace Team. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from pathlib import Path
  16. from .config_args import default_config_file, load_config_from_file
  17. from .config_utils import SubcommandHelpFormatter
  18. description = "Update an existing config file with the latest defaults while maintaining the old configuration."
  19. def update_config(args):
  20. """
  21. Update an existing config file with the latest defaults while maintaining the old configuration.
  22. """
  23. config_file = args.config_file
  24. if config_file is None and Path(default_config_file).exists():
  25. config_file = default_config_file
  26. elif not Path(config_file).exists():
  27. raise ValueError(f"The passed config file located at {config_file} doesn't exist.")
  28. config = load_config_from_file(config_file)
  29. if config_file.endswith(".json"):
  30. config.to_json_file(config_file)
  31. else:
  32. config.to_yaml_file(config_file)
  33. return config_file
  34. def update_command_parser(parser, parents):
  35. parser = parser.add_parser("update", parents=parents, help=description, formatter_class=SubcommandHelpFormatter)
  36. parser.add_argument(
  37. "--config_file",
  38. default=None,
  39. help=(
  40. "The path to the config file to update. Will default to a file named default_config.yaml in the cache "
  41. "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have "
  42. "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed "
  43. "with 'huggingface'."
  44. ),
  45. )
  46. parser.set_defaults(func=update_config_command)
  47. return parser
  48. def update_config_command(args):
  49. config_file = update_config(args)
  50. print(f"Successfully updated the configuration file at {config_file}.")