(raulenrique) | dfdda47 | 2018-06-04 12:02:29 -0700 | [diff] [blame] | 1 | # Copyright (C) 2018 The Android Open Source Project |
| 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 | """Tool functions to deal with files.""" |
| 15 | |
| 16 | import datetime |
| 17 | import os |
Haibo Huang | 329e681 | 2020-05-29 14:12:20 -0700 | [diff] [blame] | 18 | from pathlib import Path |
(raulenrique) | dfdda47 | 2018-06-04 12:02:29 -0700 | [diff] [blame] | 19 | |
Haibo Huang | 329e681 | 2020-05-29 14:12:20 -0700 | [diff] [blame] | 20 | # pylint: disable=import-error |
| 21 | from google.protobuf import text_format # type: ignore |
(raulenrique) | dfdda47 | 2018-06-04 12:02:29 -0700 | [diff] [blame] | 22 | |
Haibo Huang | 329e681 | 2020-05-29 14:12:20 -0700 | [diff] [blame] | 23 | # pylint: disable=import-error |
| 24 | import metadata_pb2 # type: ignore |
(raulenrique) | dfdda47 | 2018-06-04 12:02:29 -0700 | [diff] [blame] | 25 | |
Haibo Huang | 329e681 | 2020-05-29 14:12:20 -0700 | [diff] [blame] | 26 | ANDROID_TOP = Path(os.environ.get('ANDROID_BUILD_TOP', os.getcwd())) |
| 27 | EXTERNAL_PATH = ANDROID_TOP / 'external' |
(raulenrique) | dfdda47 | 2018-06-04 12:02:29 -0700 | [diff] [blame] | 28 | |
| 29 | METADATA_FILENAME = 'METADATA' |
| 30 | |
| 31 | |
Haibo Huang | a08fb60 | 2020-05-29 16:24:13 -0700 | [diff] [blame^] | 32 | def get_absolute_project_path(proj_path: Path) -> Path: |
(raulenrique) | dfdda47 | 2018-06-04 12:02:29 -0700 | [diff] [blame] | 33 | """Gets absolute path of a project. |
| 34 | |
| 35 | Path resolution starts from external/. |
| 36 | """ |
Haibo Huang | a08fb60 | 2020-05-29 16:24:13 -0700 | [diff] [blame^] | 37 | return EXTERNAL_PATH / proj_path |
(raulenrique) | dfdda47 | 2018-06-04 12:02:29 -0700 | [diff] [blame] | 38 | |
| 39 | |
Haibo Huang | a08fb60 | 2020-05-29 16:24:13 -0700 | [diff] [blame^] | 40 | def get_metadata_path(proj_path: Path) -> Path: |
(raulenrique) | dfdda47 | 2018-06-04 12:02:29 -0700 | [diff] [blame] | 41 | """Gets the absolute path of METADATA for a project.""" |
Haibo Huang | a08fb60 | 2020-05-29 16:24:13 -0700 | [diff] [blame^] | 42 | return get_absolute_project_path(proj_path) / METADATA_FILENAME |
(raulenrique) | dfdda47 | 2018-06-04 12:02:29 -0700 | [diff] [blame] | 43 | |
| 44 | |
Haibo Huang | a08fb60 | 2020-05-29 16:24:13 -0700 | [diff] [blame^] | 45 | def get_relative_project_path(proj_path: Path) -> Path: |
(raulenrique) | dfdda47 | 2018-06-04 12:02:29 -0700 | [diff] [blame] | 46 | """Gets the relative path of a project starting from external/.""" |
Haibo Huang | a08fb60 | 2020-05-29 16:24:13 -0700 | [diff] [blame^] | 47 | return get_absolute_project_path(proj_path).relative_to(EXTERNAL_PATH) |
(raulenrique) | dfdda47 | 2018-06-04 12:02:29 -0700 | [diff] [blame] | 48 | |
| 49 | |
Haibo Huang | a08fb60 | 2020-05-29 16:24:13 -0700 | [diff] [blame^] | 50 | def read_metadata(proj_path: Path) -> metadata_pb2.MetaData: |
(raulenrique) | dfdda47 | 2018-06-04 12:02:29 -0700 | [diff] [blame] | 51 | """Reads and parses METADATA file for a project. |
| 52 | |
| 53 | Args: |
| 54 | proj_path: Path to the project. |
| 55 | |
| 56 | Returns: |
| 57 | Parsed MetaData proto. |
| 58 | |
| 59 | Raises: |
| 60 | text_format.ParseError: Occurred when the METADATA file is invalid. |
| 61 | FileNotFoundError: Occurred when METADATA file is not found. |
| 62 | """ |
| 63 | |
Haibo Huang | 329e681 | 2020-05-29 14:12:20 -0700 | [diff] [blame] | 64 | with get_metadata_path(proj_path).open('r') as metadata_file: |
(raulenrique) | dfdda47 | 2018-06-04 12:02:29 -0700 | [diff] [blame] | 65 | metadata = metadata_file.read() |
| 66 | return text_format.Parse(metadata, metadata_pb2.MetaData()) |
| 67 | |
| 68 | |
Haibo Huang | a08fb60 | 2020-05-29 16:24:13 -0700 | [diff] [blame^] | 69 | def write_metadata(proj_path: Path, metadata: metadata_pb2.MetaData) -> None: |
(raulenrique) | dfdda47 | 2018-06-04 12:02:29 -0700 | [diff] [blame] | 70 | """Writes updated METADATA file for a project. |
| 71 | |
| 72 | This function updates last_upgrade_date in metadata and write to the project |
| 73 | directory. |
| 74 | |
| 75 | Args: |
| 76 | proj_path: Path to the project. |
| 77 | metadata: The MetaData proto to write. |
| 78 | """ |
| 79 | |
| 80 | date = metadata.third_party.last_upgrade_date |
| 81 | now = datetime.datetime.now() |
| 82 | date.year = now.year |
| 83 | date.month = now.month |
| 84 | date.day = now.day |
| 85 | text_metadata = text_format.MessageToString(metadata) |
Haibo Huang | 329e681 | 2020-05-29 14:12:20 -0700 | [diff] [blame] | 86 | with get_metadata_path(proj_path).open('w') as metadata_file: |
(raulenrique) | dfdda47 | 2018-06-04 12:02:29 -0700 | [diff] [blame] | 87 | metadata_file.write(text_metadata) |