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 , CalledProcessError
1312import argparse
14- import os
1513import logging
16- import sys
17- import glob
18- import shutil
19-
20- from tox_helper_tasks import get_pip_list_output
21- from ci_tools .parsing import ParsedSetup , parse_require
22- from ci_tools .build import create_package
23- from ci_tools .functions import get_package_from_repo , find_whl , find_sdist , discover_prebuilt_package
2414
2515logging .getLogger ().setLevel (logging .INFO )
2616
27- from ci_tools .parsing import ParsedSetup
28-
29-
30- def cleanup_build_artifacts (build_folder ):
31- # clean up egginfo
32- results = glob .glob (os .path .join (build_folder , "*.egg-info" ))
33-
34- if results :
35- print (results [0 ])
36- shutil .rmtree (results [0 ])
37-
38- # clean up build results
39- build_path = os .path .join (build_folder , "build" )
40- if os .path .exists (build_path ):
41- shutil .rmtree (build_path )
42-
43-
44- def discover_packages (setuppy_path , args ):
45- packages = []
46- if os .getenv ("PREBUILT_WHEEL_DIR" ) is not None and not args .force_create :
47- packages = discover_prebuilt_package (os .getenv ("PREBUILT_WHEEL_DIR" ), setuppy_path , args .package_type )
48- pkg = ParsedSetup .from_path (setuppy_path )
49-
50- if not packages :
51- logging .error (
52- "Package is missing in prebuilt directory {0} for package {1} and version {2}" .format (
53- os .getenv ("PREBUILT_WHEEL_DIR" ), pkg .name , pkg .version
54- )
55- )
56- exit (1 )
57- else :
58- packages = build_and_discover_package (
59- setuppy_path ,
60- args .distribution_directory ,
61- args .target_setup ,
62- args .package_type ,
63- )
64-
65- return packages
66-
67-
68- def in_ci ():
69- return os .getenv ("TF_BUILD" , False )
70-
71-
72- def build_and_discover_package (setuppy_path , dist_dir , target_setup , package_type ):
73- if package_type == "wheel" :
74- create_package (setuppy_path , dist_dir , enable_sdist = False )
75- else :
76- create_package (setuppy_path , dist_dir , enable_wheel = False )
77-
78- prebuilt_packages = [
79- f
80- for f in os .listdir (args .distribution_directory )
81- if f .endswith (".whl" if package_type == "wheel" else ".tar.gz" )
82- ]
83-
84- if not in_ci ():
85- logging .info ("Cleaning up build directories and files" )
86- cleanup_build_artifacts (target_setup )
87- return prebuilt_packages
88-
17+ from ci_tools .scenario .generation import create_package_and_install
8918
9019if __name__ == "__main__" :
9120 parser = argparse .ArgumentParser (
@@ -119,19 +48,22 @@ def build_and_discover_package(setuppy_path, dist_dir, target_setup, package_typ
11948 "--cache-dir" ,
12049 dest = "cache_dir" ,
12150 help = "Location that, if present, will be used as the pip cache directory." ,
51+ default = None
12252 )
12353
12454 parser .add_argument (
12555 "-w" ,
12656 "--work-dir" ,
12757 dest = "work_dir" ,
12858 help = "Location that, if present, will be used as working directory to run pip install." ,
59+ default = None
12960 )
13061
13162 parser .add_argument (
13263 "--force-create" ,
13364 dest = "force_create" ,
13465 help = "Force recreate whl even if it is prebuilt" ,
66+ default = False
13567 )
13668
13769 parser .add_argument (
@@ -150,117 +82,16 @@ def build_and_discover_package(setuppy_path, dist_dir, target_setup, package_typ
15082
15183 args = parser .parse_args ()
15284
153- commands_options = []
154- built_pkg_path = ""
155- setup_py_path = os .path .join (args .target_setup , "setup.py" )
156- additional_downloaded_reqs = []
157-
158- if not os .path .exists (args .distribution_directory ):
159- os .mkdir (args .distribution_directory )
160-
161- tmp_dl_folder = os .path .join (args .distribution_directory , "dl" )
162- if not os .path .exists (tmp_dl_folder ):
163- os .mkdir (tmp_dl_folder )
164-
165- # preview version is enabled when installing dev build so pip will install dev build version from devpos feed
166- if os .getenv ("SetDevVersion" , "false" ) == "true" :
167- commands_options .append ("--pre" )
168-
169- if args .cache_dir :
170- commands_options .extend (["--cache-dir" , args .cache_dir ])
171-
172- discovered_packages = discover_packages (setup_py_path , args )
173-
174- if args .skip_install :
175- logging .info ("Flag to skip install whl is passed. Skipping package installation" )
176- else :
177- for built_package in discovered_packages :
178- if os .getenv ("PREBUILT_WHEEL_DIR" ) is not None and not args .force_create :
179- # find the prebuilt package in the set of prebuilt wheels
180- package_path = os .path .join (os .environ ["PREBUILT_WHEEL_DIR" ], built_package )
181- if os .path .isfile (package_path ):
182- built_pkg_path = package_path
183- logging .info ("Installing {w} from directory" .format (w = built_package ))
184- # it does't exist, so we need to error out
185- else :
186- logging .error ("{w} not present in the prebuilt package directory. Exiting." .format (w = built_package ))
187- exit (1 )
188- else :
189- built_pkg_path = os .path .abspath (os .path .join (args .distribution_directory , built_package ))
190- logging .info ("Installing {w} from fresh built package." .format (w = built_package ))
191-
192- if not args .pre_download_disabled :
193- requirements = ParsedSetup .from_path (
194- os .path .join (os .path .abspath (args .target_setup ), "setup.py" )
195- ).requires
196- azure_requirements = [req .split (";" )[0 ] for req in requirements if req .startswith ("azure-" )]
197-
198- if azure_requirements :
199- logging .info (
200- "Found {} azure requirement(s): {}" .format (len (azure_requirements ), azure_requirements )
201- )
202-
203- download_command = [
204- sys .executable ,
205- "-m" ,
206- "pip" ,
207- "download" ,
208- "-d" ,
209- tmp_dl_folder ,
210- "--no-deps" ,
211- ]
212-
213- installation_additions = []
214-
215- # only download a package if the requirement is not already met, so walk across
216- # direct install_requires
217- for req in azure_requirements :
218- addition_necessary = True
219- # get all installed packages
220- installed_pkgs = get_pip_list_output ()
221-
222- # parse the specifier
223- req_name , req_specifier = parse_require (req )
224-
225- # if we have the package already present...
226- if req_name in installed_pkgs :
227- # if there is no specifier for the requirement, we can ignore it
228- if req_specifier is None :
229- addition_necessary = False
230-
231- # ...do we need to install the new version? if the existing specifier matches, we're fine
232- if req_specifier is not None and installed_pkgs [req_name ] in req_specifier :
233- addition_necessary = False
234-
235- if addition_necessary :
236- # we only want to add an additional rec for download if it actually exists
237- # in the upstream feed (either dev or pypi)
238- # if it doesn't, we should just install the relative dep if its an azure package
239- installation_additions .append (req )
240-
241- if installation_additions :
242- non_present_reqs = []
243- for addition in installation_additions :
244- try :
245- check_call (
246- download_command + [addition ] + commands_options ,
247- env = dict (os .environ , PIP_EXTRA_INDEX_URL = "" ),
248- )
249- except CalledProcessError as e :
250- req_name , req_specifier = parse_require (addition )
251- non_present_reqs .append (req_name )
252-
253- additional_downloaded_reqs = [
254- os .path .abspath (os .path .join (tmp_dl_folder , pth )) for pth in os .listdir (tmp_dl_folder )
255- ] + [get_package_from_repo (relative_req ).folder for relative_req in non_present_reqs ]
85+ create_package_and_install (
86+ distribution_directory = args .distribution_directory ,
87+ target_setup = args .target_setup ,
88+ skip_install = args .skip_install ,
89+ cache_dir = args .cache_dir ,
90+ work_dir = args .work_dir ,
91+ force_create = args .force_create ,
92+ package_type = args .package_type ,
93+ pre_download_disabled = args .pre_download_disabled ,
94+ )
25695
257- commands = [sys .executable , "-m" , "pip" , "install" , built_pkg_path ]
258- commands .extend (additional_downloaded_reqs )
259- commands .extend (commands_options )
26096
261- if args .work_dir and os .path .exists (args .work_dir ):
262- logging .info ("Executing command from {0}:{1}" .format (args .work_dir , commands ))
263- check_call (commands , cwd = args .work_dir )
264- else :
265- check_call (commands )
266- logging .info ("Installed {w}" .format (w = built_package ))
97+
0 commit comments