1818CHANGE_SUFFIXES = [".py" , ".rst" , ".cfg" , "" ]
1919# Files not to change
2020IGNORE_FILES = ["CHANGELOG.rst" , "test_boilerplate_removed.py" , "_version_git.py" ]
21- # Markers to ignore from
22- IGNORE_MARKERS = {"CONTRIBUTING.rst" : "\n Updating the tools\n " }
21+ # Ranges to ignore between
22+ IGNORE_RANGES = {
23+ "CONTRIBUTING.rst" : ("\n Updating the tools\n " , None ),
24+ "api.rst" : (
25+ "Version number as calculated by" ,
26+ "https://github.com/dls-controls/versiongit" ,
27+ ),
28+ }
2329
2430
2531def git (* args , cwd = None ) -> str :
@@ -54,6 +60,15 @@ def merge_skeleton(
5460 package = override_package or repo
5561 valid = re .match ("[a-zA-Z][a-zA-Z_0-9]*$" , package )
5662 assert valid , f"'{ package } ' is not a valid python package name"
63+
64+ def replace_text (text : str ) -> str :
65+ text = text .replace ("dls-controls" , org )
66+ text = text .replace ("dls-python3-skeleton" , repo )
67+ text = text .replace ("dls_python3_skeleton" , package )
68+ text = text .replace ("Firstname Lastname" , full_name )
69+ text = text .
replace (
"[email protected] " ,
email )
70+ return text
71+
5772 with GitTemporaryDirectory () as git_tmp :
5873 # Clone existing repo into tmp so we don't mess up if we fail
5974 # half way through
@@ -75,17 +90,25 @@ def merge_skeleton(
7590 child = Path (git_tmp .name ) / relative_child
7691 if child .suffix in CHANGE_SUFFIXES and child .name not in IGNORE_FILES :
7792 text = child .read_text ()
78- marker = IGNORE_MARKERS .get (child .name , "" )
79- if marker :
80- text , marker , ignore = text .partition (marker )
93+ start_search , end_search = IGNORE_RANGES .get (child .name , (None , None ))
94+ if start_search :
95+ start_ignore = text .find (start_search )
96+ assert start_ignore > 0 , f"{ start_search } not in { child .name } "
97+ if end_search :
98+ end_ignore = text .find (end_search , start_ignore ) + len (
99+ end_search
100+ )
101+ assert end_ignore > 0 , f"{ end_search } not in { child .name } "
102+ else :
103+ end_ignore = len (text )
81104 else :
82- ignore = ""
83- text = text . replace ( "dls-controls" , org )
84- text = text . replace ( "dls-python3-skeleton" , repo )
85- text = text . replace ( "dls_python3_skeleton" , package )
86- text = text . replace ( "Firstname Lastname" , full_name )
87- text = text . replace ( "[email protected] " , email )
88- child . write_text ( text + marker + ignore )
105+ start_ignore = 0
106+ end_ignore = 0
107+ child . write_text (
108+ replace_text ( text [: start_ignore ] )
109+ + text [ start_ignore : end_ignore ]
110+ + replace_text ( text [ end_ignore :] )
111+ )
89112 # Commit what we have and push to the original repo
90113 git_tmp ("commit" , "-a" , "-m" , f"Rename dls-python3-skeleton -> { repo } " )
91114 git_tmp ("push" , "origin" , MERGE_BRANCH )
0 commit comments