<commit_before>#!/usr/bin/env python

# Copyright 2013-present Barefoot Networks, Inc.
#
# 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.
#

#
# <NAME> (<EMAIL>)
#
#

# -*- coding: utf-8 -*-

import argparse
import os
import sys
import gen_json
import gen_pd
import json
from pkg_resources import resource_string
import version


def get_parser():
    parser = argparse.ArgumentParser(description='p4c-bm arguments')
    parser.add_argument('source', metavar='source', type=str,
                        help='A source file to include in the P4 program.')
    parser.add_argument('--json', dest='json', type=str,
                        help='Dump the JSON representation to this file.',
                        required=False)
    parser.add_argument('--pd', dest='pd', type=str,
                        help='Generate PD C/C++ code for this P4 program'
                        ' in this directory. Directory must exist.',
                        required=False)
    parser.add_argument('--pd-from-json', action='store_true',
                        help='Generate PD from a JSON file, not a P4 file',
                        default=False)
    parser.add_argument('--p4-prefix', type=str,
                        help='P4 name use for API function prefix',
                        default="prog", required=False)
    parser.add_argument('--field-aliases', type=str,
                        help='Path to file containing field aliases. '
                        'In this file, each line contains a mapping with this '
                        'format: "<alias> <full name of field>"',
                        required=False)
    parser.add_argument('--p4-v1.1', action='store_true',
                        help='Run the compiler on a p4 v1.1 program',
                        default=False, required=False)
    parser.add_argument('--version', '-v', action='version',
                        version=version.get_version_str())
    parser.add_argument('--primitives', action='append', default=[],
                        help="A JSON file which contains additional primitive \
                        declarations")
    return parser


# to be used for a destination file
def _validate_path(path):
    path = os.path.abspath(path)
    if not os.path.isdir(os.path.dirname(path)):
        print path, "is not a valid path because",\
            os.path.dirname(path), "is not a valid directory"
        sys.exit(1)
    if os.path.exists(path) and not os.path.isfile(path):
        print path, "exists and is not a file"
        sys.exit(1)
    return path


# to be used for a source file
def _validate_file(path):
    _validate_path(path)
    if not os.path.exists(path):
        print path, "does not exist"
        sys.exit(1)


def _validate_dir(path):
    path = os.path.abspath(path)<commit_msg>yet another fix to validate_path (#50)<commit_after>#!/usr/bin/env python

# Copyright 2013-present Barefoot Networks, Inc.
#
# 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.
#

#
# <NAME> (<EMAIL>)
#
#

# -*- coding: utf-8 -*-

import argparse
import os
import sys
import gen_json
import gen_pd
import json
from pkg_resources import resource_string
import version


def get_parser():
    parser = argparse.ArgumentParser(description='p4c-bm arguments')
    parser.add_argument('source', metavar='source', type=str,
                        help='A source file to include in the P4 program.')
    parser.add_argument('--json', dest='json', type=str,
                        help='Dump the JSON representation to this file.',
                        required=False)
    parser.add_argument('--pd', dest='pd', type=str,
                        help='Generate PD C/C++ code for this P4 program'
                        ' in this directory. Directory must exist.',
                        required=False)
    parser.add_argument('--pd-from-json', action='store_true',
                        help='Generate PD from a JSON file, not a P4 file',
                        default=False)
    parser.add_argument('--p4-prefix', type=str,
                        help='P4 name use for API function prefix',
                        default="prog", required=False)
    parser.add_argument('--field-aliases', type=str,
                        help='Path to file containing field aliases. '
                        'In this file, each line contains a mapping with this '
                        'format: "<alias> <full name of field>"',
                        required=False)
    parser.add_argument('--p4-v1.1', action='store_true',
                        help='Run the compiler on a p4 v1.1 program',
                        default=False, required=False)
    parser.add_argument('--version', '-v', action='version',
                        version=version.get_version_str())
    parser.add_argument('--primitives', action='append', default=[],
                        help="A JSON file which contains additional primitive \
                        declarations")
    return parser


# to be used for a destination file
def _validate_path(path):
    path = os.path.abspath(path)
    if not os.path.isdir(os.path.dirname(path)):
        print path, "is not a valid path because",\
            os.path.dirname(path), "is not a valid directory"
        sys.exit(1)
    if os.path.exists(path) and not os.path.isfile(path):
        print path, "exists and is not a file"
        sys.exit(1)
    return path


# to be used for a source file
def _validate_file(path):
    path = _validate_path(path)
    if not os.path.exists(path):
        print path, "does not exist"
        sys.exit(1)
    return path


def _validate_dir(path):
    path = os.path.abspath(path)
{'fraction_non_alphanumeric': 0.10464414957780459, 'fraction_numerical': 0.0075392038600723766, 'mean_word_length': 2.2451076320939336, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '6094144', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1755, 'n_words': 605}