19
19
20
20
from pathlib import Path
21
21
import logging
22
- import shutil
23
22
import subprocess
24
23
import sys
25
-
24
+ import re
26
25
27
26
_SUPPORTED_SUFFIXES = [".txt" ]
28
27
# TODO : Move PKL_FILE_NAME to common location
31
30
logger = logging .getLogger (__name__ )
32
31
33
32
34
- def capture_dependencies (dependencies : str , work_dir : Path , capture_all : bool = False ):
33
+ def capture_dependencies (dependencies : dict , work_dir : Path , capture_all : bool = False ):
35
34
"""Placeholder docstring"""
36
35
path = work_dir .joinpath ("requirements.txt" )
37
36
if "auto" in dependencies and dependencies ["auto" ]:
@@ -53,24 +52,44 @@ def capture_dependencies(dependencies: str, work_dir: Path, capture_all: bool =
53
52
check = True ,
54
53
)
55
54
56
- if "requirements" in dependencies :
57
- _capture_from_customer_provided_requirements (dependencies ["requirements" ], path )
55
+ with open (path , "r" ) as f :
56
+ autodetect_depedencies = f .read ().splitlines ()
57
+ else :
58
+ autodetect_depedencies = []
58
59
60
+ module_version_dict = _parse_dependency_list (autodetect_depedencies )
61
+
62
+ if "requirements" in dependencies :
63
+ module_version_dict = _process_customer_provided_requirements (
64
+ requirements_file = dependencies ["requirements" ], module_version_dict = module_version_dict
65
+ )
59
66
if "custom" in dependencies :
67
+ module_version_dict = _process_custom_dependencies (
68
+ custom_dependencies = dependencies .get ("custom" ), module_version_dict = module_version_dict
69
+ )
70
+ with open (path , "w" ) as f :
71
+ for module , version in module_version_dict .items ():
72
+ f .write (f"{ module } { version } \n " )
60
73
61
- with open (path , "a+" ) as f :
62
- for package in dependencies ["custom" ]:
63
- f .write (f"{ package } \n " )
64
74
75
+ def _process_custom_dependencies (custom_dependencies : list , module_version_dict : dict ):
76
+ """Placeholder docstring"""
77
+ custom_module_version_dict = _parse_dependency_list (custom_dependencies )
78
+ module_version_dict .update (custom_module_version_dict )
79
+ return module_version_dict
65
80
66
- def _capture_from_customer_provided_requirements (requirements_file : str , output_path : Path ):
81
+
82
+ def _process_customer_provided_requirements (requirements_file : str , module_version_dict : dict ):
67
83
"""Placeholder docstring"""
68
- input_path = Path (requirements_file )
69
- if not input_path .is_file () or not _is_valid_requirement_file (input_path ):
84
+ requirements_file = Path (requirements_file )
85
+ if not requirements_file .is_file () or not _is_valid_requirement_file (requirements_file ):
70
86
raise Exception (f"Path: { requirements_file } to requirements.txt doesn't exist" )
71
87
logger .debug ("Packaging provided requirements.txt from %s" , requirements_file )
72
- with open (output_path , "a+" ) as f :
73
- shutil .copyfileobj (open (input_path , "r" ), f )
88
+ with open (requirements_file , "r" ) as f :
89
+ custom_dependencies = f .read ().splitlines ()
90
+
91
+ module_version_dict .update (_parse_dependency_list (custom_dependencies ))
92
+ return module_version_dict
74
93
75
94
76
95
def _is_valid_requirement_file (path ):
@@ -82,6 +101,32 @@ def _is_valid_requirement_file(path):
82
101
return False
83
102
84
103
104
+ def _parse_dependency_list (depedency_list : list ) -> dict :
105
+ """Placeholder docstring"""
106
+
107
+ # Divide a string into 2 part, first part is the module name
108
+ # and second part is its version constraint or the url
109
+ # checkout tests/unit/sagemaker/serve/detector/test_dependency_manager.py
110
+ # for examples
111
+ pattern = r"^([\w.-]+)(@[^,\n]+|((?:[<>=!~]=?[\w.*-]+,?)+)?)$"
112
+
113
+ module_version_dict = {}
114
+
115
+ for dependency in depedency_list :
116
+ if dependency .startswith ("#" ):
117
+ continue
118
+ match = re .match (pattern , dependency )
119
+ if match :
120
+ package = match .group (1 )
121
+ # Group 2 is either a URL or version constraint, if present
122
+ url_or_version = match .group (2 ) if match .group (2 ) else ""
123
+ module_version_dict .update ({package : url_or_version })
124
+ else :
125
+ module_version_dict .update ({dependency : "" })
126
+
127
+ return module_version_dict
128
+
129
+
85
130
# only required for dev testing
86
131
def prepare_wheel (code_artifact_client , whl_dir : str ):
87
132
"""Placeholder docstring"""
0 commit comments