forked from frescobaldi/frescobaldi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrescobaldi-wininst.py
122 lines (97 loc) · 3.41 KB
/
frescobaldi-wininst.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
#! python
"""
PostInstall/PostUninstall script for Frescobaldi on Windows
This install script is used by setup.py bdist_wininst
It is called with the '-install' argument after installing, and with the
'-remove' argument _after_ uninstalling.
"""
import os
import sys
# Windows register:
try:
import winreg
except ImportError:
try:
import _winreg as winreg
except ImportError:
winreg = None
# Frescobaldi meta info (Possibly not accessible anymore on uninstall):
try:
from frescobaldi_app import info
except ImportError:
info = None
# Icon:
try:
import frescobaldi_app
icon = os.path.join(frescobaldi_app.__path__[0], 'icons', 'frescobaldi.ico')
except ImportError:
icon = None
# Main application script and Python executable:
script = os.path.join(sys.prefix, 'Scripts', 'frescobaldi')
python = os.path.join(sys.exec_prefix, 'pythonw.exe') # because sys.executable points to installer
def install_association():
"""Installs a file association for the LilyPond file type.
Sets HKEY_CLASSES_ROOT\\LilyPond\\shell\\frescobaldi to 'Open with &Frescobaldi...'
Sets HKEY_CLASSES_ROOT\\LilyPond\\shell\\frescobaldi\\command
to '{python} "{script}" "%1"'
"""
key = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, "LilyPond\\shell\\frescobaldi")
with key:
winreg.SetValue(key, None, winreg.REG_SZ, "Open with &Frescobaldi...")
winreg.SetValue(key, "command", winreg.REG_SZ, '{0} "{1}" "%1"'.format(python, script))
print("* Created file association")
def remove_association():
"""Removes the file association.
Removes HKEY_CLASSES_ROOT\\LilyPond\\shell\\frescobaldi
"""
try:
key = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, "LilyPond\\shell")
with key:
winreg.DeleteKey(key, "frescobaldi\\command")
winreg.DeleteKey(key, "frescobaldi")
print("* Removed file association")
except WindowsError:
print("*** Could not remove file association ***")
def install_startmenu():
"""Installs a shortcut in the startmenu."""
for startmenu in (
get_special_folder_path('CSIDL_COMMON_STARTMENU'),
get_special_folder_path('CSIDL_STARTMENU'),
):
menudir = os.path.join(startmenu, info.appname)
if os.path.isdir(menudir):
shortcut(menudir)
break
else:
try:
os.mkdir(menudir)
except OSError:
continue
else:
directory_created(menudir)
shortcut(menudir)
break
def shortcut(directory):
"""Makes the Frescobaldi shortcut in the specified directory."""
lnk = os.path.join(directory, info.appname + ".lnk")
create_shortcut(
python, # path
info.description, # description
lnk, # link name
script, # arguments
"", # workdir
icon, # icon
0, # icon index
)
file_created(lnk)
print("* Added \"{0}\" to Start Menu".format(info.appname))
def welcome():
"""Prints a nice welcome message in the installer."""
print("\nWelcome to {0} {1}!".format(info.appname, info.version))
### Main:
if sys.argv[1] == '-install':
install_startmenu()
install_association()
welcome()
elif sys.argv[1] == '-remove':
remove_association()