File: create_windows_links.py

package info (click to toggle)
eric 23.2%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 95,096 kB
  • sloc: python: 253,898; javascript: 309; xml: 215; makefile: 32
file content (172 lines) | stat: -rw-r--r-- 5,000 bytes parent folder | download | duplicates (2)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright (c) 2018 - 2023 Detlev Offenbach <detlev@die-offenbachs.de>
#
# This is the install script for eric.

"""
Installation script for the eric IDE and all eric related tools.
"""

import contextlib
import os
import sys

try:
    import winreg
except ImportError:
    print("This script is to be used on Windows platforms only. Aborting...")
    sys.exit(1)

from eric7.Globals import getConfig


def main(argv):
    """
    Create Desktop and Start Menu links.

    @param argv list of command line arguments
    @type list of str
    """
    if sys.platform.startswith(("win", "cygwin")):
        regPath = (
            "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"
            + "\\User Shell Folders"
        )

        # 1. create desktop shortcuts
        regName = "Desktop"
        desktopFolder = os.path.normpath(
            os.path.expandvars(getWinregEntry(regName, regPath))
        )
        for linkName, targetPath, iconPath in windowsDesktopEntries():
            linkPath = os.path.join(desktopFolder, linkName)
            createWindowsShortcut(linkPath, targetPath, iconPath)

        # 2. create start menu entry and shortcuts
        regName = "Programs"
        programsEntry = getWinregEntry(regName, regPath)
        if programsEntry:
            programsFolder = os.path.normpath(os.path.expandvars(programsEntry))
            eric7EntryPath = os.path.join(programsFolder, windowsProgramsEntry())
            if not os.path.exists(eric7EntryPath):
                try:
                    os.makedirs(eric7EntryPath)
                except OSError:
                    # maybe restrictions prohibited link creation
                    return

            for linkName, targetPath, iconPath in windowsDesktopEntries():
                linkPath = os.path.join(eric7EntryPath, linkName)
                createWindowsShortcut(linkPath, targetPath, iconPath)
    else:
        print("This script is to be used on Windows platforms only. Aborting...")


def getWinregEntry(name, path):
    """
    Function to get an entry from the Windows Registry.

    @param name variable name
    @type str
    @param path registry path of the variable
    @type str
    @return value of requested registry variable
    @rtype any
    """
    try:
        registryKey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, path)
        value, _ = winreg.QueryValueEx(registryKey, name)
        winreg.CloseKey(registryKey)
        return value
    except WindowsError:
        return None


def createWindowsShortcut(linkPath, targetPath, iconPath):
    """
    Create Windows shortcut.

    @param linkPath path of the shortcut file
    @type str
    @param targetPath path the shortcut shall point to
    @type str
    @param iconPath path of the icon file
    @type str
    """
    from pywintypes import com_error
    from win32com.client import Dispatch

    with contextlib.suppress(com_error):
        shell = Dispatch("WScript.Shell")
        shortcut = shell.CreateShortCut(linkPath)
        shortcut.Targetpath = targetPath
        shortcut.WorkingDirectory = os.path.dirname(targetPath)
        shortcut.IconLocation = iconPath
        shortcut.save()


def windowsDesktopNames():
    """
    Function to generate the link names for the Windows Desktop.

    @return list of desktop link names
    @rtype list of str
    """
    return [e[0] for e in windowsDesktopEntries()]


def windowsDesktopEntries():
    """
    Function to generate data for the Windows Desktop links.

    @return list of tuples containing the desktop link name,
        the link target and the icon target
    @rtype list of tuples of (str, str, str)
    """
    majorVersion, minorVersion = sys.version_info[:2]
    entriesTemplates = [
        (
            "eric7 (Python {0}.{1}).lnk",
            os.path.join(getConfig("bindir"), "eric7_ide.cmd"),
            os.path.join(getConfig("ericPixDir"), "eric7.ico"),
        ),
        (
            "eric7 Browser (Python {0}.{1}).lnk",
            os.path.join(getConfig("bindir"), "eric7_browser.cmd"),
            os.path.join(getConfig("ericPixDir"), "ericWeb48.ico"),
        ),
    ]

    return [
        (e[0].format(majorVersion, minorVersion), e[1], e[2]) for e in entriesTemplates
    ]


def windowsProgramsEntry():
    """
    Function to generate the name of the Start Menu top entry.

    @return name of the Start Menu top entry
    @rtype str
    """
    majorVersion, minorVersion = sys.version_info[:2]
    return "eric7 IDE (Python {0}.{1})".format(majorVersion, minorVersion)


if __name__ == "__main__":
    try:
        main(sys.argv)
    except SystemExit:
        raise
    except Exception:
        print(
            """An internal error occured.  Please report all the output"""
            """ of the program,\nincluding the following traceback, to"""
            """ eric-bugs@eric-ide.python-projects.org.\n"""
        )
        raise

#
# eflag: noqa = M801, I102