-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathdeploy.py
More file actions
98 lines (83 loc) · 4.18 KB
/
deploy.py
File metadata and controls
98 lines (83 loc) · 4.18 KB
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
# Copyright 2020 Alexis Lopez Zubieta
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
import logging
import os
import pathlib
from . import listings
from .venv import Venv
class Deploy:
"""Deploy deb packages into an AppDir using apt-get to resolve the packages and their dependencies"""
def __init__(self, apt_venv: Venv):
self.apt_venv = apt_venv
self.logger = logging.getLogger("AptPackageDeploy")
def deploy(
self, include_patterns: [str], appdir_root: pathlib.Path, exclude_patterns=None
) -> [str]:
"""Deploy the packages and their dependencies to appdir_root.
Packages listed in exclude will not be deployed nor their dependencies.
Packages from the system services and graphics listings will be added by default to the exclude list.
"""
if not include_patterns:
# quick return if there is no packages to be deployed
return
self._prepare_apt_venv()
deploy_list = self._resolve_packages_to_deploy(
include_patterns, exclude_patterns
)
extracted_packages = self._extract_packages(appdir_root, deploy_list)
return [str(package) for package in extracted_packages]
def _prepare_apt_venv(self):
if not os.getenv("ABUILDER_APT_SKIP_UPDATE", False):
self.apt_venv.update()
else:
self.logger.warning(
"Skipping`apt update` execution. Newly added sources will not be available!"
)
# set apt core packages as installed, required for it to properly resolve dependencies
apt_core_packages = self.apt_venv.search_packages(listings.apt_core)
apt_core_packages = self._remove_old_packages(apt_core_packages)
self.apt_venv.set_installed_packages(apt_core_packages)
def _resolve_packages_to_deploy(self, include_patterns, exclude_patterns):
if exclude_patterns is None:
exclude_patterns = []
# extend user defined exclude listing with the default exclude listing
exclude_patterns.extend(listings.default_exclude_list)
excluded_packages = set(self.apt_venv.search_packages(exclude_patterns))
# don't exclude explicitly required packages
required_packages = set(self.apt_venv.search_packages(include_patterns))
excluded_packages = excluded_packages.difference(required_packages)
self.apt_venv.set_installed_packages(excluded_packages)
# lists packages to be installed including dependencies, without explicitly excluded packages
# if a dependency is required to be deployed, please include it in the include list
deploy_list = set(self.apt_venv.resolve_packages(include_patterns))
deploy_list = deploy_list.difference(excluded_packages)
return deploy_list
def _extract_packages(self, appdir_root, packages):
# ensure target directories exists
appdir_root.mkdir(exist_ok=True, parents=True)
for package in packages:
final_target = appdir_root
self.logger.info(
"Deploying %s to %s" % (package.get_expected_file_name(), final_target)
)
self.apt_venv.extract_package(package, final_target)
return packages
def _remove_old_packages(self, apt_core_packages):
latest_packages = {}
for package in apt_core_packages:
pkg_tuple = (package.name, package.arch)
if pkg_tuple not in latest_packages:
latest_packages[pkg_tuple] = package
else:
if package > latest_packages[pkg_tuple]:
latest_packages[pkg_tuple] = package
return latest_packages.values()