Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ln -s trick you can do with firejail, but with everything. In Python.
ln -s ~/.bin/ ~/.bin/echo
import sys
import subprocess
import os
import sys
from pathlib import Path
def figure_path_list():
got = os.getenv('LNS_TRICK_PATH')
got = got and got.split(';') or []
if len(got) == 0 or got[-1] == '+':
return [os.getenv('HOME') + "/.config/lns-trick/", "/etc/lns-trick/",
"/usr/share/lib/lns-trick/defaults/", ""] + got
return got
def find_file(path_list, file):
for p in path_list:
uf = p + "/" + file + ".args"
if Path(uf).is_file():
return uf
def parse_file(fd, path_list):
for line in fd.readlines():
if line.startswith(':inc'):
file = find_file(path_list, line[5:-1])
if file:
with open(file) as sfd:
yield parse_file(sfd, path_list)
# elif line.startswith(':as line'): # Better allow quotes?
# yield line[9:-1]
# elif line.startswith(':just print'): # TODO maybe allow disabling..
# assert line == ":just print\n"
# yield 1
assert not line.startswith(':'), "Dont have this command; " + line
if line.startswith('\\'): line = line[1:]
for el in line[:-1].split(" "): # TODO quotation..
yield el
def flatten(gen):
for el in gen:
if type(el) == str:
yield el
for sel in flatten(el):
yield sel
def prog():
path_list = figure_path_list()
progname = sys.argv[0].split('/')[-1]
file = find_file(path_list, progname)
if file:
with open(file) as fd:
#print("\n".join(list(flatten(parse_file(fd, path_list)))
# + sys.argv[1:]))
# TODO instead do $PATH, ignoring this program itself?["/usr/bin/" + progname] +
list(flatten(parse_file(fd, path_list)))
+ sys.argv[1:])
print("Didn't find file for:", progname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment