File: exe.py

package info (click to toggle)
awscli 2.31.35-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156,692 kB
  • sloc: python: 213,816; xml: 14,082; makefile: 189; sh: 178; javascript: 8
file content (102 lines) | stat: -rw-r--r-- 3,316 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import os
from dataclasses import dataclass, field

from awscli_venv import AwsCliVenv
from constants import (
    DISTRIBUTION_SOURCE_EXE,
    EXE_ASSETS_DIR,
    PYINSTALLER_DIR,
    PYINSTALLER_EXE_NAME,
)
from utils import Utils


@dataclass
class ExeBuilder:
    workspace: str
    venv: AwsCliVenv

    _exe_dir: str = field(init=False)
    _final_dist_dir: str = field(init=False)
    _dist_dir: str = field(init=False)
    _build_dir: str = field(init=False)

    _utils: Utils = field(default_factory=lambda: Utils())

    def __post_init__(self):
        self._exe_dir = os.path.join(self.workspace, "aws")
        self._final_dist_dir = os.path.join(self._exe_dir, "dist")
        self._dist_dir = os.path.join(self.workspace, "dist")
        self._build_dir = os.path.join(self.workspace, "build")

    def build(self, cleanup=True):
        self._ensure_no_existing_build_dir()
        self._build_aws()
        self._build_aws_completer()
        self._utils.copy_directory_contents_into(EXE_ASSETS_DIR, self._exe_dir)
        self._update_metadata()
        if cleanup:
            self._cleanup()
        print(f"Built exe at {self._exe_dir}")

    def _update_metadata(self):
        self._utils.update_metadata(
            self._final_dist_dir,
            distribution_source=DISTRIBUTION_SOURCE_EXE,
        )

    def _ensure_no_existing_build_dir(self):
        if self._utils.isdir(self._dist_dir):
            self._utils.rmtree(self._dist_dir)

    def _build_aws(self):
        aws_exe_build_dir = self._run_pyinstaller("aws.spec")
        self._utils.copy_directory(aws_exe_build_dir, self._final_dist_dir)

    def _build_aws_completer(self):
        aws_completer_exe_build_dir = self._run_pyinstaller(
            "aws_completer.spec"
        )
        self._utils.copy_directory_contents_into(
            aws_completer_exe_build_dir, self._final_dist_dir
        )

    def _run_pyinstaller(self, specfile: str):
        aws_spec_path = os.path.join(PYINSTALLER_DIR, specfile)
        self._utils.run(
            [
                self.venv.python_exe,
                os.path.join(self.venv.bin_dir, PYINSTALLER_EXE_NAME),
                aws_spec_path,
                "--distpath",
                self._dist_dir,
                "--workpath",
                self._build_dir,
            ],
            cwd=PYINSTALLER_DIR,
            check=True,
        )
        return os.path.join(
            self.workspace, "dist", os.path.splitext(specfile)[0]
        )

    def _cleanup(self):
        locations = [
            self._build_dir,
            self._dist_dir,
        ]
        for location in locations:
            self._utils.rmtree(location)
            print("Deleted build directory: %s" % location)