5
5
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
6
6
7
7
import cgi
8
+ import re
8
9
import json
9
10
import logging
10
11
import os
@@ -1471,9 +1472,7 @@ def _download_file_description_artifact(self) -> Tuple[Union[str, List[str]], in
1471
1472
1472
1473
def add_artifact (
1473
1474
self ,
1474
- namespace : str ,
1475
- bucket : str ,
1476
- prefix : Optional [str ] = None ,
1475
+ uri : str ,
1477
1476
files : Optional [List [str ]] = None ,
1478
1477
):
1479
1478
"""
@@ -1497,6 +1496,9 @@ def add_artifact(
1497
1496
If `files` is provided, it only retrieves information about objects with matching file names.
1498
1497
- If no objects are found to add to the model description, a ValueError is raised.
1499
1498
"""
1499
+
1500
+ bucket , namespace , prefix = self ._extract_oci_uri_components (uri )
1501
+
1500
1502
if self .model_file_description == None :
1501
1503
self .empty_json = {
1502
1504
"version" : "1.0" ,
@@ -1510,7 +1512,7 @@ def add_artifact(
1510
1512
self .object_storage_client = oc .OCIClientFactory (** authData ).object_storage
1511
1513
1512
1514
# Remove if the model already exists
1513
- self .remove_artifact (namespace , bucket , prefix )
1515
+ self .remove_artifact (uri = uri )
1514
1516
1515
1517
def check_if_file_exists (fileName ):
1516
1518
isExists = False
@@ -1585,7 +1587,7 @@ def list_obj_versions_unpaginated():
1585
1587
)
1586
1588
self .set_spec (self .CONST_MODEL_FILE_DESCRIPTION , tmp_model_file_description )
1587
1589
1588
- def remove_artifact (self , namespace : str , bucket : str , prefix : Optional [ str ] = None ):
1590
+ def remove_artifact (self , uri : str ):
1589
1591
"""
1590
1592
Removes information about objects in a specified bucket from the model description JSON.
1591
1593
@@ -1605,6 +1607,8 @@ def remove_artifact(self, namespace: str, bucket: str, prefix: Optional[str] = N
1605
1607
- If no matching model is found, the method returns without making any changes.
1606
1608
"""
1607
1609
1610
+ bucket , namespace , prefix = self ._extract_oci_uri_components (uri )
1611
+
1608
1612
def findModelIdx ():
1609
1613
for idx , model in enumerate (self .model_file_description ["models" ]):
1610
1614
if (
@@ -1623,4 +1627,25 @@ def findModelIdx():
1623
1627
return
1624
1628
else :
1625
1629
# model found case
1626
- self .model_file_description ["models" ].pop (modelSearchIdx )
1630
+ self .model_file_description ["models" ].pop (modelSearchIdx )
1631
+
1632
+ def _extract_oci_uri_components (self , uri : str ):
1633
+ # Define the regular expression pattern to match the URI format
1634
+ pattern = r"oci://(?P<bucket_name>[^@]+)@(?P<namespace>[^/]+)(?:/(?P<prefix>.*))?"
1635
+
1636
+ # Use re.match to apply the pattern to the URI
1637
+ match = re .match (pattern , uri )
1638
+
1639
+ if match :
1640
+ # Extract named groups using the groupdict() method
1641
+ components = match .groupdict ()
1642
+ prefix = components .get ('prefix' , '' )
1643
+ # Treat a single trailing slash as no prefix
1644
+ if prefix == "" :
1645
+ return components ['bucket_name' ], components ['namespace' ], ''
1646
+ elif prefix == "/" :
1647
+ return components ['bucket_name' ], components ['namespace' ], ''
1648
+ else :
1649
+ return components ['bucket_name' ], components ['namespace' ], prefix
1650
+ else :
1651
+ raise ValueError ("The URI format is incorrect" )
0 commit comments