Skip to content

Commit

Permalink
Reorganize scripts
Browse files Browse the repository at this point in the history
In iree-org#568, I want to add another script that isn't git specific and doesn't really belong anywhere else, but we don't want yet another "scripts" directory. I created a top-level scripts directory and put the git-specific stuff under a "git" subdirectory, but mostly tried to place scripts next to the things they relate to. I gave bazel_to_cmake its own directory in build_tools and deleted build_tools/scripts entirely. Some of the scripts in there really had nothing to do with building and I tried to rehome them in a more appropriate place.

Because of the weirdness where we have to edit .github/workflow files separately, I did not delete git_scripts as part of this change. I'm fixing the workflow files to point at the new location in iree-org#672 and then I will delete the scripts in the old location.

PiperOrigin-RevId: 293214833
  • Loading branch information
GMNGeoffrey authored and copybara-github committed Feb 4, 2020
1 parent 0e4c985 commit b9aa31f
Show file tree
Hide file tree
Showing 19 changed files with 483 additions and 22 deletions.
File renamed without changes.
1 change: 1 addition & 0 deletions build_tools/bazel_to_cmake/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# Bazel rules (e.g. cc_library -> iree_cc_library.cmake).
#
# For usage, see:
# python3 build_tools/scripts/bazel_to_cmake.py --help
# python3 build_tools/bazel_to_cmake/bazel_to_cmake.py --help

import argparse
import bazel_to_cmake_targets
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion colab/mnist_tensorflow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"* Refer to [iree/docs/using_colab.md](https://github.com/google/iree/blob/master/docs/using_colab.md) for general information\n",
"* Ensure that you have a recent version of TensorFlow 2.0 [installed on your system](https://www.tensorflow.org/install)\n",
"* Enable IREE/TF integration by adding to your user.bazelrc: `build --define=iree_tensorflow=true`\n",
"* Start colab by running `python build_tools/scripts/start_colab_kernel.py` (see that file for additional instructions)\n",
"* Start colab by running `python colab/start_colab_kernel.py` (see that file for additional instructions)\n",
"* Note: you may need to restart your runtime in order to re-run certain cells. Some of the APIs are not yet stable enough for repeated invocations"
]
},
Expand Down
2 changes: 1 addition & 1 deletion colab/simple_tensorflow_module_import.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"* [Install a TensorFlow2 nightly pip](https://www.tensorflow.org/install) (or bring your own)\n",
"* Enable IREE/TF integration by adding to your user.bazelrc: `build --define=iree_tensorflow=true`\n",
"* *Optional:* Prime the build: `bazel build bindings/python/pyiree`\n",
"* Start colab by running `python build_tools/scripts/start_colab_kernel.py` (see that file for initial setup instructions)\n",
"* Start colab by running `python colab/start_colab_kernel.py` (see that file for initial setup instructions)\n",
"\n",
"## TODO:\n",
"\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.

# Usage:
# python3 build_tools/scripts/start_colab_kernel.py
# python3 colab/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.
Expand Down
2 changes: 1 addition & 1 deletion docs/getting_started_on_linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ your use.
Note that if you will be cloning frequently, it can be sped up significantly by
creating a reference repo and setting
`IREE_CLONE_ARGS="--reference=/path/to/reference/repo"`. See
`build_tools/scripts/populate_reference_repo.sh` for further details.
`scripts/git/populate_reference_repo.sh` for further details.

```shell
IREE_CLONE_ARGS=""
Expand Down
2 changes: 1 addition & 1 deletion docs/getting_started_on_windows.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ your use.
Note that if you will be cloning frequently, it can be sped up significantly by
creating a reference repo and setting
`IREE_CLONE_ARGS="--reference=/path/to/reference/repo"`. See
`build_tools/scripts/populate_reference_repo.sh` for further details.
`scripts/git/populate_reference_repo.sh` for further details.

```shell
IREE_CLONE_ARGS=""
Expand Down
22 changes: 11 additions & 11 deletions docs/repository_management.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ Shortcut commands (read below for full documentation):

```shell
# Update SUBMODULE_VERSIONS from current git submodule pointers
./git_scripts/submodule_versions.py export
./scripts/git/submodule_versions.py export

# Update current git submodule pointers based on SUBMODULE_VERSIONS
./git_scripts/submodule_versions.py import
./scripts/git/submodule_versions.py import

# Bump TensorFlow and LLVM to (TensorFlow) head.
# (Also updates SUBMODULE_VERSIONS).
./git_scripts/update_tf_llvm_submodules.py
./scripts/git/update_tf_llvm_submodules.py
```

### The special relationship with LLVM and TensorFlow
Expand Down Expand Up @@ -91,7 +91,7 @@ To generate it, run:
```shell
# Performs a submodule sync+update and stages an updated SUBMODULE_VERSIONS
# file.
./git_scripts/submodule_versions.py export
./scripts/git/submodule_versions.py export
```

If you don't know if this is required, you may run:
Expand All @@ -100,7 +100,7 @@ If you don't know if this is required, you may run:
# The check command is intended to eventually be usable as a git hook
# for verification of consistency between SUBMODULE_VERSIONS and the
# corresponding local git state.
./git_scripts/submodule_versions.py check
./scripts/git/submodule_versions.py check
```

#### Pulling dependency changes
Expand All @@ -111,7 +111,7 @@ the current git state:
```shell
# Updates the commit hash of any entries in SUBMODULE_VERSIONS that differ
# and stages the changes.
./git_scripts/submodule_versions.py import
./scripts/git/submodule_versions.py import
```

This will stage any needed changes to the submodules to bring them up to date
Expand All @@ -133,19 +133,19 @@ TODO(laurenzo): Add a GitHub hook to auto-commit submodule updates on
# third_party/llvm-project to the commit that tensorflow references.
# Also updates the IREE mirrors of key build files. All changes will
# be staged for commit.
./git_scripts/update_tf_llvm_submodules.py
./scripts/git/update_tf_llvm_submodules.py

# You can also select various other options:
# Don't update the tensorflow commit, just bring LLVM to the correct
# linked commit.
./git_scripts/update_tf_llvm_submodules.py --tensorflow_commit=KEEP
./scripts/git/update_tf_llvm_submodules.py --tensorflow_commit=KEEP

# Don't update the LLVM commit.
./git_scripts/update_tf_llvm_submodules.py --llvm_commit=KEEP
./scripts/git/update_tf_llvm_submodules.py --llvm_commit=KEEP

# Update to a specific TensorFlow commit (and corresponding LLVM).
./git_scripts/update_tf_llvm_submodules.py --tensorflow_commit=<somehash>
./scripts/git/update_tf_llvm_submodules.py --tensorflow_commit=<somehash>

# Update to a specific LLVM commit.
./git_scripts/update_tf_llvm_submodules.py --llvm_commit=<somehash>
./scripts/git/update_tf_llvm_submodules.py --llvm_commit=<somehash>
```
2 changes: 1 addition & 1 deletion docs/using_colab.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ out e2e flows that cross the ML framework, compiler and runtime interactively.
Run:

```shell
./build_tools/scripts/start_colab_kernel.py
./colab/start_colab_kernel.py
```

This will start a jupyter notebook on port 8888. Then navigate to
Expand Down
4 changes: 2 additions & 2 deletions kokoro/gcp_ubuntu/bazel/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export PYTHON_BIN="$(which python3)"
# Kokoro checks out the repository here.
cd ${KOKORO_ARTIFACTS_DIR?}/github/iree
echo "Initializing submodules"
./git_scripts/submodule_versions.py init
./scripts/git/submodule_versions.py init

echo "Building and testing with bazel"
./build_tools/scripts/bazel_build.sh
./build_tools/bazel/build.sh
4 changes: 2 additions & 2 deletions kokoro/gcp_ubuntu/cmake/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export CXX="$(which clang++-6.0)"
# Kokoro checks out the repository here.
cd ${KOKORO_ARTIFACTS_DIR?}/github/iree
echo "Initializing submodules"
./git_scripts/submodule_versions.py init
./scripts/git/submodule_versions.py init

echo "Building and testing with cmake"
./build_tools/scripts/cmake_build.sh
./build_tools/cmake/build.sh
1 change: 1 addition & 0 deletions scripts/git/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

File renamed without changes.
193 changes: 193 additions & 0 deletions scripts/git/submodule_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
#!/usr/bin/env python3
# Lint as: 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.

# pylint: disable=missing-docstring
"""submodule_versions.
Synchronizes the tracked SUBMODULE_VERSIONS file with the submodule state
in git.
Typical usage:
--------------
Exporting current git submodule state to SUBMODULE_VERSIONS:
Syntax: ./scripts/git/submodule_versions.py export
Importing versions in SUBMODULE_VERSIONS to git submodule state:
Syntax: ./scripts/git/submodule_versions.py import
Checking whether SUBMODULE_VERSIONS and git state are in sync:
Syntax: ./scripts/git/submodule_versions.py check
"""

import argparse
import os
import re
import sys

import utils

VERSIONS_FILE = "SUBMODULE_VERSIONS"


def get_submodule_versions(repo_dir):
raw_status = utils.execute(["git", "submodule", "status"],
cwd=repo_dir,
silent=True,
capture_output=True).decode("UTF-8")
status_lines = []
for line in raw_status.splitlines():
# Format is a status char followed by revision, space and path.
m = re.match(r"""^.([0-9a-z]+)\s+([^\s]+)""", line)
if m:
# Output as just the commit hash followed by space and path.
status_lines.append(m.group(1) + " " + m.group(2))
return "\n".join(status_lines) + "\n"


def export_versions(repo_dir):
current_versions = get_submodule_versions(repo_dir)
versions_file_path = os.path.join(repo_dir, VERSIONS_FILE)
print("*** Exporting current submodule versions to:", versions_file_path)
with open(versions_file_path, "w", encoding="UTF-8") as f:
f.write(current_versions)
utils.execute(["git", "add", VERSIONS_FILE], cwd=repo_dir)


def parse_versions(versions_text):
versions = dict()
for line in versions_text.splitlines():
comps = line.split(" ", maxsplit=2)
if len(comps) != 2:
continue
versions[comps[1]] = comps[0]
return versions


def get_diff_versions(repo_dir):
current_versions = parse_versions(get_submodule_versions(repo_dir))
with open(os.path.join(repo_dir, VERSIONS_FILE), "r", encoding="UTF-8") as f:
written_versions = parse_versions(f.read())
diff_versions = current_versions.items() ^ written_versions.items()
return {
k: (current_versions.get(k), written_versions.get(k))
for k, _ in diff_versions
}


def sync_and_update_submodules(repo_dir):
print("*** Synchronizing/updating submodules")
utils.execute(["git", "submodule", "sync"], cwd=repo_dir)
utils.execute(["git", "submodule", "update"], cwd=repo_dir)


def import_versions(repo_dir):
print("*** Importing versions to git submodule state")
diff_versions = get_diff_versions(repo_dir)
if not diff_versions:
print("*** No submodule updates required")
return
for path, (current, written) in diff_versions.items():
if current is None:
print(("Warning: Submodule %s does not exist but is "
"still in the version file") % (path,))
continue
if written is None:
print("Warning: Submodule %s is not in the version file" % (current,))
continue
# Directly update the submodule commit hash in the index.
# See: https://stackoverflow.com/questions/33514642
command = ["git", "update-index", "--cacheinfo", "160000", written, path]
print("Updating", path, "to", written)
utils.execute(command, cwd=repo_dir)


def init_submodules(repo_dir):
print("*** Initializing submodules")
utils.execute(["git", "submodule", "init"], cwd=repo_dir)


def parallel_shallow_update_submodules(repo_dir):
print("*** Making shallow clone of submodules")
# TODO(gcmn) Figure out a way to quickly fetch submodules without relying on
# target SHA being within 10000 commits of HEAD.
magic_depth = 10000
utils.execute([
"git", "submodule", "update", "--jobs", "8", "--depth",
str(magic_depth)
],
cwd=repo_dir)


def check_submodule_versions(repo_dir):
diff_versions = get_diff_versions(repo_dir)
if diff_versions:
print(
"Submodule state differs from SUBMODULE_VERSIONS file. Run (and commit) one of:"
)
print(
" ./scripts/git/submodule_versions.py import # Use version in SUBMODULE_VERSIONS"
)
print(
" ./scripts/git/submodule_versions.py export # Use version in git state"
)
for k, (current, written) in diff_versions.items():
print("%s : actual=%s written=%s" % (k, current, written))
return False
return True


def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("--repo", help="Repository root directory")
parser.add_argument(
"command", help="Command to run (show|import|export|check|init)")
args = parser.parse_args()

# Default repo path.
if args.repo is None:
args.repo = utils.find_git_toplevel()
return args


def main(args):
if args.command == "show":
print(get_submodule_versions(args.repo))
elif args.command == "export":
sync_and_update_submodules(args.repo)
export_versions(args.repo)
elif args.command == "check":
if not check_submodule_versions(args.repo):
sys.exit(1)
elif args.command == "import":
import_versions(args.repo)
sync_and_update_submodules(args.repo)
elif args.command == "init":
init_submodules(args.repo)
# Redundant, since import_versions will only update if they differ,
# but good to only print output about the import if it's actually
# needed.
if not check_submodule_versions(args.repo):
print("Warning: git submodule state does not match SUBMODULE_VERSIONS. "
"Using state in SUBMODULE_VERSIONS")
import_versions(args.repo)
parallel_shallow_update_submodules(args.repo)
else:
print("Unrecognized command:", args.command)
sys.exit(1)


if __name__ == "__main__":
main(parse_arguments())
Loading

0 comments on commit b9aa31f

Please sign in to comment.