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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
from ast import Not
from packaging.specifiers import SpecifierSet
from packaging.version import Version, parse
from pkg_resources import Requirement
from ci_tools.variables import discover_repo_root, get_artifact_directory, DEV_BUILD_IDENTIFIER
import os, sys, platform, glob, re
from ci_tools.parsing import ParsedSetup
from pypi_tools.pypi import PyPIClient
from typing import List
import logging
OMITTED_CI_PACKAGES = [
"azure-mgmt-documentdb",
"azure-servicemanagement-legacy",
"azure-mgmt-scheduler",
"azure",
"azure-mgmt",
"azure-storage",
"azure-monitor",
"azure-mgmt-regionmove",
]
MANAGEMENT_PACKAGE_IDENTIFIERS = [
"mgmt",
"azure-cognitiveservices",
"azure-servicefabric",
"nspkg",
"azure-keyvault",
"azure-synapse",
"azure-ai-anomalydetector",
]
META_PACKAGES = ["azure", "azure-mgmt", "azure-keyvault"]
REGRESSION_EXCLUDED_PACKAGES = [
"azure-common",
]
MANAGEMENT_PACKAGES_FILTER_EXCLUSIONS = [
"azure-mgmt-core",
]
TEST_COMPATIBILITY_MAP = {}
omit_regression = (
lambda x: "nspkg" not in x
and "mgmt" not in x
and os.path.basename(x) not in MANAGEMENT_PACKAGE_IDENTIFIERS
and os.path.basename(x) not in META_PACKAGES
and os.path.basename(x) not in REGRESSION_EXCLUDED_PACKAGES
)
omit_docs = lambda x: "nspkg" not in x and os.path.basename(x) not in META_PACKAGES
omit_build = lambda x: x # Dummy lambda to match omit type
lambda_filter_azure_pkg = lambda x: x.startswith("azure") and "-nspkg" not in x
omit_mgmt = lambda x: "mgmt" not in x or os.path.basename(x) in MANAGEMENT_PACKAGES_FILTER_EXCLUSIONS
# dict of filter type and filter function
omit_function_dict = {
"Build": omit_build,
"Docs": omit_docs,
"Regression": omit_regression,
"Omit_management": omit_mgmt,
}
def filter_for_compatibility(package_set: List[str]) -> List[str]:
"""
This function takes in a set of paths to python packages. It returns the set filtered by compatibility with the currently running python executable.
If a package is unsupported by the executable, it will be omitted from the returned list.
A manual override list of these compatibilities is also supported, but requires code change to enable. Check TEST_COMPATIBILITY_MAP in this same file.
:param List[str] package_set: a list of paths to python packages. Each item can either be a package folder or a direct path to setup.py.
"""
collected_packages = []
v = sys.version_info
running_major_version = Version(".".join([str(v[0]), str(v[1]), str(v[2])]))
for pkg in package_set:
spec_set = SpecifierSet(ParsedSetup.from_path(pkg).python_requires)
pkg_specs_override = TEST_COMPATIBILITY_MAP.get(os.path.basename(pkg), None)
if pkg_specs_override:
spec_set = SpecifierSet(pkg_specs_override)
if running_major_version in spec_set:
collected_packages.append(pkg)
return collected_packages
def compare_python_version(version_spec: str):
current_sys_version = parse(platform.python_version())
spec_set = SpecifierSet(version_spec)
return current_sys_version in spec_set
def str_to_bool(input_string: str) -> bool:
"""
Takes a boolean string representation and returns a bool type value.
"""
if isinstance(input_string, bool):
return input_string
elif input_string.lower() in ("true", "t", "1"):
return True
elif input_string.lower() in ("false", "f", "0"):
return False
else:
return False
def discover_targeted_packages(
glob_string: str,
target_root_dir: str,
additional_contains_filter: str = "",
filter_type: str = "Build",
compatibility_filter: bool = True,
) -> List[str]:
"""
During build and test, the set of targeted packages may expand or contract depending on the needs of the invocation.
This function centralizes business and material requirements and outputs the set of packages that should be targeted.
:param str glob_string: The basic glob used to query packages within the repo. Defaults to "azure-*"
:param str target_root_dir: The root directory in which globbing will begin.
:param str additional_contains_filter: Additional filter option. Used when needing to provide one-off filtration that doesn't merit an additional filter_type. Defaults to empty string.
:param str filter_type: One a string representing a filter function as a set of options. Options [ "Build", "Docs", "Regression", "Omit_management" ] Defaults to "Build".
:param bool compatibility_filter: Enables or disables compatibility filtering of found packages. If the invoking python executable does not match a found package's specifiers, the package will be omitted. Defaults to True.
"""
if glob_string:
individual_globs = glob_string.split(",")
else:
individual_globs = "azure-*"
collected_top_level_directories = []
for glob_string in individual_globs:
globbed = glob.glob(os.path.join(target_root_dir, glob_string, "setup.py")) + glob.glob(
os.path.join(target_root_dir, "sdk/*/", glob_string, "setup.py")
)
collected_top_level_directories.extend([os.path.dirname(p) for p in globbed])
# deduplicate, in case we have double coverage from the glob strings. Example: "azure-mgmt-keyvault,azure-mgmt-*"
collected_directories = list(set([p for p in collected_top_level_directories if additional_contains_filter in p]))
pkg_set_ci_filtered = collected_directories
# if we have individually queued this specific package, it's obvious that we want to build it specifically
# in this case, do not honor the omission list
if len(collected_directories) == 1:
if compatibility_filter:
pkg_set_ci_filtered = filter_for_compatibility(collected_directories)
# however, if there are multiple packages being built, we should honor the omission list and NOT build the omitted
# packages
else:
allowed_package_set = remove_omitted_packages(collected_directories)
if compatibility_filter:
pkg_set_ci_filtered = filter_for_compatibility(allowed_package_set)
# Apply filter based on filter type. for e.g. Docs, Regression, Management
pkg_set_ci_filtered = list(filter(omit_function_dict.get(filter_type, omit_build), pkg_set_ci_filtered))
logging.info("Target packages after filtering by CI Type: {}".format(pkg_set_ci_filtered))
logging.info(
"Package(s) omitted by CI filter: {}".format(list(set(collected_directories) - set(pkg_set_ci_filtered)))
)
return sorted(pkg_set_ci_filtered)
def remove_omitted_packages(collected_directories):
packages = [
package_dir for package_dir in collected_directories if os.path.basename(package_dir) not in OMITTED_CI_PACKAGES
]
return packages
def update_requires(setup_py_path, requires_dict):
# This method changes package requirement by overriding the specifier
contents = []
with open(setup_py_path, "r") as setup_file:
contents = setup_file.readlines()
# find and replace all existing package requirement with new requirement
for i in range(0, len(contents) - 1):
keys = [k for k in requires_dict.keys() if k in contents[i]]
for key in keys:
contents[i] = contents[i].replace(key, requires_dict[key])
with open(setup_py_path, "w") as setup_file:
setup_file.writelines(contents)
def is_required_version_on_pypi(package_name, spec):
client = PyPIClient()
try:
pypi_results = client.get_ordered_versions(package_name)
except:
pypi_results = []
versions = [str(v) for v in pypi_results if str(v) in spec]
return versions
def get_package_from_repo(pkg_name: str, repo_root: str = None) -> ParsedSetup:
root_dir = discover_repo_root(repo_root)
glob_path = os.path.join(root_dir, "sdk", "*", pkg_name, "setup.py")
paths = glob.glob(glob_path)
if paths:
setup_py_path = paths[0]
parsed_setup = ParsedSetup.from_path(setup_py_path)
return parsed_setup
return None
def get_version_from_repo(pkg_name: str, repo_root: str = None):
pkg_info = get_package_from_repo(pkg_name, repo_root)
if pkg_info:
# Remove dev build part if version for this package is already updated to dev build
# When building package with dev build version, version for packages in same service is updated to dev build
# and other packages will not have dev build number
# strip dev build number so we can check if package exists in PyPI and replace
version_obj = Version(pkg_info.version)
if version_obj.pre:
if version_obj.pre[0] == DEV_BUILD_IDENTIFIER:
return version_obj.base_version
return str(version_obj)
else:
logging.error("setup.py is not found for package {} to identify current version".format(pkg_name))
exit(1)
def get_base_version(pkg_name):
root_dir = discover_repo_root()
# find version for the package from source. This logic should be revisited to find version from devops feed
glob_path = os.path.join(root_dir, "sdk", "*", pkg_name, "setup.py")
paths = glob.glob(glob_path)
if paths:
setup_py_path = paths[0]
parsed_setup = ParsedSetup.from_path(setup_py_path)
version_obj = Version(parsed_setup.version)
return version_obj.base_version
else:
logging.error("setup.py is not found for package {} to identify current version".format(pkg_name))
exit(1)
def process_requires(setup_py_path: str):
"""
This method processes a setup.py's package requirements to verify if all required packages are available on PyPI.
If any azure sdk package is not available on PyPI then requirement will be updated to refer to the sdk "dev_identifier".
Examples:
azure-storage-blob >= 1.0.1b1
<there is no azure-storage-blob with any 1.0.1 patch version>
update to require 1.0.1a1 to allow previously published dev versions to be allowed.
"""
pkg_details = ParsedSetup.from_path(setup_py_path)
azure_requirements = [Requirement.parse(r) for r in pkg_details.requires if r.startswith("azure")]
# Find package requirements that are not available on PyPI
requirement_to_update = {}
for req in azure_requirements:
pkg_name = req.key
spec = SpecifierSet(str(req).replace(pkg_name, ""))
if not is_required_version_on_pypi(pkg_name, spec):
old_req = str(req)
version = get_version_from_repo(pkg_name)
base_version = get_base_version(pkg_name)
logging.info("Updating version {0} in requirement {1} to dev build version".format(version, old_req))
# {} = <must have a base version>
# ( <optionally, we have a prerelease version>
# ( <we must have 0 or 1 prerelease versions>
# (a|b|rc) <we must have a prelease identifier>
# \d+ <followed by a number of digits N>
# )?
# ( <optionally, we have a postrelease version>
# \.post <which is ".post">
# \d+ <followed by number of digits N>
# )?
# )?
rx = r"{}(((a|b|rc)\d+)?(\.post\d+)?)?".format(base_version)
new_req = re.sub(rx, "{}{}1".format(base_version, DEV_BUILD_IDENTIFIER), str(req), flags=re.IGNORECASE)
logging.info("New requirement for package {0}: {1}".format(pkg_name, new_req))
requirement_to_update[old_req] = new_req
if not requirement_to_update:
logging.info("All required packages are available on PyPI")
else:
logging.info("Packages not available on PyPI:{}".format(requirement_to_update))
update_requires(setup_py_path, requirement_to_update)
logging.info("Package requirement is updated in setup.py")
|