Skip to content

Commit 2f2cd97

Browse files
committed
other/pack: Move __main__.py template to inside pack.py
1 parent 16193ab commit 2f2cd97

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

other/pack.mainpy.tpl

Lines changed: 0 additions & 4 deletions
This file was deleted.

other/pack.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,34 @@ def __init__(self, templates='.'):
4040
self.path = templates + '/'
4141
self.tag = re.compile('{{ *(?P<tag>\w+) *}}')
4242

43-
def render(self, template, vardict=None, **kwargs):
43+
def render(self, tplfile, vardict=None, **kwargs):
4444
"""returns unicode str"""
45+
tpltext = open(self.path + tplfile).read()
46+
return self.render_string(tpltext, vardict, **kwargs)
47+
48+
def render_string(self, tpltext, vardict=None, **kwargs):
4549
data = vardict or {}
4650
data.update(kwargs)
4751

4852
def lookup(match):
4953
return data[match.group('tag')]
5054

51-
tpl = open(self.path + template).read()
5255
if not self.PY3K:
53-
return unicode(self.tag.sub(lookup, tpl))
56+
return unicode(self.tag.sub(lookup, tpltext))
5457
else:
55-
return self.tag.sub(lookup, tpl)
58+
return self.tag.sub(lookup, tpltext)
5659

5760
# ---
5861

5962
BASE = os.path.abspath(os.path.dirname(__file__))
6063

64+
MAINTPL = """\
65+
import sys
66+
67+
import {{ module }}
68+
sys.exit({{ module }}.main())
69+
"""
70+
6171
if __name__ == '__main__':
6272
if not sys.argv[1:]:
6373
sys.exit("usage: pack.py <module.py>")
@@ -74,7 +84,7 @@ def lookup(match):
7484
zf = zipadd(packname, modpath, os.path.basename(modpath))
7585
print("[*] Making %s executable" % (packname))
7686
# http://techtonik.rainforce.org/2015/01/shipping-python-tools-in-executable-zip.html
77-
text = MiniJinja(BASE).render('pack.mainpy.tpl', module=modname)
87+
text = MiniJinja(BASE).render_string(MAINTPL, module=modname)
7888
zf.writestr('__main__.py', text)
7989
print("[*] Making %s installable" % (packname))
8090
text2 = MiniJinja(BASE).render('pack.setuppy.tpl', module=modname, version=version)

0 commit comments

Comments
 (0)