me.fier.chroot/mfchroot

88 lines
1.9 KiB
Python
Executable File

#!/usr/bin/env python3
import sys
import os
import subprocess
p = os.path.join
pUp = os.path.dirname
s = False
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
s = os.path.realpath(sys.executable)
else:
s = os.path.realpath(__file__)
sd = pUp(s)
def runCode(st,name,glb = False):
if glb == False: glb = {}
code = compile(st,name,"exec")
exec(code,glb)
def runFile(path,glb = False):
return runCode(open(path,encoding="utf-8").read(),path,glb)
def getScriptFiles(paths):
sfiles = {}
for path in paths:
for root,dirs,files in os.walk(path):
for file in sorted(files):
ffile = p(root,file)
lfile = ffile.replace(root + os.path.sep,"",1)
if not lfile in sfiles: sfiles[lfile] = ffile
break
return sfiles
def runStage(d,stage,env = False):
if env == False: env = {}
env["dir"] = d
env["stage"] = stage
glb = {}
glb["_g"] = globals()
glb["_chroot"] = env
sdirs = []
sdir = p(d,"opt","mfchroot","scripts",stage)
if os.path.isdir(sdir): sdirs.append(sdir)
sdir = p(sd,"scripts",stage)
if os.path.isdir(sdir): sdirs.append(sdir)
sfiles = getScriptFiles(sdirs)
for sfile in sorted(sfiles.keys()):
runFile(sfiles[sfile],glb)
def call(cmd,*args,**kwargs):
proc = subprocess.Popen(cmd,*args,**kwargs)
rtn = proc.wait()
if rtn != 0: raise subprocess.CalledProcessError(rtn,cmd)
return proc
def callRtn(cmd,*args,**kwargs):
proc = subprocess.Popen(cmd,*args,**kwargs)
return proc.wait()
def mountBind(start,end):
if callRtn(["mountpoint","-q",end]) == 0: return
if not os.path.isdir(end): os.makedirs(end)
call(["mount","-o","x-gvfs-hide","--bind",start,end])
def umount(end):
if callRtn(["mountpoint","-q",end]) != 0: return
call(["umount","-lR",end])
def main():
if sys.argv[1] == "-u":
runStage(sys.argv[2],"umount")
return
if sys.argv[1] == "-m":
runStage(sys.argv[2],"mount")
return
runStage(sys.argv[1],"mount")
env = {}
env["args"] = sys.argv[2:]
runStage(sys.argv[1],"chroot",env)
return
main()