~ubuntu-branches/ubuntu/lucid/landscape-client/lucid

« back to all changes in this revision

Viewing changes to landscape/manager/scriptexecution.py

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2009-12-16 10:50:05 UTC
  • mfrom: (1.2.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 27.
  • Revision ID: james.westby@ubuntu.com-20091216105005-bmki8i2of1dmcdkc
Tags: 1.4.0-0ubuntu0.9.10.0
* New upstream release (LP: #497351)

* Bug fixes:
  - Fix landscape daemons fail to start when too many groups are
    available (LP: #456124)
  - Fix landscape programs wake up far too much. (LP: #340843)
  - Fix Package manager fails with 'no such table: task' (LP #465846)
  - Fix test suite leaving temporary files around (LP #476418)
  - Fix the 1hr long wait for user data to be uploaded following a
    resynchronisation (LP #369000)

* Add support for Ubuntu release upgrades:
  - Add helper function to fetch many files at once (LP: #450629)
  - Handle release-upgrade messages in the packagemanager
    plugin (LP: #455217)
  - Add a release-upgrader task handler (LP: #462543)
  - Support upgrade-tool environment variables (LP: #463321)

* Add initial support for Smart package locking:
  - Detect and report changes about Smart package locks (#488108)

* Packaging fixes:
  - Turn unnecessary Pre-Depends on python-gobject into a regular Depends
  - If it's empty, remove /etc/landscape upon purge

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
import shutil
11
11
 
12
12
from twisted.internet.protocol import ProcessProtocol
13
 
from twisted.internet.defer import Deferred, fail
 
13
from twisted.internet.defer import Deferred, fail, maybeDeferred
14
14
from twisted.internet.error import ProcessDone
15
15
 
16
16
from landscape.lib.scriptcontent import build_script
37
37
        username_str = username.encode("utf-8")
38
38
        try:
39
39
            info = pwd.getpwnam(username_str)
40
 
        except KeyError, e:
 
40
        except KeyError:
41
41
            raise UnknownUserError(u"Unknown user '%s'" % username)
42
42
        uid = info.pw_uid
43
43
        gid = info.pw_gid
73
73
 
74
74
class UnknownInterpreterError(Exception):
75
75
    """Raised when the interpreter specified to run a script is invalid.
76
 
           
77
 
       @ivar interpreter: the interpreter specified for the script.
 
76
 
 
77
    @ivar interpreter: the interpreter specified for the script.
78
78
    """
79
79
 
80
80
    def __init__(self, interpreter):
136
136
            "execute-script", self._handle_execute_script)
137
137
 
138
138
    def _respond(self, status, data, opid, result_code=None):
139
 
        message =  {"type": "operation-result",
140
 
                    "status": status,
141
 
                    # Let's decode result-text, replacing non-printable
142
 
                    # characters
143
 
                    "result-text": data.decode("utf-8", "replace"),
144
 
                    "operation-id": opid}
 
139
        if not isinstance(data, unicode):
 
140
            # Let's decode result-text, replacing non-printable
 
141
            # characters
 
142
            data = data.decode("utf-8", "replace")
 
143
        message = {"type": "operation-result",
 
144
                   "status": status,
 
145
                   "result-text": data,
 
146
                   "operation-id": opid}
145
147
        if result_code:
146
148
            message["result-code"] = result_code
147
149
        return self.registry.broker.send_message(message, True)
167
169
            raise
168
170
 
169
171
    def _format_exception(self, e):
170
 
        return u"%s: %s" % (e.__class__.__name__, e)
 
172
        return u"%s: %s" % (e.__class__.__name__, e.args[0])
171
173
 
172
174
    def _respond_success(self, data, opid):
173
175
        return self._respond(SUCCEEDED, data, opid)
218
220
            "USER": user or "",
219
221
            "HOME": path or "",
220
222
            }
221
 
        attachment_dir = ""
222
223
        old_umask = os.umask(0022)
223
 
        try:
 
224
 
 
225
        def run_with_attachments():
224
226
            if attachments:
225
227
                attachment_dir = tempfile.mkdtemp()
226
228
                env["LANDSCAPE_ATTACHMENTS"] = attachment_dir
237
239
                if uid is not None:
238
240
                    os.chown(attachment_dir, uid, gid)
239
241
 
240
 
            result = self._run_script(
 
242
            return self._run_script(
241
243
                filename, uid, gid, path, env, time_limit)
242
 
            return result.addBoth(
243
 
                self._remove_script, filename, attachment_dir, old_umask)
244
 
        except:
245
 
            os.umask(old_umask)
246
 
            raise
247
 
 
248
 
    def _remove_script(self, result, filename, attachment_dir, old_umask):
 
244
 
 
245
        result = maybeDeferred(run_with_attachments)
 
246
        return result.addBoth(self._cleanup, filename, env, old_umask)
 
247
 
 
248
    def _cleanup(self, result, filename, env, old_umask):
249
249
        try:
250
250
            os.unlink(filename)
251
251
        except:
252
252
            pass
253
 
        if attachment_dir:
 
253
        if "LANDSCAPE_ATTACHMENTS" in env:
254
254
            try:
255
 
                shutil.rmtree(attachment_dir)
 
255
                shutil.rmtree(env["LANDSCAPE_ATTACHMENTS"])
256
256
            except:
257
257
                pass
258
258
        os.umask(old_umask)
307
307
            if reason.check(ProcessDone):
308
308
                self.result_deferred.callback(data)
309
309
            else:
310
 
                self.result_deferred.errback(ProcessFailedError(data, exit_code))
 
310
                self.result_deferred.errback(ProcessFailedError(data,
 
311
                                                                exit_code))
311
312
 
312
313
    def _cancel(self):
313
314
        """