@@ -73,62 +73,57 @@ def replace_text(text: str) -> str:
7373 f"{ MERGE_BRANCH } already exists. \
7474 Please run 'dls-python3-skeleton clean' to remove it."
7575 )
76- else :
77- with GitTemporaryDirectory () as git_tmp :
78- # Clone existing repo into tmp so we don't mess up if we fail
79- # half way through
80- git_tmp ("clone" , path , git_tmp .name )
81- # We will use this branch to put the skeleton changes on
82- git_tmp ("checkout" , "--orphan" , MERGE_BRANCH )
83- # Delete all the current files if there are any
84- git_tmp ("rm" , "-rf" , "." , "--ignore-unmatch" )
85- # And make sure src isn't there otherwise the git mv below
86- # will do the wrong thing
87- shutil .rmtree (git_tmp / "src" , ignore_errors = True )
88- # Merge in the skeleton commits
89- git_tmp ("pull" , SKELETON , "skeleton" )
90- # Move things around
91- git_tmp ("mv" , "src/dls_python3_skeleton" , f"src/{ package } " )
92- git_tmp (
93- "mv" , "tests/test_dls_python3_skeleton.py" , f"tests/test_{ package } .py"
94- )
95- # Change contents of all children known to git
96- for relative_child in git_tmp ("ls-files" ).splitlines ():
97- child = Path (git_tmp .name ) / relative_child
98- if child .suffix in CHANGE_SUFFIXES and child .name not in IGNORE_FILES :
99- text = child .read_text ()
100- start_search , end_search = IGNORE_RANGES .get (
101- child .name , (None , None )
102- )
103- if start_search :
104- start_ignore = text .find (start_search )
105- assert start_ignore > 0 , f"{ start_search } not in { child .name } "
106- if end_search :
107- end_ignore = text .find (end_search , start_ignore ) + len (
108- end_search
109- )
110- assert end_ignore > 0 , f"{ end_search } not in { child .name } "
111- else :
112- end_ignore = len (text )
76+ with GitTemporaryDirectory () as git_tmp :
77+ # Clone existing repo into tmp so we don't mess up if we fail
78+ # half way through
79+ git_tmp ("clone" , path , git_tmp .name )
80+ # We will use this branch to put the skeleton changes on
81+ git_tmp ("checkout" , "--orphan" , MERGE_BRANCH )
82+ # Delete all the current files if there are any
83+ git_tmp ("rm" , "-rf" , "." , "--ignore-unmatch" )
84+ # And make sure src isn't there otherwise the git mv below
85+ # will do the wrong thing
86+ shutil .rmtree (git_tmp / "src" , ignore_errors = True )
87+ # Merge in the skeleton commits
88+ git_tmp ("pull" , SKELETON , "skeleton" )
89+ # Move things around
90+ git_tmp ("mv" , "src/dls_python3_skeleton" , f"src/{ package } " )
91+ git_tmp ("mv" , "tests/test_dls_python3_skeleton.py" , f"tests/test_{ package } .py" )
92+ # Change contents of all children known to git
93+ for relative_child in git_tmp ("ls-files" ).splitlines ():
94+ child = Path (git_tmp .name ) / relative_child
95+ if child .suffix in CHANGE_SUFFIXES and child .name not in IGNORE_FILES :
96+ text = child .read_text ()
97+ start_search , end_search = IGNORE_RANGES .get (child .name , (None , None ))
98+ if start_search :
99+ start_ignore = text .find (start_search )
100+ assert start_ignore > 0 , f"{ start_search } not in { child .name } "
101+ if end_search :
102+ end_ignore = text .find (end_search , start_ignore ) + len (
103+ end_search
104+ )
105+ assert end_ignore > 0 , f"{ end_search } not in { child .name } "
113106 else :
114- start_ignore = 0
115- end_ignore = 0
116- child .write_text (
117- replace_text (text [:start_ignore ])
118- + text [start_ignore :end_ignore ]
119- + replace_text (text [end_ignore :])
120- )
121- # Commit what we have and push to the original repo
122- git_tmp ("commit" , "-a" , "-m" , f"Rename dls-python3-skeleton -> { repo } " )
123- git_tmp ("push" , "origin" , MERGE_BRANCH )
124- try :
125- git ("merge" , MERGE_BRANCH , "--allow-unrelated-histories" , cwd = path )
126- except CalledProcessError :
127- # The merge failed, so ask the user to fix it
128- print ("Please fix the conflicts above, then you can run:" )
129- print (f" git branch -d { MERGE_BRANCH } " )
130- else :
131- git ("branch" , "-d" , MERGE_BRANCH , cwd = path )
107+ end_ignore = len (text )
108+ else :
109+ start_ignore = 0
110+ end_ignore = 0
111+ child .write_text (
112+ replace_text (text [:start_ignore ])
113+ + text [start_ignore :end_ignore ]
114+ + replace_text (text [end_ignore :])
115+ )
116+ # Commit what we have and push to the original repo
117+ git_tmp ("commit" , "-a" , "-m" , f"Rename dls-python3-skeleton -> { repo } " )
118+ git_tmp ("push" , "origin" , MERGE_BRANCH )
119+ try :
120+ git ("merge" , MERGE_BRANCH , "--allow-unrelated-histories" , cwd = path )
121+ except CalledProcessError :
122+ # The merge failed, so ask the user to fix it
123+ print ("Please fix the conflicts above, then you can run:" )
124+ print (f" git branch -d { MERGE_BRANCH } " )
125+ else :
126+ git ("branch" , "-d" , MERGE_BRANCH , cwd = path )
132127 print ("Instructions on how to develop this module are in CONTRIBUTING.rst" )
133128
134129
0 commit comments