Skip to content

Commit 1ce0dfa

Browse files
committed
improve git checks/behavior and api quota exceeded handling
1 parent 63167fc commit 1ce0dfa

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

scripts/1-fetch/gcs_fetched.py

+20-15
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,19 @@ def query_gcs(args, service, last_completed_plan_index, plan):
220220

221221
except HttpError as e:
222222
if e.status_code == 429:
223-
LOGGER.warning(
224-
f"{e.status_code}: {e.reason}. retrying in"
225-
f" {initial_delay} seconds"
226-
)
227-
time.sleep(initial_delay)
228-
initial_delay *= 2 # Exponential backoff
223+
if (
224+
"Quota exceeded" in e.reason
225+
and "Queries per day" in e.reason
226+
):
227+
LOGGER.warning(f"{e.status_code}: {e.reason}.")
228+
return # abort queries
229+
else:
230+
LOGGER.warning(
231+
f"{e.status_code}: {e.reason}. retrying in"
232+
f" {initial_delay} seconds"
233+
)
234+
time.sleep(initial_delay)
235+
initial_delay *= 2 # Exponential backoff
229236
else:
230237
LOGGER.error(f"Error fetching results: {e}")
231238
if success:
@@ -249,15 +256,13 @@ def main():
249256
return
250257
plan = load_plan()
251258
query_gcs(args, service, last_completed_plan_index, plan)
252-
if args.enable_git:
253-
shared.add_and_commit(
254-
args,
255-
PATHS["repo"],
256-
PATHS["data_quarter"],
257-
"Add and commit new Google Custom Search (GCS) data for"
258-
f" {QUARTER}",
259-
)
260-
shared.push_changes(args, PATHS["repo"])
259+
args = shared.git_add_and_commit(
260+
args,
261+
PATHS["repo"],
262+
PATHS["data_quarter"],
263+
"Add and commit new Google Custom Search (GCS) data for" f" {QUARTER}",
264+
)
265+
shared.git_push_changes(args, PATHS["repo"])
261266

262267

263268
if __name__ == "__main__":

scripts/shared.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ def log_paths(logger, paths):
6060
logger.info(f"PATHS:{paths_list}")
6161

6262

63-
def fetch_and_merge(repo_path, branch=None):
63+
def git_fetch_and_merge(args, repo_path, branch=None):
64+
if not args.enable_git:
65+
return
6466
try:
6567
repo = Repo(repo_path)
6668
origin = repo.remote(name="origin")
@@ -87,12 +89,16 @@ def fetch_and_merge(repo_path, branch=None):
8789
raise QuantifyingException(f"Error during fetch and merge: {e}", 1)
8890

8991

90-
def add_and_commit(repo_path, add_path, message):
92+
def git_add_and_commit(args, repo_path, add_path, message):
93+
if not args.enable_git:
94+
return args
9195
try:
9296
repo = Repo(repo_path)
93-
if not repo.is_dirty(untracked_files=True):
94-
logging.info("No changes to commit")
95-
return
97+
if not repo.is_dirty(untracked_files=True, path=add_path):
98+
relative_add_path = os.path.relpath(add_path, repo_path)
99+
logging.info(f"No changes to commit in: {relative_add_path}")
100+
args.enable_git = False
101+
return args
96102
repo.index.add([add_path])
97103
repo.index.commit(message)
98104
logging.info(f"Changes committed: {message}")
@@ -104,7 +110,9 @@ def add_and_commit(repo_path, add_path, message):
104110
raise QuantifyingException(f"Error during add and commit: {e}", 1)
105111

106112

107-
def push_changes(repo_path):
113+
def git_push_changes(args, repo_path):
114+
if not args.enable_git:
115+
return
108116
try:
109117
repo = Repo(repo_path)
110118
origin = repo.remote(name="origin")

0 commit comments

Comments
 (0)