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

from __future__ import print_function
import errno, json, os

def make_markdown_cell(source):
    return {
        'cell_type': 'markdown',
        'metadata': {},
        'source': source.splitlines(True),
    }

def make_code_cell(source):
    return {
        'cell_type': 'code',
        'execution_count': None,
        'metadata': {
            'collapsed': False,
        },
        'outputs': [],
        'source': source.splitlines(True),
    }

def make_notebook(cells):
    return {
        'cells': cells,
        'metadata': {
            'kernelspec': {
                'display_name': 'Regent',
                'language': 'regent',
                'name': 'regent',
            },
            'language_info': {
                'file_extension': 'rg',
                'mimetype': 'text/x-regent',
                'name': 'regent',
                'pygments_lexer': 'lua',
            },
        },
        'nbformat': 4,
        'nbformat_minor': 0,
    }

def make_session_notebook(source_dir, notebook_path):
    instructions_path = os.path.join(source_dir, 'instructions.md')
    instructions = make_markdown_cell(open(instructions_path, 'rb').read())
    syntax_path = os.path.join(source_dir, 'syntax.rg')
    syntax = make_code_cell(open(syntax_path, 'rb').read())
    circuit_path = os.path.join(source_dir, 'circuit.rg')
    circuit = make_code_cell(open(circuit_path, 'rb').read())
    notebook = make_notebook([instructions, syntax, circuit])
    with open(notebook_path, 'wb') as f: json.dump(notebook, f, indent=1)

def make_all_sessions(source_dir, notebook_dir):
    try:
        os.mkdir(notebook_dir)
    except OSError as e:
        if e.errno != errno.EEXIST or not os.path.isdir(notebook_dir):
            raise

    def walk(_, path_dir, names):
        if 'instructions.md' in names:
            part = os.path.basename(path_dir).partition('part')
            session = os.path.basename(os.path.dirname(path_dir)).partition('session')
            assert(part[0] == '' and session[0] == '')
            notebook_name = 'Session {} Part {}.ipynb'.format(session[2], part[2])
            make_session_notebook(path_dir, os.path.join(notebook_dir, notebook_name))
    os.path.walk(source_dir, walk, None)

if __name__ == '__main__':
    root_dir = os.path.dirname(os.path.realpath(__file__))
    exercises_dir = os.path.join(root_dir, 'regent', 'exercises')
    notebook_dir = os.path.join(root_dir, 'regent', 'notebooks')
    make_all_sessions(exercises_dir, notebook_dir)<commit_msg>Include helpers in notebook.<commit_after>#!/usr/bin/env python

from __future__ import print_function
import errno, json, os, shutil, subprocess, sys

def make_markdown_cell(source):
    return {
        'cell_type': 'markdown',
        'metadata': {},
        'source': source.splitlines(True),
    }

def make_code_cell(source):
    return {
        'cell_type': 'code',
        'execution_count': None,
        'metadata': {
            'collapsed': False,
        },
        'outputs': [],
        'source': source.splitlines(True),
    }

def make_notebook(cells):
    return {
        'cells': cells,
        'metadata': {
            'kernelspec': {
                'display_name': 'Regent',
                'language': 'regent',
                'name': 'regent',
            },
            'language_info': {
                'file_extension': 'rg',
                'mimetype': 'text/x-regent',
                'name': 'regent',
                'pygments_lexer': 'lua',
            },
        },
        'nbformat': 4,
        'nbformat_minor': 0,
    }

def make_session_notebook(source_dir, notebook_path):
    instructions_path = os.path.join(source_dir, 'instructions.md')
    instructions = make_markdown_cell(open(instructions_path, 'rb').read())
    syntax_path = os.path.join(source_dir, 'syntax.rg')
    syntax = make_code_cell(open(syntax_path, 'rb').read())
    circuit_path = os.path.join(source_dir, 'circuit.rg')
    circuit = make_code_cell(open(circuit_path, 'rb').read())
    notebook = make_notebook([instructions, syntax, circuit])
    with open(notebook_path, 'wb') as f: json.dump(notebook, f, indent=1)

def make_all_sessions(exercise_dir, helpers_dir, notebook_dir):
    dirty = subprocess.check_output(['git', '-C', helpers_dir, 'clean', '-nxd'])
    if len(dirty.strip()) > 0:
        print('Please clean up {} and run again.'.format(os.path.relpath(helpers_dir)))
        print('(E.g. with git -C {} clean -fxd.)'.format(os.path.relpath(helpers_dir)))
        print(dirty)
        sys.exit(1)

    shutil.rmtree(notebook_dir, True)
    os.mkdir(notebook_dir)

    def make(_, path_dir, names):
        if 'instructions.md' in names:
            part = os.path.basename(path_dir).partition('part')
            session = os.path.basename(os.path.dirname(path_dir)).partition('session')
            assert(part[0] == '' and session[0] == '')
            notebook_name = 'Session {} Part {}.ipynb'.format(session[2], part[2])
            make_session_notebook(path_dir, os.path.join(notebook_dir, notebook_name))
    os.path.walk(exercise_dir, make, None)

    for name in os.listdir(helpers_dir):
        src_dir = os.path.join(helpers_dir, name)
        dst_dir = os.path.join(notebook_dir, name)
        if os.path.isdir(src_dir):
            shutil.copytree(src_dir, dst_dir)

if __name__ == '__main__':
    root_dir = os.path.dirname(os.path.realpath(__file__))
    exercises_dir = os.path.join(root_dir, 'regent', 'exercises')
    helpers_dir = os.path.join(root_dir, 'regent', 'helpers')
    notebook_dir = os.path.join(root_dir, 'regent', 'notebooks')
    make_all_sessions(exercises_dir, helpers_dir, notebook_dir)
{'fraction_non_alphanumeric': 0.1466106148187073, 'fraction_numerical': 0.0028025923979681207, 'mean_word_length': 2.7053861129136925, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '1253650', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1876, 'n_words': 332}