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