<commit_before>#!/usr/bin/env python
#
# Copyright 2016 VMware, Inc. All Rights Reserved.
#
# 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.#

# Simple API to access VSAN policy information.
# Uses objtool to extract and set policy in VSAN objects
#
# To obtain a connection to the local SI use vmdk_ops.get_si()
#

import logging
import json
import os.path
import vmdk_ops

OBJTOOL = '/usr/lib/vmware/osfs/bin/objtool '
OBJTOOL_SET_POLICY = OBJTOOL + "setPolicy -u {0} -p '{1}'"
OBJTOOL_GET_ATTR = OBJTOOL + "getAttr -u {0} --format=json"


def get_vsan_datastore():
    """Returns Datastore management object for vsanDatastore, or None"""
    si = vmdk_ops.get_si()
    stores = si.content.rootFolder.childEntity[0].datastore
    try:
        return [d for d in stores if d.summary.type == "vsan"][0]
    except:
        return None


def get_vsan_dockvols_path():
    """
    Return the VSAN datastore dockvols path for a given cluster. Default to the
    first datastore for now, so we can test without VSAN.
    """
    datastore = get_vsan_datastore()
    if datastore:
        path, err = vmdk_ops.get_vol_path(datastore.info.name)
        if not err:
            return path
    else:
        return None


def is_on_vsan(vmdk_path):
    """Returns True if path is on VSAN datastore, False otherwise"""
    ds = get_vsan_datastore()
    if not ds:
        return False
    return os.path.realpath(vmdk_path).startswith(os.path.realpath(
        ds.info.url))


def set_policy(vmdk_path, policy_string):
    """
    Sets policy for an object backing <vmdk_path> to <policy>.
    Returns None on success else return the output error. This can be
    displayed at the client
    """
    uuid = vmdk_ops.get_vsan_uuid(vmdk_path)
    rc, out = vmdk_ops.RunCommand(OBJTOOL_SET_POLICY.format(uuid,
                                                            policy_string))
    if rc != 0:
        logging.warning("Failed to set policy for %s : %s", vmdk_path, out)
        return out
    return None


def same_policy(vmdk_path, policy_string):<commit_msg>Optimize vmdkops_admin ls operation (#1867)<commit_after>#!/usr/bin/env python
#
# Copyright 2016 VMware, Inc. All Rights Reserved.
#
# 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.#

# Simple API to access VSAN policy information.
# Uses objtool to extract and set policy in VSAN objects
#
# To obtain a connection to the local SI use vmdk_ops.get_si()
#

import logging
import json
import os.path
import vmdk_ops

OBJTOOL = '/usr/lib/vmware/osfs/bin/objtool '
OBJTOOL_SET_POLICY = OBJTOOL + "setPolicy -u {0} -p '{1}'"
OBJTOOL_GET_ATTR = OBJTOOL + "getAttr -u {0} --format=json"

# VSAN datastore
_vsan_ds = None

def get_vsan_datastore():
    """Returns Datastore management object for vsanDatastore, or None"""
    si = vmdk_ops.get_si()
    stores = si.content.rootFolder.childEntity[0].datastore
    try:
        return [d for d in stores if d.summary.type == "vsan"][0]
    except:
        return None


def get_vsan_dockvols_path():
    """
    Return the VSAN datastore dockvols path for a given cluster. Default to the
    first datastore for now, so we can test without VSAN.
    """
    datastore = get_vsan_datastore()
    if datastore:
        path, err = vmdk_ops.get_vol_path(datastore.info.name)
        if not err:
            return path
    else:
        return None


def is_on_vsan(vmdk_path):
    """Returns True if path is on VSAN datastore, False otherwise"""
    global _vsan_ds
    if not _vsan_ds:
        _vsan_ds = get_vsan_datastore()
        if not _vsan_ds:
            return False
    return os.path.realpath(vmdk_path).startswith(os.path.realpath(
        _vsan_ds.info.url))


def set_policy(vmdk_path, policy_string):
    """
    Sets policy for an object backing <vmdk_path> to <policy>.
    Returns None on success else return the output error. This can be
    displayed at the client
    """
    uuid = vmdk_ops.get_vsan_uuid(vmdk_path)
    rc, out = vmdk_ops.RunCommand(OBJTOOL_SET_POLICY.format(uuid,
                                                            policy_string))
    if rc != 0:
        logging.warning("Failed to set policy for %s : %s", vmdk_path, out)
        return out
    return None


def same_policy(vmdk_path, policy_string):
{'fraction_non_alphanumeric': 0.0859106529209622, 'fraction_numerical': 0.0061092019854906456, 'mean_word_length': 3.0238095238095237, 'pattern_counts': {'":': 0, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5692601', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1660, 'n_words': 570}