source: rtems-central/rtemsqual/util.py @ 8230acb

Last change on this file since 8230acb was 8230acb, checked in by Sebastian Huber <sebastian.huber@…>, on 04/17/20 at 12:29:00

util: Add copy_files()

  • Property mode set to 100644
File size: 2.9 KB
RevLine 
[4733ad3]1# SPDX-License-Identifier: BSD-2-Clause
2""" This module provides utility functions. """
3
4# Copyright (C) 2020 embedded brains GmbH (http://www.embedded-brains.de)
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26
27import os
[8230acb]28import shutil
29from typing import Any, List
[4733ad3]30import yaml
31
32
[8230acb]33def copy_files(src_dir: str, dst_dir: str, files: List[str]) -> None:
34    """
35    Copies a list of files in the source directory to the destination
36    directory preserving the directory of the files relative to the source
37    directory.
38    """
39    for a_file in files:
40        src = os.path.join(src_dir, a_file)
41        dst = os.path.join(dst_dir, a_file)
42        os.makedirs(os.path.dirname(dst), exist_ok=True)
43        shutil.copy2(src, dst)
44
45
[4733ad3]46def load_config(config_filename: str) -> Any:
47    """ Loads the configuration file with recursive includes. """
48    class IncludeLoader(yaml.SafeLoader):  # pylint: disable=too-many-ancestors
49        """ YAML loader customization to process custom include tags. """
50        _filenames = [config_filename]
51
52        def include(self, node):
53            """ Processes the custom include tag. """
54            container = IncludeLoader._filenames[0]
55            dirname = os.path.dirname(container)
56            filename = os.path.join(dirname, self.construct_scalar(node))
57            IncludeLoader._filenames.insert(0, filename)
58            with open(filename, "r") as included_file:
59                data = yaml.load(included_file, IncludeLoader)
60            IncludeLoader._filenames.pop()
61            return data
62
63    IncludeLoader.add_constructor("!include", IncludeLoader.include)
64    with open(config_filename, "r") as config_file:
65        return yaml.load(config_file.read(), Loader=IncludeLoader)
Note: See TracBrowser for help on using the repository browser.