mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-15 20:50:05 +00:00
speed up template detection
This commit is contained in:
@@ -6,6 +6,9 @@ jinja2 template generator
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import io
|
||||||
|
import re
|
||||||
|
import mmap
|
||||||
from jinja2 import Environment, FileSystemLoader, \
|
from jinja2 import Environment, FileSystemLoader, \
|
||||||
ChoiceLoader, FunctionLoader, TemplateNotFound, \
|
ChoiceLoader, FunctionLoader, TemplateNotFound, \
|
||||||
StrictUndefined
|
StrictUndefined
|
||||||
@@ -244,16 +247,19 @@ class Templategen:
|
|||||||
"""test if file pointed by path is a template"""
|
"""test if file pointed by path is a template"""
|
||||||
if not os.path.isfile(path):
|
if not os.path.isfile(path):
|
||||||
return False
|
return False
|
||||||
|
if os.stat(path).st_size == 0:
|
||||||
|
return False
|
||||||
|
markers = [BLOCK_START, VAR_START, COMMENT_START]
|
||||||
|
patterns = [re.compile(marker.encode()) for marker in markers]
|
||||||
try:
|
try:
|
||||||
with open(path, 'r') as f:
|
with io.open(path, "r", encoding="utf-8") as f:
|
||||||
data = f.read()
|
m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
|
||||||
|
for pattern in patterns:
|
||||||
|
if pattern.search(m):
|
||||||
|
return True
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
# is binary so surely no template
|
# is binary so surely no template
|
||||||
return False
|
return False
|
||||||
markers = [BLOCK_START, VAR_START, COMMENT_START]
|
|
||||||
for marker in markers:
|
|
||||||
if marker in data:
|
|
||||||
return True
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _debug_dict(self, title, elems):
|
def _debug_dict(self, title, elems):
|
||||||
|
|||||||
@@ -67,8 +67,8 @@ config:
|
|||||||
actions:
|
actions:
|
||||||
pre:
|
pre:
|
||||||
first: 'echo first > ${tmpa}/cookie'
|
first: 'echo first > ${tmpa}/cookie'
|
||||||
second: 'echo second >> ${tmpa}/cookie'
|
second: 'sleep 1; echo second >> ${tmpa}/cookie'
|
||||||
third: 'echo third >> ${tmpa}/cookie'
|
third: 'sleep 1; echo third >> ${tmpa}/cookie'
|
||||||
dotfiles:
|
dotfiles:
|
||||||
f_first:
|
f_first:
|
||||||
dst: ${tmpd}/first
|
dst: ${tmpd}/first
|
||||||
|
|||||||
Reference in New Issue
Block a user