17 |
17 |
18 |
18 |
19 def main(argv): |
19 def main(argv): |
20 """ |
20 """ |
21 Create Desktop and Start Menu links. |
21 Create Desktop and Start Menu links. |
22 |
22 |
23 @param argv list of command line arguments |
23 @param argv list of command line arguments |
24 @type list of str |
24 @type list of str |
25 """ |
25 """ |
26 if sys.platform.startswith(("win", "cygwin")): |
26 if sys.platform.startswith(("win", "cygwin")): |
27 regPath = ( |
27 regPath = ( |
28 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer" + |
28 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer" |
29 "\\User Shell Folders" |
29 + "\\User Shell Folders" |
30 ) |
30 ) |
31 |
31 |
32 # 1. create desktop shortcuts |
32 # 1. create desktop shortcuts |
33 regName = "Desktop" |
33 regName = "Desktop" |
34 desktopFolder = os.path.normpath( |
34 desktopFolder = os.path.normpath( |
35 os.path.expandvars(getWinregEntry(regName, regPath))) |
35 os.path.expandvars(getWinregEntry(regName, regPath)) |
|
36 ) |
36 for linkName, targetPath, iconPath in windowsDesktopEntries(): |
37 for linkName, targetPath, iconPath in windowsDesktopEntries(): |
37 linkPath = os.path.join(desktopFolder, linkName) |
38 linkPath = os.path.join(desktopFolder, linkName) |
38 createWindowsShortcut(linkPath, targetPath, iconPath) |
39 createWindowsShortcut(linkPath, targetPath, iconPath) |
39 |
40 |
40 # 2. create start menu entry and shortcuts |
41 # 2. create start menu entry and shortcuts |
41 regName = "Programs" |
42 regName = "Programs" |
42 programsEntry = getWinregEntry(regName, regPath) |
43 programsEntry = getWinregEntry(regName, regPath) |
43 if programsEntry: |
44 if programsEntry: |
44 programsFolder = os.path.normpath(os.path.expandvars(programsEntry)) |
45 programsFolder = os.path.normpath(os.path.expandvars(programsEntry)) |
47 try: |
48 try: |
48 os.makedirs(eric7EntryPath) |
49 os.makedirs(eric7EntryPath) |
49 except OSError: |
50 except OSError: |
50 # maybe restrictions prohibited link creation |
51 # maybe restrictions prohibited link creation |
51 return |
52 return |
52 |
53 |
53 for linkName, targetPath, iconPath in windowsDesktopEntries(): |
54 for linkName, targetPath, iconPath in windowsDesktopEntries(): |
54 linkPath = os.path.join(eric7EntryPath, linkName) |
55 linkPath = os.path.join(eric7EntryPath, linkName) |
55 createWindowsShortcut(linkPath, targetPath, iconPath) |
56 createWindowsShortcut(linkPath, targetPath, iconPath) |
56 else: |
57 else: |
57 print("This script is to be used on Windows platforms only. Aborting...") |
58 print("This script is to be used on Windows platforms only. Aborting...") |
58 |
59 |
59 |
60 |
60 def getWinregEntry(name, path): |
61 def getWinregEntry(name, path): |
61 """ |
62 """ |
62 Function to get an entry from the Windows Registry. |
63 Function to get an entry from the Windows Registry. |
63 |
64 |
64 @param name variable name |
65 @param name variable name |
65 @type str |
66 @type str |
66 @param path registry path of the variable |
67 @param path registry path of the variable |
67 @type str |
68 @type str |
68 @return value of requested registry variable |
69 @return value of requested registry variable |
70 """ |
71 """ |
71 try: |
72 try: |
72 import winreg |
73 import winreg |
73 except ImportError: |
74 except ImportError: |
74 return None |
75 return None |
75 |
76 |
76 try: |
77 try: |
77 registryKey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, path, 0, |
78 registryKey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, path, 0, winreg.KEY_READ) |
78 winreg.KEY_READ) |
|
79 value, _ = winreg.QueryValueEx(registryKey, name) |
79 value, _ = winreg.QueryValueEx(registryKey, name) |
80 winreg.CloseKey(registryKey) |
80 winreg.CloseKey(registryKey) |
81 return value |
81 return value |
82 except WindowsError: |
82 except WindowsError: |
83 return None |
83 return None |
84 |
84 |
85 |
85 |
86 def createWindowsShortcut(linkPath, targetPath, iconPath): |
86 def createWindowsShortcut(linkPath, targetPath, iconPath): |
87 """ |
87 """ |
88 Create Windows shortcut. |
88 Create Windows shortcut. |
89 |
89 |
90 @param linkPath path of the shortcut file |
90 @param linkPath path of the shortcut file |
91 @type str |
91 @type str |
92 @param targetPath path the shortcut shall point to |
92 @param targetPath path the shortcut shall point to |
93 @type str |
93 @type str |
94 @param iconPath path of the icon file |
94 @param iconPath path of the icon file |
95 @type str |
95 @type str |
96 """ |
96 """ |
97 from win32com.client import Dispatch |
97 from win32com.client import Dispatch |
98 from pywintypes import com_error |
98 from pywintypes import com_error |
99 |
99 |
100 with contextlib.suppress(com_error): |
100 with contextlib.suppress(com_error): |
101 shell = Dispatch('WScript.Shell') |
101 shell = Dispatch("WScript.Shell") |
102 shortcut = shell.CreateShortCut(linkPath) |
102 shortcut = shell.CreateShortCut(linkPath) |
103 shortcut.Targetpath = targetPath |
103 shortcut.Targetpath = targetPath |
104 shortcut.WorkingDirectory = os.path.dirname(targetPath) |
104 shortcut.WorkingDirectory = os.path.dirname(targetPath) |
105 shortcut.IconLocation = iconPath |
105 shortcut.IconLocation = iconPath |
106 shortcut.save() |
106 shortcut.save() |
107 |
107 |
108 |
108 |
109 def windowsDesktopNames(): |
109 def windowsDesktopNames(): |
110 """ |
110 """ |
111 Function to generate the link names for the Windows Desktop. |
111 Function to generate the link names for the Windows Desktop. |
112 |
112 |
113 @return list of desktop link names |
113 @return list of desktop link names |
114 @rtype list of str |
114 @rtype list of str |
115 """ |
115 """ |
116 return [e[0] for e in windowsDesktopEntries()] |
116 return [e[0] for e in windowsDesktopEntries()] |
117 |
117 |
118 |
118 |
119 def windowsDesktopEntries(): |
119 def windowsDesktopEntries(): |
120 """ |
120 """ |
121 Function to generate data for the Windows Desktop links. |
121 Function to generate data for the Windows Desktop links. |
122 |
122 |
123 @return list of tuples containing the desktop link name, |
123 @return list of tuples containing the desktop link name, |
124 the link target and the icon target |
124 the link target and the icon target |
125 @rtype list of tuples of (str, str, str) |
125 @rtype list of tuples of (str, str, str) |
126 """ |
126 """ |
127 majorVersion, minorVersion = sys.version_info[:2] |
127 majorVersion, minorVersion = sys.version_info[:2] |
128 entriesTemplates = [ |
128 entriesTemplates = [ |
129 ("eric7 (Python {0}.{1}).lnk", |
129 ( |
130 os.path.join(getConfig("bindir"), "eric7.cmd"), |
130 "eric7 (Python {0}.{1}).lnk", |
131 os.path.join(getConfig("ericPixDir"), "eric7.ico")), |
131 os.path.join(getConfig("bindir"), "eric7.cmd"), |
132 ("eric7 Browser (Python {0}.{1}).lnk", |
132 os.path.join(getConfig("ericPixDir"), "eric7.ico"), |
133 os.path.join(getConfig("bindir"), "eric7_browser.cmd"), |
133 ), |
134 os.path.join(getConfig("ericPixDir"), "ericWeb48.ico")), |
134 ( |
|
135 "eric7 Browser (Python {0}.{1}).lnk", |
|
136 os.path.join(getConfig("bindir"), "eric7_browser.cmd"), |
|
137 os.path.join(getConfig("ericPixDir"), "ericWeb48.ico"), |
|
138 ), |
135 ] |
139 ] |
136 |
140 |
137 return [ |
141 return [ |
138 (e[0].format(majorVersion, minorVersion), e[1], e[2]) |
142 (e[0].format(majorVersion, minorVersion), e[1], e[2]) for e in entriesTemplates |
139 for e in entriesTemplates |
|
140 ] |
143 ] |
141 |
144 |
142 |
145 |
143 def windowsProgramsEntry(): |
146 def windowsProgramsEntry(): |
144 """ |
147 """ |
145 Function to generate the name of the Start Menu top entry. |
148 Function to generate the name of the Start Menu top entry. |
146 |
149 |
147 @return name of the Start Menu top entry |
150 @return name of the Start Menu top entry |
148 @rtype str |
151 @rtype str |
149 """ |
152 """ |
150 majorVersion, minorVersion = sys.version_info[:2] |
153 majorVersion, minorVersion = sys.version_info[:2] |
151 return "eric7 (Python {0}.{1})".format(majorVersion, minorVersion) |
154 return "eric7 (Python {0}.{1})".format(majorVersion, minorVersion) |
152 |
155 |
153 |
156 |
154 if __name__ == "__main__": |
157 if __name__ == "__main__": |
155 try: |
158 try: |
156 main(sys.argv) |
159 main(sys.argv) |
157 except SystemExit: |
160 except SystemExit: |
158 raise |
161 raise |
159 except Exception: |
162 except Exception: |
160 print("""An internal error occured. Please report all the output""" |
163 print( |
161 """ of the program,\nincluding the following traceback, to""" |
164 """An internal error occured. Please report all the output""" |
162 """ eric-bugs@eric-ide.python-projects.org.\n""") |
165 """ of the program,\nincluding the following traceback, to""" |
|
166 """ eric-bugs@eric-ide.python-projects.org.\n""" |
|
167 ) |
163 raise |
168 raise |
164 |
169 |
165 # |
170 # |
166 # eflag: noqa = M801 |
171 # eflag: noqa = M801 |