summaryrefslogtreecommitdiff
path: root/lib/thandy/packagesys/ExePackages.py
blob: 1928ef3045886dad2c75d7cff92025a2665e13f4 (plain)
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
# Copyright 2008 The Tor Project, Inc.  See LICENSE for licensing information.

import subprocess
import logging
import re
import os

import thandy.util
import thandy.packagesys.PackageSystem as PS
import thandy.packagesys.PackageDB as PDB

class RegistryChecker(PS.Checker):
    def __init__(self, key, version):
        PS.Checker.__init__(self)
        self._key = key
        self._version = version

    def __repr__(self):
        return "RegistryChecker(%r, %r)"%(self._key, self._version)

    def getInstalledVersions(self):
        try:
            return [ thandy.util.getRegistryValue(self._key) ]
        except thandy.util.NoRegistry:
            raise thandy.CheckNotSupported("This OS has no registry.")

    def isInstalled(self):
        return self._version in self.getInstalledVersions()

class CommandInstaller(PS.Installer):
    def __init__(self, relPath, installCommand, removeCommand=None):
        PS.Installer.__init__(self, relPath)
        self._installCommand = installCommand
        self._removeCommand = removeCommand

    def __repr__(self):
        parts = [ "CommandInstaller(%r, %r" %(self._relPath,
                                              self._installCommand) ]
        if self._removeCommand:
            parts.append(", %r"%self._removeCommand)
        parts.append(")")
        return "".join(parts)

    def install(self):
        self._runCommand(self._installCommand)

    def remove(self):
        if self._removeCommand:
            raise thandy.RemoveNotSupported()
        self._runCommand(self._removeCommand)

    def _runCommand(self, command):
        d = { "FILE": self.getFilename() }
        def replace(m):
            return d[m.group(1)]
        try:
            c = [ re.sub(r'\$\{([\w_]+)\}', replace, word) for word in command ]
        except KeyError:
            raise thandy.InstallFailed("Unrecognized option in command %s"
                                       %command)
        logging.info("Installing %s.  Command is %s", self._relPath, c)

        return_code = self._execute(c)
        if return_code != 0:
            raise thandy.InstallFailed("Return code %s from calling %s"%
                                       (return_code, c))

    def _execute(self, cmd):
        try:
            return subprocess.call(cmd)
        except OSError, e:
            logging.warn("Error from trying to call %s: %s", cmd, e)
            raise thandy.InstallFailed("Could not execute install command %s"
                                       %cmd)