@@ -177,35 +177,6 @@ def prepare_dependency_archive(directory: str) -> None:
177177 logger .debug (f"Dependencies downloaded and archived to { archive_file } " )
178178
179179
180- def zip_and_upload_directory (directory : str , name : str ) -> None :
181- # file_upload_url = unescape(file_upload_url)
182-
183- logger .debug (f"Zipping directory... { directory } " )
184-
185- # Create a zip file excluding .DS_Store files
186- import zipfile
187-
188- zip_filename = f"{ name } .zip"
189- with zipfile .ZipFile (zip_filename , 'w' , zipfile .ZIP_DEFLATED ) as zipf :
190- for root , dirs , files in os .walk (directory ):
191- # Skip .DS_Store files when adding to zip
192- for file in files :
193- if file != '.DS_Store' :
194- file_path = os .path .join (root , file )
195- # Preserve relative path structure in the zip file
196- arcname = os .path .relpath (file_path , directory )
197- zipf .write (file_path , arcname )
198-
199- logger .debug (f"Created zip file: { zip_filename } (excluding .DS_Store files)" )
200-
201- # logger.debug(f"Uploading deployment to {file_upload_url}")
202- # with open(ZIP_FILE_NAME, "rb") as zip_file:
203- # response = requests.put(
204- # file_upload_url, data=zip_file, headers={"Content-Type": "application/zip"}
205- # )
206- # response.raise_for_status()
207-
208-
209180class DeploymentsResponse (BaseModel ):
210181 deploymentStatus : str
211182
@@ -342,59 +313,70 @@ def create_data_transform(
342313 response = _make_api_call (url , "POST" , token = access_token .access_token , json = body )
343314 return response
344315
316+
345317def has_nonempty_requirements_file (directory : str ) -> bool :
346318 """
347- Check if requirements.txt exists in the given directory and has at least one non-comment line.
319+ Check if requirements.txt exists in the given directory and has at least
320+ one non-comment line.
348321 Args:
349322 directory (str): The directory to check for requirements.txt.
350323 Returns:
351- bool: True if requirements.txt exists and has a non-comment line, False otherwise.
324+ bool: True if requirements.txt exists and has a non-comment line,
325+ False otherwise.
352326 """
353327 # Look for requirements.txt in the parent directory of the given directory
354328 requirements_path = os .path .join (os .path .dirname (directory ), "requirements.txt" )
355- print (requirements_path )
356329
357330 try :
358331 if os .path .isfile (requirements_path ):
359- #print the contents of the file
360- with open (requirements_path , "r" , encoding = "utf-8" ) as f :
361- print (f .read ())
362332 with open (requirements_path , "r" , encoding = "utf-8" ) as f :
363333 for line in f :
364- # Consider non-empty if any line is not a comment (ignoring leading whitespace)
365- if line .strip () and not line .lstrip ().startswith ('#' ):
334+ # Consider non-empty if any line is not a comment (ignoring
335+ # leading whitespace)
336+ if line .strip () and not line .lstrip ().startswith ("#" ):
366337 return True
367338 except Exception as e :
368339 logger .error (f"Error reading requirements.txt: { e } " )
369340 return False
370341
371342
343+ def upload_zip (file_upload_url : str ) -> None :
344+ file_upload_url = unescape (file_upload_url )
345+ with open (ZIP_FILE_NAME , "rb" ) as zip_file :
346+ response = requests .put (
347+ file_upload_url , data = zip_file , headers = {"Content-Type" : "application/zip" }
348+ )
349+ response .raise_for_status ()
350+
351+
372352def zip (
373353 directory : str ,
374- metadata : TransformationJobMetadata ,
375- credentials : Credentials ,
376- name : str ,
377- callback = None ,
378- ) -> AccessTokenResponse :
379- """Deploy a data transform in the DataCloud."""
380- access_token = _retrieve_access_token (credentials )
354+ ):
355+ # Create a zip file excluding .DS_Store files
356+ import zipfile
381357
382358 # prepare payload only if requirements.txt is non-empty
383359 if has_nonempty_requirements_file (directory ):
384360 prepare_dependency_archive (directory )
385361 else :
386- logger .info (f"Skipping dependency archive: requirements.txt is missing or empty in { directory } " )
387- # create_data_transform_config(directory)
362+ logger .info (
363+ f"Skipping dependency archive: requirements.txt is missing or empty "
364+ f"in { directory } "
365+ )
388366
389- # create deployment and upload payload
390- # deployment = create_deployment(access_token, metadata)
391- zip_and_upload_directory (directory , name )
392- #, deployment.fileUploadUrl)
393- # wait_for_deployment(access_token, metadata, callback)
367+ logger .debug (f"Zipping directory... { directory } " )
394368
395- # create data transform
396- # create_data_transform(directory, access_token, metadata)
397- return access_token
369+ with zipfile .ZipFile (ZIP_FILE_NAME , "w" , zipfile .ZIP_DEFLATED ) as zipf :
370+ for root , dirs , files in os .walk (directory ):
371+ # Skip .DS_Store files when adding to zip
372+ for file in files :
373+ if file != ".DS_Store" :
374+ file_path = os .path .join (root , file )
375+ # Preserve relative path structure in the zip file
376+ arcname = os .path .relpath (file_path , directory )
377+ zipf .write (file_path , arcname )
378+
379+ logger .debug (f"Created zip file: { ZIP_FILE_NAME } " )
398380
399381
400382def deploy_full (
@@ -412,7 +394,8 @@ def deploy_full(
412394
413395 # create deployment and upload payload
414396 deployment = create_deployment (access_token , metadata )
415- zip_and_upload_directory (directory , deployment .fileUploadUrl )
397+ zip (directory )
398+ upload_zip (deployment .fileUploadUrl )
416399 wait_for_deployment (access_token , metadata , callback )
417400
418401 # create data transform
0 commit comments