|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +'''Simple installer for the graphical user interface of pix2tex''' |
| 4 | + |
| 5 | +import argparse |
| 6 | +import os |
| 7 | +import sys |
| 8 | + |
| 9 | + |
| 10 | +def _check_file( |
| 11 | + main_file |
| 12 | +): |
| 13 | + if os.path.exists(main_file): |
| 14 | + return |
| 15 | + raise FileNotFoundError( |
| 16 | + f'Unable to find file {main_file}' |
| 17 | + ) |
| 18 | + |
| 19 | + |
| 20 | +def _make_desktop_file( |
| 21 | + desktop_path, |
| 22 | + desktop_entry |
| 23 | +): |
| 24 | + with open(desktop_path, 'w') as desktop_file: |
| 25 | + desktop_file.write(desktop_entry) |
| 26 | + |
| 27 | + |
| 28 | +def setup_desktop( |
| 29 | + gui_file = 'gui.py', |
| 30 | + icon_file = 'resources/icon.svg', |
| 31 | +): |
| 32 | + '''Main function for setting up .desktop files (on Linux)''' |
| 33 | + parser = argparse.ArgumentParser( |
| 34 | + description='Simple installer for the pix2tex GUI' |
| 35 | + ) |
| 36 | + |
| 37 | + parser.add_argument( |
| 38 | + 'pix2tex_dir', |
| 39 | + default='.', |
| 40 | + nargs='?', |
| 41 | + help='The directory where pix2tex was downloaded' |
| 42 | + ) |
| 43 | + |
| 44 | + parser.add_argument( |
| 45 | + '--uninstall', '-u', |
| 46 | + action='store_true', |
| 47 | + help='Uninstalls the desktop entry' |
| 48 | + ) |
| 49 | + |
| 50 | + parser.add_argument( |
| 51 | + '--venv_dir', '-e', |
| 52 | + help='In case a virtual environment is needed for running pix2tex, specifies its directory' |
| 53 | + ) |
| 54 | + |
| 55 | + parser.add_argument( |
| 56 | + '--overwrite', '-o', |
| 57 | + action='store_true', |
| 58 | + help='Unconditionally overwrite .desktop file (if it exists)' |
| 59 | + ) |
| 60 | + |
| 61 | + args = parser.parse_args() |
| 62 | + |
| 63 | + # where the desktop file will be created |
| 64 | + desktop_dir = os.environ.get( |
| 65 | + 'XDG_DATA_HOME', |
| 66 | + os.path.join(os.environ.get('HOME'), '.local/share/applications') |
| 67 | + ) |
| 68 | + desktop_path = os.path.abspath(os.path.join(desktop_dir, 'pix2tex.desktop')) |
| 69 | + |
| 70 | + # check if we want to uninstall it instead |
| 71 | + if args.uninstall: |
| 72 | + if os.path.exists(desktop_path): |
| 73 | + remove = input( |
| 74 | + f'Are you sure you want to remove the pix2tex desktop entry {desktop_path}? [y/n]' |
| 75 | + ) |
| 76 | + if remove.lower() == 'y': |
| 77 | + try: |
| 78 | + os.remove(desktop_path) |
| 79 | + print('Successfully uninstalled the desktop entry') |
| 80 | + return 0 |
| 81 | + except: |
| 82 | + raise OSError( |
| 83 | + f'Something went wrong, unable to remove the desktop entry {desktop_path}' |
| 84 | + ) |
| 85 | + elif remove.lower() == 'n': |
| 86 | + print( |
| 87 | + 'Not removing the desktop entry;' \ |
| 88 | + 'if you wish to install/uninstall pix2tex, please run this script again' |
| 89 | + ) |
| 90 | + return 0 |
| 91 | + else: |
| 92 | + print('No file to remove') |
| 93 | + return 0 |
| 94 | + |
| 95 | + _check_file(os.path.join(args.pix2tex_dir, gui_file)) |
| 96 | + _check_file(os.path.join(args.pix2tex_dir, icon_file)) |
| 97 | + |
| 98 | + pix2tex_dir = os.path.abspath(args.pix2tex_dir) |
| 99 | + gui_path = os.path.join(pix2tex_dir, gui_file) |
| 100 | + icon_path = os.path.join(pix2tex_dir, icon_file) |
| 101 | + |
| 102 | + interpreter_path = \ |
| 103 | + os.path.join(args.venv_dir, 'bin/python3') \ |
| 104 | + if (args.venv_dir and os.path.exists(os.path.join(args.venv_dir, 'bin/python3'))) \ |
| 105 | + else sys.executable |
| 106 | + interpreter_path = os.path.abspath(interpreter_path) |
| 107 | + |
| 108 | + desktop_entry = f"""[Desktop Entry] |
| 109 | +Version=1.0 |
| 110 | +Name=pix2tex |
| 111 | +Comment=LaTeX math recognition using machine learning |
| 112 | +Exec={interpreter_path} {gui_path} |
| 113 | +Icon={icon_path} |
| 114 | +Terminal=false |
| 115 | +Type=Application |
| 116 | +Categories=Utility; |
| 117 | +""" |
| 118 | + |
| 119 | + if os.path.exists(desktop_path): |
| 120 | + if not args.overwrite: |
| 121 | + overwrite = input( |
| 122 | + f'Desktop entry {desktop_path} exists, do you wish to overwrite it? [y/n]' |
| 123 | + ) |
| 124 | + if overwrite.lower() == 'y': |
| 125 | + _make_desktop_file(desktop_path, desktop_entry) |
| 126 | + elif overwrite.lower() == 'n': |
| 127 | + print('Not overwriting existing desktop entry, exiting...', file=sys.stderr) |
| 128 | + return 1 |
| 129 | + else: |
| 130 | + print('Unable to understand input, exiting...', file=sys.stderr) |
| 131 | + return 255 |
| 132 | + else: |
| 133 | + _make_desktop_file(desktop_path, desktop_entry) |
| 134 | + else: |
| 135 | + _make_desktop_file(desktop_path, desktop_entry) |
| 136 | + |
| 137 | + return 0 |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == '__main__': |
| 141 | + setup_desktop() |
0 commit comments