blob: d7dd0faa7bdb174b23e778437532811ef446b9bb [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
(raulenrique)dfdda472018-06-04 12:02:29 -070019
Haibo Huang329e6812020-05-29 14:12:20 -070020# pylint: disable=import-error
21from google.protobuf import text_format # type: ignore
(raulenrique)dfdda472018-06-04 12:02:29 -070022
Haibo Huang329e6812020-05-29 14:12:20 -070023# pylint: disable=import-error
24import metadata_pb2 # type: ignore
(raulenrique)dfdda472018-06-04 12:02:29 -070025
Haibo Huang329e6812020-05-29 14:12:20 -070026ANDROID_TOP = Path(os.environ.get('ANDROID_BUILD_TOP', os.getcwd()))
27EXTERNAL_PATH = ANDROID_TOP / 'external'
(raulenrique)dfdda472018-06-04 12:02:29 -070028
29METADATA_FILENAME = 'METADATA'
30
31
Haibo Huanga08fb602020-05-29 16:24:13 -070032def get_absolute_project_path(proj_path: Path) -> Path:
(raulenrique)dfdda472018-06-04 12:02:29 -070033 """Gets absolute path of a project.
34
35 Path resolution starts from external/.
36 """
Haibo Huanga08fb602020-05-29 16:24:13 -070037 return EXTERNAL_PATH / proj_path
(raulenrique)dfdda472018-06-04 12:02:29 -070038
39
Haibo Huanga08fb602020-05-29 16:24:13 -070040def get_metadata_path(proj_path: Path) -> Path:
(raulenrique)dfdda472018-06-04 12:02:29 -070041 """Gets the absolute path of METADATA for a project."""
Haibo Huanga08fb602020-05-29 16:24:13 -070042 return get_absolute_project_path(proj_path) / METADATA_FILENAME
(raulenrique)dfdda472018-06-04 12:02:29 -070043
44
Haibo Huanga08fb602020-05-29 16:24:13 -070045def get_relative_project_path(proj_path: Path) -> Path:
(raulenrique)dfdda472018-06-04 12:02:29 -070046 """Gets the relative path of a project starting from external/."""
Haibo Huanga08fb602020-05-29 16:24:13 -070047 return get_absolute_project_path(proj_path).relative_to(EXTERNAL_PATH)
(raulenrique)dfdda472018-06-04 12:02:29 -070048
49
Haibo Huanga08fb602020-05-29 16:24:13 -070050def read_metadata(proj_path: Path) -> metadata_pb2.MetaData:
(raulenrique)dfdda472018-06-04 12:02:29 -070051 """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 Huang329e6812020-05-29 14:12:20 -070064 with get_metadata_path(proj_path).open('r') as metadata_file:
(raulenrique)dfdda472018-06-04 12:02:29 -070065 metadata = metadata_file.read()
66 return text_format.Parse(metadata, metadata_pb2.MetaData())
67
68
Haibo Huanga08fb602020-05-29 16:24:13 -070069def write_metadata(proj_path: Path, metadata: metadata_pb2.MetaData) -> None:
(raulenrique)dfdda472018-06-04 12:02:29 -070070 """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 Huang329e6812020-05-29 14:12:20 -070086 with get_metadata_path(proj_path).open('w') as metadata_file:
(raulenrique)dfdda472018-06-04 12:02:29 -070087 metadata_file.write(text_metadata)