1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tools/update_storage_modules.py Thu Jan 26 17:16:21 2017 +0100
1.3 @@ -0,0 +1,86 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Update the storage modules import manifest.
1.8 +
1.9 +Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from glob import glob
1.26 +from os import listdir
1.27 +from os.path import commonprefix, isdir, join, split, splitext
1.28 +import imiptools.stores
1.29 +
1.30 +reserved = ["__init__.py", "common.py", "manifest.py"]
1.31 +
1.32 +def get_extensions(dirname):
1.33 + filenames = []
1.34 + for filename in glob(join(dirname, "*.py")):
1.35 + leafname = split(filename)[-1]
1.36 + if leafname not in reserved:
1.37 + filenames.append(filename)
1.38 + return filenames
1.39 +
1.40 +# The main program generating a new version of the manifest module.
1.41 +
1.42 +if __name__ == "__main__":
1.43 + dirname = join(split(imiptools.stores.__file__)[0], "")
1.44 + manifest = join(dirname, "manifest.py")
1.45 +
1.46 + # Get all Python files in the stores directory, filtering out the
1.47 + # reserved files that do not provide storage functions.
1.48 +
1.49 + filenames = get_extensions(dirname)
1.50 +
1.51 + # Get all extensions from directories in the stores directory.
1.52 +
1.53 + for filename in listdir(dirname):
1.54 + filename = join(dirname, filename)
1.55 + if isdir(filename):
1.56 + filenames += get_extensions(filename)
1.57 +
1.58 + # Open the manifest module and write code to import and combine the
1.59 + # functions from each module.
1.60 +
1.61 + f = open(manifest, "w")
1.62 + try:
1.63 + print >>f, """\
1.64 +stores = {}
1.65 +"""
1.66 +
1.67 + for filename in filenames:
1.68 + relative = filename[len(commonprefix([filename, dirname])):]
1.69 +
1.70 + # NOTE: Converting POSIX paths to module paths.
1.71 +
1.72 + module = splitext(relative)[0].replace("/", ".")
1.73 + module_parts = module.rsplit(".", 1)
1.74 +
1.75 + # Get subpackage location and module.
1.76 +
1.77 + module_parents = len(module_parts) > 1 and module_parts[0]
1.78 + module_name = module_parts[-1]
1.79 +
1.80 + print >>f, """\
1.81 +from imiptools.stores%s import %s
1.82 +stores[%r] = %s
1.83 +""" % (module_parents and ".%s" % module_parents or "",
1.84 + module_name, module_name, module_name)
1.85 +
1.86 + finally:
1.87 + f.close()
1.88 +
1.89 +# vim: tabstop=4 expandtab shiftwidth=4