scripts/create_windows_links.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7192
a22eee00b052
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2018 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
5 #
6 # This is the install script for eric6.
7
8 """
9 Installation script for the eric6 IDE and all eric6 related tools.
10 """
11
12 from __future__ import unicode_literals, print_function
13
14 import os
15 import sys
16
17 from eric6config import getConfig
18
19 # Define file name markers for Python variants
20 PythonMarkers = {
21 2: "_py2",
22 3: "_py3",
23 }
24
25 includePythonVariant = False
26
27
28 def main(argv):
29 """
30 Create Desktop and Start Menu links.
31
32 @param argv list of command line arguments
33 @type list of str
34 """
35 global includePythonVariant
36
37 if "-y" in argv:
38 includePythonVariant = True
39
40 regPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer" + \
41 "\\User Shell Folders"
42
43 # 1. create desktop shortcuts
44 regName = "Desktop"
45 desktopFolder = os.path.normpath(
46 os.path.expandvars(getWinregEntry(regName, regPath)))
47 for linkName, targetPath, iconPath in windowsDesktopEntries():
48 linkPath = os.path.join(desktopFolder, linkName)
49 createWindowsShortcut(linkPath, targetPath, iconPath)
50
51 # 2. create start menu entry and shortcuts
52 regName = "Programs"
53 programsEntry = getWinregEntry(regName, regPath)
54 if programsEntry:
55 programsFolder = os.path.normpath(os.path.expandvars(programsEntry))
56 eric6EntryPath = os.path.join(programsFolder, windowsProgramsEntry())
57 if not os.path.exists(eric6EntryPath):
58 try:
59 os.makedirs(eric6EntryPath)
60 except EnvironmentError:
61 # maybe restrictions prohibited link creation
62 return
63
64 for linkName, targetPath, iconPath in windowsDesktopEntries():
65 linkPath = os.path.join(eric6EntryPath, linkName)
66 createWindowsShortcut(linkPath, targetPath, iconPath)
67
68
69 def getWinregEntry(name, path):
70 """
71 Function to get an entry from the Windows Registry.
72
73 @param name variable name
74 @type str
75 @param path registry path of the variable
76 @type str
77 @return value of requested registry variable
78 @rtype any
79 """
80 try:
81 import _winreg as winreg
82 except ImportError:
83 try:
84 import winreg
85 except ImportError:
86 return None
87
88 try:
89 registryKey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, path, 0,
90 winreg.KEY_READ)
91 value, _ = winreg.QueryValueEx(registryKey, name)
92 winreg.CloseKey(registryKey)
93 return value
94 except WindowsError:
95 return None
96
97
98 def createWindowsShortcut(linkPath, targetPath, iconPath):
99 """
100 Create Windows shortcut.
101
102 @param linkPath path of the shortcut file
103 @type str
104 @param targetPath path the shortcut shall point to
105 @type str
106 @param iconPath path of the icon file
107 @type str
108 """
109 from win32com.client import Dispatch
110 from pywintypes import com_error
111
112 try:
113 shell = Dispatch('WScript.Shell')
114 shortcut = shell.CreateShortCut(linkPath)
115 shortcut.Targetpath = targetPath
116 shortcut.WorkingDirectory = os.path.dirname(targetPath)
117 shortcut.IconLocation = iconPath
118 shortcut.save()
119 except com_error:
120 # maybe restrictions prohibited link creation
121 pass
122
123
124 def windowsDesktopNames():
125 """
126 Function to generate the link names for the Windows Desktop.
127
128 @return list of desktop link names
129 @rtype list of str
130 """
131 return [e[0] for e in windowsDesktopEntries()]
132
133
134 def windowsDesktopEntries():
135 """
136 Function to generate data for the Windows Desktop links.
137
138 @return list of tuples containing the desktop link name,
139 the link target and the icon target
140 @rtype list of tuples of (str, str, str)
141 """
142 global includePythonVariant
143
144 if includePythonVariant:
145 marker = PythonMarkers[sys.version_info.major]
146 else:
147 marker = ""
148
149 majorVersion, minorVersion = sys.version_info[:2]
150 entriesTemplates = [
151 ("eric6 (Python {0}.{1}).lnk",
152 os.path.join(getConfig("bindir"), "eric6" + marker + ".cmd"),
153 os.path.join(getConfig("ericPixDir"), "eric6.ico")),
154 ]
155 if sys.version_info.major == 2:
156 entriesTemplates.append((
157 "eric6 Browser (Python {0}.{1}).lnk",
158 os.path.join(getConfig("bindir"),
159 "eric6_webbrowser" + marker + ".cmd"),
160 os.path.join(getConfig("ericPixDir"), "ericWeb48.ico")
161 ))
162 else:
163 entriesTemplates.append((
164 "eric6 Browser (Python {0}.{1}).lnk",
165 os.path.join(getConfig("bindir"),
166 "eric6_browser" + marker + ".cmd"),
167 os.path.join(getConfig("ericPixDir"), "ericWeb48.ico")
168 ))
169
170 return [
171 (e[0].format(majorVersion, minorVersion), e[1], e[2])
172 for e in entriesTemplates
173 ]
174
175
176 def windowsProgramsEntry():
177 """
178 Function to generate the name of the Start Menu top entry.
179
180 @return name of the Start Menu top entry
181 @rtype str
182 """
183 majorVersion, minorVersion = sys.version_info[:2]
184 return "eric6 (Python {0}.{1})".format(majorVersion, minorVersion)
185
186
187 if __name__ == "__main__":
188 try:
189 main(sys.argv)
190 except SystemExit:
191 raise
192 except Exception:
193 print("""An internal error occured. Please report all the output"""
194 """ of the program,\nincluding the following traceback, to"""
195 """ eric-bugs@eric-ide.python-projects.org.\n""")
196 raise
197
198 #
199 # eflag: noqa = M801

eric ide

mercurial