|
1 | 1 | # ============================================================================= |
2 | 2 | # EXTRACTOR UTILS |
3 | 3 | # ============================================================================= |
| 4 | + |
| 5 | + |
4 | 6 | import pyparsing |
5 | 7 |
|
6 | 8 | if not hasattr(pyparsing, "DelimitedList"): |
|
9 | 11 | from googleapiclient.discovery import build |
10 | 12 | from google.cloud import storage |
11 | 13 | from typing import Any, TypeAlias |
| 14 | +from datetime import datetime as dt |
| 15 | +from zoneinfo import ZoneInfo |
| 16 | + |
12 | 17 |
|
13 | 18 | GoogleDriveService: TypeAlias = Any |
14 | 19 |
|
@@ -80,6 +85,16 @@ def check_handshake(service: GoogleDriveService, folder_id: str) -> bool: |
80 | 85 | return "file-upload=safe" in content |
81 | 86 |
|
82 | 87 |
|
| 88 | +def get_target_folder_name(folder_name: str): |
| 89 | + """ |
| 90 | + Creates target folder name with recent date as suffix (e.g. operations_YYYY_MM_DD). |
| 91 | + """ |
| 92 | + |
| 93 | + pht_now = dt.now(ZoneInfo("Asia/Manila")) |
| 94 | + today = pht_now.strftime("%Y_%m_%d") |
| 95 | + return f"{folder_name}_{today}" |
| 96 | + |
| 97 | + |
83 | 98 | # ------------------------------------------------------------ |
84 | 99 | # API UTILITIES |
85 | 100 | # ------------------------------------------------------------ |
@@ -135,3 +150,24 @@ def upload_to_gcs( |
135 | 150 | bucket = client.bucket(bucket_name) |
136 | 151 | blob = bucket.blob(destination_blob_name) |
137 | 152 | blob.upload_from_string(data, content_type=content_type) |
| 153 | + |
| 154 | + |
| 155 | +def plant_success_flag(bucket_name: str, folder_path: str): |
| 156 | + """ |
| 157 | + Uploads an empty _SUCCESS.txt file to signal the pipeline. |
| 158 | +
|
| 159 | + Args: |
| 160 | + bucket_name: Target GCS bucket. |
| 161 | + folder_path: The full path for the success mark |
| 162 | + """ |
| 163 | + |
| 164 | + bucket_name = bucket_name.replace("gs://", "") |
| 165 | + |
| 166 | + client = storage.Client() |
| 167 | + bucket = client.bucket(bucket_name) |
| 168 | + blob = bucket.blob(folder_path) |
| 169 | + |
| 170 | + # Upload an empty string just to create the file |
| 171 | + blob.upload_from_string("") |
| 172 | + |
| 173 | + print("[INFO] Flag planted successfully") |
0 commit comments