py3: Switch almost all shebang lines to use `python3`.
This causes `upgrade-zulip-from-git`, as well as a no-option run of
`tools/build-release-tarball`, to produce a Zulip install running
Python 3, rather than Python 2. In particular this means that the
virtualenv we create, in which all application code runs, is Python 3.
One shebang line, on `zulip-ec2-configure-interfaces`, explicitly
keeps Python 2, and at least one external ops script, `wal-e`, also
still runs on Python 2. See discussion on the respective previous
commits that made those explicit. There may also be some other
third-party scripts we use, outside of this source tree and running
outside our virtualenv, that still run on Python 2.
2017-08-02 23:15:16 +02:00
|
|
|
#!/usr/bin/env python3
|
2016-08-02 00:14:01 +02:00
|
|
|
from lib.template_parser import validate
|
2017-01-06 15:11:15 +01:00
|
|
|
from lib.html_branches import build_id_dict
|
2017-03-12 22:24:26 +01:00
|
|
|
from lib.pretty_print import validate_indent_html
|
2016-08-29 00:28:59 +02:00
|
|
|
import argparse
|
2013-10-25 23:46:02 +02:00
|
|
|
import sys
|
2017-01-06 15:11:15 +01:00
|
|
|
import logging
|
2016-08-02 00:14:01 +02:00
|
|
|
|
2017-02-05 21:24:28 +01:00
|
|
|
# check for the venv
|
|
|
|
from lib import sanity_check
|
|
|
|
sanity_check.check_venv(__file__)
|
|
|
|
|
2018-08-04 22:02:09 +02:00
|
|
|
from zulint import lister
|
2019-02-02 23:53:29 +01:00
|
|
|
from typing import cast, Dict, Iterable, List
|
2013-10-25 23:46:02 +02:00
|
|
|
|
2017-01-06 15:11:15 +01:00
|
|
|
EXCLUDED_FILES = [
|
|
|
|
## Test data Files for testing modules in tests
|
|
|
|
"tools/tests/test_template_data",
|
2018-11-13 17:50:56 +01:00
|
|
|
# Our parser doesn't handle the way its conditionals are layered
|
2018-12-31 09:54:59 +01:00
|
|
|
'templates/zerver/emails/missed_message.source.html',
|
2017-01-06 15:11:15 +01:00
|
|
|
]
|
|
|
|
|
2017-12-13 19:38:15 +01:00
|
|
|
def check_our_files(modified_only, all_dups, targets):
|
|
|
|
# type: (bool, bool, List[str]) -> None
|
2016-07-24 15:24:48 +02:00
|
|
|
by_lang = cast(
|
|
|
|
Dict[str, List[str]],
|
|
|
|
lister.list_files(
|
2017-03-19 21:02:58 +01:00
|
|
|
targets=targets,
|
2016-08-29 00:28:59 +02:00
|
|
|
modified_only=args.modified,
|
2016-07-24 15:24:48 +02:00
|
|
|
ftypes=['handlebars', 'html'],
|
2017-01-06 15:11:15 +01:00
|
|
|
group_by_ftype=True, exclude=EXCLUDED_FILES))
|
2013-10-25 20:47:03 +02:00
|
|
|
|
2017-03-19 20:46:01 +01:00
|
|
|
check_handlebar_templates(by_lang['handlebars'])
|
|
|
|
check_html_templates(by_lang['html'], args.all_dups)
|
2013-11-18 22:57:55 +01:00
|
|
|
|
2017-12-13 19:38:15 +01:00
|
|
|
def check_html_templates(templates, all_dups):
|
|
|
|
# type: (Iterable[str], bool) -> None
|
2016-07-09 02:43:32 +02:00
|
|
|
# Our files with .html extensions are usually for Django, but we also
|
2016-07-12 21:28:08 +02:00
|
|
|
# have a few static .html files.
|
|
|
|
#
|
|
|
|
# We also have .html files that we vendored from Casper.
|
|
|
|
# The casperjs files use HTML5 (whereas Zulip prefers XHTML), and
|
|
|
|
# there are also cases where Casper deliberately uses invalid HTML,
|
|
|
|
# so we exclude them from our linter.
|
2017-01-06 15:11:15 +01:00
|
|
|
logging.basicConfig(format='%(levelname)s:%(message)s')
|
2016-07-12 21:28:08 +02:00
|
|
|
templates = filter(
|
2017-06-14 01:02:13 +02:00
|
|
|
lambda fn: ('casperjs' not in fn),
|
2016-07-12 21:28:08 +02:00
|
|
|
templates)
|
2016-07-09 02:43:32 +02:00
|
|
|
templates = sorted(list(templates))
|
2017-11-17 19:50:55 +01:00
|
|
|
# Use of underscore templates <%= %>.
|
2017-11-25 18:41:18 +01:00
|
|
|
if 'templates/zerver/team.html' in templates:
|
|
|
|
templates.remove('templates/zerver/team.html')
|
2013-10-25 20:47:03 +02:00
|
|
|
|
2018-04-27 15:04:40 +02:00
|
|
|
def check_for_duplicate_ids(templates: List[str]) -> Dict[str, List[str]]:
|
|
|
|
template_id_dict = build_id_dict(templates)
|
|
|
|
# TODO: Clean up these cases of duplicate ids in the code
|
|
|
|
IGNORE_IDS = [
|
|
|
|
'api-example-tabs',
|
|
|
|
'errors',
|
2018-08-04 17:22:30 +02:00
|
|
|
'error-message-box',
|
2018-04-27 15:04:40 +02:00
|
|
|
'email',
|
|
|
|
'messages',
|
|
|
|
'registration',
|
|
|
|
'pw_strength',
|
|
|
|
'id_password',
|
|
|
|
'top_navbar',
|
|
|
|
'id_email',
|
|
|
|
'id_terms',
|
|
|
|
'send_confirm',
|
|
|
|
'register',
|
|
|
|
'footer',
|
2018-07-14 11:39:33 +02:00
|
|
|
# Temporary while we have searchbox forked
|
|
|
|
'search_exit',
|
|
|
|
'search_query',
|
|
|
|
'tab_bar',
|
|
|
|
'search_arrows',
|
|
|
|
'searchbox_form',
|
|
|
|
'searchbox',
|
2018-04-27 15:04:40 +02:00
|
|
|
]
|
|
|
|
bad_ids_dict = {ids: fns for ids, fns in template_id_dict.items()
|
|
|
|
if (ids not in IGNORE_IDS) and len(fns) > 1}
|
2017-01-06 15:11:15 +01:00
|
|
|
|
2018-04-27 15:04:40 +02:00
|
|
|
if all_dups:
|
|
|
|
ignorable_ids_dict = {ids: fns for ids, fns in template_id_dict.items()
|
|
|
|
if ids in IGNORE_IDS and len(fns) > 1}
|
2017-01-06 15:11:15 +01:00
|
|
|
|
2018-04-27 15:04:40 +02:00
|
|
|
for ids, fns in ignorable_ids_dict.items():
|
|
|
|
logging.warning("Duplicate ID(s) detected :Id '" + ids +
|
|
|
|
"' present at following files:")
|
|
|
|
for fn in fns:
|
|
|
|
print(fn)
|
|
|
|
|
|
|
|
for ids, fns in bad_ids_dict.items():
|
|
|
|
logging.error("Duplicate ID(s) detected :Id '" + ids +
|
|
|
|
"' present at following files:")
|
2017-01-06 15:11:15 +01:00
|
|
|
for fn in fns:
|
|
|
|
print(fn)
|
2018-04-27 15:04:40 +02:00
|
|
|
return bad_ids_dict
|
|
|
|
|
|
|
|
bad_ids_list = [] # type: List[str]
|
|
|
|
archive_templates = list(filter(
|
|
|
|
lambda fn: ('templates/zerver/archive' in fn),
|
|
|
|
templates))
|
|
|
|
templates = list(filter(
|
|
|
|
lambda fn: ('templates/zerver/archive' not in fn),
|
|
|
|
templates))
|
2017-01-06 15:11:15 +01:00
|
|
|
|
2018-04-27 15:04:40 +02:00
|
|
|
bad_ids_list += list(check_for_duplicate_ids(archive_templates).keys())
|
|
|
|
bad_ids_list += list(check_for_duplicate_ids(templates).keys())
|
2017-01-06 15:11:15 +01:00
|
|
|
|
2018-04-27 15:04:40 +02:00
|
|
|
if bad_ids_list:
|
2017-01-06 15:11:15 +01:00
|
|
|
print('Exiting--please clean up all duplicates before running this again.')
|
|
|
|
sys.exit(1)
|
|
|
|
|
2014-02-27 16:41:47 +01:00
|
|
|
for fn in templates:
|
2016-07-12 22:32:36 +02:00
|
|
|
# Many of our Django templates have strange indentation. The
|
|
|
|
# indentation errors are often harmless, even stylistically
|
|
|
|
# harmless, but they tend to be in files that might be old
|
|
|
|
# and might eventually require more scrutiny for things like
|
|
|
|
# localization. See github #1236.
|
|
|
|
bad_files = [
|
2017-10-20 01:54:07 +02:00
|
|
|
# These use various whitespace-dependent formatting that
|
|
|
|
# prevent cleaning them.
|
2016-07-12 22:32:36 +02:00
|
|
|
'templates/corporate/zephyr-mirror.html',
|
2017-10-20 01:51:14 +02:00
|
|
|
# Can't clean this because of `preserve_spaces`
|
2018-04-22 07:02:19 +02:00
|
|
|
'templates/zerver/app/markdown_help.html',
|
2016-07-12 22:32:36 +02:00
|
|
|
]
|
2016-08-04 01:44:15 +02:00
|
|
|
validate(fn=fn, check_indent=(fn not in bad_files))
|
2014-02-27 16:41:47 +01:00
|
|
|
|
2017-03-12 22:24:26 +01:00
|
|
|
# Ignore these files since these have not been cleaned yet :/
|
|
|
|
IGNORE_FILES = [
|
2018-01-28 00:17:28 +01:00
|
|
|
# zephyr-mirror.html has some whitespace-dependent formatting
|
|
|
|
# for code blocks that prevent cleaning it. Might make sense
|
|
|
|
# to convert it to a /help/ markdown article.
|
|
|
|
'templates/corporate/zephyr-mirror.html',
|
2018-01-27 18:44:00 +01:00
|
|
|
# Can't clean this because of `preserve_spaces`
|
2018-04-22 07:02:19 +02:00
|
|
|
'templates/zerver/app/markdown_help.html',
|
2017-03-12 22:24:26 +01:00
|
|
|
]
|
|
|
|
# TODO: Clean these files
|
|
|
|
for fn in templates:
|
|
|
|
if fn not in IGNORE_FILES:
|
|
|
|
if not validate_indent_html(fn):
|
|
|
|
sys.exit(1)
|
|
|
|
|
2017-12-13 19:38:15 +01:00
|
|
|
def check_handlebar_templates(templates):
|
|
|
|
# type: (Iterable[str]) -> None
|
2016-08-02 00:14:01 +02:00
|
|
|
# Check all our handlebars templates.
|
|
|
|
templates = [fn for fn in templates if fn.endswith('.handlebars')]
|
2017-08-28 07:01:00 +02:00
|
|
|
|
|
|
|
IGNORE_FILES = [
|
|
|
|
# TODO: Add some exclude mechanism for the line-wrapping issue here.
|
|
|
|
'static/templates/recipient_row.handlebars',
|
|
|
|
]
|
|
|
|
|
2016-08-02 00:14:01 +02:00
|
|
|
for fn in templates:
|
2017-08-28 07:01:00 +02:00
|
|
|
if fn in IGNORE_FILES:
|
|
|
|
continue
|
2016-08-04 01:44:15 +02:00
|
|
|
validate(fn=fn, check_indent=True)
|
2016-08-02 00:14:01 +02:00
|
|
|
|
2017-03-12 22:24:26 +01:00
|
|
|
for fn in templates:
|
2017-08-28 07:01:00 +02:00
|
|
|
if fn in IGNORE_FILES:
|
|
|
|
continue
|
2017-05-12 17:17:05 +02:00
|
|
|
if not validate_indent_html(fn):
|
|
|
|
sys.exit(1)
|
2017-03-12 22:24:26 +01:00
|
|
|
|
2014-02-27 16:41:47 +01:00
|
|
|
if __name__ == '__main__':
|
2017-03-19 20:59:22 +01:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('-m', '--modified',
|
|
|
|
action='store_true', default=False,
|
|
|
|
help='only check modified files')
|
|
|
|
parser.add_argument('--all-dups',
|
|
|
|
action="store_true", default=False,
|
|
|
|
help='Run lint tool to detect duplicate ids on ignored files as well')
|
2017-03-19 21:02:58 +01:00
|
|
|
parser.add_argument('targets', nargs=argparse.REMAINDER)
|
2017-03-19 20:59:22 +01:00
|
|
|
args = parser.parse_args()
|
2017-03-19 21:02:58 +01:00
|
|
|
check_our_files(args.modified, args.all_dups, args.targets)
|