fwinebox/fwinebox.py

143 lines
3.3 KiB
Python
Executable File

#!/usr/bin/env python3
import sys
import os
import subprocess
import configparser
import readline
readline.parse_and_bind("tab: complete")
sp = os.path.dirname(os.path.realpath(__file__))
p = os.path.join
#initializing the default configuration
config = configparser.ConfigParser()
config["DEFAULT"] = {
"prefixLocation": "$sd+/prefixes",
"homeLocation": "$config:DEFAULT/prefixLocation+/+$prefix+/home"
}
configVars = {
"sd": sp,
"cd": os.getcwd()
}
def walklevel(some_dir, level=0):
some_dir = some_dir.rstrip(os.path.sep)
assert os.path.isdir(some_dir)
num_sep = some_dir.count(os.path.sep)
for root, dirs, files in os.walk(some_dir):
yield root, dirs, files
num_sep_this = root.count(os.path.sep)
if num_sep + level <= num_sep_this:
del dirs[:]
def loadConfig():
global config
config.read(p(sp,"fwinebox.ini"))
def parseConfigValue(line):
out = ""
valList = line.split("+")
for value in valList:
if (value[0] == "$"):
value = value[1:]
if (value.startswith("config:")):
value = value[len("config:"):]
value = value.split("/")
value = parseConfigValue(config[value[0]][value[1]])
out = out + value
continue
out = out + configVars[value]
continue
out = out + value
#input("parsed: " +out)
return out
cv = parseConfigValue
def launchPrefix(prefix,cmd,raw = False):
configVars["prefix"] = prefix
home = os.environ["HOME"]
os.environ["HOME"] = cv(config["DEFAULT"]["homeLocation"])
os.environ["WINEPREFIX"] = p(cv(config["DEFAULT"]["prefixLocation"]),prefix)
subprocess.call(cmd)
os.environ["HOME"] = home
del os.environ["WINEPREFIX"]
del configVars["prefix"]
def finput(dir,text = ""):
cd = os.getcwd()
os.chdir(dir)
inp = input(text)
if (len(inp) > 0):
if (inp[len(inp) - 1]) == "/": inp = inp[:len(inp) - 1]
os.chdir(cd)
return inp
def main():
args = sys.argv[1:]
if args != []:
prefixLocation = cv(config["DEFAULT"]["prefixLocation"])
if not os.path.isdir(p(prefixLocation,args[0])):
print("error: prefix does not exist")
return
cmd = ["bash"]
if (len(args) > 1):
cmd = args[1:]
launchPrefix(args[0],cmd)
return
while True:
prefixLocation = cv(config["DEFAULT"]["prefixLocation"])
if not (os.path.isdir(prefixLocation)):
os.makedirs(prefixLocation)
print("list of prefixes:")
for root,dirs,files in walklevel(prefixLocation):
for dir in dirs:
print(dir)
prefix = finput(prefixLocation,"\nprefix: ")
prefixFolder = p(prefixLocation,prefix)
if not os.path.isdir(prefixFolder):
while True:
print("\nthis wine-prefix does not exist yet, would you like to create it? (y/n)")
choice = input().lower()
if (choice == "n"): print(""); break
if (choice != "y"): continue
while True:
print("\n(6)4 or (3)2 bit?")
choice = input().lower()
syswow = False
if (choice == "6"):
syswow = True
elif (choice != "3"): continue
os.makedirs(prefixFolder)
if (syswow == False): os.environ["WINEARCH"] = "win32"
launchPrefix(prefix,["wine","winecfg"])
if "WINEARCH" in os.environ: del os.environ["WINEARCH"]
break
break
if os.path.isdir(prefixFolder):
print("\nlaunching " +prefix+ "...")
try:
launchPrefix(prefix,["bash"])
except KeyboardInterrupt:
pass
print("\nended wine-session")
return
if __name__ == "__main__":
loadConfig()
main()