@@ -169,25 +169,14 @@ def prepare_dependency_archive(directory: str) -> None:
169169 archive_file = os .path .join (archives_dir , DEPENDENCIES_ARCHIVE_NAME )
170170 with tarfile .open (archive_file , "w:gz" ) as tar :
171171 for file in os .listdir (temp_dir ):
172+ # Exclude requirements.txt from the archive
173+ if file == "requirements.txt" :
174+ continue
172175 tar .add (os .path .join (temp_dir , file ), arcname = file )
173176
174177 logger .debug (f"Dependencies downloaded and archived to { archive_file } " )
175178
176179
177- def zip_and_upload_directory (directory : str , file_upload_url : str ) -> None :
178- file_upload_url = unescape (file_upload_url )
179-
180- logger .debug (f"Zipping directory... { directory } " )
181- shutil .make_archive (ZIP_FILE_NAME .rstrip (".zip" ), "zip" , directory )
182-
183- logger .debug (f"Uploading deployment to { file_upload_url } " )
184- with open (ZIP_FILE_NAME , "rb" ) as zip_file :
185- response = requests .put (
186- file_upload_url , data = zip_file , headers = {"Content-Type" : "application/zip" }
187- )
188- response .raise_for_status ()
189-
190-
191180class DeploymentsResponse (BaseModel ):
192181 deploymentStatus : str
193182
@@ -325,6 +314,71 @@ def create_data_transform(
325314 return response
326315
327316
317+ def has_nonempty_requirements_file (directory : str ) -> bool :
318+ """
319+ Check if requirements.txt exists in the given directory and has at least
320+ one non-comment line.
321+ Args:
322+ directory (str): The directory to check for requirements.txt.
323+ Returns:
324+ bool: True if requirements.txt exists and has a non-comment line,
325+ False otherwise.
326+ """
327+ # Look for requirements.txt in the parent directory of the given directory
328+ requirements_path = os .path .join (os .path .dirname (directory ), "requirements.txt" )
329+
330+ try :
331+ if os .path .isfile (requirements_path ):
332+ with open (requirements_path , "r" , encoding = "utf-8" ) as f :
333+ for line in f :
334+ # Consider non-empty if any line is not a comment (ignoring
335+ # leading whitespace)
336+ if line .strip () and not line .lstrip ().startswith ("#" ):
337+ return True
338+ except Exception as e :
339+ logger .error (f"Error reading requirements.txt: { e } " )
340+ return False
341+
342+
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+
352+ def zip (
353+ directory : str ,
354+ ):
355+ # Create a zip file excluding .DS_Store files
356+ import zipfile
357+
358+ # prepare payload only if requirements.txt is non-empty
359+ if has_nonempty_requirements_file (directory ):
360+ prepare_dependency_archive (directory )
361+ else :
362+ logger .info (
363+ f"Skipping dependency archive: requirements.txt is missing or empty "
364+ f"in { directory } "
365+ )
366+
367+ logger .debug (f"Zipping directory... { directory } " )
368+
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 } " )
380+
381+
328382def deploy_full (
329383 directory : str ,
330384 metadata : TransformationJobMetadata ,
@@ -340,7 +394,8 @@ def deploy_full(
340394
341395 # create deployment and upload payload
342396 deployment = create_deployment (access_token , metadata )
343- zip_and_upload_directory (directory , deployment .fileUploadUrl )
397+ zip (directory )
398+ upload_zip (deployment .fileUploadUrl )
344399 wait_for_deployment (access_token , metadata , callback )
345400
346401 # create data transform
0 commit comments