win_install.py 5.68 KB
Newer Older
1
#!/usr/bin/env python
2 3
# -*- coding: utf-8 -*-

4 5 6
"""
Windows icon installer for CIRCLE client application
"""
7

8 9 10
import os
import argparse
import subprocess
11
import pythoncom
12
import shutil
13
from win32com.shell import shell, shellcon
14
import windowsclasses
15
try:
16
    from collections import *  # noqa
17
except ImportError:
18 19
    from OrderedDict import *  # noqa

20

21 22 23 24 25 26 27 28 29 30
def parse_arguments():
    """
    Argument parser, based on argparse module

    Keyword arguments:
    @return args -- arguments given by console
    """
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-d", "--driver",
31 32
        help="Select webdriver. Aside from Firefox, you have to install "
        + "first the proper driver.", type=str, default="firefox",
33
        required=False, choices=['firefox', 'chrome',
34
                                 'iexplore', 'opera'])
35 36
    if windowsclasses.DecideArchitecture.Is64Windows():
        local_default = (windowsclasses.DecideArchitecture.GetProgramFiles64()
37
                         + "\\CIRCLE\\")
38
    else:
39
        local_default = (windowsclasses.DecideArchitecture.GetProgramFiles32()
40 41
                         + "\\CIRCLE\\")
    if (not os.path.exists(local_default[:-1]) and
Csók Tamás committed
42
            os.path.exists(os.environ['APPDATA'] + "\\CIRCLE")):
43
        local_default = os.environ['APPDATA'] + "\\CIRCLE\\"
44 45 46 47 48 49 50 51 52 53 54 55 56 57
    parser.add_argument(
        "-l", "--location", help="Location of the client files in the system",
        default=local_default, required=False)
    parser.add_argument(
        "-r", "--remove",
        help="Remove installed files instead of creating them",
        action="store_true", required=False)
    parser.add_argument(
        "-u", "--url",
        help="Whether we installed and we want to use selenium or not",
        action="store_true", required=False)
    parser.add_argument(
        "-t", "--target",
        help="If this is an URL icon, where should it point",
58
        default="https://cloud.bme.hu/", required=False)
59
    args = parser.parse_args()
60
    return args
61 62


63
def custom_protocol_register(custom_protocol):
64 65 66 67 68 69 70
    """
    Custom protocol register based on RegistryHandler module
    Can raise AttributeError on wrongly provided dictionary chain

    Keyword arguments:
    @param dict_chain -- OrderedDictionary chain of the registry tree
    """
71 72 73 74
    if not isinstance(custom_protocol, OrderedDict):
        raise AttributeError
    print "\t"+custom_protocol.iterkeys().next()
    try:
75
        custom_arguments = windowsclasses.Struct()
76
        custom_arguments.registry = "HKCR"
77
        handler = windowsclasses.RegistryHandler(custom_arguments)
78 79 80 81
        handler.create_registry_from_dict_chain(custom_protocol, True)
        print "\t\tDone!"
    except:
        raise
82

83

84
def main():
85 86
    """
    Main program
87

88 89 90 91 92
    Jobs:
    read the parameters
    create a proper icon with the proper driver (or delete)
    call the nx_client_installer
    """
93 94
    try:
        args = parse_arguments()
95
        shortcut = pythoncom.CoCreateInstance(
96
            shell.CLSID_ShellLink,
97 98 99 100
            None,
            pythoncom.CLSCTX_INPROC_SERVER,
            shell.IID_IShellLink
        )
101
        desktop_path = shell.SHGetFolderPath(
102
            0, shellcon.CSIDL_DESKTOP, 0, 0)
103
        if args.remove:
104
            location = os.path.join(desktop_path, "Cloud GUI")
105 106 107 108 109
            if os.path.isfile("%s%s" % (location, ".lnk")):
                os.remove("%s%s" % (location, ".lnk"))
            if os.path.isfile("%s%s" % (location, ".url")):
                os.remove("%s%s" % (location, ".url"))
        else:
110 111 112
            subprocess.call(
                "python %s\\nx_client_installer.py" % os.path.dirname(
                    os.path.realpath(__file__)))
113 114 115 116 117 118 119
            if args.url:
                location = os.path.join(desktop_path, "Cloud GUI.url")
                shortcut = file(location, 'w')
                shortcut.write('[InternetShortcut]\n')
                shortcut.write('URL='+args.target)
                shortcut.close()
            else:
120
                shortcut.SetPath(args.location+"cloud.py")
121 122 123 124 125 126
                if args.driver == "chrome":
                    shortcut.SetArguments("-d chrome")
                elif args.driver == "iexplore":
                    shortcut.SetArguments("-d ie")
                elif args.driver == "opera":
                    shortcut.SetArguments("-d opera")
127 128
                shortcut.SetDescription("Tool to use CIRCLE Cloud")
                shortcut.SetIconLocation(args.location+"cloud.ico", 0)
129 130 131 132
                desktop_path = shell.SHGetFolderPath(
                    0, shellcon.CSIDL_DESKTOP, 0, 0)
                persist_file = shortcut.QueryInterface(
                    pythoncom.IID_IPersistFile)
133 134
                location = os.path.join(desktop_path, "Cloud GUI.lnk")
                persist_file.Save(location, 0)
135
            print "Icon successfully created on desktop"
136
            shutil.copy(location, args.location)
137 138
            print "Creating custom URL protocol handlers"
            try:
139 140
                custom_protocol = OrderedDict([
                    ('circle', ["default",
Csók Tamás committed
141 142 143
                                "URL:circle Protocol",
                                "URL Protocol",
                                ""]),
144 145 146
                    ('circle\\URL Protocol', ""),
                    ('circle\\DefaultIcon', args.location+"cloud.ico"),
                    ('circle\\shell', {'open': {
147 148
                        'command': "\"pythonw.exe\" \"%s" % args.location
                        + "cloud.py\" \"%1\""}})])
149
                custom_protocol_register(custom_protocol)
150 151
            except:
                print "Error! URL Protocol handler installation aborted!"
152 153
    except:
        print "Unknown error occurred! Please contact the developers!"
154 155


156
if __name__ == "__main__":
157
    main()