diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b24d71e --- /dev/null +++ b/.gitignore @@ -0,0 +1,50 @@ +# These are some examples of commonly ignored file patterns. +# You should customize this list as applicable to your project. +# Learn more about .gitignore: +# https://www.atlassian.com/git/tutorials/saving-changes/gitignore + +# Node artifact files +node_modules/ +dist/ + +# Compiled Java class files +*.class + +# Compiled Python bytecode +*.py[cod] + +# Log files +*.log + +# Package files +*.jar + +# Maven +target/ +dist/ + +# JetBrains IDE +.idea/ + +# Unit test reports +TEST*.xml + +# Generated by MacOS +.DS_Store + +# Generated by Windows +Thumbs.db + +# Applications +*.app +*.exe +*.war + +# Large media files +*.mp4 +*.tiff +*.avi +*.flv +*.mov +*.wmv + diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 0000000..d52f194 --- /dev/null +++ b/.pylintrc @@ -0,0 +1,548 @@ +[MASTER] + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code +extension-pkg-whitelist= + +# Add files or directories to the blacklist. They should be base names, not +# paths. +ignore=CVS + +# Add files or directories matching the regex patterns to the blacklist. The +# regex matches against base names, not paths. +ignore-patterns= + +# Python code to execute, usually for sys.path manipulation such as +# pygtk.require(). +#init-hook= + +# Use multiple processes to speed up Pylint. +jobs=1 + +# List of plugins (as comma separated values of python modules names) to load, +# usually to register additional checkers. +load-plugins= + +# Pickle collected data for later comparisons. +persistent=yes + +# Specify a configuration file. +#rcfile= + +# When enabled, pylint would attempt to guess common misconfiguration and emit +# user-friendly hints instead of false-positive error messages +suggestion-mode=yes + +# Allow loading of arbitrary C extensions. Extensions are imported into the +# active Python interpreter and may run arbitrary code. +unsafe-load-any-extension=no + + +[MESSAGES CONTROL] + +# Only show warnings with the listed confidence levels. Leave empty to show +# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED +confidence= + +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifiers separated by comma (,) or put this +# option multiple times (only on the command line, not in the configuration +# file where it should appear only once).You can also use "--disable=all" to +# disable everything first and then reenable specific checks. For example, if +# you want to run only the similarities checker, you can use "--disable=all +# --enable=similarities". If you want to run only the classes checker, but have +# no Warning level messages displayed, use"--disable=all --enable=classes +# --disable=W" +disable=print-statement, + parameter-unpacking, + unpacking-in-except, + old-raise-syntax, + backtick, + long-suffix, + old-ne-operator, + old-octal-literal, + import-star-module-level, + non-ascii-bytes-literal, + raw-checker-failed, + bad-inline-option, + locally-disabled, + locally-enabled, + file-ignored, + suppressed-message, + useless-suppression, + deprecated-pragma, + apply-builtin, + basestring-builtin, + buffer-builtin, + cmp-builtin, + coerce-builtin, + execfile-builtin, + file-builtin, + long-builtin, + raw_input-builtin, + reduce-builtin, + standarderror-builtin, + unicode-builtin, + xrange-builtin, + coerce-method, + delslice-method, + getslice-method, + setslice-method, + no-absolute-import, + old-division, + dict-iter-method, + dict-view-method, + next-method-called, + metaclass-assignment, + indexing-exception, + raising-string, + reload-builtin, + oct-method, + hex-method, + nonzero-method, + cmp-method, + input-builtin, + round-builtin, + intern-builtin, + unichr-builtin, + map-builtin-not-iterating, + zip-builtin-not-iterating, + range-builtin-not-iterating, + filter-builtin-not-iterating, + using-cmp-argument, + eq-without-hash, + div-method, + idiv-method, + rdiv-method, + exception-message-attribute, + invalid-str-codec, + sys-max-int, + bad-python3-import, + deprecated-string-function, + deprecated-str-translate-call, + deprecated-itertools-function, + deprecated-types-field, + next-method-defined, + dict-items-not-iterating, + dict-keys-not-iterating, + dict-values-not-iterating, + + R0201, R0205, + C0103, C0114, C0326, C0330, C0411, + W0122, W0123, W0201, W0311, W0312, W0603, W0703, W1505, + E0401, E0611, E1101 + +# Enable the message, report, category or checker with the given id(s). You can +# either give multiple identifier separated by comma (,) or put this option +# multiple time (only on the command line, not in the configuration file where +# it should appear only once). See also the "--disable" option for examples. +enable=c-extension-no-member + + +[REPORTS] + +# Python expression which should return a note less than 10 (10 is the highest +# note). You have access to the variables errors warning, statement which +# respectively contain the number of errors / warnings messages and the total +# number of statements analyzed. This is used by the global evaluation report +# (RP0004). +evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10) + +# Template used to display messages. This is a python new-style format string +# used to format the message information. See doc for all details +msg-template='{msg_id}:{line:3d},{column}: {obj}: {msg}' + +# Set the output format. Available formats are text, parseable, colorized, json +# and msvs (visual studio).You can also give a reporter class, eg +# mypackage.mymodule.MyReporterClass. +output-format=text + +# Tells whether to display a full report or only the messages +reports=no + +# Activate the evaluation score. +score=yes + + +[REFACTORING] + +# Maximum number of nested blocks for function / method body +max-nested-blocks=10 + +# Complete name of functions that never returns. When checking for +# inconsistent-return-statements if a never returning function is called then +# it will be considered as an explicit return statement and no message will be +# printed. +never-returning-functions=optparse.Values,sys.exit + + +[SPELLING] + +# Limits count of emitted suggestions for spelling mistakes +max-spelling-suggestions=4 + +# Spelling dictionary name. Available dictionaries: none. To make it working +# install python-enchant package. +spelling-dict= + +# List of comma separated words that should not be checked. +spelling-ignore-words= + +# A path to a file that contains private dictionary; one word per line. +spelling-private-dict-file= + +# Tells whether to store unknown words to indicated private dictionary in +# --spelling-private-dict-file option instead of raising a message. +spelling-store-unknown-words=no + + +[SIMILARITIES] + +# Ignore comments when computing similarities. +ignore-comments=yes + +# Ignore docstrings when computing similarities. +ignore-docstrings=yes + +# Ignore imports when computing similarities. +ignore-imports=no + +# Minimum lines number of a similarity. +min-similarity-lines=50 + + +[MISCELLANEOUS] + +# List of note tags to take in consideration, separated by a comma. +notes=FIXME, + XXX, + TODO + + +[FORMAT] + +# Expected format of line ending, e.g. empty (any line ending), LF or CRLF. +expected-line-ending-format= + +# Regexp for a line that is allowed to be longer than the limit. +ignore-long-lines=^\s*(# )??$ + +# Number of spaces of indent required inside a hanging or continued line. +indent-after-paren=4 + +# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 +# tab). +indent-string=' ' + +# Maximum number of characters on a single line. +max-line-length=300 + +# Maximum number of lines in a module +max-module-lines=1000 + +# List of optional constructs for which whitespace checking is disabled. `dict- +# separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}. +# `trailing-comma` allows a space between comma and closing bracket: (a, ). +# `empty-line` allows space-only lines. +no-space-check=trailing-comma, + dict-separator + +# Allow the body of a class to be on the same line as the declaration if body +# contains single statement. +single-line-class-stmt=no + +# Allow the body of an if to be on the same line as the test if there is no +# else. +single-line-if-stmt=no + + +[LOGGING] + +# Logging modules to check that the string format arguments are in logging +# function parameter format +logging-modules=logging + + +[VARIABLES] + +# List of additional names supposed to be defined in builtins. Remember that +# you should avoid to define new builtins when possible. +additional-builtins= + +# Tells whether unused global variables should be treated as a violation. +allow-global-unused-variables=yes + +# List of strings which can identify a callback function by name. A callback +# name must start or end with one of those strings. +callbacks=cb_, + _cb + +# A regular expression matching the name of dummy variables (i.e. expectedly +# not used). +#dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_ +dummy-variables-rgx=(_+[a-zA-Z0-9_]*?$)|dummy + +# Argument names that match this expression will be ignored. Default to name +# with leading underscore +ignored-argument-names=_.*|^ignored_|^unused_ + +# Tells whether we should check for unused import in __init__ files. +init-import=no + +# List of qualified module names which can have objects that can redefine +# builtins. +redefining-builtins-modules=six.moves,past.builtins,future.builtins + + +[TYPECHECK] + +# List of decorators that produce context managers, such as +# contextlib.contextmanager. Add to this list to register other decorators that +# produce valid context managers. +contextmanager-decorators=contextlib.contextmanager + +# List of members which are set dynamically and missed by pylint inference +# system, and so shouldn't trigger E1101 when accessed. Python regular +# expressions are accepted. +generated-members= + +# Tells whether missing members accessed in mixin class should be ignored. A +# mixin class is detected if its name ends with "mixin" (case insensitive). +ignore-mixin-members=yes + +# This flag controls whether pylint should warn about no-member and similar +# checks whenever an opaque object is returned when inferring. The inference +# can return multiple potential results while evaluating a Python object, but +# some branches might not be evaluated, which results in partial inference. In +# that case, it might be useful to still emit no-member and other checks for +# the rest of the inferred objects. +ignore-on-opaque-inference=yes + +# List of class names for which member attributes should not be checked (useful +# for classes with dynamically set attributes). This supports the use of +# qualified names. +ignored-classes=optparse.Values,thread._local,_thread._local + +# List of module names for which member attributes should not be checked +# (useful for modules/projects where namespaces are manipulated during runtime +# and thus existing member attributes cannot be deduced by static analysis. It +# supports qualified module names, as well as Unix pattern matching. +ignored-modules= + +# Show a hint with possible names when a member name was not found. The aspect +# of finding the hint is based on edit distance. +missing-member-hint=yes + +# The minimum edit distance a name should have in order to be considered a +# similar match for a missing member name. +missing-member-hint-distance=1 + +# The total number of similar names that should be taken in consideration when +# showing a hint for a missing member. +missing-member-max-choices=1 + + +[BASIC] + +# Naming style matching correct argument names +argument-naming-style=snake_case + +# Regular expression matching correct argument names. Overrides argument- +# naming-style +#argument-rgx= + +# Naming style matching correct attribute names +attr-naming-style=snake_case + +# Regular expression matching correct attribute names. Overrides attr-naming- +# style +#attr-rgx= + +# Bad variable names which should always be refused, separated by a comma +bad-names=foo, + bar, + baz, + toto, + tutu, + tata + +# Naming style matching correct class attribute names +class-attribute-naming-style=any + +# Regular expression matching correct class attribute names. Overrides class- +# attribute-naming-style +#class-attribute-rgx= + +# Naming style matching correct class names +class-naming-style=PascalCase + +# Regular expression matching correct class names. Overrides class-naming-style +#class-rgx= + +# Naming style matching correct constant names +const-naming-style=UPPER_CASE + +# Regular expression matching correct constant names. Overrides const-naming- +# style +#const-rgx= + +# Minimum line length for functions/classes that require docstrings, shorter +# ones are exempt. +docstring-min-length=1000 + +# Naming style matching correct function names +function-naming-style=camelCase + +# Regular expression matching correct function names. Overrides function- +# naming-style +#function-rgx= + +# Good variable names which should always be accepted, separated by a comma +good-names=i, + j, + k, + ex, + Run, + _ + +# Include a hint for the correct naming format with invalid-name +include-naming-hint=no + +# Naming style matching correct inline iteration names +inlinevar-naming-style=any + +# Regular expression matching correct inline iteration names. Overrides +# inlinevar-naming-style +#inlinevar-rgx= + +# Naming style matching correct method names +method-naming-style=camelCase + +# Regular expression matching correct method names. Overrides method-naming- +# style +#method-rgx= + +# Naming style matching correct module names +module-naming-style=any + +# Regular expression matching correct module names. Overrides module-naming- +# style +#module-rgx= + +# Colon-delimited sets of names that determine each other's naming style when +# the name regexes allow several styles. +name-group= + +# Regular expression which should only match function or class names that do +# not require a docstring. +no-docstring-rgx=^_ + +# List of decorators that produce properties, such as abc.abstractproperty. Add +# to this list to register other decorators that produce valid properties. +property-classes=abc.abstractproperty + +# Naming style matching correct variable names +variable-naming-style=snake_case + +# Regular expression matching correct variable names. Overrides variable- +# naming-style +#variable-rgx= + + +[IMPORTS] + +# Allow wildcard imports from modules that define __all__. +allow-wildcard-with-all=no + +# Analyse import fallback blocks. This can be used to support both Python 2 and +# 3 compatible code, which means that the block might have code that exists +# only in one or another interpreter, leading to false positives when analysed. +analyse-fallback-blocks=no + +# Deprecated modules which should not be used, separated by a comma +deprecated-modules=regsub, + TERMIOS, + Bastion, + rexec + +# Create a graph of external dependencies in the given file (report RP0402 must +# not be disabled) +ext-import-graph= + +# Create a graph of every (i.e. internal and external) dependencies in the +# given file (report RP0402 must not be disabled) +import-graph= + +# Create a graph of internal dependencies in the given file (report RP0402 must +# not be disabled) +int-import-graph= + +# Force import order to recognize a module as part of the standard +# compatibility libraries. +known-standard-library= + +# Force import order to recognize a module as part of a third party library. +known-third-party=enchant + + +[DESIGN] + +# Maximum number of arguments for function / method +max-args=100 + +# Maximum number of attributes for a class (see R0902). +max-attributes=100 + +# Maximum number of boolean expressions in a if statement +max-bool-expr=100 + +# Maximum number of branch for function / method body +max-branches=100 + +# Maximum number of locals for function / method body +max-locals=100 + +# Maximum number of parents for a class (see R0901). +max-parents=100 + +# Maximum number of public methods for a class (see R0904). +max-public-methods=100 + +# Maximum number of return / yield for function / method body +max-returns=100 + +# Maximum number of statements in function / method body +max-statements=200 + +# Minimum number of public methods for a class (see R0903). +min-public-methods=0 + + +[CLASSES] + +# List of method names used to declare (i.e. assign) instance attributes. +defining-attr-methods=__init__, + __new__, + setUp + +# List of member names, which should be excluded from the protected access +# warning. +exclude-protected=_asdict, + _fields, + _replace, + _source, + _make + +# List of valid names for the first argument in a class method. +valid-classmethod-first-arg=cls + +# List of valid names for the first argument in a metaclass class method. +valid-metaclass-classmethod-first-arg=mcs + + +[EXCEPTIONS] + +# Exceptions that will emit a warning when being caught. Defaults to +# "Exception" +overgeneral-exceptions=Exception diff --git a/CONTROL/control b/CONTROL/control new file mode 100644 index 0000000..588915b --- /dev/null +++ b/CONTROL/control @@ -0,0 +1,5 @@ +Description: JobCockpit +Maintainer: dream-alpha +Package: enigma2-plugin-systemplugins-jobcockpit +Version: 0.3.16 +Architecture: all diff --git a/CONTROL/postinst b/CONTROL/postinst new file mode 100755 index 0000000..7add5f6 --- /dev/null +++ b/CONTROL/postinst @@ -0,0 +1,16 @@ +#!/bin/sh +if [ ! -f /usr/lib/enigma2/python/Components/Task.py.org ]; then + cp /usr/lib/enigma2/python/Components/Task.py /usr/lib/enigma2/python/Components/Task.py.org +fi +cp /usr/lib/enigma2/python/Plugins/SystemPlugins/JobCockpit/Components/Task.py /usr/lib/enigma2/python/Components/Task.py + +echo "***********************************" +echo "* JobCockpit *" +echo "* by *" +echo "* dream-alpha *" +echo "***********************************" +echo "" +echo "Plugin installed successfully." +echo "Please restart DreamOS now!" +echo "" +exit 0 diff --git a/CONTROL/postrm b/CONTROL/postrm new file mode 100755 index 0000000..39e1389 --- /dev/null +++ b/CONTROL/postrm @@ -0,0 +1,9 @@ +#!/bin/sh +if [ "$1" = "remove" ]; then + rm -rf /usr/lib/enigma2/python/Plugins/SystemPlugins/JobCockpit > /dev/null 2>&1 + cp /usr/lib/enigma2/python/Components/Task.py.org /usr/lib/enigma2/python/Components/Task.py + echo "JobCockpit plugin removed successfully." +else + find /usr/lib/enigma2/python/Plugins/SystemPlugins/JobCockpit -type f -name "*.pyo" -exec rm -f {} \; > /dev/null 2>&1 +fi +exit 0 diff --git a/CONTROL/preinst b/CONTROL/preinst new file mode 100755 index 0000000..9194113 --- /dev/null +++ b/CONTROL/preinst @@ -0,0 +1,5 @@ +#!/bin/sh +if [ ! -f /usr/lib/enigma2/python/Components/Task.py.org ]; then + mv /usr/lib/enigma2/python/Components/Task.py /usr/lib/enigma2/python/Components/Task.py.org +fi +exit 0 diff --git a/Components/Makefile.am b/Components/Makefile.am new file mode 100644 index 0000000..721302f --- /dev/null +++ b/Components/Makefile.am @@ -0,0 +1,2 @@ +installdir = $(libdir)/enigma2/python/Plugins/SystemPlugins/JobCockpit/Components +install_PYTHON = *.py diff --git a/Components/Task.py b/Components/Task.py new file mode 100644 index 0000000..25a94d2 --- /dev/null +++ b/Components/Task.py @@ -0,0 +1,469 @@ +import os +from enigma import eConsoleAppContainer +from Tools.CList import CList +from Tools import Notifications + + +Notifications.notificationQueue.registerDomain("JobManager", _("Job Manager"), Notifications.ICON_DEFAULT) # pylint: disable=E0602 + + +class Job(object): + NOT_STARTED, IN_PROGRESS, FINISHED, FAILED = range(4) + + def __init__(self, name): + self.tasks = [] + self.resident_tasks = [] + self.workspace = "/tmp" + self.current_task = 0 + self.callback = None + self.name = name + self.finished = False + self.end = 100 + self.__progress = 0 + self.weightScale = 1 + self.afterEvent = None + self.state_changed = CList() + self.status = self.NOT_STARTED + + # description is a dict + def fromDescription(self, description): + pass + + def createDescription(self): + return None + + def getProgress(self): + # if self.current_task == len(self.tasks): + # return self.end + # t = self.tasks[self.current_task] + # jobprogress = t.weighting * t.progress / float(t.end) + sum([task.weighting for task in self.tasks[:self.current_task]]) + jobprogress = sum([task.weighting * task.progress / float(task.end) for task in self.tasks]) + progress = int(jobprogress * self.weightScale) + print("JOB: getProgress: progress: %s" % progress) + return int(jobprogress * self.weightScale) + + progress = property(getProgress) + + def getStatustext(self): + return {self.NOT_STARTED: _("Waiting"), self.IN_PROGRESS: _("In Progress"), self.FINISHED: _("Finished"), self.FAILED: _("Failed")}[self.status] # pylint: disable=E0602 + + def task_progress_changed_CB(self): + self.state_changed() + + def addTask(self, task): + print("JOB: addTask: %s" % task) + task.job = self + task.task_progress_changed = self.task_progress_changed_CB + self.tasks.append(task) + + def start(self, callback): + print("JOB: start: %s" % callback) + assert self.callback is None + self.callback = callback + self.restart() + + def restart(self): + print("JOB: restart") + self.status = self.IN_PROGRESS + self.state_changed() + self.runNext() + sumTaskWeightings = sum([t.weighting for t in self.tasks]) or 1 + self.weightScale = self.end / float(sumTaskWeightings) + + def runNext(self): + print("JOB: runNext: current_task: %s, tasks: %s" % (self.current_task, self.tasks)) + if self.current_task == len(self.tasks): + if len(self.resident_tasks) == 0: + self.status = self.FINISHED + self.state_changed() + self.callback(self, None, []) + self.callback = None + else: + print("JOB: still waiting for %d resident task(s) %s to finish" % (len(self.resident_tasks), str(self.resident_tasks))) + else: + print("JOB: current_task: %s" % self.current_task) + print("JOB: tasks: %s" % self.tasks) + self.tasks[self.current_task].run(self.taskCallback) + self.state_changed() + + def taskCallback(self, task, res, stay_resident=False): + print("JOB: taskCallback: task: %s, res: %s, stay_residents: %s" % (task, res, stay_resident)) + cb_idx = self.tasks.index(task) + if stay_resident: + if cb_idx not in self.resident_tasks: + self.resident_tasks.append(self.current_task) + print("JOB: task going resident:", task) + else: + print("JOB: task keeps staying resident:", task) + return + if len(res) > 0: + print("JOB: >>> Error:", res) + self.status = self.FAILED + self.state_changed() + self.callback(self, task, res) + if cb_idx != self.current_task: + if cb_idx in self.resident_tasks: + print("JOB: resident task finished:", task) + self.resident_tasks.remove(cb_idx) + if res == []: + self.state_changed() + self.current_task += 1 + self.runNext() + + def retry(self): + assert self.status == self.FAILED + self.restart() + + def abort(self, *args): + print("JOB: abort") + if self.current_task < len(self.tasks): + self.tasks[self.current_task].abort(*args) + for i in self.resident_tasks: + self.tasks[i].abort(*args) + + def cancel(self, *args): + print("JOB: cancel") + self.abort(*args) + + def remove(self, callback): + print("JOB: remove: callback: %s" % callback) + if self.status == self.IN_PROGRESS: + self.abort() + else: + callback(self, None, []) + + +class Task(object): + def __init__(self, job, name): + self.name = name + self.immediate_preconditions = [] + self.global_preconditions = [] + self.postconditions = [] + self.returncode = None + self.initial_input = None + self.job = None + self.end = 100 + self.weighting = 100 + self.__progress = 0 + self.cmd = None + self.cwd = "/tmp" + self.args = [] + self.cmdline = None + self.task_progress_changed = None + self.output_line = "" + job.addTask(self) + self.container = None + + def setCommandline(self, cmd, args): + self.cmd = cmd + self.args = args + + def setTool(self, tool): + self.cmd = tool + self.args = [tool] + self.global_preconditions.append(ToolExistsPrecondition()) + self.postconditions.append(ReturncodePostcondition()) + + def setCmdline(self, cmdline): + self.cmdline = cmdline + + def checkPreconditions(self, immediate=False): + not_met = [] + if immediate: + preconditions = self.immediate_preconditions + else: + preconditions = self.global_preconditions + for precondition in preconditions: + if not precondition.check(self): + not_met.append(precondition) + return not_met + + def run(self, callback): + failed_preconditions = self.checkPreconditions(True) + self.checkPreconditions(False) + if len(failed_preconditions) > 0: + callback(self, failed_preconditions) + return + self.prepare() + + self.callback = callback + self.container = eConsoleAppContainer() + self.appClosed_conn = self.container.appClosed.connect(self.processFinished) + self.stdoutAvail_conn = self.container.stdoutAvail.connect(self.processStdout) + self.stderrAvail_conn = self.container.stderrAvail.connect(self.processStderr) + + if self.cwd is not None: + self.container.setCWD(self.cwd) + + if not self.cmd and self.cmdline: + print("TASK: execute:", self.container.execute(self.cmdline), self.cmdline) + else: + assert self.cmd is not None + assert len(self.args) >= 1 + print("TASK: execute:", self.container.execute(self.cmd, *self.args), ' '.join(self.args)) + if self.initial_input: + self.writeInput(self.initial_input) + + def prepare(self): + pass + + def cleanup(self, failed): + pass + + def processStdout(self, data): + self.processOutput(data) + + def processStderr(self, data): + self.processOutput(data) + + def processOutput(self, data): + self.output_line += data + while True: + i = self.output_line.find('\n') + if i == -1: + break + self.processOutputLine(self.output_line[:i + 1]) + self.output_line = self.output_line[i + 1:] + + def processOutputLine(self, line): + pass + + def processFinished(self, returncode): + self.returncode = returncode + self.finish() + + def abort(self): + if self.container: + self.container.kill() + self.finish(aborted=True) + + def finish(self, aborted=False): + self.afterRun() + not_met = [] + if aborted: + not_met.append(AbortedPostcondition()) + else: + for postcondition in self.postconditions: + if not postcondition.check(self): + not_met.append(postcondition) + self.cleanup(not_met) + self.callback(self, not_met) + + def afterRun(self): + pass + + def writeInput(self, ainput): + self.container.write(ainput) + + def getProgress(self): + print("TASK: getProgress: __progress: %s" % self.__progress) + return self.__progress + + def setProgress(self, progress): + print("TASK: setProgress: progress: %s" % progress) + if progress > self.end: + progress = self.end + if progress < 0: + progress = 0 + self.__progress = progress + if self.task_progress_changed: + self.task_progress_changed() # pylint: disable=E1102 + print("TASK: setProgress: __progress: %s" % self.__progress) + + progress = property(getProgress, setProgress) + + +# The jobmanager will execute multiple jobs, each after another. +# later, it will also support suspending jobs (and continuing them after reboot etc) +# It also supports a notification when some error occured, and possibly a retry. + + +class JobManager: + def __init__(self, domain="JobManager"): + self.active_jobs = [] + self.successfull_jobs = [] + self.failed_jobs = [] + self.job_classes = [] + self.in_background = False + self.active_job = None + self.domain = domain + + def AddJob(self, job): + self.active_jobs.append(job) + self.kick() + + def AbortJob(self, job, *args): + if job.status == Job.IN_PROGRESS: + job.abort(*args) + + def RemoveJob(self, job): + if job.status == Job.NOT_STARTED: + if job in self.active_jobs: + self.active_jobs.remove(job) + elif job.status == Job.FINISHED: + if job in self.successfull_jobs: + self.successfull_jobs.remove(job) + elif job.status == Job.FAILED: + if job in self.failed_jobs: + self.failed_jobs.remove(job) + + def CleanupJobs(self): + self.successfull_jobs = [] + self.failed_jobs = [] + + def kick(self): + if self.active_job is None: + if len(self.active_jobs) > 0: + self.active_job = self.active_jobs.pop(0) + self.active_job.start(self.jobDone) + + def jobDone(self, job, task, problems): + print("JOBMGR: job", job, "completed with", problems, "in", task) + if self.in_background: + from Screens.TaskView import JobView # pylint: disable=C0415 + self.in_background = False + Notifications.AddNotification(JobView, self.active_job, domain=self.domain) + if problems: + from Screens.MessageBox import MessageBox # pylint: disable=C0415 + if problems[0].RECOVERABLE: + Notifications.AddNotificationWithCallback(self.errorCB, MessageBox, _("Error: %s\nRetry?") % (problems[0].getErrorMessage(task)), domain=self.domain) # pylint: disable=E0602 + else: + Notifications.AddNotification(MessageBox, _("Error") + (': %s') % (problems[0].getErrorMessage(task)), type=MessageBox.TYPE_ERROR, domain=self.domain) # pylint: disable=E0602 + self.errorCB(False) + if hasattr(self.active_job, "keep") and self.active_job.keep: + self.failed_jobs.append(self.active_job) + return + if hasattr(self.active_job, "keep") and self.active_job.keep: + self.successfull_jobs.append(self.active_job) + + self.active_job = None + self.kick() + + def errorCB(self, answer): + if answer: + print("JOBMGR: retrying job") + self.active_job.retry() + else: + print("JOBMGR: not retrying job.") + self.failed_jobs.append(self.active_job) + self.active_job = None + self.kick() + + def getPendingJobs(self): + jobs = [] + if self.active_job: + jobs.append(self.active_job) + jobs += self.active_jobs + return jobs + + def getSuccessfullJobs(self): + return self.successfull_jobs + + def getFailedJobs(self): + return self.failed_jobs + + +# some examples: +# class PartitionExistsPostcondition: +# def __init__(self, device): +# self.device = device +# +# def check(self, task): +# import os +# return os.access(self.device + "part1", os.F_OK) +# +# class CreatePartitionTask(Task): +# def __init__(self, device): +# Task.__init__(self, _("Create Partition")) +# self.device = device +# self.setTool("/sbin/sfdisk") +# self.args += ["-f", self.device + "disc"] +# self.initial_input = "0,\n;\n;\n;\ny\n" +# self.postconditions.append(PartitionExistsPostcondition(self.device)) +# +# class CreateFilesystemTask(Task): +# def __init__(self, device, partition = 1, largefile = True): +# Task.__init__(self, _("Create Filesystem")) +# self.setTool("/sbin/mkfs.ext") +# if largefile: +# self.args += ["-T", "largefile"] +# self.args.append("-m0") +# self.args.append(device + "part%d" % partition) +# +# class FilesystemMountTask(Task): +# def __init__(self, device, partition = 1, filesystem = "ext3"): +# Task.__init__(self, _("Mounting Filesystem")) +# self.setTool("/bin/mount") +# if filesystem is not None: +# self.args += ["-t", filesystem] +# self.args.append(device + "part%d" % partition) + + +class Condition: + RECOVERABLE = False + + def getErrorMessage(self, task): + return _("An unknown error occured!") + " (%s @ task %s)" % (self.__class__.__name__, task.__class__.__name__) # pylint: disable=E0602 + + +class DiskspacePrecondition(Condition): + def __init__(self, diskspace_required): + self.diskspace_required = diskspace_required + self.diskspace_available = 0 + + def check(self, task): + try: + s = os.statvfs(task.job.workspace) + self.diskspace_available = s.f_bsize * s.f_bavail + return self.diskspace_available >= self.diskspace_required + except OSError: + return False + + def getErrorMessage(self, task): + return _("Not enough diskspace. Please free up some diskspace and try again. (%d MB required, %d MB available)") % (self.diskspace_required / 1024 / 1024, self.diskspace_available / 1024 / 1024) # pylint: disable=E0602 + + +class ToolExistsPrecondition(Condition): + def check(self, task): + if task.cmd[0] == '/': + self.realpath = task.cmd + print("[Task.py][ToolExistsPrecondition] WARNING: usage of absolute paths for tasks should be avoided!") + return os.access(self.realpath, os.X_OK) + self.realpath = task.cmd + path = os.environ.get('PATH', '').split(os.pathsep) + path.append(task.cwd + '/') + absolutes = filter(lambda file: os.access(file, os.X_OK), map(lambda directory, file=task.cmd: os.path.join(directory, file), path)) + if len(absolutes) > 0: + self.realpath = task.cmd[0] + return True + return False + + def getErrorMessage(self, task): + return _("A required tool (%s) was not found.") % (self.realpath) # pylint: disable=E0602 + + +class AbortedPostcondition(Condition): + def getErrorMessage(self, task): + return "Cancelled upon user request" + + +class ReturncodePostcondition(Condition): + def check(self, task): + return task.returncode == 0 + + +# class HDDInitJob(Job): +# def __init__(self, device): +# Job.__init__(self, _("Initialize Harddisk")) +# self.device = device +# self.fromDescription(self.createDescription()) +# +# def fromDescription(self, description): +# self.device = description["device"] +# self.addTask(CreatePartitionTask(self.device)) +# self.addTask(CreateFilesystemTask(self.device)) +# self.addTask(FilesystemMountTask(self.device)) +# +# def createDescription(self): +# return {"device": self.device} + +job_manager = JobManager() diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..f56f3bc --- /dev/null +++ b/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = Components src po diff --git a/README.md b/README.md new file mode 100755 index 0000000..1373c97 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +[](https://app.codacy.com/gh/dream-alpha/JobCockpit/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade) +[](https://gemfury.com/f/partner) + +# JOBCockpit (JOC) +DreamOS plugin replacing standard job manager + +## Features +- Keeps jobs after completion. +- Adds functions to query successfully completed and failed jobs. +- Provides separate task managers by plugin... that allow parallel execution of jobs. + +## Languages +- n/a + +## Links +- Installation: https://dream-alpha.github.io/JobCockpit +- Support: https://github.com/dream-alpha/JobCockpit/discussions +- Package feed: https://gemfury.com/dream-alpha diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 0000000..1c551b4 --- /dev/null +++ b/docs/index.html @@ -0,0 +1,15 @@ + +
+