示例#1
0
 def testKeyValueStringToDict(self):
   """Tests the KeyValueStringToDict() function."""
   s = 'key=value::none=None::true=True::false=False'
   expected_d = {
       'key': 'value', 'none': None, 'true': 'True', 'false': 'False'}
   d = common.KeyValueStringToDict(s, delimiter='::')
   self.assertEqual(d, expected_d)
示例#2
0
    def _LogInstalls(self, installs, computer):
        """Logs a batch of installs for a given computer.

    Args:
      installs: list, of str install data from a preflight/postflight report.
      computer: models.Computer entity.
    """
        if not installs:
            return

        on_corp = self.request.get('on_corp')
        if on_corp == '1':
            on_corp = True
        elif on_corp == '0':
            on_corp = False
        else:
            on_corp = None

        to_put = []
        for install in installs:
            if install.startswith('Install of'):
                d = {
                    'applesus': 'false',
                    'duration_seconds': None,
                    'download_kbytes_per_sec': None,
                    'name': install,
                    'status': 'UNKNOWN',
                    'version': '',
                    'unattended': 'false',
                }
                # support for old 'Install of FooPkg-1.0: SUCCESSFUL' style strings.
                try:
                    m = LEGACY_INSTALL_RESULTS_STRING_REGEX.search(install)
                    if not m:
                        raise ValueError
                    elif m.group(3) == INSTALL_RESULT_SUCCESSFUL:
                        d['status'] = 0
                    else:
                        d['status'] = m.group(4)
                    d['name'] = m.group(1)
                    d['version'] = m.group(2)
                except (IndexError, AttributeError, ValueError):
                    logging.warning('Unknown install string format: %s',
                                    install)
            else:
                # support for new 'name=pkg|version=foo|...' style strings.
                d = common.KeyValueStringToDict(install)

            name = d.get('display_name', '') or d.get('name', '')
            version = d.get('version', '')
            status = str(d.get('status', ''))
            applesus = common.GetBoolValueFromString(d.get('applesus', '0'))
            unattended = common.GetBoolValueFromString(d.get(
                'unattended', '0'))
            try:
                duration_seconds = int(d.get('duration_seconds', None))
            except (TypeError, ValueError):
                duration_seconds = None
            try:
                dl_kbytes_per_sec = int(d.get('download_kbytes_per_sec', None))
                # Ignore zero KB/s download speeds, as that's how Munki reports
                # unknown speed.
                if dl_kbytes_per_sec == 0:
                    dl_kbytes_per_sec = None
            except (TypeError, ValueError):
                dl_kbytes_per_sec = None

            try:
                install_datetime = util.Datetime.utcfromtimestamp(
                    d.get('time', None))
            except ValueError, e:
                logging.warning('Ignoring invalid install_datetime; %s',
                                str(e))
                install_datetime = datetime.datetime.utcnow()
            except util.EpochExtremeFutureValueError, e:
                logging.warning('Ignoring future install_datetime; %s', str(e))
                install_datetime = datetime.datetime.utcnow()