Skip to content

Instantly share code, notes, and snippets.

@giotta
Last active May 7, 2019 21:33
Show Gist options
  • Save giotta/ed99022931650c3d8c48f8da6b2816eb to your computer and use it in GitHub Desktop.
Save giotta/ed99022931650c3d8c48f8da6b2816eb to your computer and use it in GitHub Desktop.
#! /usr/bin/python3
# Python 3.6
# https://github.com/pkp/pkp-lib/issues/4727
import glob
files = glob.glob('ojs/**/*.*', recursive=True)
templates = glob.glob('ojs/**/*.tpl', recursive=True)
# TEMPLATES
# skip block.tpl
templates = [t for t in templates if t.split('/')[-1] != 'block.tpl']
errors = []
templates_data = {}
# for each template file
for template in templates:
# convert template's path to strings to search in files
searches = []
if template.startswith('ojs/lib/pkp/templates/'):
# core
search = template.replace('ojs/lib/pkp/templates/', 'core:')
searches.append(search)
search = template.replace('ojs/lib/pkp/templates/', '')
searches.append(search)
elif template.startswith('ojs/templates/'):
# ojs
search = template.replace('ojs/templates/', '')
searches.append(search)
elif template.startswith('ojs/plugins/'):
# plugins
filename = template.split('/')[-1]
# single quote call
search = f"getTemplateResource('{filename}')"
searches.append(search)
# double quote call
search = f'getTemplateResource("{filename}")'
searches.append(search)
else:
search = template
searches.append(search)
# init templates data
templates_data[template] = {}
templates_data[template]['searches'] = searches
templates_data[template]['dead'] = True
# search in all files
for path in files:
# don't search in the template file itself
if path != template:
try:
for search in templates_data[template]['searches']:
with open(path, 'r') as f:
for line in f:
if search in line:
templates_data[template]['dead'] = False
except UnicodeDecodeError:
errors.append(path)
except IsADirectoryError:
pass
deads = [k for k, v in templates_data.items() if v['dead']]
# report
print('## Templates')
print(71 * '-')
print()
nb = len(templates)
nb_deads = len(deads)
print(f'{nb_deads} deads (checks) out of {nb} templates')
for template in templates:
dead = templates_data[template]['dead']
md_dead = '[x]'
if not dead:
md_dead = '[ ]'
print(f'- {md_dead} {template}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment