blob: 17202c857115397e8a2c963f8fddfc86a9bf7238 [file] [log] [blame]
(raulenrique)dfdda472018-06-04 12:02:29 -07001# 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
16import datetime
17import os
Haibo Huang329e6812020-05-29 14:12:20 -070018from pathlib import Path
Haibo Huang27d2e0b2021-02-10 15:20:19 -080019import textwrap
(raulenrique)dfdda472018-06-04 12:02:29 -070020
Haibo Huang329e6812020-05-29 14:12:20 -070021# pylint: disable=import-error
22from google.protobuf import text_format # type: ignore
(raulenrique)dfdda472018-06-04 12:02:29 -070023
Haibo Huang329e6812020-05-29 14:12:20 -070024# pylint: disable=import-error
25import metadata_pb2 # type: ignore
(raulenrique)dfdda472018-06-04 12:02:29 -070026
Haibo Huang329e6812020-05-29 14:12:20 -070027ANDROID_TOP = Path(os.environ.get('ANDROID_BUILD_TOP', os.getcwd()))
28EXTERNAL_PATH = ANDROID_TOP / 'external'
(raulenrique)dfdda472018-06-04 12:02:29 -070029
30METADATA_FILENAME = 'METADATA'
31
32
Haibo Huanga08fb602020-05-29 16:24:13 -070033def get_absolute_project_path(proj_path: Path) -> Path:
(raulenrique)dfdda472018-06-04 12:02:29 -070034 """Gets absolute path of a project.
35
36 Path resolution starts from external/.
37 """
Haibo Huanga08fb602020-05-29 16:24:13 -070038 return EXTERNAL_PATH / proj_path
(raulenrique)dfdda472018-06-04 12:02:29 -070039
40
Haibo Huanga08fb602020-05-29 16:24:13 -070041def get_metadata_path(proj_path: Path) -> Path:
(raulenrique)dfdda472018-06-04 12:02:29 -070042 """Gets the absolute path of METADATA for a project."""
Haibo Huanga08fb602020-05-29 16:24:13 -070043 return get_absolute_project_path(proj_path) / METADATA_FILENAME
(raulenrique)dfdda472018-06-04 12:02:29 -070044
45
Haibo Huanga08fb602020-05-29 16:24:13 -070046def get_relative_project_path(proj_path: Path) -> Path:
(raulenrique)dfdda472018-06-04 12:02:29 -070047 """Gets the relative path of a project starting from external/."""
Haibo Huanga08fb602020-05-29 16:24:13 -070048 return get_absolute_project_path(proj_path).relative_to(EXTERNAL_PATH)
(raulenrique)dfdda472018-06-04 12:02:29 -070049
50
Haibo Huanga08fb602020-05-29 16:24:13 -070051def read_metadata(proj_path: Path) -> metadata_pb2.MetaData:
(raulenrique)dfdda472018-06-04 12:02:29 -070052 """Reads and parses METADATA file for a project.
53
54 Args:
55 proj_path: Path to the project.
56
57 Returns:
58 Parsed MetaData proto.
59
60 Raises:
61 text_format.ParseError: Occurred when the METADATA file is invalid.
62 FileNotFoundError: Occurred when METADATA file is not found.
63 """
64
Haibo Huang329e6812020-05-29 14:12:20 -070065 with get_metadata_path(proj_path).open('r') as metadata_file:
(raulenrique)dfdda472018-06-04 12:02:29 -070066 metadata = metadata_file.read()
67 return text_format.Parse(metadata, metadata_pb2.MetaData())
68
69
Chih-Hung Hsiehdd1915d2020-09-29 14:22:12 -070070def write_metadata(proj_path: Path, metadata: metadata_pb2.MetaData, keep_date: bool) -> None:
(raulenrique)dfdda472018-06-04 12:02:29 -070071 """Writes updated METADATA file for a project.
72
73 This function updates last_upgrade_date in metadata and write to the project
74 directory.
75
76 Args:
77 proj_path: Path to the project.
78 metadata: The MetaData proto to write.
Chih-Hung Hsiehdd1915d2020-09-29 14:22:12 -070079 keep_date: Do not change date.
(raulenrique)dfdda472018-06-04 12:02:29 -070080 """
81
Chih-Hung Hsiehdd1915d2020-09-29 14:22:12 -070082 if not keep_date:
83 date = metadata.third_party.last_upgrade_date
84 now = datetime.datetime.now()
85 date.year = now.year
86 date.month = now.month
87 date.day = now.day
(raulenrique)dfdda472018-06-04 12:02:29 -070088 text_metadata = text_format.MessageToString(metadata)
Haibo Huang329e6812020-05-29 14:12:20 -070089 with get_metadata_path(proj_path).open('w') as metadata_file:
Haibo Huang27d2e0b2021-02-10 15:20:19 -080090 if metadata.third_party.license_type == metadata_pb2.LicenseType.BY_EXCEPTION_ONLY:
91 metadata_file.write(textwrap.dedent("""\
92 # *** THIS PACKAGE HAS SPECIAL LICENSING CONDITIONS. PLEASE
93 # CONSULT THE OWNERS AND opensource-licensing@google.com BEFORE
94 # DEPENDING ON IT IN YOUR PROJECT. ***
95 """))
(raulenrique)dfdda472018-06-04 12:02:29 -070096 metadata_file.write(text_metadata)