Anthonios Partheniou | aff037a | 2021-04-21 11:00:09 -0400 | [diff] [blame] | 1 | # Copyright 2021 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 | # https://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 | |
| 16 | import pathlib |
| 17 | import shutil |
| 18 | import subprocess |
| 19 | import tempfile |
| 20 | |
| 21 | import describe |
| 22 | import changesummary |
| 23 | |
| 24 | |
| 25 | SCRIPTS_DIR = pathlib.Path(__file__).parent.resolve() |
| 26 | DISCOVERY_DOC_DIR = SCRIPTS_DIR / ".." / "googleapiclient" / "discovery_cache" / "documents" |
| 27 | REFERENCE_DOC_DIR = SCRIPTS_DIR / ".." / "docs" / "dyn" |
| 28 | TEMP_DIR = SCRIPTS_DIR / "temp" |
| 29 | |
| 30 | # Clear discovery documents and reference documents directory |
| 31 | shutil.rmtree(DISCOVERY_DOC_DIR, ignore_errors=True) |
| 32 | shutil.rmtree(REFERENCE_DOC_DIR, ignore_errors=True) |
| 33 | |
| 34 | # Clear temporary directory |
| 35 | shutil.rmtree(TEMP_DIR, ignore_errors=True) |
| 36 | |
| 37 | # Check out a fresh copy |
| 38 | subprocess.call(['git', 'checkout', DISCOVERY_DOC_DIR]) |
| 39 | subprocess.call(['git', 'checkout', REFERENCE_DOC_DIR]) |
| 40 | |
| 41 | # Snapshot current discovery artifacts to a temporary directory |
| 42 | with tempfile.TemporaryDirectory() as current_discovery_doc_dir: |
| 43 | shutil.copytree(DISCOVERY_DOC_DIR, current_discovery_doc_dir, dirs_exist_ok=True) |
| 44 | |
| 45 | # Download discovery artifacts and generate documentation |
| 46 | describe.generate_all_api_documents() |
| 47 | |
| 48 | # Get a list of files changed using `git diff` |
| 49 | git_diff_output = subprocess.check_output(['git', |
| 50 | 'diff', |
| 51 | 'origin/master', |
| 52 | '--name-only', |
| 53 | '--', |
| 54 | DISCOVERY_DOC_DIR / '*.json', |
| 55 | REFERENCE_DOC_DIR / '*.html', |
| 56 | REFERENCE_DOC_DIR / '*.md', |
| 57 | ], |
| 58 | universal_newlines=True) |
| 59 | |
| 60 | # Create lists of the changed files |
| 61 | all_changed_files = [pathlib.Path(file_name).name for file_name in git_diff_output.split('\n')] |
| 62 | json_changed_files = [file for file in all_changed_files if file.endswith(".json")] |
| 63 | |
| 64 | # Create temporary directory |
| 65 | pathlib.Path(TEMP_DIR).mkdir() |
| 66 | |
| 67 | # Analyze the changes in discovery artifacts using the changesummary module |
| 68 | changesummary.ChangeSummary(DISCOVERY_DOC_DIR, current_discovery_doc_dir, TEMP_DIR, json_changed_files).detect_discovery_changes() |
| 69 | |
| 70 | # Write a list of the files changed to a file called `changed files` which will be used in the `createcommits.sh` script. |
| 71 | with open(TEMP_DIR / "changed_files", "w") as f: |
| 72 | f.writelines('\n'.join(all_changed_files)) |