<commit_before># -*- coding: utf-8 -*-


"""Script to compact all Brython scripts in a single one."""


import datetime
import os
import re
import sys
import tarfile
import zipfile

import make_doc  # lint:ok

try:
    import slimit
    minify = slimit.minify
except ImportError:
    minify = slimit = None


# path of parent directory
pdir = os.path.dirname(os.getcwd())
# version info
version = [3, 3, 0, "alpha", 0]
implementation = [3, 1, 2, 'alpha', 0]

def custom_minify(src):
    _res, pos = '', 0
    while pos < len(src):
        if src[pos] in ('"', "'"):
            # the end of the string is the next quote if it is not
            # after an odd number of backslashes
            start = pos
            while True:
                end = src.find(src[pos], start + 1)
                if end == -1:
                    line = src[:pos].count('\n')
                    raise SyntaxError('string not closed in %s line %s : %s' %
                                      (fname, line, src[pos:pos + 20]))
                else:
                    # count number of backslashes before the quote
                    nb = 0
                    while src[end-nb-1] == '\\':
                        nb += 1
                    if not nb % 2:
                        break
                    else:
                        start = end+1
            _res += src[pos:end+1]
            pos = end+1
        elif src[pos] == '\r':
            pos += 1
        elif src[pos] == ' ':
            if _res and _res[-1] in '({=[)}];|\n':
                pos += 1
                continue
            _res += ' '
            while pos < len(src) and src[pos] == ' ':
                pos += 1
        elif src[pos:pos + 2] == '//':
            end = src.find('\n', pos)
            if end == -1:
                break
            pos = end
        elif src[pos:pos + 2] == '/*':
            end = src.find('*/', pos)
            if end == -1:
                break
            pos = end+2
        elif src[pos] in '={[(' and _res and _res[-1] == ' ':
            _res = _res[:-1]+src[pos]
            pos += 1
        elif src[pos] == ';' and pos < len(src) - 1 and src[pos + 1] in '\r\n':
            pos += 1
        elif src[pos] in '{[,':
            _res += src[pos]
            while pos < len(src) - 1 and src[pos + 1] in ' \r\n':
                pos += 1
            pos += 1
        elif src[pos] == '}':
            _res += src[pos]
            nxt = pos + 1
            while nxt < len(src) and src[nxt] in ' \r\n':
                nxt += 1
            if nxt < len(src) and src[nxt] == '}':
                pos = nxt - 1
            pos += 1
        else:
            _res += src[pos]
            pos += 1
    while '\n\n' in _res:
        _res = _res.replace('\n\n', '\n')
    return _res


abs_path = lambda _pth: os.path.join(os.path.dirname(os.getcwd()), 'www', 'src', _pth)
now = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')

# update version number
with open(abs_path('version_info.js'), 'wb') as vinfo_file_out:
    # implementation[2] = now
    vinfo_file_out.write('__BRYTHON__.implementation = %s\n' % implementation)
    vinfo_file_out.write('__BRYTHON__.__MAGIC__ = "%s"\n' %
                         '.'.join(['%s' % _i for _i in implementation[:3]]))
    vinfo_file_out.write('__BRYTHON__.version_info = %s\n' % str(version))
    vinfo_file_out.write('__BRYTHON__.compiled_date = "%s"\n' % str(datetime.datetime.now()))
    # builtin module names = list of scripts in src/libs
    vinfo_file_out.write('__BRYTHON__.builtin_module_names = ["posix","sys",')
    _modules=['"%s"' % fname.split('.')[0] 
               for fname in os.listdir(abs_path('libs')) if fname.endswith('.js')]
    _modules.sort()    #sort modules so that git diff's don't change between runs
    vinfo_file_out.write(',\n    '.join(_modules))
    # add Python scripts in Lib that start with _ and arent found in CPython Lib
    # using sys.executable to find stdlib dir doesn't work under linux.
    stdlib_path = os.path.dirname(os.__file__)
    # stdlib_path = os.path.join(os.path.dirname(sys.executable),'Lib')
    stdlib_mods = [f for f in os.listdir(stdlib_path) if f.startswith('_')]
    stdlib_mods.sort()
    brython_mods = [f for f in os.listdir(abs_path('Lib'))
                    if f.startswith('_') and f != '__pycache__']
    brython_py_builtins = [os.path.splitext(x)[0]
                           for x in brython_mods if x not in stdlib_mods]
    brython_py_builtins.sort()
    vinfo_file_out.write(',\n    ' + ',\n    '.join(
                     ['"%s"' % f for f in brython_py_builtins]))
    vinfo_file_out.write(']\n')

    #log.info("Finished Writing file: " + abs_path('version_info.js'))

# Create file stdlib_paths.js : static mapping between module names and paths
# in the standard library
libfolder = os.path.join(os.path.dirname(os.getcwd()), 'www', 'src')
simple_javascript_template_string = """;(function($B){\n
$B.stdlib = {}
"""
with open(os.path.join(libfolder, 'stdlib_paths.js'), 'wb') as out:
    out.write(simple_javascript_template_string)<commit_msg>add errno and time to list of builtin modules<commit_after># -*- coding: utf-8 -*-


"""Script to compact all Brython scripts in a single one."""


import datetime
import os
import re
import sys
import tarfile
import zipfile

import make_doc  # lint:ok

try:
    import slimit
    minify = slimit.minify
except ImportError:
    minify = slimit = None


# path of parent directory
pdir = os.path.dirname(os.getcwd())
# version info
version = [3, 3, 0, "alpha", 0]
implementation = [3, 1, 2, 'alpha', 0]

def custom_minify(src):
    _res, pos = '', 0
    while pos < len(src):
        if src[pos] in ('"', "'"):
            # the end of the string is the next quote if it is not
            # after an odd number of backslashes
            start = pos
            while True:
                end = src.find(src[pos], start + 1)
                if end == -1:
                    line = src[:pos].count('\n')
                    raise SyntaxError('string not closed in %s line %s : %s' %
                                      (fname, line, src[pos:pos + 20]))
                else:
                    # count number of backslashes before the quote
                    nb = 0
                    while src[end-nb-1] == '\\':
                        nb += 1
                    if not nb % 2:
                        break
                    else:
                        start = end+1
            _res += src[pos:end+1]
            pos = end+1
        elif src[pos] == '\r':
            pos += 1
        elif src[pos] == ' ':
            if _res and _res[-1] in '({=[)}];|\n':
                pos += 1
                continue
            _res += ' '
            while pos < len(src) and src[pos] == ' ':
                pos += 1
        elif src[pos:pos + 2] == '//':
            end = src.find('\n', pos)
            if end == -1:
                break
            pos = end
        elif src[pos:pos + 2] == '/*':
            end = src.find('*/', pos)
            if end == -1:
                break
            pos = end+2
        elif src[pos] in '={[(' and _res and _res[-1] == ' ':
            _res = _res[:-1]+src[pos]
            pos += 1
        elif src[pos] == ';' and pos < len(src) - 1 and src[pos + 1] in '\r\n':
            pos += 1
        elif src[pos] in '{[,':
            _res += src[pos]
            while pos < len(src) - 1 and src[pos + 1] in ' \r\n':
                pos += 1
            pos += 1
        elif src[pos] == '}':
            _res += src[pos]
            nxt = pos + 1
            while nxt < len(src) and src[nxt] in ' \r\n':
                nxt += 1
            if nxt < len(src) and src[nxt] == '}':
                pos = nxt - 1
            pos += 1
        else:
            _res += src[pos]
            pos += 1
    while '\n\n' in _res:
        _res = _res.replace('\n\n', '\n')
    return _res


abs_path = lambda _pth: os.path.join(os.path.dirname(os.getcwd()), 'www', 'src', _pth)
now = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')

# update version number
with open(abs_path('version_info.js'), 'wb') as vinfo_file_out:
    # implementation[2] = now
    vinfo_file_out.write('__BRYTHON__.implementation = %s\n' % implementation)
    vinfo_file_out.write('__BRYTHON__.__MAGIC__ = "%s"\n' %
                         '.'.join(['%s' % _i for _i in implementation[:3]]))
    vinfo_file_out.write('__BRYTHON__.version_info = %s\n' % str(version))
    vinfo_file_out.write('__BRYTHON__.compiled_date = "%s"\n' % str(datetime.datetime.now()))
    # builtin module names = list of scripts in src/libs
    vinfo_file_out.write('__BRYTHON__.builtin_module_names = ["posix","sys","errno", "time",')
    _modules=['"%s"' % fname.split('.')[0] 
               for fname in os.listdir(abs_path('libs')) if fname.endswith('.js')]
    _modules.sort()    #sort modules so that git diff's don't change between runs
    vinfo_file_out.write(',\n    '.join(_modules))
    # add Python scripts in Lib that start with _ and arent found in CPython Lib
    # using sys.executable to find stdlib dir doesn't work under linux.
    stdlib_path = os.path.dirname(os.__file__)
    # stdlib_path = os.path.join(os.path.dirname(sys.executable),'Lib')
    stdlib_mods = [f for f in os.listdir(stdlib_path) if f.startswith('_')]
    stdlib_mods.sort()
    brython_mods = [f for f in os.listdir(abs_path('Lib'))
                    if f.startswith('_') and f != '__pycache__']
    brython_py_builtins = [os.path.splitext(x)[0]
                           for x in brython_mods if x not in stdlib_mods]
    brython_py_builtins.sort()
    vinfo_file_out.write(',\n    ' + ',\n    '.join(
                     ['"%s"' % f for f in brython_py_builtins]))
    vinfo_file_out.write(']\n')

    #log.info("Finished Writing file: " + abs_path('version_info.js'))

# Create file stdlib_paths.js : static mapping between module names and paths
# in the standard library
libfolder = os.path.join(os.path.dirname(os.getcwd()), 'www', 'src')
simple_javascript_template_string = """;(function($B){\n
$B.stdlib = {}
"""
with open(os.path.join(libfolder, 'stdlib_paths.js'), 'wb') as out:
    out.write(simple_javascript_template_string)
{'fraction_non_alphanumeric': 0.1465466640579143, 'fraction_numerical': 0.009587164938368226, 'mean_word_length': 1.803894679100384, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4186274', 'n_tokens_mistral': 3486, 'n_tokens_neox': 3321, 'n_words': 886}