Skip to content

Commit c86892a

Browse files
scbeddsemick-dev
andauthored
allowing create_package_and_install to install relative deps automagically. (Azure#27832)
Co-authored-by: Scott Beddall <[email protected]>
1 parent 82c2a2a commit c86892a

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

eng/tox/create_package_and_install.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,25 @@
99
# it should be executed from tox with `{toxenvdir}/python` to ensure that the package
1010
# can be successfully tested from within a tox environment.
1111

12-
from subprocess import check_call
12+
from subprocess import check_call, CalledProcessError
1313
import argparse
1414
import os
1515
import logging
1616
import sys
1717
import glob
1818
import shutil
19-
import pdb
2019
from pkg_resources import parse_version
2120

2221
from tox_helper_tasks import find_whl, find_sdist, get_pip_list_output
2322
from ci_tools.parsing import ParsedSetup, parse_require
2423
from ci_tools.build import create_package
24+
from ci_tools.functions import get_package_from_repo
2525

2626
logging.getLogger().setLevel(logging.INFO)
2727

2828
from ci_tools.parsing import ParsedSetup
2929

30+
3031
def cleanup_build_artifacts(build_folder):
3132
# clean up egginfo
3233
results = glob.glob(os.path.join(build_folder, "*.egg-info"))
@@ -170,7 +171,7 @@ def build_and_discover_package(setuppy_path, dist_dir, target_setup, package_typ
170171
os.mkdir(tmp_dl_folder)
171172

172173
# preview version is enabled when installing dev build so pip will install dev build version from devpos feed
173-
if os.getenv("SetDevVersion", 'false') == 'true':
174+
if os.getenv("SetDevVersion", "false") == "true":
174175
commands_options.append("--pre")
175176

176177
if args.cache_dir:
@@ -197,7 +198,9 @@ def build_and_discover_package(setuppy_path, dist_dir, target_setup, package_typ
197198
logging.info("Installing {w} from fresh built package.".format(w=built_package))
198199

199200
if not args.pre_download_disabled:
200-
requirements = ParsedSetup.from_path(os.path.join(os.path.abspath(args.target_setup), "setup.py")).requires
201+
requirements = ParsedSetup.from_path(
202+
os.path.join(os.path.abspath(args.target_setup), "setup.py")
203+
).requires
201204
azure_requirements = [req.split(";")[0] for req in requirements if req.startswith("azure")]
202205

203206
if azure_requirements:
@@ -238,19 +241,28 @@ def build_and_discover_package(setuppy_path, dist_dir, target_setup, package_typ
238241
addition_necessary = False
239242

240243
if addition_necessary:
244+
# we only want to add an additional rec for download if it actually exists
245+
# in the upstream feed (either dev or pypi)
246+
# if it doesn't, we should just install the relative dep if its an azure package
241247
installation_additions.append(req)
242248

243249
if installation_additions:
244-
download_command.extend(installation_additions)
245-
download_command.extend(commands_options)
250+
non_present_reqs = []
251+
for addition in installation_additions:
252+
try:
253+
check_call(
254+
download_command + [addition] + commands_options,
255+
env=dict(os.environ, PIP_EXTRA_INDEX_URL=""),
256+
)
257+
except CalledProcessError as e:
258+
req_name, req_specifier = parse_require(addition)
259+
non_present_reqs.append(req_name)
246260

247-
check_call(download_command, env=dict(os.environ, PIP_EXTRA_INDEX_URL=""))
248261
additional_downloaded_reqs = [
249262
os.path.abspath(os.path.join(tmp_dl_folder, pth)) for pth in os.listdir(tmp_dl_folder)
250-
]
263+
] + [get_package_from_repo(relative_req).folder for relative_req in non_present_reqs]
251264

252265
commands = [sys.executable, "-m", "pip", "install", built_pkg_path]
253-
254266
commands.extend(additional_downloaded_reqs)
255267
commands.extend(commands_options)
256268

tools/azure-sdk-tools/ci_tools/functions.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,27 +203,33 @@ def is_required_version_on_pypi(package_name, spec):
203203
return versions
204204

205205

206-
def get_version_from_repo(pkg_name: str, repo_root: str = None):
206+
def get_package_from_repo(pkg_name: str, repo_root: str = None) -> ParsedSetup:
207207
root_dir = discover_repo_root(repo_root)
208208

209-
# find version for the package from source. This logic should be revisited to find version from devops feed
210209
glob_path = os.path.join(root_dir, "sdk", "*", pkg_name, "setup.py")
211210
paths = glob.glob(glob_path)
211+
212212
if paths:
213213
setup_py_path = paths[0]
214214
parsed_setup = ParsedSetup.from_path(setup_py_path)
215+
return parsed_setup
216+
217+
return None
215218

219+
220+
def get_version_from_repo(pkg_name: str, repo_root: str = None):
221+
pkg_info = get_package_from_repo(pkg_name, repo_root)
222+
if pkg_info:
216223
# Remove dev build part if version for this package is already updated to dev build
217224
# When building package with dev build version, version for packages in same service is updated to dev build
218225
# and other packages will not have dev build number
219226
# strip dev build number so we can check if package exists in PyPI and replace
220-
221-
version_obj = Version(parsed_setup.version)
227+
version_obj = Version(pkg_info.version)
222228
if version_obj.pre:
223229
if version_obj.pre[0] == DEV_BUILD_IDENTIFIER:
224-
version = version_obj.base_version
230+
return version_obj.base_version
225231

226-
return version
232+
return str(version_obj)
227233
else:
228234
logging.error("setup.py is not found for package {} to identify current version".format(pkg_name))
229235
exit(1)

tools/azure-sdk-tools/ci_tools/parsing/parse_functions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def __init__(
4646
self.namespace = name_space
4747
self.package_data = package_data
4848
self.include_package_data = include_package_data
49+
self.folder = os.path.dirname(self.setup_filename)
4950

5051
@classmethod
5152
def from_path(cls, parse_directory_or_file: str):
@@ -200,7 +201,7 @@ def parse_require(req: str) -> Tuple[str, SpecifierSet]:
200201
return [pkg_name, None]
201202

202203
# regex details ripped from https://peps.python.org/pep-0508/
203-
isolated_spec = re.sub(r'^([a-zA-Z0-9\-\_\.]+)(\[[a-zA-Z0-9\-\_\.\,]*\])?', "", str(req_object))
204+
isolated_spec = re.sub(r"^([a-zA-Z0-9\-\_\.]+)(\[[a-zA-Z0-9\-\_\.\,]*\])?", "", str(req_object))
204205
spec = SpecifierSet(isolated_spec)
205206
return (pkg_name, spec)
206207

tools/azure-sdk-tools/tests/test_parse_functionality.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,4 @@ def test_sdk_sample_setup(test_patch):
113113
assert result.namespace == "ci_tools"
114114
assert "pytyped" in result.package_data
115115
assert result.include_package_data == True
116+
assert result.folder == package_root

0 commit comments

Comments
 (0)