src/eric7/eric7_post_install.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9346
eda0bdb22e67
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
22 def createWindowsLinks(): 22 def createWindowsLinks():
23 """ 23 """
24 Create Desktop and Start Menu links. 24 Create Desktop and Start Menu links.
25 """ 25 """
26 regPath = ( 26 regPath = (
27 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer" + 27 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"
28 "\\User Shell Folders" 28 + "\\User Shell Folders"
29 ) 29 )
30 30
31 # 1. create desktop shortcuts 31 # 1. create desktop shortcuts
32 regName = "Desktop" 32 regName = "Desktop"
33 desktopFolder = os.path.normpath( 33 desktopFolder = os.path.normpath(
34 os.path.expandvars(getWinregEntry(regName, regPath))) 34 os.path.expandvars(getWinregEntry(regName, regPath))
35 )
35 for linkName, targetPath, iconPath in windowsDesktopEntries(): 36 for linkName, targetPath, iconPath in windowsDesktopEntries():
36 linkPath = os.path.join(desktopFolder, linkName) 37 linkPath = os.path.join(desktopFolder, linkName)
37 createWindowsShortcut(linkPath, targetPath, iconPath) 38 createWindowsShortcut(linkPath, targetPath, iconPath)
38 39
39 # 2. create start menu entry and shortcuts 40 # 2. create start menu entry and shortcuts
40 regName = "Programs" 41 regName = "Programs"
41 programsEntry = getWinregEntry(regName, regPath) 42 programsEntry = getWinregEntry(regName, regPath)
42 if programsEntry: 43 if programsEntry:
43 programsFolder = os.path.normpath(os.path.expandvars(programsEntry)) 44 programsFolder = os.path.normpath(os.path.expandvars(programsEntry))
46 try: 47 try:
47 os.makedirs(eric7EntryPath) 48 os.makedirs(eric7EntryPath)
48 except OSError: 49 except OSError:
49 # maybe restrictions prohibited link creation 50 # maybe restrictions prohibited link creation
50 return 51 return
51 52
52 for linkName, targetPath, iconPath in windowsDesktopEntries(): 53 for linkName, targetPath, iconPath in windowsDesktopEntries():
53 linkPath = os.path.join(eric7EntryPath, linkName) 54 linkPath = os.path.join(eric7EntryPath, linkName)
54 createWindowsShortcut(linkPath, targetPath, iconPath) 55 createWindowsShortcut(linkPath, targetPath, iconPath)
55 56
56 57
57 def getWinregEntry(name, path): 58 def getWinregEntry(name, path):
58 """ 59 """
59 Function to get an entry from the Windows Registry. 60 Function to get an entry from the Windows Registry.
60 61
61 @param name variable name 62 @param name variable name
62 @type str 63 @type str
63 @param path registry path of the variable 64 @param path registry path of the variable
64 @type str 65 @type str
65 @return value of requested registry variable 66 @return value of requested registry variable
67 """ 68 """
68 try: 69 try:
69 import winreg 70 import winreg
70 except ImportError: 71 except ImportError:
71 return None 72 return None
72 73
73 try: 74 try:
74 registryKey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, path, 0, 75 registryKey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, path, 0, winreg.KEY_READ)
75 winreg.KEY_READ)
76 value, _ = winreg.QueryValueEx(registryKey, name) 76 value, _ = winreg.QueryValueEx(registryKey, name)
77 winreg.CloseKey(registryKey) 77 winreg.CloseKey(registryKey)
78 return value 78 return value
79 except WindowsError: 79 except WindowsError:
80 return None 80 return None
81 81
82 82
83 def windowsDesktopEntries(): 83 def windowsDesktopEntries():
84 """ 84 """
85 Function to generate data for the Windows Desktop links. 85 Function to generate data for the Windows Desktop links.
86 86
87 @return list of tuples containing the desktop link name, 87 @return list of tuples containing the desktop link name,
88 the link target and the icon target 88 the link target and the icon target
89 @rtype list of tuples of (str, str, str) 89 @rtype list of tuples of (str, str, str)
90 """ 90 """
91 import eric7 91 import eric7
92 92
93 majorVersion, minorVersion = sys.version_info[:2] 93 majorVersion, minorVersion = sys.version_info[:2]
94 scriptsDir = sysconfig.get_path("scripts") 94 scriptsDir = sysconfig.get_path("scripts")
95 iconsDir = os.path.join(os.path.dirname(eric7.__file__), "pixmaps") 95 iconsDir = os.path.join(os.path.dirname(eric7.__file__), "pixmaps")
96 entriesTemplates = [ 96 entriesTemplates = [
97 ("eric7 (Python {0}.{1}).lnk", 97 (
98 os.path.join(scriptsDir, "eric7.exe"), 98 "eric7 (Python {0}.{1}).lnk",
99 os.path.join(iconsDir, "eric7.ico") 99 os.path.join(scriptsDir, "eric7.exe"),
100 ), 100 os.path.join(iconsDir, "eric7.ico"),
101 ("eric7 Browser (Python {0}.{1}).lnk", 101 ),
102 os.path.join(scriptsDir, "eric7_browser.exe"), 102 (
103 os.path.join(iconsDir, "ericWeb48.ico") 103 "eric7 Browser (Python {0}.{1}).lnk",
104 ), 104 os.path.join(scriptsDir, "eric7_browser.exe"),
105 os.path.join(iconsDir, "ericWeb48.ico"),
106 ),
105 ] 107 ]
106 108
107 return [ 109 return [
108 (e[0].format(majorVersion, minorVersion), e[1], e[2]) 110 (e[0].format(majorVersion, minorVersion), e[1], e[2]) for e in entriesTemplates
109 for e in entriesTemplates
110 ] 111 ]
111 112
112 113
113 def createWindowsShortcut(linkPath, targetPath, iconPath): 114 def createWindowsShortcut(linkPath, targetPath, iconPath):
114 """ 115 """
115 Create Windows shortcut. 116 Create Windows shortcut.
116 117
117 @param linkPath path of the shortcut file 118 @param linkPath path of the shortcut file
118 @type str 119 @type str
119 @param targetPath path the shortcut shall point to 120 @param targetPath path the shortcut shall point to
120 @type str 121 @type str
121 @param iconPath path of the icon file 122 @param iconPath path of the icon file
122 @type str 123 @type str
123 """ 124 """
124 from win32com.client import Dispatch 125 from win32com.client import Dispatch
125 from pywintypes import com_error 126 from pywintypes import com_error
126 127
127 with contextlib.suppress(com_error): 128 with contextlib.suppress(com_error):
128 shell = Dispatch('WScript.Shell') 129 shell = Dispatch("WScript.Shell")
129 shortcut = shell.CreateShortCut(linkPath) 130 shortcut = shell.CreateShortCut(linkPath)
130 shortcut.Targetpath = targetPath 131 shortcut.Targetpath = targetPath
131 shortcut.WorkingDirectory = os.path.dirname(targetPath) 132 shortcut.WorkingDirectory = os.path.dirname(targetPath)
132 shortcut.IconLocation = iconPath 133 shortcut.IconLocation = iconPath
133 shortcut.save() 134 shortcut.save()
134 135
135 136
136 def windowsProgramsEntry(): 137 def windowsProgramsEntry():
137 """ 138 """
138 Function to generate the name of the Start Menu top entry. 139 Function to generate the name of the Start Menu top entry.
139 140
140 @return name of the Start Menu top entry 141 @return name of the Start Menu top entry
141 @rtype str 142 @rtype str
142 """ 143 """
143 majorVersion, minorVersion = sys.version_info[:2] 144 majorVersion, minorVersion = sys.version_info[:2]
144 return "eric7 (Python {0}.{1})".format(majorVersion, minorVersion) 145 return "eric7 (Python {0}.{1})".format(majorVersion, minorVersion)
145 146
147
146 ###################################################################### 148 ######################################################################
147 ## Post installation hooks for Linux below 149 ## Post installation hooks for Linux below
148 ###################################################################### 150 ######################################################################
149 151
150 152
151 def copyLinuxMetaData(): 153 def copyLinuxMetaData():
152 """ 154 """
153 Function to copy the meta data files. 155 Function to copy the meta data files.
154 """ 156 """
155 import eric7 157 import eric7
156 158
157 scriptsDir = sysconfig.get_path("scripts") 159 scriptsDir = sysconfig.get_path("scripts")
158 dstDir = os.path.join(os.path.expanduser("~"), ".local", "share") 160 dstDir = os.path.join(os.path.expanduser("~"), ".local", "share")
159 iconsDir = os.path.join(os.path.dirname(eric7.__file__), "pixmaps") 161 iconsDir = os.path.join(os.path.dirname(eric7.__file__), "pixmaps")
160 svgIconsDir = os.path.join(os.path.dirname(eric7.__file__), 162 svgIconsDir = os.path.join(os.path.dirname(eric7.__file__), "icons", "breeze-dark")
161 "icons", "breeze-dark")
162 linuxDir = os.path.join(os.path.dirname(eric7.__file__), "data", "linux") 163 linuxDir = os.path.join(os.path.dirname(eric7.__file__), "data", "linux")
163 164
164 for metaDir in ["appdata", "metainfo"]: 165 for metaDir in ["appdata", "metainfo"]:
165 copyMetaFile(os.path.join(linuxDir, "eric7.appdata.xml"), 166 copyMetaFile(
166 os.path.join(dstDir, metaDir), 167 os.path.join(linuxDir, "eric7.appdata.xml"),
167 "eric7.appdata.xml") 168 os.path.join(dstDir, metaDir),
168 169 "eric7.appdata.xml",
170 )
171
169 for svgIcon in ("eric.svg", "ericWeb48.svg"): 172 for svgIcon in ("eric.svg", "ericWeb48.svg"):
170 copyMetaFile(os.path.join(svgIconsDir, svgIcon), 173 copyMetaFile(
171 os.path.join(dstDir, "icons"), 174 os.path.join(svgIconsDir, svgIcon), os.path.join(dstDir, "icons"), svgIcon
172 svgIcon) 175 )
173 for icon in ("eric48_icon.png", "ericWeb48_icon.png"): 176 for icon in ("eric48_icon.png", "ericWeb48_icon.png"):
174 copyMetaFile(os.path.join(iconsDir, icon), 177 copyMetaFile(os.path.join(iconsDir, icon), os.path.join(dstDir, "icons"), icon)
175 os.path.join(dstDir, "icons"), 178 copyMetaFile(
176 icon) 179 os.path.join(iconsDir, "eric48_icon.png"),
177 copyMetaFile(os.path.join(iconsDir, "eric48_icon.png"), 180 os.path.join(dstDir, "icons", "hicolor", "48x48", "apps"),
178 os.path.join(dstDir, "icons", "hicolor", "48x48", "apps"), 181 "eric.png",
179 "eric.png") 182 )
180 copyMetaFile(os.path.join(iconsDir, "ericWeb48_icon.png"), 183 copyMetaFile(
181 os.path.join(dstDir, "icons", "hicolor", "48x48", "apps"), 184 os.path.join(iconsDir, "ericWeb48_icon.png"),
182 "ericWeb.png") 185 os.path.join(dstDir, "icons", "hicolor", "48x48", "apps"),
183 186 "ericWeb.png",
187 )
188
184 for desktop in ["eric7.desktop", "eric7_browser.desktop"]: 189 for desktop in ["eric7.desktop", "eric7_browser.desktop"]:
185 copyDesktopFile( 190 copyDesktopFile(
186 os.path.join(linuxDir, desktop), 191 os.path.join(linuxDir, desktop),
187 os.path.join(dstDir, "applications", desktop), 192 os.path.join(dstDir, "applications", desktop),
188 scriptsDir 193 scriptsDir,
189 ) 194 )
190 195
191 196
192 def copyMetaFile(srcname, dstpath, dstname): 197 def copyMetaFile(srcname, dstpath, dstname):
193 """ 198 """
194 Function to copy a file to its destination. 199 Function to copy a file to its destination.
195 200
196 @param srcname name of the source file 201 @param srcname name of the source file
197 @type str 202 @type str
198 @param dstpath name of the destination path 203 @param dstpath name of the destination path
199 @type str 204 @type str
200 @param dstname name of the destination file (without path) 205 @param dstname name of the destination file (without path)
208 213
209 214
210 def copyDesktopFile(src, dst, scriptsdir): 215 def copyDesktopFile(src, dst, scriptsdir):
211 """ 216 """
212 Modify a desktop file and write it to its destination. 217 Modify a desktop file and write it to its destination.
213 218
214 @param src source file name (string) 219 @param src source file name (string)
215 @param dst destination file name (string) 220 @param dst destination file name (string)
216 @param scriptsdir directory containing the scripts (string) 221 @param scriptsdir directory containing the scripts (string)
217 """ 222 """
218 with open(src, "r", encoding="utf-8") as f: 223 with open(src, "r", encoding="utf-8") as f:
219 text = f.read() 224 text = f.read()
220 225
221 text = text.replace("@BINDIR@", scriptsdir) 226 text = text.replace("@BINDIR@", scriptsdir)
222 227
223 with open(dst, "w", encoding="utf-8") as f: 228 with open(dst, "w", encoding="utf-8") as f:
224 f.write(text) 229 f.write(text)
225 os.chmod(dst, 0o644) 230 os.chmod(dst, 0o644)
231
226 232
227 ###################################################################### 233 ######################################################################
228 ## Main script below 234 ## Main script below
229 ###################################################################### 235 ######################################################################
230 236
235 """ 241 """
236 if sys.platform.startswith(("win", "cygwin")): 242 if sys.platform.startswith(("win", "cygwin")):
237 createWindowsLinks() 243 createWindowsLinks()
238 elif sys.platform.startswith("linux"): 244 elif sys.platform.startswith("linux"):
239 copyLinuxMetaData() 245 copyLinuxMetaData()
240 246
241 sys.exit(0) 247 sys.exit(0)
242 248
243 249
244 if __name__ == "__main__": 250 if __name__ == "__main__":
245 main() 251 main()

eric ide

mercurial