Skip to content

Instantly share code, notes, and snippets.

@m1st0
Last active September 12, 2023 16:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m1st0/a7fb9240914ec5b37a21818bbb7b0ed8 to your computer and use it in GitHub Desktop.
Save m1st0/a7fb9240914ec5b37a21818bbb7b0ed8 to your computer and use it in GitHub Desktop.
Automatic FlatPak CLI Aliases
#!/usr/bin/python3
"""
flatpak_directory_alias.py
================
Description: Enable ability to run flatpak apps from BASH aliases using flatpak directory names.
Copyright (C) 2023 Maulik Mistry <mistry01@gmail.com>
https://github.com/m1st0
Please share support: https://www.paypal.com/paypalme/m1st0
# This program is licensed under the CC BY-SA 4.0 license.
# https://creativecommons.org/licenses/by-sa/4.0/
Special thanks to slackermedia for inspiration to make this a more automated process.
https://gitlab.com/slackermedia/pakrat
"""
import os
import subprocess
import argparse
from colorama import Fore
flatpak_aliases = os.path.expanduser('~/.flatpak_aliases')
# Create the aliases using command line output.
def create_alias_cli(application_id):
app_parts = application_id.split(".")
app_name = app_parts[-1].lower()
# Handle the case when the application ID ends with "desktop" .
if app_name == "desktop":
# Check if there's another part before "desktop" .
if len(app_parts) > 2:
# Extract the relevant part from the middle of the application ID.
app_name = app_parts[-2].lower()
alias_command = f"alias {app_name}='flatpak run {application_id}'"
return alias_command
# Create the aliases using directory names.
def create_alias_dir(directory_name):
app_name = os.path.basename(directory_name)
alias_parts = app_name.lower().split(".")
# Handle the case when the directory name ends with "desktop" such as for telegram.
if alias_parts[-1] == "desktop":
alias_name = alias_parts[-2]
else:
alias_name = alias_parts[-1]
alias_command = f"alias {alias_name}='flatpak run {app_name}'"
return alias_command
# Use a BASH "source" command to execute the file in the current shell.
def update_aliases():
# Note if successful.
print(Fore.BLUE + "Alias file ~/.flatpak_aliases created with the following content:\n")
with open(flatpak_aliases, "r") as alias_file:
print(Fore.WHITE + alias_file.read())
print(Fore.BLUE + "You can source the file as " + Fore.WHITE + ". ~/.flatpak_aliases" + Fore.BLUE + " or in your shell initialization file.")
print("If errors occur, you can fix this script or try the other \"cli\" or \"dir\" based writer instead.")
# Assuming success for this current session.
subprocess.run(f'. {flatpak_aliases}', shell=True, check=True, capture_output=False)
# Obtain app names by directory listing.
def use_directories():
flatpak_apps_path = "/var/lib/flatpak/app/"
# Use list comprehension to obtain the needed directories.
directories = [d for d in os.listdir(flatpak_apps_path) if os.path.isdir(os.path.join(flatpak_apps_path, d))]
with open(flatpak_aliases, "w+") as file:
for directory_item in directories:
alias_command = create_alias_dir(directory_item)
file.write(alias_command + "\n")
def use_cli():
# Run the "flatpak list --app" command and capture stdout and stderr.
result = subprocess.run(["flatpak", "list", "--app", "--columns=application"], check=True, capture_output=True, text=True)
# Split the output into lines.
lines = result.stdout.strip().split("\n")
with open(flatpak_aliases, "w+") as file:
for line in lines:
alias_command = create_alias_cli(line)
file.write(alias_command + "\n")
def main():
parser = argparse.ArgumentParser(description='Create cli aliases for flatpaks.')
parser.add_argument("term", choices=['cli', 'dir'],
help='Use cli or directory listing for alias creation.')
term = parser.parse_args().term
match term:
case "cli":
use_cli()
case "dir":
use_directories()
update_aliases()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment