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@1208 | 25 | import imp |
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@1208 | 31 | found = glob(join(dirname, "*.py")) |
paul@1208 | 32 | found.sort() |
paul@1208 | 33 | |
paul@1208 | 34 | for filename in found: |
paul@1206 | 35 | leafname = split(filename)[-1] |
paul@1206 | 36 | if leafname not in reserved: |
paul@1206 | 37 | filenames.append(filename) |
paul@1208 | 38 | |
paul@1206 | 39 | return filenames |
paul@1206 | 40 | |
paul@1206 | 41 | # The main program generating a new version of the manifest module. |
paul@1206 | 42 | |
paul@1206 | 43 | if __name__ == "__main__": |
paul@1208 | 44 | _f, dirname, _d = imp.find_module("imiptools/stores") |
paul@1208 | 45 | dirname = join(dirname, "") |
paul@1206 | 46 | manifest = join(dirname, "manifest.py") |
paul@1206 | 47 | |
paul@1206 | 48 | # Get all Python files in the stores directory, filtering out the |
paul@1206 | 49 | # reserved files that do not provide storage functions. |
paul@1206 | 50 | |
paul@1206 | 51 | filenames = get_extensions(dirname) |
paul@1206 | 52 | |
paul@1206 | 53 | # Get all extensions from directories in the stores directory. |
paul@1206 | 54 | |
paul@1208 | 55 | found = listdir(dirname) |
paul@1208 | 56 | found.sort() |
paul@1208 | 57 | |
paul@1208 | 58 | for filename in found: |
paul@1206 | 59 | filename = join(dirname, filename) |
paul@1206 | 60 | if isdir(filename): |
paul@1206 | 61 | filenames += get_extensions(filename) |
paul@1206 | 62 | |
paul@1206 | 63 | # Open the manifest module and write code to import and combine the |
paul@1206 | 64 | # functions from each module. |
paul@1206 | 65 | |
paul@1206 | 66 | f = open(manifest, "w") |
paul@1206 | 67 | try: |
paul@1206 | 68 | print >>f, """\ |
paul@1206 | 69 | stores = {} |
paul@1206 | 70 | """ |
paul@1206 | 71 | |
paul@1206 | 72 | for filename in filenames: |
paul@1206 | 73 | relative = filename[len(commonprefix([filename, dirname])):] |
paul@1206 | 74 | |
paul@1206 | 75 | # NOTE: Converting POSIX paths to module paths. |
paul@1206 | 76 | |
paul@1206 | 77 | module = splitext(relative)[0].replace("/", ".") |
paul@1206 | 78 | module_parts = module.rsplit(".", 1) |
paul@1206 | 79 | |
paul@1206 | 80 | # Get subpackage location and module. |
paul@1206 | 81 | |
paul@1206 | 82 | module_parents = len(module_parts) > 1 and module_parts[0] |
paul@1206 | 83 | module_name = module_parts[-1] |
paul@1206 | 84 | |
paul@1206 | 85 | print >>f, """\ |
paul@1206 | 86 | from imiptools.stores%s import %s |
paul@1206 | 87 | stores[%r] = %s |
paul@1206 | 88 | """ % (module_parents and ".%s" % module_parents or "", |
paul@1206 | 89 | module_name, module_name, module_name) |
paul@1206 | 90 | |
paul@1206 | 91 | finally: |
paul@1206 | 92 | f.close() |
paul@1206 | 93 | |
paul@1206 | 94 | # vim: tabstop=4 expandtab shiftwidth=4 |