<commit_before>#!/usr/bin/python3
# Copyright 2019 Google LLC
#
# 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
#
#      https://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.

# Usage:
#   build_tools/scripts/start_colab_kernel.py
# This will build the python bindings and start a colab kernel with them
# on the path. It takes some care to ensure that the build is running with
# the same python interpreter as is used to launch this script.
#
# Pre-requisites:
# Install Jupyter (from https://jupyter.org/install)
#   python3 -m pip install --upgrade pip
#   python3 -m pip install jupyter
# Setup colab (https://research.google.com/colaboratory/local-runtimes.html)
#   python3 -m pip install jupyter_http_over_ws
#   jupyter serverextension enable --py jupyter_http_over_ws

import os
import subprocess
import sys

repo_root = None
bazel_env = dict(os.environ)
bazel_bin = None


def setup_environment():
  """Sets up some environment globals."""
  global bazel_bin
  global repo_root

  # Determine the repository root (two dir-levels up).
  repo_root = os.path.dirname(
      os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  print("Repository root: %s" % (repo_root,))

  # Detect python and query bazel for its output.
  print("Setting Bazel PYTHON_BIN=%s" % (sys.executable,))
  bazel_env["PYTHON_BIN"] = sys.executable
  bazel_bin = subprocess.check_output(["bazel", "info", "bazel-bin"],
                                      cwd=repo_root,
                                      env=bazel_env).decode("utf-8")
  bazel_bin = bazel_bin.splitlines()[0]
  print("Found Bazel bin: %s" % (bazel_bin))


def build():
  """Builds the python bundle."""
  print("Building python bindings...")
  subprocess.check_call(
      ["bazel", "build", "//bindings/python/pyiree:everything_for_colab"],
      cwd=repo_root,
      env=bazel_env)


def run():
  """Runs the Jupyter notebook."""
  runfiles_dir = os.path.join(bazel_bin, "bindings", "python", "pyiree",
                              "everything_for_colab.runfiles")
  # Top level directories under the runfiles get added to the sys path.
  extra_python_path = []
  # The iree_core/bindings/python directory under runfiles needs to come
  # first on the path.
  extra_python_path.append(
      os.path.join(runfiles_dir, "iree_core", "bindings", "python"))<commit_msg>Tweaks to make colab work on windows.<commit_after>#!/usr/bin/env python3
# Copyright 2019 Google LLC
#
# 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
#
#      https://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.

# Usage:
#   python3 build_tools/scripts/start_colab_kernel.py
#
# Note that in the case that multiple python interpreters are present on
# your path, it is best to not risk it: use an explicit one.
#
# This will build the python bindings and start a colab kernel with them
# on the path. It takes some care to ensure that the build is running with
# the same python interpreter as is used to launch this script.
#
# Pre-requisites:
# Install Jupyter (from https://jupyter.org/install)
#   python3 -m pip install --upgrade pip
#   python3 -m pip install jupyter
# Setup colab (https://research.google.com/colaboratory/local-runtimes.html)
#   python3 -m pip install jupyter_http_over_ws
#   jupyter serverextension enable --py jupyter_http_over_ws

import os
import subprocess
import sys

repo_root = None
bazel_env = dict(os.environ)
bazel_bin = None


def setup_environment():
  """Sets up some environment globals."""
  global bazel_bin
  global repo_root

  # Determine the repository root (two dir-levels up).
  repo_root = os.path.dirname(
      os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  print("Repository root: %s" % (repo_root,))

  # Detect python and query bazel for its output.
  print("Setting Bazel PYTHON_BIN=%s" % (sys.executable,))
  bazel_env["PYTHON_BIN"] = sys.executable
  bazel_bin = subprocess.check_output(["bazel", "info", "bazel-bin"],
                                      cwd=repo_root,
                                      env=bazel_env).decode("utf-8")
  bazel_bin = bazel_bin.splitlines()[0]
  # Bazel always reports the path with '/'. On windows, switch it
  # since we need native path manipulation code below to have it the
  # right way.
  if os.path.sep == "\\":
    bazel_bin = bazel_bin.replace("/", "\\")
  print("Found Bazel bin: %s" % (bazel_bin))


def build():
  """Builds the python bundle."""
  print("Building python bindings...")
  subprocess.check_call(
      ["bazel", "build", "//bindings/python/pyiree:everything_for_colab"],
      cwd=repo_root,
      env=bazel_env)


def run():
  """Runs the Jupyter notebook."""
  runfiles_suffix = ".runfiles"
  if os.path.sep == "\\":
    runfiles_suffix = ".exe.runfiles"  # Windows uses a special name

  runfiles_dir = os.path.join(bazel_bin, "bindings", "python", "pyiree",
                              "everything_for_colab" + runfiles_suffix)
  # Top level directories under the runfiles get added to the sys path.
  extra_python_path = []
  # The iree_core/bindings/python directory under runfiles needs to come
  # first on the path.
  extra_python_path.append(
      os.path.join(runfiles_dir, "iree_core", "bindings", "python"))
{'fraction_non_alphanumeric': 0.10527182866556836, 'fraction_numerical': 0.0047775947281713345, 'mean_word_length': 3.533980582524272, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6875791', 'n_tokens_mistral': 1964, 'n_tokens_neox': 1882, 'n_words': 620}