Skip to content

Instantly share code, notes, and snippets.

@jmhobbs
Created August 3, 2010 21:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmhobbs/507221 to your computer and use it in GitHub Desktop.
Save jmhobbs/507221 to your computer and use it in GitHub Desktop.
Combine PHP Source Files Into THE MONOLITH
# -*- coding: utf-8 -*-
# This script is used to combine PHP source files together into one big glob.
# I wrote this so I could distribute a single file web application, but develop
# it in a sane fashion.
# Copyright (c) 2010, John M. Hobbs
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of the <ORGANIZATION> nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import os
import re
php_open_tags = re.compile( r'<\?php' )
php_close_tags = re.compile( r'\?>' )
php_include_file = re.compile( r'^.*[\'"](.*)[\'"].*' )
def is_include ( line ):
head = line.strip()
return head[0:12] == "include_once" or head[0:12] == "require_once" or head[0:7] == "include" or head[0:7] == "require"
def expand_file ( parent, filename, outhandle ):
working_directory = os.path.dirname( filename )
unclosed_tags = 0
try:
with open( filename, 'r' ) as inhandle:
print "INCLUDE:", parent, "=>", filename
for line in inhandle:
unclosed_tags += len( php_open_tags.findall( line ) )
unclosed_tags -= len( php_close_tags.findall( line ) )
if is_include( line ):
included_filename = php_include_file.sub( r'\1', line.strip() )
outhandle.write( "?>\n" )
expand_file( filename, "%s/%s" % ( working_directory, included_filename ), outhandle )
outhandle.write( "<?php\n" )
else:
outhandle.write( line )
except IOError, e:
print "WARNING: In %s, could not find %s" % ( parent, filename )
if unclosed_tags == 1:
outhandle.write( "?>\n" );
elif unclosed_tags > 1:
print "ERROR: Too many unclosed tags in", filename
if __name__ == "__main__":
import sys
if len( sys.argv ) < 3:
print "Usage: %s [input] [output]" % sys.argv[0]
exit( 1 )
# Extract!
with open( "%s.tmp" % sys.argv[2], 'w' ) as out:
expand_file( os.path.dirname( os.path.abspath( __file__ ) ), os.path.abspath( sys.argv[1] ), out )
# Minor post-processing to strip useless lines
with open( sys.argv[2], 'w' ) as outfile:
with open( "%s.tmp" % sys.argv[2], 'r' ) as infile:
last_line = None
for line in infile:
# ?>
# <?php
if last_line == "?>\n" and line == "<?php\n":
last_line = None
continue
# <?php
# ?>
if last_line == "<?php\n" and line == "?>\n":
last_line = None
continue
if last_line:
outfile.write( last_line )
last_line = line
if last_line:
outfile.write( last_line )
os.unlink( "%s.tmp" % sys.argv[2] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment