Skip to content

Instantly share code, notes, and snippets.

@prometheanfire
Created July 25, 2019 05:20
Show Gist options
  • Save prometheanfire/155202c82d04737c71997e5550354bd2 to your computer and use it in GitHub Desktop.
Save prometheanfire/155202c82d04737c71997e5550354bd2 to your computer and use it in GitHub Desktop.
stdin
#!/usr/bin/python-exec2c
# vim:fileencoding=utf-8:ft=python
# (c) 2012-2016 Michał Górny
# Released under the terms of the 2-clause BSD license.
#
# This is not the script you are looking for. This is just a wrapper.
# The actual scripts of this application were installed
# in subdirectories of /usr/lib/python-exec.
# You are most likely looking for one of those.
from __future__ import with_statement
# prepare a clean globals for exec()
new_globals = dict(globals())
# we have to keep the import top, so delete it from globals
del new_globals['with_statement']
import errno, os, os.path, sys
try:
from epython import EPYTHON
except ImportError:
EPYTHON = os.path.basename(sys.executable)
if '' and EPYTHON.endswith(''):
EPYTHON = EPYTHON[:-len('')]
# Alike python-exec2c, perform symlink resolution on target until
# EINVAL is hit. If the final target is python-exec2, use the last
# symlink name preceding it. Otherwise, use the final target.
prev_target = None
target = sys.argv[0]
while True:
try:
next_target = os.path.join(os.path.dirname(target),
os.readlink(target))
except OSError as e:
if e.errno == errno.EINTR:
# retry
continue
elif e.errno == errno.EINVAL:
# if the final target is python-exec2, use last symlink
if os.path.basename(target) in ( 'python-exec2', 'python-exec2' ):
if prev_target is None:
sys.stderr.write('%s: python-exec2 is a wrapper, it must not be run directly.\n'
% target)
sys.exit(127)
target = prev_target
break
else:
raise
else:
prev_target = target
target = next_target
target = os.path.join('/usr/lib/python-exec', EPYTHON,
os.path.basename(target))
data = None
while data is None:
try:
kwargs = {}
if sys.version_info[0] >= 3:
import tokenize
# need to provide encoding
with open(target, 'rb') as f:
kwargs['encoding'] = tokenize.detect_encoding(f.readline)[0]
with open(target, 'r', **kwargs) as f:
data = f.read()
except IOError as e:
if e.errno == errno.EINTR:
# retry
continue
elif e.errno == errno.ENOENT:
sys.stderr.write('%s: this Python implementation (%s) is not supported by the script.\n'
% (target, EPYTHON))
sys.exit(127)
else:
raise
sys.argv[0] = target
new_globals['__file__'] = target
exec(data, new_globals)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment