aic-utils 2.0.4__tar.gz → 2.0.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {aic_utils-2.0.4 → aic_utils-2.0.6}/PKG-INFO +1 -1
- {aic_utils-2.0.4 → aic_utils-2.0.6}/aic_utils/__init__.py +1 -1
- {aic_utils-2.0.4 → aic_utils-2.0.6}/aic_utils/gitlabmanager.py +141 -49
- {aic_utils-2.0.4 → aic_utils-2.0.6}/aic_utils.egg-info/PKG-INFO +1 -1
- {aic_utils-2.0.4 → aic_utils-2.0.6}/setup.py +1 -1
- {aic_utils-2.0.4 → aic_utils-2.0.6}/README.md +0 -0
- {aic_utils-2.0.4 → aic_utils-2.0.6}/aic_utils/aic.py +0 -0
- {aic_utils-2.0.4 → aic_utils-2.0.6}/aic_utils/dataset_converter.py +0 -0
- {aic_utils-2.0.4 → aic_utils-2.0.6}/aic_utils/gitlab_init.py +0 -0
- {aic_utils-2.0.4 → aic_utils-2.0.6}/aic_utils/slack_logger.py +0 -0
- {aic_utils-2.0.4 → aic_utils-2.0.6}/aic_utils.egg-info/SOURCES.txt +0 -0
- {aic_utils-2.0.4 → aic_utils-2.0.6}/aic_utils.egg-info/dependency_links.txt +0 -0
- {aic_utils-2.0.4 → aic_utils-2.0.6}/aic_utils.egg-info/requires.txt +0 -0
- {aic_utils-2.0.4 → aic_utils-2.0.6}/aic_utils.egg-info/top_level.txt +0 -0
- {aic_utils-2.0.4 → aic_utils-2.0.6}/setup.cfg +0 -0
|
@@ -555,14 +555,27 @@ class GitLabManager:
|
|
|
555
555
|
commit_msg_prefix=f"Sync code for pipeline '{pipeline_name}'",
|
|
556
556
|
)
|
|
557
557
|
|
|
558
|
-
def push_to_git(self):
|
|
558
|
+
def push_to_git(self, prune: bool = False, force: bool = False):
|
|
559
559
|
"""
|
|
560
|
-
Push pipeline JSON and code to GitLab using
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
560
|
+
Push pipeline JSON and code to GitLab using batched commits API (≤50
|
|
561
|
+
file writes per commit) with content-hash change detection so unchanged
|
|
562
|
+
files produce no commit.
|
|
563
|
+
|
|
564
|
+
Args:
|
|
565
|
+
prune: when True, also delete `config/<name>.json` and `code/<name>/...`
|
|
566
|
+
for any pipeline not in `self.aic.pipeline_configs`. **Refuses
|
|
567
|
+
to run unless the AIC instance was loaded with the FULL workspace**
|
|
568
|
+
(`pipelines=['*']` or equivalent), to avoid deleting files for
|
|
569
|
+
pipelines that just happened to not be loaded into this session.
|
|
570
|
+
force: when True, suppresses the interactive confirmation prompt
|
|
571
|
+
that normally guards pushes to the production (default) branch.
|
|
572
|
+
Use this for automated/cron jobs. Default False.
|
|
573
|
+
|
|
574
|
+
Change detection: respects `self.use_hash_comparison` (constructor flag,
|
|
575
|
+
default True). Compares git blob SHAs from a single tree-list call vs
|
|
576
|
+
locally-computed git blob SHAs of new content. No per-file fetches.
|
|
564
577
|
"""
|
|
565
|
-
if self._branch == self.default_branch:
|
|
578
|
+
if self._branch == self.default_branch and not force:
|
|
566
579
|
force_push = input('⚠️ WARNING: You are attempting to overwrite the Production branch with existing AIC code. Proceed? (y/n)')
|
|
567
580
|
if force_push.lower() != 'y':
|
|
568
581
|
raise ValueError('Production Push refused. Exiting.')
|
|
@@ -577,11 +590,22 @@ class GitLabManager:
|
|
|
577
590
|
f"Allowed hotfix prefixes: {self._hotfix_prefixes}\n"
|
|
578
591
|
)
|
|
579
592
|
|
|
593
|
+
# Safety gate for prune BEFORE we do any work
|
|
594
|
+
if prune:
|
|
595
|
+
loaded = len(self.aic.pipeline_configs)
|
|
596
|
+
total_in_ws = len(self.aic.pipelines)
|
|
597
|
+
if loaded < total_in_ws:
|
|
598
|
+
raise RuntimeError(
|
|
599
|
+
f"❌ prune=True requires the FULL workspace to be loaded "
|
|
600
|
+
f"(pipelines=['*'] in your AIC(...) call). "
|
|
601
|
+
f"Currently {loaded}/{total_in_ws} pipelines loaded; pruning "
|
|
602
|
+
f"now would delete files for the {total_in_ws - loaded} unloaded pipelines."
|
|
603
|
+
)
|
|
604
|
+
|
|
580
605
|
self.ensure_branch_exists(branch_name=self._branch, ref=self._branch)
|
|
581
606
|
|
|
582
607
|
# Build the full list of (file_path, content) pairs for this push.
|
|
583
608
|
all_pairs = []
|
|
584
|
-
results = []
|
|
585
609
|
per_pipeline_pairs = {}
|
|
586
610
|
for cfg in self.aic.pipeline_configs:
|
|
587
611
|
name = cfg['name']
|
|
@@ -592,49 +616,101 @@ class GitLabManager:
|
|
|
592
616
|
per_pipeline_pairs[name] = pairs
|
|
593
617
|
all_pairs.extend(pairs)
|
|
594
618
|
|
|
595
|
-
|
|
596
|
-
print("No pipeline content to push.")
|
|
597
|
-
self._send_email(*self._format_push_summary(self._branch, []), testing=False)
|
|
598
|
-
return
|
|
599
|
-
|
|
600
|
-
# Pre-fetch existing files on branch so we can choose create vs update.
|
|
619
|
+
# Pre-fetch existing tree (path → blob SHA) for change detection + create/update decisions
|
|
601
620
|
try:
|
|
602
|
-
|
|
621
|
+
existing_shas = self._list_blob_shas(self._branch)
|
|
603
622
|
except Exception as e:
|
|
604
|
-
print(f"⚠️ Could not list existing tree on '{self._branch}' ({e});
|
|
605
|
-
|
|
623
|
+
print(f"⚠️ Could not list existing tree on '{self._branch}' ({e}); proceeding without change detection.")
|
|
624
|
+
existing_shas = None
|
|
606
625
|
|
|
607
626
|
actions = []
|
|
608
|
-
for
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
627
|
+
per_pipeline_status = {name: 'Skipped' for name in per_pipeline_pairs}
|
|
628
|
+
per_pipeline_changed_files = {name: 0 for name in per_pipeline_pairs}
|
|
629
|
+
skipped_unchanged = 0
|
|
630
|
+
for cfg in self.aic.pipeline_configs:
|
|
631
|
+
name = cfg['name']
|
|
632
|
+
for path, content in per_pipeline_pairs[name]:
|
|
633
|
+
if existing_shas is not None:
|
|
634
|
+
old_sha = existing_shas.get(path)
|
|
635
|
+
if self.use_hash_comparison and old_sha is not None:
|
|
636
|
+
new_sha = self._git_blob_sha(content)
|
|
637
|
+
if old_sha == new_sha:
|
|
638
|
+
skipped_unchanged += 1
|
|
639
|
+
continue
|
|
640
|
+
action_type = "update" if old_sha is not None else "create"
|
|
641
|
+
else:
|
|
642
|
+
action_type = "update" # _commit_batched_actions auto-flips on 400
|
|
643
|
+
actions.append({"action": action_type, "file_path": path, "content": content})
|
|
644
|
+
per_pipeline_status[name] = 'Success'
|
|
645
|
+
per_pipeline_changed_files[name] += 1
|
|
646
|
+
|
|
647
|
+
# Compute prune deletions if requested
|
|
648
|
+
prune_deletes = []
|
|
649
|
+
if prune:
|
|
650
|
+
allowed = {cfg['name'] for cfg in self.aic.pipeline_configs}
|
|
651
|
+
paths_for_check = existing_shas.keys() if existing_shas is not None else self._list_all_paths(self._branch)
|
|
652
|
+
for path in paths_for_check:
|
|
653
|
+
if path.startswith('config/') and path.endswith('.json'):
|
|
654
|
+
pname = path[len('config/'):-len('.json')]
|
|
655
|
+
if pname not in allowed:
|
|
656
|
+
prune_deletes.append(path)
|
|
657
|
+
elif path.startswith('code/'):
|
|
658
|
+
parts = path[len('code/'):].split('/', 1)
|
|
659
|
+
if parts and parts[0] not in allowed:
|
|
660
|
+
prune_deletes.append(path)
|
|
661
|
+
|
|
662
|
+
n_changed = len(actions)
|
|
623
663
|
n_pipelines = len(self.aic.pipeline_configs)
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
664
|
+
|
|
665
|
+
if skipped_unchanged:
|
|
666
|
+
print(f"Change detection: {skipped_unchanged} unchanged file(s) skipped (hash match).")
|
|
667
|
+
|
|
668
|
+
if n_changed == 0 and not prune_deletes:
|
|
669
|
+
print(f"✓ No changes to push to '{self._branch}'.")
|
|
670
|
+
results = [(name, 'Skipped', 'No change') for name in per_pipeline_pairs]
|
|
671
|
+
subj, body = self._format_push_summary(self._branch, results)
|
|
672
|
+
self._send_email(subj, body, testing=False)
|
|
673
|
+
return
|
|
674
|
+
|
|
675
|
+
if n_changed > 0:
|
|
676
|
+
msg_prefix = f"Sync {n_pipelines} pipeline(s) ({n_changed} changed file(s)) from AIC workspace '{self.aic.workspace}'"
|
|
677
|
+
print(f"Pushing {n_changed} changed file(s) across {n_pipelines} pipeline(s) to '{self._branch}' in batches of 50...")
|
|
678
|
+
try:
|
|
679
|
+
self._commit_batched_actions(
|
|
680
|
+
actions=actions,
|
|
681
|
+
branch=self._branch,
|
|
682
|
+
commit_msg_prefix=msg_prefix,
|
|
683
|
+
)
|
|
684
|
+
except Exception as e:
|
|
685
|
+
print(f"❌ Batched push failed: {e}")
|
|
686
|
+
for name in per_pipeline_pairs:
|
|
687
|
+
per_pipeline_status[name] = 'Failure'
|
|
688
|
+
results = [(name, 'Failure', str(e)[:160]) for name in per_pipeline_pairs]
|
|
689
|
+
subj, body = self._format_push_summary(self._branch, results)
|
|
690
|
+
self._send_email(subj, body, testing=False)
|
|
691
|
+
return
|
|
692
|
+
|
|
693
|
+
if prune_deletes:
|
|
694
|
+
print(f"Prune: deleting {len(prune_deletes)} obsolete file(s) for pipelines not in current workspace state...")
|
|
627
695
|
self._commit_batched_actions(
|
|
628
|
-
actions=
|
|
696
|
+
actions=[{"action":"delete","file_path":p} for p in prune_deletes],
|
|
629
697
|
branch=self._branch,
|
|
630
|
-
commit_msg_prefix=
|
|
698
|
+
commit_msg_prefix=f"Prune {len(prune_deletes)} obsolete file(s) from workspace '{self.aic.workspace}'",
|
|
631
699
|
)
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
700
|
+
|
|
701
|
+
# Build per-pipeline results for email
|
|
702
|
+
results = []
|
|
703
|
+
for name in per_pipeline_pairs:
|
|
704
|
+
st = per_pipeline_status[name]
|
|
705
|
+
if st == 'Success':
|
|
706
|
+
msg = f"{per_pipeline_changed_files[name]} changed file(s)"
|
|
707
|
+
elif st == 'Skipped':
|
|
708
|
+
msg = 'No change'
|
|
709
|
+
else:
|
|
710
|
+
msg = 'Failed'
|
|
711
|
+
results.append((name, st, msg))
|
|
712
|
+
if prune_deletes:
|
|
713
|
+
results.append(('(prune)', 'Success', f"{len(prune_deletes)} file(s) deleted"))
|
|
638
714
|
|
|
639
715
|
subj, body = self._format_push_summary(self._branch, results)
|
|
640
716
|
self._send_email(subj, body, testing=False)
|
|
@@ -922,15 +998,17 @@ class GitLabManager:
|
|
|
922
998
|
f"(status {resp.status_code}): {resp.text}"
|
|
923
999
|
)
|
|
924
1000
|
|
|
925
|
-
def
|
|
1001
|
+
def _list_blob_shas(self, branch: str) -> dict:
|
|
926
1002
|
"""
|
|
927
|
-
Return
|
|
928
|
-
|
|
1003
|
+
Return {path: blob_sha} for every blob on `branch`. The blob_sha is
|
|
1004
|
+
Git's content-addressed SHA-1 (matches `git hash-object`), enabling
|
|
1005
|
+
change detection by computing the same hash locally on candidate
|
|
1006
|
+
new content and comparing.
|
|
929
1007
|
"""
|
|
930
1008
|
proj = self.project_path.replace('/', '%2F')
|
|
931
1009
|
page = 1
|
|
932
1010
|
per_page = 100
|
|
933
|
-
|
|
1011
|
+
out = {}
|
|
934
1012
|
while True:
|
|
935
1013
|
resp = requests.get(
|
|
936
1014
|
f"{self.gitlab_base}/projects/{proj}/repository/tree",
|
|
@@ -939,18 +1017,32 @@ class GitLabManager:
|
|
|
939
1017
|
timeout=30,
|
|
940
1018
|
)
|
|
941
1019
|
if resp.status_code == 404:
|
|
942
|
-
return
|
|
1020
|
+
return out
|
|
943
1021
|
resp.raise_for_status()
|
|
944
1022
|
items = resp.json()
|
|
945
1023
|
if not items:
|
|
946
1024
|
break
|
|
947
1025
|
for it in items:
|
|
948
1026
|
if it.get('type') == 'blob':
|
|
949
|
-
|
|
1027
|
+
out[it['path']] = it.get('id')
|
|
950
1028
|
if len(items) < per_page:
|
|
951
1029
|
break
|
|
952
1030
|
page += 1
|
|
953
|
-
return
|
|
1031
|
+
return out
|
|
1032
|
+
|
|
1033
|
+
def _list_all_paths(self, branch: str) -> list:
|
|
1034
|
+
"""Return every blob path on `branch`. Backed by `_list_blob_shas`."""
|
|
1035
|
+
return list(self._list_blob_shas(branch).keys())
|
|
1036
|
+
|
|
1037
|
+
@staticmethod
|
|
1038
|
+
def _git_blob_sha(content) -> str:
|
|
1039
|
+
"""
|
|
1040
|
+
Compute Git's blob SHA-1 for a file content (matches `git hash-object`).
|
|
1041
|
+
Used by `push_to_git` to detect unchanged files without a per-file fetch.
|
|
1042
|
+
"""
|
|
1043
|
+
data = content.encode('utf-8') if isinstance(content, str) else content
|
|
1044
|
+
header = f"blob {len(data)}\0".encode('utf-8')
|
|
1045
|
+
return hashlib.sha1(header + data).hexdigest()
|
|
954
1046
|
|
|
955
1047
|
def list_all_blobs(self, subpath: str) -> list[str]:
|
|
956
1048
|
"""
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|