paul@1206 | 1 | #!/usr/bin/env python |
paul@1206 | 2 | |
paul@1206 | 3 | """ |
paul@1206 | 4 | Update the storage modules import manifest. |
paul@1206 | 5 | |
paul@1206 | 6 | Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@1206 | 7 | |
paul@1206 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@1206 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@1206 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@1206 | 11 | version. |
paul@1206 | 12 | |
paul@1206 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@1206 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@1206 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@1206 | 16 | details. |
paul@1206 | 17 | |
paul@1206 | 18 | You should have received a copy of the GNU General Public License along with |
paul@1206 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@1206 | 20 | """ |
paul@1206 | 21 | |
paul@1206 | 22 | from glob import glob |
paul@1206 | 23 | from os import listdir |
paul@1206 | 24 | from os.path import commonprefix, isdir, join, split, splitext |
paul@1206 | 25 | import imiptools.stores |
paul@1206 | 26 | |
paul@1206 | 27 | reserved = ["__init__.py", "common.py", "manifest.py"] |
paul@1206 | 28 | |
paul@1206 | 29 | def get_extensions(dirname): |
paul@1206 | 30 | filenames = [] |
paul@1206 | 31 | for filename in glob(join(dirname, "*.py")): |
paul@1206 | 32 | leafname = split(filename)[-1] |
paul@1206 | 33 | if leafname not in reserved: |
paul@1206 | 34 | filenames.append(filename) |
paul@1206 | 35 | return filenames |
paul@1206 | 36 | |
paul@1206 | 37 | # The main program generating a new version of the manifest module. |
paul@1206 | 38 | |
paul@1206 | 39 | if __name__ == "__main__": |
paul@1206 | 40 | dirname = join(split(imiptools.stores.__file__)[0], "") |
paul@1206 | 41 | manifest = join(dirname, "manifest.py") |
paul@1206 | 42 | |
paul@1206 | 43 | # Get all Python files in the stores directory, filtering out the |
paul@1206 | 44 | # reserved files that do not provide storage functions. |
paul@1206 | 45 | |
paul@1206 | 46 | filenames = get_extensions(dirname) |
paul@1206 | 47 | |
paul@1206 | 48 | # Get all extensions from directories in the stores directory. |
paul@1206 | 49 | |
paul@1206 | 50 | for filename in listdir(dirname): |
paul@1206 | 51 | filename = join(dirname, filename) |
paul@1206 | 52 | if isdir(filename): |
paul@1206 | 53 | filenames += get_extensions(filename) |
paul@1206 | 54 | |
paul@1206 | 55 | # Open the manifest module and write code to import and combine the |
paul@1206 | 56 | # functions from each module. |
paul@1206 | 57 | |
paul@1206 | 58 | f = open(manifest, "w") |
paul@1206 | 59 | try: |
paul@1206 | 60 | print >>f, """\ |
paul@1206 | 61 | stores = {} |
paul@1206 | 62 | """ |
paul@1206 | 63 | |
paul@1206 | 64 | for filename in filenames: |
paul@1206 | 65 | relative = filename[len(commonprefix([filename, dirname])):] |
paul@1206 | 66 | |
paul@1206 | 67 | # NOTE: Converting POSIX paths to module paths. |
paul@1206 | 68 | |
paul@1206 | 69 | module = splitext(relative)[0].replace("/", ".") |
paul@1206 | 70 | module_parts = module.rsplit(".", 1) |
paul@1206 | 71 | |
paul@1206 | 72 | # Get subpackage location and module. |
paul@1206 | 73 | |
paul@1206 | 74 | module_parents = len(module_parts) > 1 and module_parts[0] |
paul@1206 | 75 | module_name = module_parts[-1] |
paul@1206 | 76 | |
paul@1206 | 77 | print >>f, """\ |
paul@1206 | 78 | from imiptools.stores%s import %s |
paul@1206 | 79 | stores[%r] = %s |
paul@1206 | 80 | """ % (module_parents and ".%s" % module_parents or "", |
paul@1206 | 81 | module_name, module_name, module_name) |
paul@1206 | 82 | |
paul@1206 | 83 | finally: |
paul@1206 | 84 | f.close() |
paul@1206 | 85 | |
paul@1206 | 86 | # vim: tabstop=4 expandtab shiftwidth=4 |