Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions dgf/src/bin/BUILD
Original file line number Diff line number Diff line change
@@ -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",
],
)
14 changes: 14 additions & 0 deletions dgf/src/bin/README.md
Original file line number Diff line number Diff line change
@@ -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
```
57 changes: 57 additions & 0 deletions dgf/src/bin/validate_graph.py
Original file line number Diff line number Diff line change
@@ -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)
39 changes: 39 additions & 0 deletions dgf/src/bin/validate_graph_test.py
Original file line number Diff line number Diff line change
@@ -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()
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down