diff options
author | Richard Maw <richard.maw@codethink.co.uk> | 2014-02-11 14:27:17 +0000 |
---|---|---|
committer | Richard Maw <richard.maw@codethink.co.uk> | 2014-02-11 15:19:25 +0000 |
commit | 62fa7e08f76a5b6bb8410add49c40656b3e73acd (patch) | |
tree | ddb17bfaa101ac3a65336d7d71ed0f08bdd5b487 /yarnlib/shell_libraries.py | |
parent | 1b80990bd5e58091dc2da50adda747a10922b29e (diff) | |
parent | c43e94039a017e60743e49822d932facef54da93 (diff) | |
download | cmdtest-62fa7e08f76a5b6bb8410add49c40656b3e73acd.tar.gz |
Merge branch 'baserock/richardmaw/S10275/factor-out-modules-v3' into baserock/morph
Update the version of cmdtest used in Baserock and include the patches
to move functionality into yarnlib.
Reviewed-by: Lars Wirzenius
Diffstat (limited to 'yarnlib/shell_libraries.py')
-rw-r--r-- | yarnlib/shell_libraries.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/yarnlib/shell_libraries.py b/yarnlib/shell_libraries.py new file mode 100644 index 0000000..3607adc --- /dev/null +++ b/yarnlib/shell_libraries.py @@ -0,0 +1,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) |