~charles/charles-util

14a9e778acd5be31ec0afeba9e8ef4c637025854 — Charles Daniels 5 years ago c204f20
bacchus can now generate launchers from start menu
1 files changed, 73 insertions(+), 10 deletions(-)

M bin/bacchus
M bin/bacchus => bin/bacchus +73 -10
@@ 237,24 237,76 @@ def generate_launcher(prefix, winearch, target, name=None):

    logging.info("Wrote launcher to '{}'".format(launcher_file))

def list_startmenu(prefix):
def resolve_startmenu_entry(prefix, target):
    smpath = pathlib.Path(os.path.join(get_prefix_dir(prefix),
        "drive_c/ProgramData/Microsoft/Windows/Start Menu/Programs/"))
    for path in smpath.glob("**/*.lnk"):
        print(path.name)
    lnkpath = smpath / target 

def run_start(prefix, winearch, name):
    smpath = pathlib.Path(os.path.join(get_prefix_dir(prefix),
        "drive_c/ProgramData/Microsoft/Windows/Start Menu/Programs/"))
    lnkpath = smpath / name
    logging.debug("resolving start menu entry {}".format(target))

    if not lnkpath.exists():
        logging.warning("no such start menu entry {}".format(lnkpath))
        lnkpath = smpath / "{}.lnk".format(name)
        lnkpath = smpath / "{}.lnk".format(target)

    if not lnkpath.exists():
        logging.error("no such start menu entry {}".format(lnkpath))
        return None

    logging.debug("start menu entry {} resolved to {}".format(target, lnkpath))
    return lnkpath

def generate_start_launcher(prefix, winearch, target, name=None):

    ensure_prefix(prefix)

    target = resolve_startmenu_entry(prefix, target)

    s = ""
    logging.debug("Generating launcher for start menu entry'{}'".format(target))

    # make sure the launcher dir exists
    launcher_dir = os.path.join(get_prefix_dir(prefix), "bacchus")
    if not os.path.exists(launcher_dir):
        logging.debug("Creating {}".format(launcher_dir))
        os.makedirs(launcher_dir, exist_ok=True)

    parent = os.path.dirname(target)

    # generate a name if needed
    if name is None:
        name = os.path.basename(target)
        if '.' in name:
            # strip extension
            name = '.'.join(name.split('.')[:1])
        logging.debug("generated name {}".format(name))

    launcher_file = os.path.join(launcher_dir, name)
    logging.debug("launcher file is {}".format(launcher_file))

    # write the launcher
    with open(launcher_file, 'w') as f:
        f.write("#!/bin/sh\n")
        f.write("\n")
        f.write("# Launcher generated by bacchus at {}\n\n"
                .format(datetime.datetime.now()))
        f.write('cd "{}"\n'.format(parent))
        f.write("export WINEARCH={}\n".format(winearch))
        f.write("export WINEPREFIX={}\n".format(get_prefix_dir(prefix)))
        f.write("wine start /unix ./{}\n".format(os.path.basename(target)))

    # mark executable
    os.chmod(launcher_file, os.stat(launcher_file).st_mode | stat.S_IEXEC)

    logging.info("Wrote launcher to '{}'".format(launcher_file))

def list_startmenu(prefix):
    smpath = pathlib.Path(os.path.join(get_prefix_dir(prefix),
        "drive_c/ProgramData/Microsoft/Windows/Start Menu/Programs/"))
    for path in smpath.glob("**/*.lnk"):
        print(path.name)

def run_start(prefix, winearch, name):

    lnkpath = resolve_startmenu_entry(prefix, name)
    invoke_wine(prefix, winearch, ["start", "/unix", str(lnkpath)])

def generate_links(prefix, target):


@@ 294,7 346,7 @@ def main():

    parser.add_argument("--name", "-n", default=None,
            help="Specify launcher name. Ignored for any action "+
            "except for --launcher.")
            "except for --launcher and --startlauncher.")

    action = parser.add_mutually_exclusive_group(required=True)



@@ 317,6 369,10 @@ def main():
    action.add_argument("--launcher", "-l", default=None,
            help="Generate a launcher script for the specified file.")

    action.add_argument("--startlauncher", "-A", default=None,
            help="Generate a launcher script for the specified start " +
            "menu entry")

    action.add_argument("--lstart", "-s", default=False, action="store_true",
            help="List start menu entries.")



@@ 363,6 419,13 @@ def main():
                    target = args.launcher,
                    name = args.name)

        elif args.startlauncher is not None:
            generate_start_launcher(
                    prefix = args.prefix,
                    winearch = args.winearch,
                    target = args.startlauncher,
                    name = args.name)

        elif args.lstart:
            list_startmenu(args.prefix);