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