-
Notifications
You must be signed in to change notification settings - Fork 3.3k
add snippet update step #26899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add snippet update step #26899
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d5eac74
add snippet update step
xiangyan99 d96a017
Get latest main (#26934)
xiangyan99 f125151
Merge branch 'main' into CI_update_snippet
xiangyan99 1240691
update
xiangyan99 dfde3d4
update
xiangyan99 815ce54
update
xiangyan99 513e95f
update
xiangyan99 590812a
update
xiangyan99 8c6ec25
Update update_snippet.yml
xiangyan99 6daaa0f
Update eng/pipelines/templates/steps/analyze.yml
xiangyan99 9687207
Update update_snippet.yml
xiangyan99 c765ed0
update
xiangyan99 65066d2
update
xiangyan99 e54fa76
update
xiangyan99 4481b4d
updates
xiangyan99 62daef1
update
xiangyan99 04557c8
Updates
xiangyan99 3a96f11
updates
xiangyan99 c0bead3
updates
xiangyan99 f322b1c
update
xiangyan99 b0a6c02
Add comments
xiangyan99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| parameters: | ||
| ServiceDirectory: '' | ||
| ValidateFormatting: false | ||
| EnvVars: {} | ||
|
|
||
| steps: | ||
| - task: UsePythonVersion@0 | ||
| displayName: 'Use Python 3.7' | ||
| inputs: | ||
| versionSpec: '3.7' | ||
|
xiangyan99 marked this conversation as resolved.
Outdated
|
||
| condition: succeededOrFailed() | ||
|
|
||
| - task: PythonScript@0 | ||
| displayName: 'Update Snippets' | ||
| inputs: | ||
| scriptPath: 'scripts/devops_tasks/python_snippet_updater.py' | ||
|
xiangyan99 marked this conversation as resolved.
Outdated
|
||
| arguments: >- | ||
| ${{ parameters.ScanPath }} | ||
| condition: and(succeededOrFailed(), ne(variables['Skip.UpdateSnippet'],'true')) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import sys | ||
| from pathlib import Path | ||
| import re | ||
|
|
||
| snippets = {} | ||
| not_up_to_date = False | ||
|
|
||
| target_snippet_sources = ["samples/*.py", "samples/*/*.py"] | ||
| target_md_files = ["README.md"] | ||
|
|
||
| def get_snippet(file): | ||
| with open(file, 'r') as f: | ||
| content = f.read() | ||
| pattern = "# \\[START[A-Z a-z0-9_]+\\][\\s\\S]+?# \\[END[A-Z a-z0-9_]+\\]" | ||
|
scbedd marked this conversation as resolved.
Outdated
|
||
| matches = re.findall(pattern, content) | ||
| for match in matches: | ||
| s = match | ||
| pos1 = s.index("[START") | ||
|
xiangyan99 marked this conversation as resolved.
Outdated
|
||
| pos2 = s.index("]") | ||
| name = s[pos1 + 6:pos2].strip() | ||
| s = s[pos2 + 1:] | ||
| pos1 = s.index("# [END") | ||
| snippet = s[:pos1-1] | ||
| # Remove extra spaces | ||
| spaces = "" | ||
| for char in snippet[1:]: | ||
| if char == " ": | ||
| spaces += char | ||
| else: | ||
| break | ||
| snippet = snippet.replace("\n" + spaces, "\n") | ||
| # Remove first newline | ||
| snippet = snippet[1:].rstrip() | ||
| if snippet[-1] == "\n": | ||
| snippet = snippet[:-1] | ||
|
|
||
| file_name = str(file.name)[:-3] | ||
| identifier = ".".join([file_name, name]) | ||
| if identifier in snippets.keys(): | ||
| print(f'Warning: found duplicated snippet name "{identifier}".') | ||
| print(file) | ||
| # print(f"Found: {file.name}.{name}") | ||
|
xiangyan99 marked this conversation as resolved.
Outdated
|
||
| snippets[identifier] = snippet | ||
|
|
||
|
|
||
| def update_snippet(file): | ||
|
xiangyan99 marked this conversation as resolved.
Outdated
|
||
| with open(file, 'r') as f: | ||
| content = f.read() | ||
| pattern = "<!-- SNIPPET:[A-Z a-z0-9_.]+-->[\\s\\S]*?<!-- END SNIPPET -->" | ||
| matches = re.findall(pattern, content) | ||
| for match in matches: | ||
| s = match | ||
| pos1 = s.index("-->") | ||
|
xiangyan99 marked this conversation as resolved.
Outdated
|
||
| header = s[:pos1+3] | ||
| name = s[13:pos1].strip() | ||
| # print(f"Info: found name: {name}") | ||
| if name not in snippets.keys(): | ||
| print(f'Warning: cannot found snippet name "{name}".') | ||
| exit(1) | ||
| target = "".join([header, "\n```python\n", snippets[name], "\n```\n", "<!-- END SNIPPET -->"]) | ||
| if s != target: | ||
| print(f'Warning: snippet "{name}" is not up to date.') | ||
| global not_up_to_date | ||
| not_up_to_date = True | ||
| content = content.replace(s, target) | ||
| with open(file, 'w') as f: | ||
| f.write(content) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| arg_len = len(sys.argv) | ||
|
xiangyan99 marked this conversation as resolved.
Outdated
|
||
| if arg_len < 2: | ||
| print(f"Usage: PythonSnippetUpdater <path>") | ||
| exit(1) | ||
| path = sys.argv[1] | ||
| print(f"Path: {path}") | ||
|
xiangyan99 marked this conversation as resolved.
Outdated
|
||
| for source in target_snippet_sources: | ||
| for py_file in Path(path).rglob(source): | ||
| try: | ||
| get_snippet(py_file) | ||
| except UnicodeDecodeError: | ||
| pass | ||
| # for key in snippets.keys(): | ||
|
xiangyan99 marked this conversation as resolved.
Outdated
|
||
| # print(f"Info: found snippet: {key}") | ||
| for target in target_md_files: | ||
| for md_file in Path(path).rglob(target): | ||
| try: | ||
| update_snippet(md_file) | ||
| except UnicodeDecodeError: | ||
| pass | ||
| if not_up_to_date: | ||
| print(f'Error: code snippets are out of sync. Please run Python PythonSnippetUpdater.py "{path}" to fix it.') | ||
| exit(1) | ||
| print(f"README.md under {path} is up to date.") | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.