You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by pa...@apache.org on 2021/12/10 16:30:07 UTC

[beam] branch master updated: add type of precompiled object

This is an automated email from the ASF dual-hosted git repository.

pabloem pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/master by this push:
     new a000621  add type of precompiled object
     new bb38872  Merge pull request #16184 from [BEAM-13413][Playground][Bugfix] add type of precompiled object
a000621 is described below

commit a00062186e88f7a521d79a24485f7f93c9d6bd73
Author: daria-malkova <da...@akvelon.com>
AuthorDate: Thu Dec 9 15:35:07 2021 +0300

    add type of precompiled object
---
 playground/infrastructure/cd_helper.py   |  4 +++-
 playground/infrastructure/config.py      |  7 ++++++
 playground/infrastructure/helper.py      | 40 +++++++++++++++++++++++++++++---
 playground/infrastructure/test_helper.py | 21 +++++++++++++----
 4 files changed, 64 insertions(+), 8 deletions(-)

diff --git a/playground/infrastructure/cd_helper.py b/playground/infrastructure/cd_helper.py
index 5d51986..81ff58f 100644
--- a/playground/infrastructure/cd_helper.py
+++ b/playground/infrastructure/cd_helper.py
@@ -116,7 +116,9 @@ class CDHelper:
         extension=PrecompiledExample.META_EXTENSION)
     file_names[code_path] = example.code
     file_names[output_path] = example.output
-    file_names[meta_path] = json.dumps(example.tag._asdict())
+    meta = example.tag._asdict()
+    meta["type"] = example.type
+    file_names[meta_path] = json.dumps(meta)
     for file_name, file_content in file_names.items():
       local_file_path = os.path.join(
           Config.TEMP_FOLDER, example.pipeline_id, file_name)
diff --git a/playground/infrastructure/config.py b/playground/infrastructure/config.py
index 6acda79..418c34e 100644
--- a/playground/infrastructure/config.py
+++ b/playground/infrastructure/config.py
@@ -63,3 +63,10 @@ class PrecompiledExample:
   OUTPUT_EXTENSION = "output"
   META_NAME = "meta"
   META_EXTENSION = "info"
+
+
+@dataclass(frozen=True)
+class PrecompiledExampleType:
+  examples = "examples"
+  katas = "katas"
+  test_ends = ("test", "it")
\ No newline at end of file
diff --git a/playground/infrastructure/helper.py b/playground/infrastructure/helper.py
index 2d25367..1f396ce 100644
--- a/playground/infrastructure/helper.py
+++ b/playground/infrastructure/helper.py
@@ -29,8 +29,10 @@ from yaml import YAMLError
 
 from api.v1.api_pb2 import SDK_UNSPECIFIED, STATUS_UNSPECIFIED, Sdk, \
   STATUS_VALIDATING, STATUS_PREPARING, \
-  STATUS_COMPILING, STATUS_EXECUTING
-from config import Config, TagFields
+  STATUS_COMPILING, STATUS_EXECUTING, PRECOMPILED_OBJECT_TYPE_UNIT_TEST, \
+  PRECOMPILED_OBJECT_TYPE_KATA, PRECOMPILED_OBJECT_TYPE_UNSPECIFIED, \
+  PRECOMPILED_OBJECT_TYPE_EXAMPLE, PrecompiledObjectType
+from config import Config, TagFields, PrecompiledExampleType
 from grpc_client import GRPCClient
 
 Tag = namedtuple(
@@ -57,6 +59,7 @@ class Example:
   output: str
   status: STATUS_UNSPECIFIED
   tag: Tag
+  type: PrecompiledObjectType = PRECOMPILED_OBJECT_TYPE_UNSPECIFIED
 
 
 def find_examples(work_dir: str,
@@ -213,11 +216,20 @@ def _get_example(filepath: str, filename: str, tag: dict) -> Example:
   """
   name = _get_name(filename)
   sdk = _get_sdk(filename)
+  object_type = _get_object_type(filename, filepath)
   with open(filepath, encoding="utf-8") as parsed_file:
     content = parsed_file.read()
 
   return Example(
-      name, "", sdk, filepath, content, "", STATUS_UNSPECIFIED, Tag(**tag))
+      name,
+      "",
+      sdk,
+      filepath,
+      content,
+      "",
+      STATUS_UNSPECIFIED,
+      Tag(**tag),
+      object_type)
 
 
 def _validate(tag: dict, supported_categories: List[str]) -> bool:
@@ -346,3 +358,25 @@ async def _update_example_status(example: Example, client: GRPCClient):
     await asyncio.sleep(Config.PAUSE_DELAY)
     status = await client.check_status(pipeline_id)
   example.status = status
+
+
+def _get_object_type(filename, filepath):
+  """
+  Get type of an object based on it filename/filepath
+
+  Args:
+      filename: object's filename
+      filepath: object's filepath
+
+  Returns: type of the object (example, kata, unit-test)
+  """
+  filename_no_ext = (os.path.splitext(filename)[0]).lower()
+  if filename_no_ext.endswith(PrecompiledExampleType.test_ends):
+    object_type = PRECOMPILED_OBJECT_TYPE_UNIT_TEST
+  elif PrecompiledExampleType.katas in filepath.split(os.sep):
+    object_type = PRECOMPILED_OBJECT_TYPE_KATA
+  elif PrecompiledExampleType.examples in filepath.split(os.sep):
+    object_type = PRECOMPILED_OBJECT_TYPE_EXAMPLE
+  else:
+    object_type = PRECOMPILED_OBJECT_TYPE_UNSPECIFIED
+  return object_type
diff --git a/playground/infrastructure/test_helper.py b/playground/infrastructure/test_helper.py
index a556f8a..bb21240 100644
--- a/playground/infrastructure/test_helper.py
+++ b/playground/infrastructure/test_helper.py
@@ -19,12 +19,12 @@ import mock
 import pytest
 
 from api.v1.api_pb2 import SDK_UNSPECIFIED, STATUS_UNSPECIFIED, SDK_JAVA, \
-  SDK_PYTHON, SDK_GO, STATUS_VALIDATING, \
-  STATUS_FINISHED
+    SDK_PYTHON, SDK_GO, STATUS_VALIDATING, \
+    STATUS_FINISHED, PRECOMPILED_OBJECT_TYPE_EXAMPLE, PRECOMPILED_OBJECT_TYPE_KATA, PRECOMPILED_OBJECT_TYPE_UNIT_TEST
 from grpc_client import GRPCClient
 from helper import find_examples, Example, _get_example, _get_name, _get_sdk, \
-  get_tag, _validate, Tag, get_statuses, \
-  _update_example_status, get_supported_categories, _check_file
+    get_tag, _validate, Tag, get_statuses, \
+    _update_example_status, get_supported_categories, _check_file, _get_object_type
 
 
 @mock.patch("helper._check_file")
@@ -285,3 +285,16 @@ async def test__update_example_status(
   assert example.status == STATUS_FINISHED
   mock_grpc_client_run_code.assert_called_once_with(example.code, example.sdk)
   mock_grpc_client_check_status.assert_has_calls([mock.call("pipeline_id")])
+
+
+def test__get_object_type():
+  result_example = _get_object_type(
+      "filename.extension", "filepath/examples/filename.extension")
+  result_kata = _get_object_type(
+      "filename.extension", "filepath/katas/filename.extension")
+  result_test = _get_object_type(
+      "filename_test.extension", "filepath/examples/filename_test.extension")
+
+  assert result_example == PRECOMPILED_OBJECT_TYPE_EXAMPLE
+  assert result_kata == PRECOMPILED_OBJECT_TYPE_KATA
+  assert result_test == PRECOMPILED_OBJECT_TYPE_UNIT_TEST