File: taskscheduler.py

package info (click to toggle)
displaycal-py3 3.9.16-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 29,120 kB
  • sloc: python: 115,777; javascript: 11,540; xml: 598; sh: 257; makefile: 173
file content (517 lines) | stat: -rw-r--r-- 15,620 bytes parent folder | download
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# -*- coding: utf-8 -*-
"""
Task Scheduler interface. Currently only implemented for Windows (Vista and up).
The implementation is currently minimal and incomplete when it comes to
creating tasks (all tasks are created for the 'INTERACTIVE' group and with
only logon triggers and exec actions available).

Note that most of the functionality requires administrative privileges.

Has a dict-like interface to query existing tasks.

>>> ts = TaskScheduler()

Check if task "name" exists:
>>> "name" in ts
or
>>> ts.has_task("name")

Get existing task "name":
>>> task = ts["name"]
or
>>> ts.get("name")

Run task:
>>> task.Run()
or
>>> ts.run("name")

Get task exit and startup error codes:
>>> exitcode, startup_error_code = task.GetExitCode()
or
>>> exitcode, startup_error_code = ts.get_exit_code(task)

Create a new task to be run under the current user account at logon:
>>> task = ts.create("name", "program.exe", ["arg1", "arg2", "argn"])

"""

import codecs
import os
import subprocess as sp
import sys
import tempfile

import pywintypes
import winerror

from DisplayCAL.meta import name as appname
from DisplayCAL.safe_print import enc
from DisplayCAL.util_os import getenvu
from DisplayCAL.util_str import indent, universal_newlines
from DisplayCAL.util_win import run_as_admin


RUNLEVEL_HIGHESTAVAILABLE = "HighestAvailable"
RUNLEVEL_LEASTPRIVILEGE = "LeastPrivilege"

MULTIPLEINSTANCES_IGNORENEW = "IgnoreNew"
MULTIPLEINSTANCES_STOPEXISTING = "StopExisting"


class _Dict2XML(dict):
    # Subclass this

    def __init__(self, *args, **kwargs):
        dict.__init__(self, *args, **kwargs)
        if "cls_name" not in self:
            self["cls_name"] = self.__class__.__name__
        if "cls_attr" not in self:
            self["cls_attr"] = ""

    def __str__(self):
        items = []
        for name in self:
            value = self[name]
            if isinstance(value, bool):
                value = str(value).lower()
            elif name in ("cls_name", "cls_attr") or not value:
                continue
            if isinstance(value, _Dict2XML):
                item = str(value)
            else:
                cc = "".join(f"{part[0].upper()}{part[1:]}" for part in name.split("_"))
                if isinstance(value, (list, tuple)):
                    item = "\n".join([str(item) for item in value])
                else:
                    item = f"<{cc}>{value}</{cc}>"
            items.append(indent(item, "  "))
        return """<{cls_name}{cls_attr}>
{items}
</{cls_name}>""".format(
            cls_name=self["cls_name"],
            cls_attr=self["cls_attr"],
            items="\n".join(items),
        )


class _Trigger(_Dict2XML):
    # Subclass this

    def __init__(
        self, interval=None, duration=None, stop_at_duration_end=False, enabled=True
    ):
        repetition = (
            interval
            and _Dict2XML(
                interval=interval,
                duration=duration,
                stop_at_duration_end=stop_at_duration_end,
                cls_name="Repetition",
            )
            or ""
        )
        _Dict2XML.__init__(self, repetition=repetition, enabled=enabled)


class CalendarTrigger(_Trigger):
    def __init__(
        self,
        start_boundary="2019-09-17T00:00:00",
        days_interval=1,
        weeks_interval=0,
        days_of_week=None,
        months=None,
        days_of_month=None,
        **kwargs,
    ):
        _Trigger.__init__(self, **kwargs)
        self["start_boundary"] = start_boundary
        self["schedule_by_day"] = (
            days_interval
            and _Dict2XML(days_interval=days_interval, cls_name="ScheduleByDay")
            or ""
        )
        self["schedule_by_week"] = (
            weeks_interval
            and _Dict2XML(
                days_of_week=_Dict2XML(items=days_of_week, cls_name="DaysOfWeek"),
                weeks_interval=weeks_interval,
                cls_name="ScheduleByWeek",
            )
            or ""
        )
        self["schedule_by_month"] = (
            months
            and _Dict2XML(
                days_of_month=_Dict2XML(items=days_of_month, cls_name="DaysOfMonth"),
                months=_Dict2XML(items=months, cls_name="Months"),
                cls_name="ScheduleByMonth",
            )
            or ""
        )


class LogonTrigger(_Trigger):
    pass


class ResumeFromSleepTrigger(_Trigger):
    def __init__(self, *args, **kwargs):
        _Trigger.__init__(self, *args, **kwargs)
        self["subscription"] = (
            """&lt;QueryList&gt;&lt;Query Id="0" Path="System"&gt;&lt;Select Path="System"&gt;*[System[Provider[@Name='Microsoft-Windows-Power-Troubleshooter'] and (Level=4 or Level=0) and (EventID=1)]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;"""
        )
        self["cls_name"] = "EventTrigger"


class ExecAction(_Dict2XML):
    def __init__(self, cmd, args=None):
        # Filter any None values
        args = [arg for arg in args if arg is not None]
        _Dict2XML.__init__(
            self,
            command=cmd,
            arguments=args and sp.list2cmdline(args) or None,
            cls_name="Exec",
        )


class Task(_Dict2XML):
    def __init__(
        self,
        name="",
        author="",
        description="",
        group_id="S-1-5-4",
        run_level=RUNLEVEL_LEASTPRIVILEGE,
        multiple_instances_policy=MULTIPLEINSTANCES_IGNORENEW,
        disallow_start_if_on_batteries=False,
        stop_if_going_on_batteries=False,
        allow_hard_terminate=True,
        start_when_available=False,
        run_only_if_network_available=False,
        duration=None,
        wait_timeout=None,
        stop_on_idle_end=False,
        restart_on_idle=False,
        allow_start_on_demand=True,
        enabled=True,
        hidden=False,
        run_only_if_idle=False,
        wake_to_run=False,
        execution_time_limit="PT72H",
        priority=5,
        triggers=None,
        actions=None,
    ):
        kwargs = locals()
        idle_keys = ("duration", "wait_timeout", "stop_on_idle_end", "restart_on_idle")
        idle_settings = dict()
        for key in idle_keys:
            idle_settings[key] = kwargs[key]
        for key in (
            "self",
            "name",
            "author",
            "description",
            "group_id",
            "run_level",
            "triggers",
            "actions",
        ) + idle_keys:
            del kwargs[key]
        settings = _Dict2XML(kwargs, cls_name="Settings")
        settings["idle_settings"] = _Dict2XML(idle_settings, cls_name="IdleSettings")
        kwargs = dict()
        kwargs["registration_info"] = _Dict2XML(
            author=author,
            description=description,
            URI=f"\\{name}",
            cls_name="RegistrationInfo",
        )
        kwargs["triggers"] = _Dict2XML(items=triggers or [], cls_name="Triggers")
        kwargs["principals"] = _Dict2XML(
            items=[
                _Dict2XML(
                    group_id=group_id,
                    run_level=run_level,
                    cls_name="Principal",
                    cls_attr=' id="Author"',
                )
            ],
            cls_name="Principals",
        )
        kwargs["settings"] = settings
        kwargs["actions"] = _Dict2XML(
            items=actions or [], cls_name="Actions", cls_attr=' Context="Author"'
        )
        kwargs["cls_attr"] = (
            ' version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"'
        )
        _Dict2XML.__init__(self, kwargs)

    def add_exec_action(self, cmd, args=None):
        self["actions"]["items"].append(ExecAction(cmd, args))

    def add_logon_trigger(self, enabled=True):
        self["triggers"]["items"].append(LogonTrigger(enabled))

    def write_xml(self, xmlfilename):
        with open(xmlfilename, "wb") as xmlfile:
            xmlfile.write(codecs.BOM_UTF16_LE + str(self).encode())

    def __str__(self):
        return (
            universal_newlines(
                f'<?xml version="1.0" encoding="UTF-16"?>\n{super().__str__()}'
            )
            .replace("\n", "\r\n")
            .encode("UTF-16-LE")
        )


class TaskScheduler:
    def __init__(self):
        self.__ts = None
        self.stdout = b""
        self.lastreturncode = None

    @property
    def _ts(self):
        if not self.__ts:
            import pythoncom
            from win32com.taskscheduler.taskscheduler import (
                CLSID_CTaskScheduler,
                IID_ITaskScheduler,
            )

            self.__ts = pythoncom.CoCreateInstance(
                CLSID_CTaskScheduler,
                None,
                pythoncom.CLSCTX_INPROC_SERVER,
                IID_ITaskScheduler,
            )
        return self.__ts

    def __contains__(self, name):
        return f"{name}.job" in self._ts.Enum()

    def __getitem__(self, name):
        return self._ts.Activate(name)

    def __iter__(self):
        return iter(job[:-4] for job in self._ts.Enum())

    def create_task(
        self,
        name,
        author="",
        description="",
        group_id="S-1-5-4",
        run_level=RUNLEVEL_LEASTPRIVILEGE,
        multiple_instances_policy=MULTIPLEINSTANCES_IGNORENEW,
        disallow_start_if_on_batteries=False,
        stop_if_going_on_batteries=False,
        allow_hard_terminate=True,
        start_when_available=False,
        run_only_if_network_available=False,
        duration=None,
        wait_timeout=None,
        stop_on_idle_end=False,
        restart_on_idle=False,
        allow_start_on_demand=True,
        enabled=True,
        hidden=False,
        run_only_if_idle=False,
        wake_to_run=False,
        execution_time_limit="PT72H",
        priority=5,
        triggers=None,
        actions=None,
        replace_existing=False,
        elevated=False,
        echo=False,
    ):
        """Create a new task.

        If replace_existing evaluates to True, delete any existing task with
        same name first, otherwise raise KeyError.

        """

        kwargs = locals()
        del kwargs["self"]
        del kwargs["replace_existing"]
        del kwargs["elevated"]
        del kwargs["echo"]

        if not replace_existing and name in self:
            raise KeyError(f"The task {name} already exists!")

        tempdir = tempfile.mkdtemp(prefix=f"{appname}-")
        task = Task(**kwargs)
        xmlfilename = os.path.join(tempdir, f"{name}.xml")
        task.write_xml(xmlfilename)
        try:
            return self._schtasks(
                ["/Create", "/TN", name, "/XML", xmlfilename], elevated, echo
            )
        finally:
            os.remove(xmlfilename)
            os.rmdir(tempdir)

    def create_logon_task(
        self,
        name,
        cmd,
        args=None,
        author="",
        description="",
        group_id="S-1-5-4",
        run_level=RUNLEVEL_LEASTPRIVILEGE,
        multiple_instances_policy=MULTIPLEINSTANCES_IGNORENEW,
        disallow_start_if_on_batteries=False,
        stop_if_going_on_batteries=False,
        allow_hard_terminate=True,
        start_when_available=False,
        run_only_if_network_available=False,
        duration=None,
        wait_timeout=None,
        stop_on_idle_end=False,
        restart_on_idle=False,
        allow_start_on_demand=True,
        enabled=True,
        hidden=False,
        run_only_if_idle=False,
        wake_to_run=False,
        execution_time_limit="PT72H",
        priority=5,
        replace_existing=False,
        elevated=False,
        echo=False,
    ):
        kwargs = locals()
        del kwargs["self"]
        del kwargs["cmd"]
        del kwargs["args"]
        kwargs.update(
            {"triggers": [LogonTrigger()], "actions": [ExecAction(cmd, args)]}
        )
        return self.create_task(**kwargs)

    def delete(self, name):
        """Delete existing task"""
        self._ts.Delete(name)

    def disable(self, name, echo=False):
        """Disable (deactivate) existing task"""
        self._schtasks(["/Change", "/TN", name, "/DISABLE"], echo=echo)

    def enable(self, name, echo=False):
        """Enable (activate) existing task"""
        self._schtasks(["/Change", "/TN", name, "/ENABLE"], echo=echo)

    def get(self, name, default=None):
        """Get existing task"""
        if name in self:
            return self[name]
        return default

    def get_exit_code(self, task):
        """Shorthand for task.GetExitCode().

        Return a 2-tuple exitcode, startup_error_code.

        Call win32api.FormatMessage() on either value to get a readable message

        """
        return task.GetExitCode()

    def items(self):
        return list(zip(self, self.tasks()))

    def iteritems(self):
        return zip(self, self.itertasks())

    def itertasks(self):
        return map(self.get, self)

    def run(self, name, elevated=False, echo=False):
        """Run existing task"""
        return self._schtasks(["/Run", "/TN", name], elevated, echo)

    def has_task(self, name):
        """Same as name in self"""
        return name in self

    def query_task(self, name, echo=False):
        """Query task."""
        return self._schtasks(["/Query", "/TN", name], False, echo)

    def _schtasks(self, args, elevated=False, echo=False):
        if elevated:
            try:
                p = run_as_admin("schtasks.exe", args, close_process=False, show=False)
            except pywintypes.error as exception:
                if exception.args[0] == winerror.ERROR_CANCELLED:
                    self.lastreturncode = winerror.ERROR_CANCELLED
                else:
                    raise
            else:
                self.lastreturncode = int(p["hProcess"].handle == 0)
                p["hProcess"].Close()
            finally:
                self.stdout = b""
        else:
            args.insert(0, "schtasks.exe")
            startupinfo = sp.STARTUPINFO()
            startupinfo.dwFlags |= sp.STARTF_USESHOWWINDOW
            startupinfo.wShowWindow = sp.SW_HIDE
            p = sp.Popen(
                [str(arg) for arg in args],
                stdin=sp.PIPE,
                stdout=sp.PIPE,
                stderr=sp.STDOUT,
                startupinfo=startupinfo,
            )
            self.stdout, _ = p.communicate()
            if echo:
                print(str(self.stdout, encoding=enc, errors="replace"))
            self.lastreturncode = p.returncode
        return self.lastreturncode == 0

    def tasks(self):
        return list(map(self.get, self))


if __name__ == "__main__":

    def print_task_attr(name, attr, *args):
        print(f"{name:18s}:", end=" ")
        if callable(attr):
            try:
                print(attr(*args))
            except pywintypes.com_error as exception:
                print(WindowsError(*exception.args))
            except TypeError as exception:
                print(exception)
        else:
            print(attr)

    ts = TaskScheduler()

    for taskname in ts:
        task = ts[taskname]
        print("=" * 79)
        print("{:18s}:".format("Task"), taskname)
        for name in dir(task):
            if name == "GetRunTimes":
                continue
            attr = getattr(task, name)
            if name.startswith("Get"):
                if name in ("GetTrigger", "GetTriggerString"):
                    for i in range(task.GetTriggerCount()):
                        print_task_attr(f"{name[3:]}({i:d})", attr, i)
                else:
                    print_task_attr(name[3:], attr)