source: ogClient-Git/src/windows/ogOperations.py @ 1601ad9

Last change on this file since 1601ad9 was b5d5d29, checked in by Jose M. Guisado <jguisado@…>, 3 years ago

#1065 Support pyinstaller for Windows

Add required function call before running any process with
multiprocessing. This is required for windows executables to
work properly when using pyinstaller.

See:
https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Multiprocessing

  • Property mode set to 100644
File size: 3.3 KB
Line 
1#
2# Copyright (C) 2021 Soleta Networks <info@soleta.eu>
3#
4# This program is free software: you can redistribute it and/or modify it under
5# the terms of the GNU Affero General Public License as published by the
6# Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8
9import os
10import ctypes
11import subprocess
12from subprocess import CalledProcessError
13import multiprocessing as mp
14from multiprocessing import Process, freeze_support
15
16from PIL import Image, ImageDraw
17from pystray import Icon, Menu, MenuItem
18
19from src.ogRest import ThreadState
20
21
22EWX_POWEROFF=0x00000008
23EWX_REBOOT=0x00000002
24
25
26def _create_default_image():
27    """
28    Creates a default image for the tray icon. Use in case
29    no favicon.ico is found.
30    """
31    width = height = 250
32    color1 = (255, 255, 255)
33    color2 = (255, 0, 255)
34
35    image = Image.new('RGB', (width, height), color1)
36    dc = ImageDraw.Draw(image)
37    dc.rectangle(
38        (width // 2, 0, width, height // 2),
39        fill=color2)
40    dc.rectangle(
41        (0, height // 2, width // 2, height),
42        fill=color2)
43
44    return image
45
46
47def create_image():
48    try:
49        image = Image.open(r'./favicon.ico')
50        image = Image.composite(image, Image.new('RGB', image.size, 'white'), image)
51    except:
52        image = _create_default_image()
53    return image
54
55
56def create_systray():
57    menu = Menu(MenuItem('Powered by Soleta Networks!',
58                         lambda icon, item: 1))
59    icon = Icon('ogClient', create_image(), menu=menu)
60    assert icon.icon
61    icon.run()
62
63
64systray_p = Process(target=create_systray)
65
66
67class OgWindowsOperations:
68
69    def __init__(self):
70        freeze_support()
71        mp.set_start_method('spawn')
72        systray_p.start()
73
74    def _restartBrowser(self, url):
75        raise NotImplementedError
76
77    def poweroff(self):
78        user32 = ctypes.WinDLL('user32')
79        systray_p.terminate()
80        user32.ExitWindowsEx(EWX_POWEROFF, 0x0)
81
82    def reboot(self):
83        user32 = ctypes.WinDLL('user32')
84        systray_p.terminate()
85        user32.ExitWindowsEx(EWX_REBOOT, 0x0)
86
87    def shellrun(self, request, ogRest):
88        cmd = request.getrun()
89        try:
90            result = subprocess.run(cmd,
91                                    shell=True,
92                                    stdin=subprocess.DEVNULL,
93                                    capture_output=True,
94                                    text=True,
95                                    check=True)
96        except CalledProcessError as error:
97            if error.stderr:
98                return error.stderr
99            if error.stdout:
100                return error.stdout
101            return "{Non zero exit code and empty output}"
102        return result.stdout
103
104    def session(self, request, ogRest):
105        raise NotImplementedError
106
107    def hardware(self, path, ogRest):
108        raise NotImplementedError
109
110    def setup(self, request, ogRest):
111        raise NotImplementedError
112
113    def image_restore(self, request, ogRest):
114        raise NotImplementedError
115
116    def image_create(self, path, request, ogRest):
117        raise NotImplementedError
118
119    def refresh(self, ogRest):
120        return {"status": "WIN"}
121
122    def probe(self, ogRest):
123        return {'status': 'WIN' if ogRest.state != ThreadState.BUSY else 'BSY'}
Note: See TracBrowser for help on using the repository browser.