Skip to content

Commit e8a36cc

Browse files
m-strzelczykgcf-owl-bot[bot]parthea
authored
chore(samples): Snippet Generating System (part 1) (#199)
* chore(samples): Snippet Generating System Preparing the SGS (Snippet Generating System) script to handle the burden of duplicated code in the samples. Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent 337cb2f commit e8a36cc

14 files changed

+598
-21
lines changed

compute/compute/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Code samples for the Compute Engine library
2+
3+
In this folder you can find the source code for the code samples used throughout the
4+
[public documentation](https://cloud.google.com/compute/docs/) of Google Compute Engine.
5+
6+
The samples can be found in the `snippets` folder, where they are organized to mimic the
7+
structure of the public documentation. Files that are saved there are generated by the `sgs.py`
8+
script from pieces found in `ingredients` and `recipes`. This way, one piece of code can be easily
9+
included in multiple snippets and updating the code requires less work.
10+
11+
## Working with the SGS
12+
13+
SGS (Snippet Generating System) works by scanning the `recipes` folder, finding all files
14+
and filling them with pieces of code found in `ingredients`. The folder structure of `recipes` is
15+
reconstructed in the `snippets` folder.
16+
17+
### Adding new sample
18+
19+
To create a new sample, just prepare a new file in one of the `recipes` subfolders. The SGS will pick it up
20+
automatically when you run it, by executing `python3 sgs.py generate` in this (`samples/`) directory.
21+
22+
### Removing/moving a sample
23+
24+
To remove or move a sample, you need to simply modify the `recipes` folder to match your desired structure, then delete
25+
the generated snippet from the `snippets` directory. The SGS script will create the snippet in the new location next
26+
time you run `python3 sgs.py generate`.
27+
28+
### Interacting with GIT
29+
30+
SGS will not interact with Git repository in any way. All changes made by the script need to be committed manually -
31+
preferably in the same commit as the update to the source files.
32+
33+
## Preparing an ingredient
34+
To add a new ingredient, create a new `.py` file with the code you want to later use in the snippets. Mark the beginning
35+
of the code you want to include with `# <INGREDIENT name>` and the end with `# </INGREDIENT>`.
36+
37+
Please leave the imports required by this ingredient **OUTSIDE** the area marked with ingredient comments. The SGS
38+
script will automatically collect all the required imports and put them in the final snippet in the right place and in
39+
right order.
40+
41+
## Preparing a recipe
42+
Each recipe is a file located in the `recipes` folder. It should have the `.py` extension and should be a valid Python
43+
script. Each recipe has to have an `# <IMPORTS/>` line and at least one `# <INGREDIENT name/>` line.
44+
Apart from those restrictions, the contents of the file can be whatever you want.
45+
46+
The SGS will copy the recipe file to the destination folder in `snippets` and replace the `# <IMPORTS/>` and
47+
`# <INGREDIENT name/>` lines with the `import` statements required by the used ingredients and with the ingredient
48+
body.
49+
50+
### Regions
51+
You should use `# <REGION region_name>` and `# </REGION region_name>` lines to indicate where start and end
52+
of a region should be placed in the generated snippet. Those lines will be simply replaced with the proper
53+
`START region_name` and `END region_name` lines.
File renamed without changes.

compute/compute/noxfile_config.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
TEST_CONFIG_OVERRIDE = {
16+
# Tests in test_sample_default_values.py require separate projects to not interfere with each other.
17+
"gcloud_project_env": "BUILD_SPECIFIC_GCLOUD_PROJECT",
18+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
pytest==7.0.1
22
flaky==3.7.0
33
google-cloud-storage==2.1.0; python_version == '3.6'
4-
google-cloud-storage==2.1.0; python_version >= '3.7'
4+
google-cloud-storage==2.1.0; python_version >= '3.7'

compute/compute/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
isort==5.10.1
2+
black==22.1.0
3+
google-cloud-compute==1.0.0

0 commit comments

Comments
 (0)