<commit_before>#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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.
#

import sys as _sys

from plano import *

class _Project:
    def __init__(self):
        self.name = None
        self.build_dir = "build"
        self.extra_dirs = []
        self.default_prefix = join(get_home_dir(), ".local")

project = _Project()

@target
def build():
    assert project.name

    # XXX Make these settable
    dest_dir = ""
    prefix = project.default_prefix

    write_json(join(project.build_dir, "build.json"), {"prefix": prefix, "dest_dir": dest_dir})

    default_home = join(project.default_prefix, "lib", project.name)

    for path in find("bin", "*.in"):
        configure_file(path, join(project.build_dir, path[:-3]), {"default_home": default_home})

    for path in find("bin"):
        if path.endswith(".in"):
            continue

        copy(path, join(project.build_dir, path), inside=False, symlinks=False)

    for path in find("python", "*.py"):
        copy(path, join(project.build_dir, project.name, path), inside=False, symlinks=False)

    for dir_name in project.extra_dirs:
        for path in find(dir_name):
            copy(path, join(project.build_dir, project.name, path), inside=False, symlinks=False)

@target(requires=build)
def install():
    assert project.name
    assert is_dir(project.build_dir)

    build = read_json(join(project.build_dir, "build.json"))

    for path in find(join(project.build_dir, "bin")):
        copy(path, join(f"{build['dest_dir']}{build['prefix']}", path[6:]), inside=False, symlinks=False)

    for path in find(join(project.build_dir, project.name)):
        copy(path, join(f"{build['dest_dir']}{build['prefix']}", "lib", path[6:]), inside=False, symlinks=False)

@target
def clean():
    remove(project.build_dir)

    for path in find(".", "__pycache__"):
        remove(path)

    for path in find(".", "*.pyc"):
        remove(path)

@target(help="Initialize and update Git submodules")
def modules():
    run("git submodule update --init --remote --recursive")

@target(help="Generate environment variables for the developer's shell")
def env():
    assert project.name

    home_var = f"{project.name.upper()}_HOME"

    print(f"export {home_var}=$PWD/build/{project.name}")
    print("export PATH=$PWD/build/bin:$PWD/scripts:$PATH")

    if "PYTHONPATH" in ENV:
        print(f"export PYTHONPATH=${home_var}/python:$PWD/python:{ENV['PYTHONPATH']}")
    else:
        print(f"export PYTHONPATH=${home_var}/python:$PWD/python:{':'.join(_sys.path)}")<commit_msg>Add a context object for using the project environment<commit_after>#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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.
#

import sys as _sys

from plano import *

class _Project:
    def __init__(self):
        self.name = None
        self.build_dir = "build"
        self.extra_dirs = []
        self.default_prefix = join(get_home_dir(), ".local")

project = _Project()

@target
def build():
    assert project.name

    # XXX Make these settable
    dest_dir = ""
    prefix = project.default_prefix

    write_json(join(project.build_dir, "build.json"), {"prefix": prefix, "dest_dir": dest_dir})

    default_home = join(project.default_prefix, "lib", project.name)

    for path in find("bin", "*.in"):
        configure_file(path, join(project.build_dir, path[:-3]), {"default_home": default_home})

    for path in find("bin"):
        if path.endswith(".in"):
            continue

        copy(path, join(project.build_dir, path), inside=False, symlinks=False)

    for path in find("python", "*.py"):
        copy(path, join(project.build_dir, project.name, path), inside=False, symlinks=False)

    for dir_name in project.extra_dirs:
        for path in find(dir_name):
            copy(path, join(project.build_dir, project.name, path), inside=False, symlinks=False)

@target(requires=build)
def install():
    assert project.name
    assert is_dir(project.build_dir)

    build = read_json(join(project.build_dir, "build.json"))

    for path in find(join(project.build_dir, "bin")):
        copy(path, join(f"{build['dest_dir']}{build['prefix']}", path[6:]), inside=False, symlinks=False)

    for path in find(join(project.build_dir, project.name)):
        copy(path, join(f"{build['dest_dir']}{build['prefix']}", "lib", path[6:]), inside=False, symlinks=False)

@target
def clean():
    remove(project.build_dir)

    for path in find(".", "__pycache__"):
        remove(path)

    for path in find(".", "*.pyc"):
        remove(path)

@target(help="Initialize and update Git submodules")
def modules():
    run("git submodule update --init --remote --recursive")

@target(help="Generate shell settings for the project environment")
def env():
    assert project.name

    home_var = f"{project.name.upper()}_HOME"

    print(f"export {home_var}=$PWD/build/{project.name}")
    print("export PATH=$PWD/build/bin:$PWD/scripts:$PATH")

    if "PYTHONPATH" in ENV:
        print(f"export PYTHONPATH=${home_var}/python:$PWD/python:{ENV['PYTHONPATH']}")
    else:
        print(f"export PYTHONPATH=${home_var}/python:$PWD/python:{':'.join(_sys.path)}")

class project_env(working_env):
    def __init__(self):
        assert project.name

        bin_path = get_absolute_path("build/bin") + ":" + ENV["PATH"]
        python_path = get_absolute_path(f"build/{project.name}/python")

        super().__init__(PATH=bin_path, PYTHONPATH=python_path)
{'fraction_non_alphanumeric': 0.13481999708497303, 'fraction_numerical': 0.002040518874799592, 'mean_word_length': 3.880512091038407, 'pattern_counts': {'":': 7, '<': 3, '<?xml version=': 0, '>': 3, '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': '1732241', 'n_tokens_mistral': 2118, 'n_tokens_neox': 2094, 'n_words': 598}