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

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

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

21

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

    Keyword arguments:
    @return args -- arguments given by console
    """
    parser = argparse.ArgumentParser()
30 31
    if windowsclasses.DecideArchitecture.Is64Windows():
        local_default = (windowsclasses.DecideArchitecture.GetProgramFiles64()
32
                         + "\\CIRCLE\\")
33
    else:
34
        local_default = (windowsclasses.DecideArchitecture.GetProgramFiles32()
35 36
                         + "\\CIRCLE\\")
    if (not os.path.exists(local_default[:-1]) and
Csók Tamás committed
37
            os.path.exists(os.environ['APPDATA'] + "\\CIRCLE")):
38
        local_default = os.environ['APPDATA'] + "\\CIRCLE\\"
39 40
    parser.add_argument(
        "-l", "--location", help="Location of the client files in the system",
41
        type=unicode, default=local_default, required=False)
42 43 44 45 46
    parser.add_argument(
        "-r", "--remove",
        help="Remove installed files instead of creating them",
        action="store_true", required=False)
    parser.add_argument(
47 48
        "-n", "--nx",
        help="Whether we want to install the NX Client or not",
49 50 51 52
        action="store_true", required=False)
    parser.add_argument(
        "-t", "--target",
        help="If this is an URL icon, where should it point",
53
        type=unicode, default="https://cloud.bme.hu/", required=False)
54
    args = parser.parse_args()
55
    return args
56 57


58
def custom_protocol_register(custom_protocol):
59 60 61 62 63 64 65
    """
    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
    """
66 67
    if not isinstance(custom_protocol, OrderedDict):
        raise AttributeError
Csók Tamás committed
68
    print "\t" + custom_protocol.iterkeys().next()
69
    try:
70
        custom_arguments = windowsclasses.Struct()
71
        custom_arguments.registry = "HKCR"
72
        handler = windowsclasses.RegistryHandler(custom_arguments)
73 74 75 76
        handler.create_registry_from_dict_chain(custom_protocol, True)
        print "\t\tDone!"
    except:
        raise
77

78

79
def main():
80 81
    try:
        args = parse_arguments()
82
        shortcut = pythoncom.CoCreateInstance(
83
            shell.CLSID_ShellLink,
84 85 86 87
            None,
            pythoncom.CLSCTX_INPROC_SERVER,
            shell.IID_IShellLink
        )
88
        desktop_path = shell.SHGetFolderPath(
89
            0, shellcon.CSIDL_DESKTOP, 0, 0)
90
        if args.remove:
91
            location = os.path.join(desktop_path, "CIRCLE Client")
92 93
            if os.path.isfile("%s%s" % (location, ".lnk")):
                os.remove("%s%s" % (location, ".lnk"))
94
                print "%s%s file successfully removed" % (location, ".lnk")
95 96
            if os.path.isfile("%s%s" % (location, ".url")):
                os.remove("%s%s" % (location, ".url"))
97 98
                print "%s%s file successfully removed" % (location, ".url")
            for subdir, dirs, files in os.walk(args.location):
99 100
                for current_file in files:
                    path = os.path.join(subdir, current_file)
101 102 103 104 105 106 107 108 109
                    fileName, fileExtension = os.path.splitext(path)
                    if ((fileExtension == '.pyc' or
                            fileExtension == '.lnk' or
                            fileExtension == '.url' or
                            fileExtension == '.log') and
                            "windowsclasses" not in fileName and
                            "uninstall" not in fileName):
                                os.remove(path)
                                print "%s file successfully removed" % path
110
        else:
111
            print "Creating custom URL protocol handler"
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
            try:
                custom_protocol = OrderedDict([
                    ('circle', ["default",
                                "URL:circle Protocol",
                                "URL Protocol",
                                ""]),
                    ('circle\\URL Protocol', ""),
                    ('circle\\DefaultIcon', args.location + "cloud.ico"),
                    ('circle\\shell', {'open': {
                        'command': (
                            "\"%(prefix)s\pythonw.exe\""
                            " \"%(location)s"
                            "cloud.py\" " % {
                                'prefix': sys.exec_prefix,
                                'location': args.location
                            } + r'"%1"')
                    }})
                ])
                custom_protocol_register(custom_protocol)
            except:
                print "Error! URL Protocol handler installation aborted!"
133 134 135 136 137 138
            print "Creating icon in the installation folder"
            location = os.path.join(desktop_path, "CIRCLE Client.url")
            shortcut = open(location, "w")
            shortcut.write('[InternetShortcut]\n')
            shortcut.write('URL=' + args.target)
            shortcut.close()
139
            shutil.copy(location, args.location)
140
            print "Icon successfully created"
141 142
    except:
        print "Unknown error occurred! Please contact the developers!"
143 144


145
if __name__ == "__main__":
146
    main()