Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions gcp/workers/exporter/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.
"""OSV Exporter."""
import argparse
import base64
import concurrent.futures
import csv
import json
Expand All @@ -22,6 +23,8 @@
import zipfile
from typing import List

import google_crc32c

from google.cloud import ndb
from google.cloud import storage
from google.cloud.storage import retry
Expand Down Expand Up @@ -211,10 +214,30 @@ def _export_to_file_and_zipfile(bug: osv.Bug):
f'{ecosystem}/{filename}')


def _calculate_crc32c(file_path: str) -> str:
"""Calculate CRC32C hash of a file and return as base64 string."""
crc = 0
with open(file_path, 'rb') as f:
while chunk := f.read(8192):
crc = google_crc32c.extend(crc, chunk)
return base64.b64encode(crc.to_bytes(4, byteorder='big')).decode('utf-8')


def upload_single(bucket: Bucket, source_path: str, target_path: str):
"""Upload a single file to a GCS bucket."""
logging.info('Uploading %s', target_path)
"""Upload a single file to a GCS bucket if content has changed.

Compares CRC32C hashes to avoid uploading unchanged files.
See https://github.com/google/osv.dev/issues/3513
"""
try:
existing_blob = bucket.get_blob(target_path)
if existing_blob and existing_blob.crc32c:
local_crc = _calculate_crc32c(source_path)
if local_crc == existing_blob.crc32c:
logging.debug('Skipping %s (unchanged)', target_path)
return

logging.info('Uploading %s', target_path)
blob = bucket.blob(target_path)
blob.upload_from_filename(source_path, retry=retry.DEFAULT_RETRY)
except Exception as e:
Expand Down
2 changes: 1 addition & 1 deletion gcp/workers/worker/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gcp/workers/worker/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies = [
"google-cloud-pubsub==2.34.0",
"google-cloud-ndb==2.4.0",
"google-cloud-storage==2.19.0",
"google-crc32c>=1.0.0",
"pyyaml==6.0.3",
"redis==5.3.1",
"packageurl-python==0.17.6",
Expand Down
Loading