Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Asset url timestamping for phpbb
#!/usr/bin/env python
# Current timestamp can be obtained by, e.g.:
#
# perl -e 'print time'
import os, os.path, re, sys
if len(sys.argv) != 2:
print >>sys.stderr, "Usage: timestamp-asset-urls timestamp"
exit(2)
timestamp = sys.argv[1]
root = None
base = os.path.join(os.path.dirname(__file__), '..')
for dir in ['webroot/forums']:
candidate_root = os.path.join(base, dir)
if os.path.exists(os.path.join(candidate_root, 'config.php')):
root = candidate_root
break
if not root:
print >>sys.stderr, "Unable to locate root"
exit(3)
css_regexp = re.compile(r'url\( *((?:"|\')?)(.*?\.(png|gif|jpe?g))(\?\d+)?\1\)')
generic_regexp = re.compile(r'\b(\w+\.(?:png|gif|jpe?g|ico))(\?\d+)?\b')
js_css_regexp = re.compile(r'\b(\w+\.(?:css|js))(\?\d+)?\b')
css_functions_regexp = re.compile(r'(\.css)(\?\d+)?\b')
generic_file_regexp = re.compile(r'.*\.(php|html|js)\b$')
def read(path):
with open(path, 'r') as f:
return f.read()
def write(path, content):
with open(path, 'w') as f:
f.write(content)
def fix_css(path):
content = read(path)
new_content = css_regexp.sub(r'url(\2?%s)' % timestamp, content)
if content != new_content:
write(path, new_content)
def fix_generic(path):
content = read(path)
new_content = generic_regexp.sub(r'\1?%s' % timestamp, content)
if content != new_content:
write(path, new_content)
def fix_js_css(path):
content = read(path)
new_content = js_css_regexp.sub(r'\1?%s' % timestamp, content)
if content != new_content:
write(path, new_content)
def fix_functions(path):
content = read(path)
new_content = css_functions_regexp.sub(r'\1?%s' % timestamp, content)
if content != new_content:
write(path, new_content)
for parent, dirs, files in os.walk(root):
if '.svn' in dirs:
dirs.remove('.svn')
for file in files:
if file.endswith('.css'):
path = os.path.join(parent, file)
fix_css(path)
elif generic_file_regexp.match(file):
path = os.path.join(parent, file)
fix_generic(path)
template_paths = ('adm/style', 'styles')
for template_path in template_paths:
for parent, dirs, files in os.walk(os.path.join(root, template_path)):
if '.svn' in dirs:
dirs.remove('.svn')
for file in files:
if file.endswith('.html'):
path = os.path.join(parent, file)
fix_js_css(path)
fix_functions(os.path.join(root, 'includes/functions.php'))
# Copyright (c) 2010 Oleg Pudeyev
#
# 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.