#!/usr/bin/python """ (Or /usr/local/bin/python, depending on your path setup) Configurator.py Version 1.1 Retrieves configuration information from your *.cfg file (a file with any name usually but not necessarily ending in '.cfg', determined by the environment variable CONFIGFILE), to be used as included definitions in makefiles for various projects. This makes it a "sane version of the windows registry," and easy to modify. The information in the file is in the form of a windows-style configuration file that contains information for each project. For example, *.cfg might contain multiple entries that look similar to this: [Project1] SOURCE : C:/MyDocuments/Project1/Project1.doc DESTINATION : C:/Project1/code TEMP : C:/Temp/Project1 TARGET : %(TEMP)s/Project1.html DEPEND : %(TEMP)s/$(NAME1).bin (The '=' sign is acceptable in place of the initial ':' as a separator) You can see examples of names in the file being substituted into values. Note that the form is '%()s'; that is, the 's' is essential after the closing parenthesis. Also, you can see that the makefile substitution syntax such as $(NAME1) will pass through untouched, which is quite convenient. Note that any configuration information can be placed in the file, and you can use multiple sections for different types of information for the same project. This tool is simply a retrieval mechanism for configuration information. This program is designed to be used with Cygwin (Unix shell under Windows, see www.Cygwin.com), Linux or Unix. You must place the *.cfg file (described below) in a file specified by the environment variable CONFIGFILE, which you must set. Place this Configurator.py file somewhere permanent on your machine, in your machine's execution path. To use the configurator, you can use the standard Python ConfigFile module in a Python program, following the example in the code of this program. To produce a makefile definition file, run this program on the command line like this: python Configurator.py or, if you're running under Cygwin, Linux or Unix (I use Cygwin to run all my makefiles): Configurator.py Note that to do this, Configurator.py must be made executable: chmod +x Configurator.py And it must be in your execution path. For the above sample configuration file, the command: Configurator.py Project1 extracts only the Project1 information and produces a makefile definition file called Project1.def, which can then be included in your makefile. In the case of Project1, Project1.def looks like this: SOURCE:=C:/MyDocuments/Project1/Project1.doc DEPEND:=C:/Temp/Project1/$(NAME1).bin TARGET:=C:/Temp/Project1/Project1.html TEMP:=C:/Temp/Project1 DESTINATION:=C:/Project1/code To invoke the Configurator inside your makefile and then to include the definitions, you can use the following inside your makefile: $(PROJECT).def: $(CONFIGFILE) Configurator.py $(PROJECT) include $(PROJECT).def where $(PROJECT) expands to the name of your project, as defined in the makfile. For example: PROJECT := Project1 Note that ConfiguratorParser can also be used to fetch information from the *.cfg file for use within an ordinary Python program, as well. The code below provides a simple template for doing this. """ copyright = """(c) MindView, inc. 2004, by Bruce Eckel. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import sys, os from ConfiguratorParser import ConfiguratorParser if __name__ == "__main__": if len(sys.argv) < 2: print __doc__ sys.exit(1) section = sys.argv[1] c = ConfiguratorParser(section) # c is now a dictionary containing configuration information as key-value pairs defs = file(section + ".def", 'w') for x in c.items(section): print >>defs, x[0] + ":=" + x[1]