matthew.hart@linaro.org | 5e4fce9 | 2013-08-22 11:29:21 +0100 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | # Copyright 2013 Linaro Limited |
| 4 | # Author Matt Hart <matthew.hart@linaro.org> |
| 5 | # |
| 6 | # This program is free software; you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation; either version 2 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program; if not, write to the Free Software |
| 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | # MA 02110-1301, USA. |
| 20 | |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 21 | import logging |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 22 | log = logging.getLogger(__name__) |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 23 | |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 24 | |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 25 | class PDUDriver(object): |
matthew.hart@linaro.org | 5e4fce9 | 2013-08-22 11:29:21 +0100 | [diff] [blame] | 26 | connection = None |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 27 | hostname = "" |
matthew.hart@linaro.org | 5e4fce9 | 2013-08-22 11:29:21 +0100 | [diff] [blame] | 28 | |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 29 | def __init__(self): |
| 30 | super(PDUDriver, self).__init__() |
| 31 | |
| 32 | @classmethod |
| 33 | def select(cls, drivername): |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 34 | log.debug("adding PDUDriver subclasses: %s", |
| 35 | cls.__subclasses__()) # pylint: disable=no-member |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 36 | candidates = cls.__subclasses__() # pylint: disable=no-member |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame] | 37 | for subc in cls.__subclasses__(): # pylint: disable=no-member |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 38 | log.debug("adding %s subclasses: %s", subc, |
| 39 | subc.__subclasses__()) |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 40 | candidates = candidates + (subc.__subclasses__()) |
| 41 | for subsubc in subc.__subclasses__(): |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 42 | log.debug("adding %s subclasses: %s", subsubc, |
| 43 | subsubc.__subclasses__()) |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 44 | candidates = candidates + (subsubc.__subclasses__()) |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 45 | log.debug(candidates) |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 46 | willing = [c for c in candidates if c.accepts(drivername)] |
| 47 | if len(willing) == 0: |
| 48 | raise NotImplementedError( |
| 49 | "No driver accepted the request " |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame] | 50 | "'%s' with the specified job parameters. %s" % |
| 51 | (drivername, cls) |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 52 | ) |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 53 | log.debug("%s accepted the request", willing[0]) |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 54 | return willing[0] |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 55 | |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 56 | def handle(self, request, port_number, delay=0): |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 57 | log.debug("Driving PDU hostname: %s " |
| 58 | "PORT: %s REQUEST: %s (delay %s)", |
| 59 | self.hostname, port_number, request, delay) |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 60 | if request == "on": |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 61 | self.port_on(port_number) |
| 62 | elif request == "off": |
| 63 | self.port_off(port_number) |
| 64 | else: |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 65 | log.debug("Unknown request to handle - oops") |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 66 | raise NotImplementedError( |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame] | 67 | "Driver doesn't know how to %s " % request |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 68 | ) |
| 69 | self._cleanup() |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 70 | |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 71 | def port_on(self, port_number): |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 72 | self.port_interaction("on", port_number) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 73 | |
| 74 | def port_off(self, port_number): |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame] | 75 | self.port_interaction("off", port_number) |
| 76 | |
| 77 | def port_interaction(self, command, port_number): |
| 78 | pass |
| 79 | |
| 80 | def _bombout(self): |
| 81 | pass |
| 82 | |
| 83 | def _cleanup(self): |
| 84 | pass |