Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #2027 from getnikola/optimize-nikola-check
Optimize nikola check
  • Loading branch information
Kwpolska committed Sep 6, 2015
2 parents f3f855f + f8798e5 commit 27dca29
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions nikola/plugins/command/check.py
Expand Up @@ -46,7 +46,10 @@
from nikola.utils import get_logger, STDERR_HANDLER


def _call_nikola_list(site):
def _call_nikola_list(site, cache=None):
if cache is not None:
if 'files' in cache and 'deps' in cache:
return cache['files'], cache['deps']
files = []
deps = defaultdict(list)
for task in generate_tasks('render_site', site.gen_tasks('render_site', "Task", '')):
Expand All @@ -57,16 +60,19 @@ def _call_nikola_list(site):
files.extend(task.targets)
for target in task.targets:
deps[target].extend(task.file_dep)
if cache is not None:
cache['files'] = files
cache['deps'] = deps
return files, deps


def real_scan_files(site):
def real_scan_files(site, cache=None):
"""Scan for files."""
task_fnames = set([])
real_fnames = set([])
output_folder = site.config['OUTPUT_FOLDER']
# First check that all targets are generated in the right places
for fname in _call_nikola_list(site)[0]:
for fname in _call_nikola_list(site, cache)[0]:
fname = fname.strip()
if fname.startswith(output_folder):
task_fnames.add(fname)
Expand Down Expand Up @@ -162,17 +168,19 @@ def _execute(self, options, args):
self.logger.level = 1
else:
self.logger.level = 4
failure = False
if options['links']:
failure = self.scan_links(options['find_sources'], options['remote'])
failure |= self.scan_links(options['find_sources'], options['remote'])
if options['files']:
failure = self.scan_files()
failure |= self.scan_files()
if options['clean']:
failure = self.clean_files()
failure |= self.clean_files()
if failure:
return 1

existing_targets = set([])
checked_remote_targets = {}
cache = {}

def analyze(self, fname, find_sources=False, check_remote=False):
"""Analyze links on a page."""
Expand All @@ -186,7 +194,7 @@ def analyze(self, fname, find_sources=False, check_remote=False):

deps = {}
if find_sources:
deps = _call_nikola_list(self.site)[1]
deps = _call_nikola_list(self.site, self.cache)[1]

if url_type in ('absolute', 'full_path'):
url_netloc_to_root = urlparse(self.site.config['BASE_URL']).path
Expand Down Expand Up @@ -359,7 +367,7 @@ def scan_links(self, find_sources=False, check_remote=False):
if urlparse(self.site.config['BASE_URL']).netloc == 'example.com':
self.logger.error("You've not changed the SITE_URL (or BASE_URL) setting from \"example.com\"!")

for fname in _call_nikola_list(self.site)[0]:
for fname in _call_nikola_list(self.site, self.cache)[0]:
if fname.startswith(output_folder):
if '.html' == fname[-5:]:
if self.analyze(fname, find_sources, check_remote):
Expand All @@ -379,7 +387,7 @@ def scan_files(self):
failure = False
self.logger.info("Checking Files:")
self.logger.info("===============\n")
only_on_output, only_on_input = real_scan_files(self.site)
only_on_output, only_on_input = real_scan_files(self.site, self.cache)

# Ignore folders
only_on_output = [p for p in only_on_output if not os.path.isdir(p)]
Expand All @@ -402,7 +410,7 @@ def scan_files(self):

def clean_files(self):
"""Remove orphaned files."""
only_on_output, _ = real_scan_files(self.site)
only_on_output, _ = real_scan_files(self.site, self.cache)
for f in only_on_output:
self.logger.info('removed: {0}'.format(f))
os.unlink(f)
Expand Down

0 comments on commit 27dca29

Please sign in to comment.