<commit_before># MIT License
#
# Copyright (c) 2016 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import os
import re
import ssl
import http.client


bundle_name_re = re.compile('^[0-9]{10}-[0-9]{10}.json.gz$')
bundle_name_extract_re = re.compile('^([0-9]{10})-([0-9]{10}).json.gz$')


def path_from_urlpath(urlpath):
    return urlpath.strip('/').split('/')


def build_bundle_filename(start, end):
    return '{:010}-{:010}.json.gz'.format(start, end)


def build_package_name(i):
    return '{:03}'.format(i)


def build_log_name(dnsname, path):
    return '_'.join([dnsname] + path)


def build_package_dir(pkg_root_dir, index, package_size, bundle_size):
    pkg_num = index // (package_size * bundle_size)
    return os.path.join(
        pkg_root_dir,
        build_package_name(pkg_num)
    )


def build_package_root_dir(root_dir, dnsname, path):
    log_name = build_log_name(dnsname, path)
    return os.path.join(root_dir, log_name)


def get_bundle_list(path, tree_size):
    """ get_bundle_list returns the (sorted) list of bundles in a directory. Only bundles are listed and if multiple
    bundles exist for a same starting index, only the "most complete" bundle is returned. That is that if a bundle goes
    from the entry 1024 to the entry 1200, another goes from 1024 to 2040 and another goes from 1024 to 2047, only the
    last one will be added to the list of bundles.

    :param path: the directory path containing the bundles
    :param tree_size: the tree_size up to which the bundles should be selected
    :return: the list of bundles
    """

    global bundle_name_re
    global bundle_name_extract_re

    l = os.listdir(path)
    bundle_list = sorted([
        f
        for f in l
        if bundle_name_re.match(f)
    ])

    # Now we will filter that bundle list
    # We take advantage of the fact that bundles are named after the index of the first entry to "deduplicate".
    filtered_bundle_list = []
    bundle_list_len = len(bundle_list)
    for i in range(0, bundle_list_len):
        cur_info = bundle_name_extract_re.match(bundle_list[i])
        if i+1 < bundle_list_len:
            nxt_info = bundle_name_extract_re.match(bundle_list[i+1])
            if cur_info.group(1) == nxt_info.group(1) and int(nxt_info.group(2)) < tree_size:
                continue
        filtered_bundle_list.append(bundle_list[i])

    return filtered_bundle_list


def parse_url(url):
    pos = url.find('/')

    if pos < 0 or pos == len(url) - 1:
        return url, []
    dnsname = url[:pos]
    path = path_from_urlpath(url[pos:])<commit_msg>Bugfix: Bundles more recent than specified tree_size were added to torrents<commit_after># MIT License
#
# Copyright (c) 2016 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import os
import re
import ssl
import http.client


bundle_name_re = re.compile('^[0-9]{10}-[0-9]{10}.json.gz$')
bundle_name_extract_re = re.compile('^([0-9]{10})-([0-9]{10}).json.gz$')


def path_from_urlpath(urlpath):
    return urlpath.strip('/').split('/')


def build_bundle_filename(start, end):
    return '{:010}-{:010}.json.gz'.format(start, end)


def build_package_name(i):
    return '{:03}'.format(i)


def build_log_name(dnsname, path):
    return '_'.join([dnsname] + path)


def build_package_dir(pkg_root_dir, index, package_size, bundle_size):
    pkg_num = index // (package_size * bundle_size)
    return os.path.join(
        pkg_root_dir,
        build_package_name(pkg_num)
    )


def build_package_root_dir(root_dir, dnsname, path):
    log_name = build_log_name(dnsname, path)
    return os.path.join(root_dir, log_name)


def get_bundle_list(path, tree_size):
    """ get_bundle_list returns the (sorted) list of bundles in a directory. Only bundles are listed and if multiple
    bundles exist for a same starting index, only the "most complete" bundle is returned. That is that if a bundle goes
    from the entry 1024 to the entry 1200, another goes from 1024 to 2040 and another goes from 1024 to 2047, only the
    last one will be added to the list of bundles.

    :param path: the directory path containing the bundles
    :param tree_size: the tree_size up to which the bundles should be selected
    :return: the list of bundles
    """

    global bundle_name_re
    global bundle_name_extract_re

    l = os.listdir(path)
    bundle_list = sorted([
        f
        for f in l
        if bundle_name_re.match(f)
    ])

    # Now we will filter that bundle list
    # We take advantage of the fact that bundles are named after the index of the first entry to "deduplicate".
    filtered_bundle_list = []
    bundle_list_len = len(bundle_list)
    for i in range(0, bundle_list_len):
        cur_info = bundle_name_extract_re.match(bundle_list[i])
        if int(cur_info.group(1)) >= tree_size or int(cur_info.group(2)) >= tree_size:
            break
        if i+1 < bundle_list_len:
            nxt_info = bundle_name_extract_re.match(bundle_list[i+1])
            if cur_info.group(1) == nxt_info.group(1) and int(nxt_info.group(2)) < tree_size:
                continue
        filtered_bundle_list.append(bundle_list[i])

    return filtered_bundle_list


def parse_url(url):
    pos = url.find('/')

    if pos < 0 or pos == len(url) - 1:
        return url, []
    dnsname = url[:pos]
    path = path_from_urlpath(url[pos:])
{'fraction_non_alphanumeric': 0.08832721837206142, 'fraction_numerical': 0.016578339448294604, 'mean_word_length': 3.664131812420786, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5237397', 'n_tokens_mistral': 2345, 'n_tokens_neox': 2184, 'n_words': 828}