summaryrefslogtreecommitdiff
path: root/yarnlib/shell_libraries.py
blob: 3607adc5f90581c9f98a5c64efd9c9af22811a1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Copyright 2014  Lars Wirzenius and Codethink Limited
#
# 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/>.
#
# =*= License: GPL-3+ =*=


def load_shell_libraries(paths, pre_read_cb=lambda filename: None, open=open):
    '''Helper for loading shell libraries from files.

    `paths`: Iterable of file paths to shell libraries.
    `pre_read_cb`: Optional callback for providing progress reporting
                   and logging.
                   It is given only one parameter, of the path to the
                   shell library that is about to be loaded.
    `open`: Optional function for opening file paths. This is useful to
            override how files are opened for unit tests or virtual
            file systems.

    This function is provided so that when shell libraries are loaded
    by applications that use yarnlib, they can all load them with the
    same format, including the comments about where the snippet came from,
    and any future additions.

    '''

    libs = []
    for filename in paths:
        pre_read_cb(filename)
        with open(filename) as f:
            text = f.read()
        libs.append('# Loaded from %s\n\n%s\n\n' % (filename, text))

    return ''.join(libs)