@@ -812,88 +812,45 @@ def raw_permalink(raw_link):
812
812
813
813
@dev_group .command ('trim-version-lock' )
814
814
@click .argument ('stack_version' )
815
- @click .option ('--skip-rule-updates' , is_flag = True , help = 'Skip updating the rules' )
816
815
@click .option ('--dry-run' , is_flag = True , help = 'Print the changes rather than saving the file' )
817
- def trim_version_lock (stack_version : str , skip_rule_updates : bool , dry_run : bool ):
816
+ def trim_version_lock (stack_version : str , dry_run : bool ):
818
817
"""Trim all previous entries within the version lock file which are lower than the min_version."""
819
818
stack_versions = get_stack_versions ()
820
819
assert stack_version in stack_versions , \
821
820
f'Unknown min_version ({ stack_version } ), expected: { ", " .join (stack_versions )} '
822
821
823
822
min_version = Version .parse (stack_version )
824
823
version_lock_dict = default_version_lock .version_lock .to_dict ()
825
- removed = defaultdict (list )
826
- rule_msv_drops = []
827
-
828
- today = time .strftime ('%Y/%m/%d' )
829
- rc : RuleCollection | None = None
830
- if dry_run :
831
- rc = RuleCollection ()
832
- else :
833
- if not skip_rule_updates :
834
- click .echo ('Loading rules ...' )
835
- rc = RuleCollection .default ()
824
+ removed = {}
836
825
837
826
for rule_id , lock in version_lock_dict .items ():
838
- file_min_stack : Version | None = None
839
- if 'min_stack_version' in lock :
840
- file_min_stack = Version .parse ((lock ['min_stack_version' ]), optional_minor_and_patch = True )
841
- if file_min_stack <= min_version :
842
- removed [rule_id ].append (
843
- f'locked min_stack_version <= { min_version } - { "will remove" if dry_run else "removing" } !'
844
- )
845
- rule_msv_drops .append (rule_id )
846
- file_min_stack = None
847
-
848
- if not dry_run :
849
- lock .pop ('min_stack_version' )
850
- if not skip_rule_updates :
851
- # remove the min_stack_version and min_stack_comments from rules as well (and update date)
852
- rule = rc .id_map .get (rule_id )
853
- if rule :
854
- new_meta = dataclasses .replace (
855
- rule .contents .metadata ,
856
- updated_date = today ,
857
- min_stack_version = None ,
858
- min_stack_comments = None
859
- )
860
- contents = dataclasses .replace (rule .contents , metadata = new_meta )
861
- new_rule = TOMLRule (contents = contents , path = rule .path )
862
- new_rule .save_toml ()
863
- removed [rule_id ].append ('rule min_stack_version dropped' )
864
- else :
865
- removed [rule_id ].append ('rule not found to update!' )
866
-
867
827
if 'previous' in lock :
868
828
prev_vers = [Version .parse (v , optional_minor_and_patch = True ) for v in list (lock ['previous' ])]
869
- outdated_vers = [v for v in prev_vers if v < min_version ]
829
+ outdated_vers = [f" { v . major } . { v . minor } " for v in prev_vers if v < min_version ]
870
830
871
831
if not outdated_vers :
872
832
continue
873
833
874
834
# we want to remove all "old" versions, but save the latest that is >= the min version supplied as the new
875
835
# stack_version.
876
- latest_version = max (outdated_vers )
877
836
837
+ if dry_run :
838
+ outdated_minus_current = [str (v ) for v in outdated_vers if v < stack_version ]
839
+ if outdated_minus_current :
840
+ removed [rule_id ] = outdated_minus_current
878
841
for outdated in outdated_vers :
879
- short_outdated = f"{ outdated .major } .{ outdated .minor } "
880
- popped = lock ['previous' ].pop (str (short_outdated ))
881
- # the core of the update - we only need to keep previous entries that are newer than the min supported
882
- # version (from stack-schema-map and stack-version parameter) and older than the locked
883
- # min_stack_version for a given rule, if one exists
884
- if file_min_stack and outdated == latest_version and outdated < file_min_stack :
885
- lock ['previous' ][f'{ min_version .major } .{ min_version .minor } ' ] = popped
886
- removed [rule_id ].append (f'{ short_outdated } updated to: { min_version .major } .{ min_version .minor } ' )
887
- else :
888
- removed [rule_id ].append (f'{ outdated } dropped' )
842
+ popped = lock ['previous' ].pop (str (outdated ))
843
+ if outdated >= stack_version :
844
+ lock ['previous' ][str (Version (stack_version [:2 ]))] = popped
889
845
890
846
# remove the whole previous entry if it is now blank
891
847
if not lock ['previous' ]:
892
848
lock .pop ('previous' )
893
849
894
- click .echo (f'Changes { "that will be " if dry_run else "" } applied:' if removed else 'No changes' )
895
- click .echo ('\n ' .join (f'{ k } : { ", " .join (v )} ' for k , v in removed .items ()))
896
- if not dry_run :
850
+ if dry_run :
851
+ click .echo (f'The following versions would be collapsed to { stack_version } :' if removed else 'No changes' )
852
+ click .echo ('\n ' .join (f'{ k } : { ", " .join (v )} ' for k , v in removed .items ()))
853
+ else :
897
854
new_lock = VersionLockFile .from_dict (dict (data = version_lock_dict ))
898
855
new_lock .save_to_file ()
899
856
0 commit comments