Skip to content

Commit

Permalink
upgrade bundled jinja2
Browse files Browse the repository at this point in the history
  • Loading branch information
9001 committed Feb 3, 2021
1 parent 58f9e05 commit 0f558ec
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 150 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

turn your phone or raspi into a portable file server with resumable uploads/downloads using IE6 or any other browser

* server runs on anything with `py2.7` or `py3.2+`
* server runs on anything with `py2.7` or `py3.3+`
* *resumable* uploads need `firefox 12+` / `chrome 6+` / `safari 6+` / `IE 10+`
* code standard: `black`

Expand Down Expand Up @@ -68,9 +68,8 @@ summary: it works! you can use it! (but technically not even close to beta)
# dependencies

* `jinja2`
* pulls in `markupsafe` as of v2.7; use jinja 2.6 on py3.2

optional, enables thumbnails:
optional, will eventually enable thumbnails:
* `Pillow` (requires py2.7 or py3.5+)


Expand Down
32 changes: 18 additions & 14 deletions scripts/make-sfx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,32 @@ cd sfx
)/pe-copyparty"

echo "repack of files in $old"
cp -pR "$old/"*{jinja2,copyparty} .
mv {x.,}jinja2 2>/dev/null || true
cp -pR "$old/"*{dep-j2,copyparty} .
}

[ $repack ] || {
echo collecting jinja2
f="../build/Jinja2-2.6.tar.gz"
f="../build/Jinja2-2.11.3.tar.gz"
[ -e "$f" ] ||
(url=https://files.pythonhosted.org/packages/25/c8/212b1c2fd6df9eaf536384b6c6619c4e70a3afd2dffdd00e5296ffbae940/Jinja2-2.6.tar.gz;
(url=https://files.pythonhosted.org/packages/4f/e7/65300e6b32e69768ded990494809106f87da1d436418d5f1367ed3966fd7/Jinja2-2.11.3.tar.gz;
wget -O$f "$url" || curl -L "$url" >$f)

tar -zxf $f
mv Jinja2-*/jinja2 .
rm -rf Jinja2-* jinja2/testsuite jinja2/_markupsafe/tests.py jinja2/_stringdefs.py
mv Jinja2-*/src/jinja2 .
rm -rf Jinja2-*

f=jinja2/lexer.py
sed -r '/.*föö.*/ raise SyntaxError/' <$f >t
tmv $f

f=jinja2/_markupsafe/_constants.py
awk '!/: [0-9]+,?$/ || /(amp|gt|lt|quot|apos|nbsp).:/' <$f >t
tmv $f
echo collecting markupsafe
f="../build/MarkupSafe-1.1.1.tar.gz"
[ -e "$f" ] ||
(url=https://files.pythonhosted.org/packages/b9/2e/64db92e53b86efccfaea71321f597fa2e1b2bd3853d8ce658568f7a13094/MarkupSafe-1.1.1.tar.gz;
wget -O$f "$url" || curl -L "$url" >$f)

tar -zxf $f
mv MarkupSafe-*/src/markupsafe .
rm -rf MarkupSafe-* markupsafe/_speedups.c

mkdir dep-j2/
mv {markupsafe,jinja2} dep-j2/

# msys2 tar is bad, make the best of it
echo collecting source
Expand Down Expand Up @@ -177,7 +181,7 @@ args=(--owner=1000 --group=1000)
[ "$OSTYPE" = msys ] &&
args=()

tar -cf tar "${args[@]}" --numeric-owner copyparty jinja2
tar -cf tar "${args[@]}" --numeric-owner copyparty dep-j2

echo compressing tar
# detect best level; bzip2 -7 is usually better than -9
Expand Down
152 changes: 20 additions & 132 deletions scripts/sfx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# coding: utf-8
from __future__ import print_function, unicode_literals

import re, os, sys, time, shutil, signal, tarfile, hashlib, platform, tempfile
import os, sys, time, shutil, signal, tarfile, hashlib, platform, tempfile
import subprocess as sp

"""
Expand Down Expand Up @@ -202,93 +202,6 @@ def u8(gen):
yield s


def get_py_win(ret):
tops = []
p = str(os.getenv("LocalAppdata"))
if p:
tops.append(os.path.join(p, "Programs", "Python"))

progfiles = {}
for p in ["ProgramFiles", "ProgramFiles(x86)"]:
p = str(os.getenv(p))
if p:
progfiles[p] = 1
# 32bit apps get x86 for both
if p.endswith(" (x86)"):
progfiles[p[:-6]] = 1

tops += list(progfiles.keys())

for sysroot in [me, sys.executable]:
sysroot = sysroot[:3].upper()
if sysroot[1] == ":" and sysroot not in tops:
tops.append(sysroot)

# $WIRESHARK_SLOGAN
for top in tops:
try:
for name1 in u8(sorted(os.listdir(top), reverse=True)):
if name1.lower().startswith("python"):
path1 = os.path.join(top, name1)
try:
for name2 in u8(os.listdir(path1)):
if name2.lower() == "python.exe":
path2 = os.path.join(path1, name2)
ret[path2.lower()] = path2
except:
pass
except:
pass


def get_py_nix(ret):
ptn = re.compile(r"^(python|pypy)[0-9\.-]*$")
for bindir in os.getenv("PATH").split(":"):
if not bindir:
next

try:
for fn in u8(os.listdir(bindir)):
if ptn.match(fn):
fn = os.path.join(bindir, fn)
ret[fn.lower()] = fn
except:
pass


def read_py(binp):
cmd = [
binp,
"-c",
"import sys; sys.stdout.write(' '.join(str(x) for x in sys.version_info)); import jinja2",
]
p = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE)
ver, _ = p.communicate()
ver = ver.decode("utf-8").split(" ")[:3]
ver = [int(x) if x.isdigit() else 0 for x in ver]
return ver, p.returncode == 0


def get_pys():
ver, chk = read_py(sys.executable)
if chk or PY2:
return [[chk, ver, sys.executable]]

hits = {sys.executable.lower(): sys.executable}
if platform.system() == "Windows":
get_py_win(hits)
else:
get_py_nix(hits)

ret = []
for binp in hits.values():
ver, chk = read_py(binp)
ret.append([chk, ver, binp])
msg("\t".join(str(x) for x in ret[-1]))

return ret


def yieldfile(fn):
with open(fn, "rb") as f:
for block in iter(lambda: f.read(64 * 1024), b""):
Expand Down Expand Up @@ -440,12 +353,11 @@ def confirm():
pass


def run(tmp, py):
def run(tmp, j2ver):
global cpp

msg("OK")
msg("will use:", py)
msg("bound to:", tmp)
msg("jinja2:", j2ver or "bundled")
msg("sfxdir:", tmp)

# "systemd-tmpfiles-clean.timer"?? HOW do you even come up with this shit
try:
Expand All @@ -457,24 +369,20 @@ def run(tmp, py):
except:
pass

fp_py = os.path.join(tmp, "py")
try:
with open(fp_py, "wb") as f:
f.write(py.encode("utf-8") + b"\n")
except:
pass
ld = [tmp, os.path.join(tmp, "dep-j2")]
if j2ver:
del ld[-1]

# avoid loading ./copyparty.py
cmd = [
py,
"-c",
'import sys, runpy; sys.path.insert(0, r"'
+ tmp
+ '"); runpy.run_module("copyparty", run_name="__main__")',
] + list(sys.argv[1:])
cmd = (
"import sys, runpy; "
+ "".join(['sys.path.insert(0, r"' + x + '"); ' for x in ld])
+ 'runpy.run_module("copyparty", run_name="__main__")'
)
cmd = [sys.executable, "-c", cmd] + list(sys.argv[1:])

cmd = [str(x) for x in cmd]
msg("\n", cmd, "\n")
cpp = sp.Popen(str(x) for x in cmd)
cpp = sp.Popen(cmd)
try:
cpp.wait()
except:
Expand Down Expand Up @@ -526,33 +434,13 @@ def main():
signal.signal(signal.SIGTERM, bye)

tmp = unpack()
fp_py = os.path.join(tmp, "py")
if os.path.exists(fp_py):
with open(fp_py, "rb") as f:
py = f.read().decode("utf-8").rstrip()

return run(tmp, py)

pys = get_pys()
pys.sort(reverse=True)
j2, ver, py = pys[0]
if j2:
try:
os.rename(os.path.join(tmp, "jinja2"), os.path.join(tmp, "x.jinja2"))
except:
pass

return run(tmp, py)

msg("\n could not find jinja2; will use py2 + the bundled version\n")
for _, ver, py in pys:
if ver > [2, 7] and ver < [3, 0]:
return run(tmp, py)
try:
from jinja2 import __version__ as j2ver
except:
j2ver = None

m = "\033[1;31m\n\n\ncould not find a python with jinja2 installed; please do one of these:\n\n pip install --user jinja2\n\n install python2\n\n\033[0m"
msg(m)
confirm()
sys.exit(1)
return run(tmp, j2ver)


if __name__ == "__main__":
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ def run(self):
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.2",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
Expand Down

0 comments on commit 0f558ec

Please sign in to comment.