<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 eNovance SAS <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from hardware.generate import generate  # noqa
from hardware.generate import generate_dict  # noqa
from hardware import matcher

import copy
import netaddr
import os
import sys

import argparse
import yaml

_VERSION = "0.0.1"


def _get_content(path):
    try:
        with open(path, "r") as f:
            return f.read()
    except (OSError, IOError):
        print("Error: cannot open or read file '%s'" % path)
        sys.exit(1)


def _eval_python_file(path):
    file_content = _get_content(path)

    return eval(file_content)


def _get_yaml_content(path):
    try:
        with open(path, "r") as f:
            return yaml.load(f)
    except (OSError, IOError) as e:
        print("Error: cannot open or read file '%s': %s" % (path, e))
        sys.exit(1)


def _get_disks(specs):
    disks = []
    info = {}
    while matcher.match_spec(('disk', '$disk', 'size', '$gb'), specs, info):
        if info['gb'].startswith("gt"):
            size = int(info['gb'][3:-1]) + 1
        elif info['gb'].startswith("ge"):
            size = int(info['gb'][3:-1])
        elif info['gb'].startswith("lt"):
            size = int(info['gb'][3:-1]) - 1
        elif info['gb'].startswith("le"):
            size = int(info['gb'][3:-1])
        else:
            size = info['gb']
        disks_size = "%sGi" % size
        disks.append({"size": disks_size})
        info = {}
    return disks


def _get_nics(global_host):

    nics = []
    for mac in global_host["cmdb"]:
        if mac.startswith("mac"):
            nics.append({"mac": global_host["cmdb"][mac]})
    return nics


def _is_in_cmdb(hostname, cmdb_machines):
    for machine in cmdb_machines:
        if machine["hostname"] == hostname:
            return True
    return False


def _get_value(lines, spec, key):
    info = {}
    if (matcher.match_spec(spec, lines, info) and
        key in info and info[key][0] != '$'):
        return int(info[key])
    else:
        return None


def _get_memory(specs):
    return _get_value(specs, ('memory', 'total', 'size', '$size'), 'size')


def _get_ncpus(specs):
    return _get_value(specs, ('cpu', 'logical', 'number', '$ncpus'), 'ncpus')


def collect(config_path):
    # check config directory path
    if not os.path.exists(config_path):
        print("Error: --config-dir='%s' does not exist." % config_path)
        sys.exit(1)

    # get state file
    state_profiles = _eval_python_file("%s/edeploy/state" % config_path)

    # get global conf
    global_conf = _get_yaml_content("%s/config-tools/global.yml" % config_path)<commit_msg>virtualization/collector.py: fix pep8 warning<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 eNovance SAS <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from hardware.generate import generate  # noqa
from hardware.generate import generate_dict  # noqa
from hardware import matcher

import copy
import netaddr
import os
import sys

import argparse
import yaml

_VERSION = "0.0.1"


def _get_content(path):
    try:
        with open(path, "r") as f:
            return f.read()
    except (OSError, IOError):
        print("Error: cannot open or read file '%s'" % path)
        sys.exit(1)


def _eval_python_file(path):
    file_content = _get_content(path)

    return eval(file_content)


def _get_yaml_content(path):
    try:
        with open(path, "r") as f:
            return yaml.load(f)
    except (OSError, IOError) as e:
        print("Error: cannot open or read file '%s': %s" % (path, e))
        sys.exit(1)


def _get_disks(specs):
    disks = []
    info = {}
    while matcher.match_spec(('disk', '$disk', 'size', '$gb'), specs, info):
        if info['gb'].startswith("gt"):
            size = int(info['gb'][3:-1]) + 1
        elif info['gb'].startswith("ge"):
            size = int(info['gb'][3:-1])
        elif info['gb'].startswith("lt"):
            size = int(info['gb'][3:-1]) - 1
        elif info['gb'].startswith("le"):
            size = int(info['gb'][3:-1])
        else:
            size = info['gb']
        disks_size = "%sGi" % size
        disks.append({"size": disks_size})
        info = {}
    return disks


def _get_nics(global_host):

    nics = []
    for mac in global_host["cmdb"]:
        if mac.startswith("mac"):
            nics.append({"mac": global_host["cmdb"][mac]})
    return nics


def _is_in_cmdb(hostname, cmdb_machines):
    for machine in cmdb_machines:
        if machine["hostname"] == hostname:
            return True
    return False


def _get_value(lines, spec, key):
    info = {}
    if (matcher.match_spec(spec, lines, info) and
            key in info and info[key][0] != '$'):
        return int(info[key])
    else:
        return None


def _get_memory(specs):
    return _get_value(specs, ('memory', 'total', 'size', '$size'), 'size')


def _get_ncpus(specs):
    return _get_value(specs, ('cpu', 'logical', 'number', '$ncpus'), 'ncpus')


def collect(config_path):
    # check config directory path
    if not os.path.exists(config_path):
        print("Error: --config-dir='%s' does not exist." % config_path)
        sys.exit(1)

    # get state file
    state_profiles = _eval_python_file("%s/edeploy/state" % config_path)

    # get global conf
    global_conf = _get_yaml_content("%s/config-tools/global.yml" % config_path)
{'fraction_non_alphanumeric': 0.1385877980799009, 'fraction_numerical': 0.008206875193558377, 'mean_word_length': 2.9145454545454546, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5973750', 'n_tokens_mistral': 2139, 'n_tokens_neox': 2085, 'n_words': 612}