File: job.py

package info (click to toggle)
frescobaldi 3.0.0~git20161001.0.eec60717%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,792 kB
  • ctags: 5,843
  • sloc: python: 37,853; sh: 180; makefile: 69
file content (321 lines) | stat: -rw-r--r-- 11,498 bytes parent folder | download | duplicates (2)
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
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
#
# Copyright (c) 2008 - 2015 by Wilbert Berendsen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# See http://www.gnu.org/licenses/ for more information.

"""
A Job runs LilyPond (or another process) and captures the output
to get it later or to have a log follow it.
"""


import codecs
import os
import time

from PyQt5.QtCore import QCoreApplication, QProcess, QProcessEnvironment

import signals


# message status:
STDOUT  = 1
STDERR  = 2
NEUTRAL = 4
SUCCESS = 8
FAILURE = 16

# output from the process
OUTPUT = STDERR | STDOUT

# status messages
STATUS = NEUTRAL | SUCCESS | FAILURE

# all
ALL = OUTPUT | STATUS


class Job(object):
    """Manages a process.
    
    Set the command attribute to a list of strings describing the program and
    its arguments.
    Set the directory attribute to a working directory.
    The environment attribute is a dictionary; if you set an item it will be
    added to the environment for the process (the rest will be inherited from
    the system); if you set an item to None, it will be unset.
    
    Call start() to start the process.
    The output() signal emits output (stderr or stdout) from the process.
    The done() signal is always emitted when the process has ended.
    The history() method returns all status messages and output so far.
    
    When the process has finished, the error and success attributes are set.
    The success attribute is set to True When the process exited normally and
    successful. When the process did not exit normally and successfully, the
    error attribute is set to the QProcess.ProcessError value that occurred
    last. Before start(), error and success both are None.
    
    The status messages and output all are in one of five categories:
    STDERR, STDOUT (output from the process) or NEUTRAL, FAILURE or SUCCESS
    (status messages). When displaying these messages in a log, it is advised
    to take special care for newlines, esp when a status message is displayed.
    Status messages normally have no newlines, so you must add them if needed,
    while output coming from the process may continue in the same line.
    
    """
    output = signals.Signal()
    done = signals.Signal()
    title_changed = signals.Signal() # title (string)
    
    def __init__(self):
        self.command = []
        self.directory = ""
        self.environment = {}
        self.success = None
        self.error = None
        self._title = ""
        self._aborted = False
        self._process = None
        self._history = []
        self._starttime = 0.0
        self._elapsed = 0.0
        self.decoder_stdout = self.create_decoder(STDOUT)
        self.decoder_stderr = self.create_decoder(STDERR)
        self.decode_errors = 'strict'  # codecs error handling
    
    def create_decoder(self, channel):
        """Return a decoder for the given channel (STDOUT/STDERR).
        
        This method is called from the constructor. You can re-implement this
        method to return another decoder, or you can set the decoders manually
        by setting the `decoder_stdout` and `decoder_stderr` manually after
        construction.
        
        This decoder is then used to decode the 8bit bytestrings into Python
        unicode strings. The default implementation returns a 'latin1'
        decoder for both channels.
        
        """
        return codecs.getdecoder('latin1')
        
    def title(self):
        """Return the job title, as set with set_title().
        
        The title defaults to an empty string.
        
        """
        return self._title
        
    def set_title(self, title):
        """Set the title.
        
        If the title changed, the title_changed(title) signal is emitted.
        
        """
        old, self._title = self._title, title
        if title != old:
            self.title_changed(title)
    
    def start(self):
        """Starts the process."""
        self.success = None
        self.error = None
        self._aborted = False
        self._history = []
        self._elapsed = 0.0
        self._starttime = time.time()
        if self._process is None:
            self.set_process(QProcess())
        self.start_message()
        if os.path.isdir(self.directory):
            self._process.setWorkingDirectory(self.directory)
        if self.environment:
            self._update_process_environment()
        self._process.start(self.command[0], self.command[1:])
    
    def start_time(self):
        """Return the time this job was started.
        
        Returns 0.0 when the job has not been started yet.
        
        """
        return self._starttime
    
    def elapsed_time(self):
        """Return how many seconds this process has been running."""
        if self._elapsed:
            return self._elapsed
        elif self._starttime:
            return time.time() - self._starttime
        return 0.0

    def abort(self):
        """Abort the process."""
        if self._process:
            self._aborted = True
            self.abort_message()
            if os.name == "nt":
                self._process.kill()
            else:
                self._process.terminate()
    
    def is_aborted(self):
        """Returns True if the job was aborted by calling abort()."""
        return self._aborted
        
    def is_running(self):
        """Returns True if this job is running."""
        return bool(self._process)
    
    def failed_to_start(self):
        """Return True if the process failed to start.
        
        (Call this method after the process has finished.)
        
        """
        return self.error == QProcess.FailedToStart
    
    def set_process(self, process):
        """Sets a QProcess instance and connects the signals."""
        self._process = process
        if process.parent() is None:
            process.setParent(QCoreApplication.instance())
        process.finished.connect(self._finished)
        process.error.connect(self._error)
        process.readyReadStandardError.connect(self._readstderr)
        process.readyReadStandardOutput.connect(self._readstdout)
    
    def _update_process_environment(self):
        """(internal) initializes the environment for the process."""
        se = QProcessEnvironment.systemEnvironment()
        for k, v in self.environment.items():
            se.remove(k) if v is None else se.insert(k, v)
        self._process.setProcessEnvironment(se)
    
    def message(self, text, type=NEUTRAL):
        """Output some text as the given type (NEUTRAL, SUCCESS, FAILURE, STDOUT or STDERR)."""
        self.output(text, type)
        self._history.append((text, type))
        
    def history(self, types=ALL):
        """Yield the output messages as two-tuples (text, type) since the process started.
        
        If types is given, it should be an OR-ed combination of the status types
        STDERR, STDOUT, NEUTRAL, SUCCESS or FAILURE.
        
        """
        for msg, type in self._history:
            if type & types:
                yield msg, type
        
    def stdout(self):
        """Return the standard output of the process as unicode text."""
        return "".join(self.history(STDOUT))
    
    def stderr(self):
        """Return the standard error of the process as unicode text."""
        return "".join(self.history(STDERR))
    
    def _finished(self, exitCode, exitStatus):
        """(internal) Called when the process has finished."""
        self.finish_message(exitCode, exitStatus)
        success = exitCode == 0 and exitStatus == QProcess.NormalExit
        self._bye(success)
        
    def _error(self, error):
        """(internal) Called when an error occurs."""
        self.error_message(error)
        if self._process.state() == QProcess.NotRunning:
            self._bye(False)
    
    def _bye(self, success):
        """(internal) Ends and emits the done() signal."""
        self._elapsed = time.time() - self._starttime
        if not success:
            self.error = self._process.error()
        self.success = success
        self._process.deleteLater()
        self._process = None
        self.done(success)
        
    def _readstderr(self):
        """(internal) Called when STDERR can be read."""
        output = self._process.readAllStandardError()
        self.message(self.decoder_stderr(output, self.decode_errors)[0], STDERR)
        
    def _readstdout(self):
        """(internal) Called when STDOUT can be read."""
        output = self._process.readAllStandardOutput()
        self.message(self.decoder_stdout(output, self.decode_errors)[0], STDOUT)

    def start_message(self):
        """Called by start().
        
        Outputs a message that the process has started.
        
        """
        name = self.title() or os.path.basename(self.command[0])
        self.message(_("Starting {job}...").format(job=name), NEUTRAL)
    
    def abort_message(self):
        """Called by abort().
        
        Outputs a message that the process has been aborted.
        
        """
        name = self.title() or os.path.basename(self.command[0])
        self.message(_("Aborting {job}...").format(job=name), NEUTRAL)
    
    def error_message(self, error):
        """Called when there is an error (by _error()).
        
        Outputs a message describing the given QProcess.Error.
        
        """
        if error == QProcess.FailedToStart:
            self.message(_(
                "Could not start {program}.\n"
                "Please check path and permissions.").format(program = self.command[0]), FAILURE)
        elif error == QProcess.ReadError:
            self.message(_("Could not read from the process."), FAILURE)
        elif self._process.state() == QProcess.NotRunning:
            self.message(_("An unknown error occurred."), FAILURE)

    def finish_message(self, exitCode, exitStatus):
        """Called when the process finishes (by _finished()).
        
        Outputs a message on completion of the process.
        
        """
        if exitCode:
            self.message(_("Exited with return code {code}.").format(code=exitCode), FAILURE)
        elif exitStatus:
            self.message(_("Exited with exit status {status}.").format(status=exitStatus), FAILURE)
        else:
            time = self.elapsed2str(self.elapsed_time())
            self.message(_("Completed successfully in {time}.").format(time=time), SUCCESS)

    @staticmethod
    def elapsed2str(seconds):
        """Return a short display for the given time period (in seconds)."""
        minutes, seconds = divmod(seconds, 60)
        if minutes:
            return "{0:.0f}'{1:.0f}\"".format(minutes, seconds)
        return '{0:.1f}"'.format(seconds)