#!/usr/bin/python """ Resolve include commands in SQL files to stdout usage: sqlincludeparser.py [] If the input file is not given, stdin is used with the current path. Output is always sent to stdout and can be piped to the mysql command-line client. An include command starts with "-- @include(" and ends with a ")". Anything else on that line is treated as a comment. """ # $URL: https://opensvn.csie.org/TinyCollection/howtos/modularSvnPhpRepository/trunk/code/sqlincludeparser.py $ __author__ = '$Author: DonQuichote $'[9:-2] __version__ = '$Rev: 464 $'[6:-2] __date__ = '$Date: 2008-04-03 23:43:52 +0200 (Thu, 03 Apr 2008) $'[7:-2] import sys import os.path def ProcessLine(strDirectory, strLine): if strLine[0:12]=="-- @include(": strLine = strLine[12:] intCloseBracketPos = strLine.find(")") if intCloseBracketPos>=0: strReferencedFile = os.path.join(strDirectory, strLine[0:intCloseBracketPos]) strDirectory = os.path.dirname(strReferencedFile) inf = open(strReferencedFile, "r") for strLine in inf.readlines(): ProcessLine(strDirectory, strLine) print # Make sure a new line is started after the inclusion. inf.close() else: print strLine, if len(sys.argv)<2: arrLines = sys.stdin.readlines() strDirectory = os.getcwd() strFile = "" else: strFile = sys.argv[1] if strFile<>"-help": inf = open(strFile, "r") arrLines = inf.readlines() inf.close strDirectory = os.path.dirname(strFile) if strFile=="-help": print __doc__ else: for strLine in arrLines: ProcessLine(strDirectory,strLine)