1#!/usr/bin/env python3
2#
3# Copyright 2024 - The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the',  help="License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an',  help="AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import logging
18from pathlib import Path
19import platform
20import zipfile
21
22from tasks.task import Task
23from utils import platform_to_cmake_target
24
25
26class ZipArtifactTask(Task):
27
28  def __init__(self, args):
29    super().__init__("ZipArtifact")
30    self.build_id = args.build_id
31    self.out = Path(args.out_dir)
32    if args.target:
33      self.target = args.target.lower()
34    else:
35      self.target = platform.system().lower()
36    self.dist = Path(args.dist_dir).absolute()
37
38  def do_run(self):
39    # Make sure the dist directory exists.
40    self.dist.mkdir(exist_ok=True, parents=True)
41
42    # Zip results..
43    zip_fname = (
44        self.dist
45        / f"netsim-{platform_to_cmake_target(self.target)}-{self.build_id}.zip"
46    )
47    search_dir = self.out / "distribution" / "emulator"
48    logging.info("Creating zip file: %s", zip_fname)
49    with zipfile.ZipFile(
50        zip_fname, "w", zipfile.ZIP_DEFLATED, allowZip64=True
51    ) as zipf:
52      logging.info("Searching %s", search_dir)
53      for fname in search_dir.glob("**/*"):
54        arcname = fname.relative_to(search_dir)
55        logging.info("Adding %s as %s", fname, arcname)
56        zipf.write(fname, arcname)
57    return True
58