Repository: awsdocs/aws-doc-sdk-examples-tools

Issue:
Remove snippet tag lines from extracted snippet text.
Also SPDX headers

Discussion:

Patch:
diff --git a/aws_doc_sdk_examples_tools/doc_gen.py b/aws_doc_sdk_examples_tools/doc_gen.py
index 05ccd31..3c1c803 100644
--- a/aws_doc_sdk_examples_tools/doc_gen.py
+++ b/aws_doc_sdk_examples_tools/doc_gen.py
@@ -195,7 +195,7 @@ class DocGen:
                 entities_config = yaml.safe_load(file)
             for entity, expanded in entities_config["expanded_override"].items():
                 self.entities[entity] = expanded
-        except Exception as e:
+        except Exception:
             pass
 
         metadata = root / ".doc_gen/metadata"
diff --git a/aws_doc_sdk_examples_tools/snippets.py b/aws_doc_sdk_examples_tools/snippets.py
index b22d2c2..f438794 100644
--- a/aws_doc_sdk_examples_tools/snippets.py
+++ b/aws_doc_sdk_examples_tools/snippets.py
@@ -15,6 +15,7 @@ from .project_validator import (
     verify_no_secret_keys,
     ValidationConfig,
 )
+from . import validator_config
 
 SNIPPET_START = "snippet-start:["
 SNIPPET_END = "snippet-end:["
@@ -213,10 +214,40 @@ def collect_snippet_files(
                                 file=snippet_file,
                                 line_start=0,
                                 line_end=len(code),
-                                code="".join(code),
+                                code="".join(
+                                    strip_snippet_tags(strip_spdx_header(code))
+                                ),
                             )
 
 
+def strip_snippet_tags(lines: List[str]) -> List[str]:
+    return [line for line in lines if not has_snippet_tag_or_spdx_header(line)]
+
+
+def strip_spdx_header(file: List[str]) -> List[str]:
+    [a, b, c, *_] = file + [""] * 3
+    has_copyright = (
+        re.match(validator_config.SPDX_LEADER + validator_config.SPDX_COPYRIGHT, a)
+        is not None
+    )
+    has_license = (
+        re.match(validator_config.SPDX_LEADER + validator_config.SPDX_LICENSE, b)
+        is not None
+    )
+    has_spacer = "" == c
+    if has_copyright and has_license:
+        if has_spacer:
+            return file[3:]
+        else:
+            return file[2:]
+    else:
+        return file
+
+
+def has_snippet_tag_or_spdx_header(line: str) -> bool:
+    return "snippet-start" in line or "snippet-end" in line
+
+
 @dataclass
 class MissingSnippet(MetadataError):
     tag: Optional[str] = None


Test patch:
diff --git a/aws_doc_sdk_examples_tools/doc_gen_test.py b/aws_doc_sdk_examples_tools/doc_gen_test.py
index a5dfb15..3d0795c 100644
--- a/aws_doc_sdk_examples_tools/doc_gen_test.py
+++ b/aws_doc_sdk_examples_tools/doc_gen_test.py
@@ -172,3 +172,14 @@ def test_doc_gen_encoder(sample_doc_gen: DocGen):
     # Verify examples (empty in this case)
     assert "examples" in decoded
     assert decoded["examples"] == {}
+
+
+def test_doc_gen_load_snippets():
+    errors = MetadataErrors()
+    doc_gen = DocGen(Path(), errors).for_root(
+        Path(__file__).parent / "test_resources", incremental=False
+    )
+    doc_gen.process_metadata(doc_gen.root / "valid_metadata.yaml")
+    doc_gen.collect_snippets()
+    assert doc_gen.snippet_files == set(["snippet_file.txt"])
+    assert doc_gen.snippets["snippet_file.txt"].code == "Line A\nLine C\n"
diff --git a/aws_doc_sdk_examples_tools/metadata_test.py b/aws_doc_sdk_examples_tools/metadata_test.py
index 70042a1..48875e3 100644
--- a/aws_doc_sdk_examples_tools/metadata_test.py
+++ b/aws_doc_sdk_examples_tools/metadata_test.py
@@ -533,7 +533,7 @@ def test_verify_load_successful():
                             "php.snippet.tag.1",
                             "php.snippet.tag.2",
                         ],
-                        snippet_files=[],
+                        snippet_files=["snippet_file.txt"],
                     )
                 ],
                 add_services={},
diff --git a/aws_doc_sdk_examples_tools/snippets_test.py b/aws_doc_sdk_examples_tools/snippets_test.py
index 178ffb9..d18d579 100644
--- a/aws_doc_sdk_examples_tools/snippets_test.py
+++ b/aws_doc_sdk_examples_tools/snippets_test.py
@@ -1,3 +1,6 @@
+# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
+# SPDX-License-Identifier: Apache-2.0
+
 import pytest
 from pathlib import Path
 
@@ -65,3 +68,37 @@ def test_verify_snippet_start_end(file_contents: str, expected_error_count: int)
     _, errors = snippets.parse_snippets(file_contents.split("\n"), Path("test"), "")
     error_count = len(errors)
     assert error_count == expected_error_count
+
+
+def test_strip_snippet_tags():
+    assert ["Line A", "Line C"] == snippets.strip_snippet_tags(
+        [
+            "Line A",
+            "# snippet-start:[line b]",
+            "Line C",
+            "# snippet-end:[line d]",
+            "line E # snippet-end",
+        ]
+    )
+
+
+def test_strip_spdx_header():
+    assert ["Line A", "Line B"] == snippets.strip_spdx_header(
+        [
+            "# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.",
+            "# SPDX-License-Identifier: Apache-2.0",
+            "Line A",
+            "Line B",
+        ]
+    )
+    assert ["Line A", "Line B"] == snippets.strip_spdx_header(
+        [
+            "# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.",
+            "# SPDX-License-Identifier: Apache-2.0",
+            "",
+            "Line A",
+            "Line B",
+        ]
+    )
+
+    assert [] == snippets.strip_spdx_header([])
diff --git a/aws_doc_sdk_examples_tools/test_resources/snippet_file.txt b/aws_doc_sdk_examples_tools/test_resources/snippet_file.txt
new file mode 100644
index 0000000..87945c1
--- /dev/null
+++ b/aws_doc_sdk_examples_tools/test_resources/snippet_file.txt
@@ -0,0 +1,5 @@
+Line A
+# snippet-start:[line b]
+Line C
+# snippet-end:[line d]
+line E # snippet-end
\ No newline at end of file
diff --git a/aws_doc_sdk_examples_tools/test_resources/valid_metadata.yaml b/aws_doc_sdk_examples_tools/test_resources/valid_metadata.yaml
index 488121e..3ae4175 100644
--- a/aws_doc_sdk_examples_tools/test_resources/valid_metadata.yaml
+++ b/aws_doc_sdk_examples_tools/test_resources/valid_metadata.yaml
@@ -36,5 +36,7 @@ medical-imaging_TestExample:
               snippet_tags:
                 - php.snippet.tag.1
                 - php.snippet.tag.2
+              snippet_files:
+                - snippet_file.txt
   services:
-    medical-imaging:
\ No newline at end of file
+    medical-imaging: