diff --git a/dgf/src/bin/BUILD b/dgf/src/bin/BUILD new file mode 100644 index 0000000..4b67462 --- /dev/null +++ b/dgf/src/bin/BUILD @@ -0,0 +1,35 @@ +load("//devtools/python/blaze:pytype.bzl", "py_binary", "py_test") + +package( + + default_visibility = ["//dgf/src:__subpackages__"], +) + +# Binary +# ========= + +py_binary( + name = "validate_graph", + srcs = ["validate_graph.py"], + test_lib = True, + deps = [ + # absl:app dep, + # absl/flags dep, + "//dgf", + ], +) + +# Test +# ========= + +py_test( + name = "validate_graph_test", + srcs = ["validate_graph_test.py"], + deps = [ + ":validate_graph.testonly_lib", + # absl/flags dep, + # absl/testing:absltest dep, + # absl/testing:flagsaver dep, + "//dgf/src/util:gen_test_graph", + ], +) diff --git a/dgf/src/bin/README.md b/dgf/src/bin/README.md new file mode 100644 index 0000000..1b5a0bc --- /dev/null +++ b/dgf/src/bin/README.md @@ -0,0 +1,14 @@ +# DGF Binary Tools + +This directory contains DGF tools and utilities that can be executed directly +as binaries without creating a custom Python script. + +## Running Existing Tools + +### PIP Package +Once the `dgf` package is installed, tools can be executed directly using the +CLI: + +```shell +dgf-something --flag=1 +``` diff --git a/dgf/src/bin/validate_graph.py b/dgf/src/bin/validate_graph.py new file mode 100644 index 0000000..f17927a --- /dev/null +++ b/dgf/src/bin/validate_graph.py @@ -0,0 +1,57 @@ +# Copyright 2022 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. + +"""Loads a graph in memory and validates it. + +Usage example: + +# Pip +dgf-validate-graph --path=... +""" + +from absl import app +from absl import flags +import dgf + +FLAGS = flags.FLAGS + +flags.DEFINE_string("path", None, "Path to the DGF graph.", required=True) +flags.DEFINE_integer("verbose", 1, "Verbose level.") +flags.DEFINE_bool( + "raise_on_warning", False, "Whether to raise an error on warnings." +) +flags.DEFINE_bool( + "raise_on_error", True, "Whether to raise an error on errors." +) + + +def main(argv): + if len(argv) > 1: + raise app.UsageError("Too many command-line arguments.") + + print(f"Loading graph from {FLAGS.path}...") + graph, schema = dgf.io.read_graph(FLAGS.path, verbose=FLAGS.verbose) + + print("Validating graph...") + dgf.validate.validate_graph( + graph, + schema, + raise_on_warning=FLAGS.raise_on_warning, + raise_on_error=FLAGS.raise_on_error, + ) + print("Graph validation successful!") + + +if __name__ == "__main__": + app.run(main) diff --git a/dgf/src/bin/validate_graph_test.py b/dgf/src/bin/validate_graph_test.py new file mode 100644 index 0000000..f610996 --- /dev/null +++ b/dgf/src/bin/validate_graph_test.py @@ -0,0 +1,39 @@ +# Copyright 2022 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. + +import os +from absl import flags +from absl.testing import absltest +from absl.testing import flagsaver +from dgf.src.bin import validate_graph +from dgf.src.util import gen_test_graph + +flags.FLAGS.set_default("path", "/dummy/path") + + +class ValidateGraphTest(absltest.TestCase): + + def setUp(self): + super().setUp() + self.test_dir = self.create_tempdir().full_path + self.graph_path = os.path.join(self.test_dir, "test_graph") + gen_test_graph.generate_gf_graph(self.graph_path, edge_ids=False) + + def test_validate_graph_success(self): + with flagsaver.flagsaver(path=self.graph_path, raise_on_error=True): + validate_graph.main([]) + + +if __name__ == "__main__": + absltest.main() diff --git a/setup.py b/setup.py index e5837da..41d626d 100644 --- a/setup.py +++ b/setup.py @@ -47,6 +47,11 @@ def is_pure(self): "Source": "https://github.com/google/dgf.git", "Tracker": "https://github.com/google/dgf/issues", }, + entry_points={ + "console_scripts": [ + "dgf-validate-graph=dgf.src.bin:validate_graph", + ], + }, license="Apache 2.0", distclass=BinaryDistribution, include_package_data=True,