eric6/eric6_post_install.py

branch
setup.py
changeset 6950
62e39a353cd9
child 6951
ef3e87580dc9
equal deleted inserted replaced
6949:a5255f1ba3f0 6950:62e39a353cd9
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2019 Detlev Offenbach <detlev@die-offenbachs.de>
5 #
6
7 from __future__ import unicode_literals
8
9 import sys
10 import os
11 import shutil
12 import sysconfig
13
14 ######################################################################
15 ## Post installation hooks for Windows below
16 ######################################################################
17
18 def createWindowsLinks():
19 """
20 Create Desktop and Start Menu links.
21 """
22 regPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer" + \
23 "\\User Shell Folders"
24
25 # 1. create desktop shortcuts
26 regName = "Desktop"
27 desktopFolder = os.path.normpath(
28 os.path.expandvars(getWinregEntry(regName, regPath)))
29 for linkName, targetPath, iconPath in windowsDesktopEntries():
30 linkPath = os.path.join(desktopFolder, linkName)
31 createWindowsShortcut(linkPath, targetPath, iconPath)
32
33 # 2. create start menu entry and shortcuts
34 regName = "Programs"
35 programsEntry = getWinregEntry(regName, regPath)
36 if programsEntry:
37 programsFolder = os.path.normpath(os.path.expandvars(programsEntry))
38 eric6EntryPath = os.path.join(programsFolder, windowsProgramsEntry())
39 if not os.path.exists(eric6EntryPath):
40 try:
41 os.makedirs(eric6EntryPath)
42 except EnvironmentError:
43 # maybe restrictions prohibited link creation
44 return
45
46 for linkName, targetPath, iconPath in windowsDesktopEntries():
47 linkPath = os.path.join(eric6EntryPath, linkName)
48 createWindowsShortcut(linkPath, targetPath, iconPath)
49
50
51 def getWinregEntry(name, path):
52 """
53 Function to get an entry from the Windows Registry.
54
55 @param name variable name
56 @type str
57 @param path registry path of the variable
58 @type str
59 @return value of requested registry variable
60 @rtype any
61 """
62 try:
63 import _winreg as winreg
64 except ImportError:
65 try:
66 import winreg
67 except ImportError:
68 return None
69
70 try:
71 registryKey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, path, 0,
72 winreg.KEY_READ)
73 value, _ = winreg.QueryValueEx(registryKey, name)
74 winreg.CloseKey(registryKey)
75 return value
76 except WindowsError:
77 return None
78
79
80 def windowsDesktopEntries():
81 """
82 Function to generate data for the Windows Desktop links.
83
84 @return list of tuples containing the desktop link name,
85 the link target and the icon target
86 @rtype list of tuples of (str, str, str)
87 """
88 majorVersion, minorVersion = sys.version_info[:2]
89 scriptsDir = sysconfig.get_path("scripts")
90 entriesTemplates = [
91 ("eric6 (Python {0}.{1}).lnk",
92 os.path.join(scriptsDir, "eric6.exe"),
93 os.path.join(scriptsDir, "eric6.ico")
94 ),
95 ("eric6 Browser (Python {0}.{1}).lnk",
96 os.path.join(scriptsDir, "eric6_browser.exe"),
97 os.path.join(scriptsDir, "ericWeb48.ico")
98 ),
99 ]
100
101 return [
102 (e[0].format(majorVersion, minorVersion), e[1], e[2])
103 for e in entriesTemplates
104 ]
105
106
107 def createWindowsShortcut(linkPath, targetPath, iconPath):
108 """
109 Create Windows shortcut.
110
111 @param linkPath path of the shortcut file
112 @type str
113 @param targetPath path the shortcut shall point to
114 @type str
115 @param iconPath path of the icon file
116 @type str
117 """
118 from win32com.client import Dispatch
119 from pywintypes import com_error
120
121 try:
122 shell = Dispatch('WScript.Shell')
123 shortcut = shell.CreateShortCut(linkPath)
124 shortcut.Targetpath = targetPath
125 shortcut.WorkingDirectory = os.path.dirname(targetPath)
126 shortcut.IconLocation = iconPath
127 shortcut.save()
128 except com_error:
129 # maybe restrictions prohibited link creation
130 pass
131
132
133 def windowsProgramsEntry():
134 """
135 Function to generate the name of the Start Menu top entry.
136
137 @return name of the Start Menu top entry
138 @rtype str
139 """
140 majorVersion, minorVersion = sys.version_info[:2]
141 return "eric6 (Python {0}.{1})".format(majorVersion, minorVersion)
142
143 ######################################################################
144 ## Post installation hooks for Linux below
145 ######################################################################
146
147 def copyLinuxMetaData():
148 """
149 Function to copy the meta data files.
150 """
151 # TODO: .desktop files need patching of the exec line
152 srcDir = os.path.join(os.path.dirname(sysconfig.get_path("scripts")),
153 "share")
154 dstDir = os.path.join(os.path.expanduser("~"), ".local", "share")
155 for metaDir in ["applications", "icons", "appdata", "metainfo"]:
156 copyMetaFilesTree(os.path.join(srcDir, metaDir),
157 os.path.join(dstDir, metaDir))
158
159
160 def copyMetaFilesTree(src, dst):
161 """
162 Function to copy the files of a directory tree.
163
164 @param src name of the source directory
165 @param dst name of the destination directory
166 """
167 try:
168 names = os.listdir(src)
169 except OSError:
170 # ignore missing directories (most probably the i18n directory)
171 return
172
173 for name in names:
174 srcname = os.path.join(src, name)
175 dstname = os.path.join(dst, name)
176 if not os.path.isdir(dst):
177 os.makedirs(dst)
178 shutil.copy2(srcname, dstname)
179 os.chmod(dstname, 0o644)
180
181 if os.path.isdir(srcname):
182 copyMetaFilesTree(srcname, dstname)
183
184 ######################################################################
185 ## Main script below
186 ######################################################################
187
188 def main():
189 """
190 Main script
191 """
192 if sys.platform.startswith(("win", "cygwin")):
193 createWindowsLinks()
194 elif sys.platform.startswith("linux"):
195 copyLinuxMetaData()
196
197 sys.exit(0)
198
199
200 if __name__ == "__main__":
201 main()

eric ide

mercurial