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