44import sys
55
66# Parameters
7- CLING_TAG_ROOT_HASH_PREFIX = ' __internal-root-'
7+ CLING_TAG_ROOT_HASH_PREFIX = " __internal-root-"
88# Arbitrarily choose a commit hash which is "old enough"
99# In this case, we choose the first commit after the latest
1010# cling release at the time of writing, 1.2, i.e. 5ea7949 (in cling)
1111# 08f123f8e7 (in ROOT)
1212# See Cling https://github.com/root-project/cling/commit/5ea7949
1313# ROOT https://github.com/root-project/root/commit/08f123f8e7
14- DEFAULT_STARTING_ROOT_HASH = '08f123f8e7'
15- CLING_REPO_DIR_NAME = 'cling'
16- ROOT_REPO_DIR_NAME = 'root'
17- INTERP_DIR_NAME = 'interpreter/cling'
18- DEST_INTERP_DIR_NAME = ''
14+ DEFAULT_STARTING_ROOT_HASH = "08f123f8e7"
15+ CLING_REPO_DIR_NAME = "cling"
16+ ROOT_REPO_DIR_NAME = "root"
17+ INTERP_DIR_NAME = "interpreter/cling"
18+ DEST_INTERP_DIR_NAME = ""
19+
1920
2021def printError (msg ):
21- print (f'*** Error: { msg } ' )
22+ print (f"*** Error: { msg } " )
23+
2224
2325def printWarning (msg ):
24- print (f'*** Warning: { msg } ' )
26+ print (f"*** Warning: { msg } " )
27+
2528
2629def printInfo (msg ):
27- print (f'Info: { msg } ' )
30+ print (f"Info: { msg } " )
31+
2832
29- def execCommand (cmd , thisCwd = './' , theInput = None , desc = "" ):
30- '''
33+ def execCommand (cmd , thisCwd = "./" , theInput = None , desc = "" ):
34+ """
3135 Execute a command and return the output. For logging reasons, the command
3236 is also printed.
3337 If "desc" is specificed, the command is not printed but "desc".
34- '''
35- if '' == desc :
36- printInfo (f' In directory { thisCwd } *** { cmd } { " with std input" if theInput else "" } ' )
38+ """
39+ if "" == desc :
40+ printInfo (f" In directory { thisCwd } *** { cmd } { ' with std input' if theInput else '' } " )
3741 else :
3842 print (desc )
39- compProc = subprocess .run (cmd , shell = True , capture_output = True , text = True ,
40- cwd = thisCwd , input = theInput , encoding = 'latin1' )
43+ compProc = subprocess .run (
44+ cmd , shell = True , capture_output = True , text = True , cwd = thisCwd , input = theInput , encoding = "latin1"
45+ )
4146 if 0 != compProc .returncode :
4247 print (f"Error:\n { compProc .stderr .strip ()} " )
4348 raise ValueError (f'Command "{ cmd } " failed ({ compProc .returncode } )' )
4449 out = compProc .stdout .strip ()
4550 return out
4651
52+
4753def getAllClingTags ():
48- execCommand (f'git fetch --tags' , CLING_REPO_DIR_NAME )
54+ execCommand ("git fetch --tags" , CLING_REPO_DIR_NAME )
55+
4956
5057def getRootSyncTag ():
5158 # We try to get the tags, to get the latest ROOT commit that was synchronized
5259 getAllClingTags ()
53- tagsStr = execCommand (f' git tag' , CLING_REPO_DIR_NAME )
54- tags = tagsStr .split (' \n ' )
55- if tags == ['' ]:
56- printInfo (f' No tags found locally. Looking in the source repository.' )
57- tagsStr = execCommand (f' git ls-remote --tags origin' , CLING_REPO_DIR_NAME )
58- tags = tagsStr .split (' \n ' )
60+ tagsStr = execCommand (" git tag" , CLING_REPO_DIR_NAME )
61+ tags = tagsStr .split (" \n " )
62+ if tags == ["" ]:
63+ printInfo (" No tags found locally. Looking in the source repository." )
64+ tagsStr = execCommand (" git ls-remote --tags origin" , CLING_REPO_DIR_NAME )
65+ tags = tagsStr .split (" \n " )
5966 print (tags )
60- tags = list (map (lambda t : t .split ('/' )[- 1 ] if '/' in t else t , tags ))
67+ tags = list (map (lambda t : t .split ("/" )[- 1 ] if "/" in t else t , tags ))
6168 print (tags )
6269
63- printInfo (f' Tags found: { str (tags )} ' )
70+ printInfo (f" Tags found: { str (tags )} " )
6471
6572 rootTags = list (filter (lambda tag : tag .startswith (CLING_TAG_ROOT_HASH_PREFIX ), tags ))
6673 if not rootTags :
67- raise ValueError (f' No sync tags starting with { CLING_TAG_ROOT_HASH_PREFIX } were found!' )
74+ raise ValueError (f" No sync tags starting with { CLING_TAG_ROOT_HASH_PREFIX } were found!" )
6875 if len (rootTags ) > 1 :
69- raise ValueError (f' More than one sync tag were found: { str (rootTags )} !' )
76+ raise ValueError (f" More than one sync tag were found: { str (rootTags )} !" )
7077 return rootTags [0 ]
7178
79+
7280def getStartingRootHash (rootTag ):
73- printInfo (' Getting the starting ROOT Hash from the tag' )
81+ printInfo (" Getting the starting ROOT Hash from the tag" )
7482 prefixLen = len (CLING_TAG_ROOT_HASH_PREFIX )
7583 defHashLen = len (DEFAULT_STARTING_ROOT_HASH )
76- hash = rootTag [prefixLen : prefixLen + defHashLen ]
84+ hash = rootTag [prefixLen : prefixLen + defHashLen ]
7785 return hash
7886
79- def getHashes (repoDirName , startingHash , dirInRepoName = '' ):
80- out = execCommand (f'git log --oneline { startingHash } ..HEAD { dirInRepoName } ' , thisCwd = repoDirName )
87+
88+ def getHashes (repoDirName , startingHash , dirInRepoName = "" ):
89+ out = execCommand (f"git log --oneline { startingHash } ..HEAD { dirInRepoName } " , thisCwd = repoDirName )
8190 hashes = []
8291 if not out :
8392 return hashes
8493 # skip the first line since it's '\n'
85- hashes = [line .split (' ' , 1 )[0 ] for line in out .split (' \n ' )]
94+ hashes = [line .split (" " , 1 )[0 ] for line in out .split (" \n " )]
8695 return hashes
8796
97+
8898def createPatches (rootHashes , interpHashes ):
8999 patches = []
90100
@@ -99,49 +109,52 @@ def createPatches(rootHashes, interpHashes):
99109 # directory and hash is there for debugging purposes.
100110 for rootHashtoSync in reversed (rootHashesToSync ):
101111 keys = []
102- if rootHashtoSync in interpHashesSet : keys .append (INTERP_DIR_NAME )
112+ if rootHashtoSync in interpHashesSet :
113+ keys .append (INTERP_DIR_NAME )
103114 for key in keys :
104115 patchAsStr = execCommand (f"git format-patch -1 { rootHashtoSync } { key } --stdout" , ROOT_REPO_DIR_NAME )
105116 patches .append ([key , rootHashtoSync , patchAsStr ])
106117 return patches
107118
119+
108120def applyPatches (patches ):
109121 for dirInRepo , hash , patchAsStr in patches :
110- ignorePathLevel = dirInRepo .count ('/' ) + 2
122+ ignorePathLevel = dirInRepo .count ("/" ) + 2
111123 destDirName = DEST_INTERP_DIR_NAME
112- directoryOption = f'--directory { destDirName } ' if destDirName else ''
113- printInfo (f'Applying { hash } restricted to { dirInRepo } to repository { CLING_REPO_DIR_NAME } ' )
114- execCommand (f'git am -p { ignorePathLevel } { directoryOption } ' , CLING_REPO_DIR_NAME , patchAsStr )
124+ directoryOption = f"--directory { destDirName } " if destDirName else ""
125+ printInfo (f"Applying { hash } restricted to { dirInRepo } to repository { CLING_REPO_DIR_NAME } " )
126+ execCommand (f"git am -p { ignorePathLevel } { directoryOption } " , CLING_REPO_DIR_NAME , patchAsStr )
127+
115128
116129def syncTagAndPush (oldSyncTag , rootSyncHash ):
117- '''
130+ """
118131 Replace the tag mentioning the previous root commit to which the repo was synchronized.
119132 Push the changes and tags upstream.
120- '''
133+ """
121134 # We fetch the remote and local tags to make the following operations more resilient
122- remoteTags = execCommand (f' git ls-remote --tags origin' , CLING_REPO_DIR_NAME )
123- localTags = execCommand (f' git tag' , CLING_REPO_DIR_NAME )
135+ remoteTags = execCommand (" git ls-remote --tags origin" , CLING_REPO_DIR_NAME )
136+ localTags = execCommand (" git tag" , CLING_REPO_DIR_NAME )
124137
125138 # Clean the old tag
126- printInfo (f' Found a sync tag ({ oldSyncTag } ): deleting it.' )
139+ printInfo (f" Found a sync tag ({ oldSyncTag } ): deleting it." )
127140 if oldSyncTag in localTags :
128- execCommand (f' git tag -d { oldSyncTag } ' , CLING_REPO_DIR_NAME )
141+ execCommand (f" git tag -d { oldSyncTag } " , CLING_REPO_DIR_NAME )
129142 if oldSyncTag in remoteTags :
130- execCommand (f' git push --delete origin { oldSyncTag } ' , CLING_REPO_DIR_NAME )
143+ execCommand (f" git push --delete origin { oldSyncTag } " , CLING_REPO_DIR_NAME )
131144 else :
132- printWarning (f' Tag { oldSyncTag } was not found in the upstream repository.' )
145+ printWarning (f" Tag { oldSyncTag } was not found in the upstream repository." )
133146
134147 # Time to push the sync commits!
135- execCommand (f' git push' , CLING_REPO_DIR_NAME )
148+ execCommand (" git push" , CLING_REPO_DIR_NAME )
136149
137150 # And finally we tag and push the tag
138- newTag = CLING_TAG_ROOT_HASH_PREFIX + rootSyncHash
139- printInfo (f' Creating a new tag ({ newTag } ).' )
140- execCommand (f' git tag { newTag } ' , CLING_REPO_DIR_NAME )
141- execCommand (f' git push origin tag { newTag } ' , CLING_REPO_DIR_NAME )
151+ newTag = CLING_TAG_ROOT_HASH_PREFIX + rootSyncHash
152+ printInfo (f" Creating a new tag ({ newTag } )." )
153+ execCommand (f" git tag { newTag } " , CLING_REPO_DIR_NAME )
154+ execCommand (f" git push origin tag { newTag } " , CLING_REPO_DIR_NAME )
142155
143- def principal ():
144156
157+ def principal ():
145158 # We want a starting hash not to deal with the entire commit history
146159 # of ROOT
147160 rootSyncTag = getRootSyncTag ()
@@ -155,29 +168,30 @@ def principal():
155168 # If we have no commits to sync, we quit.
156169 if not interpHashes :
157170 # nothing to do, we have no commits to sync
158- printInfo (' No commit to sync. Exiting now.' )
171+ printInfo (" No commit to sync. Exiting now." )
159172 return 0
160173
161- printInfo (f' We found:\n - { len (interpHashes )} patches from the directory { INTERP_DIR_NAME } ' )
174+ printInfo (f" We found:\n - { len (interpHashes )} patches from the directory { INTERP_DIR_NAME } " )
162175
163176 # We now create the patches we want to apply to the cling repo
164177 patches = createPatches (rootHashes , interpHashes )
165178
166179 # We now apply the patches
167180 if not patches :
168- printError (' No patch was distilled: this status should not be reachable' )
181+ printError (" No patch was distilled: this status should not be reachable" )
169182 return 1
170183
171184 # We now need to apply patches, update the tag that mentions the ROOT commit
172185 # to which the cling repo was synchronised and push everything.
173186 # First of all we need to acquire an identity
174- printInfo (' Acquiring an identity in preparation to the upstreaming of the changes' )
187+ printInfo (" Acquiring an identity in preparation to the upstreaming of the changes" )
175188 execCommand ('git config user.email "root@persona.com"' , CLING_REPO_DIR_NAME )
176189 execCommand ('git config user.name "Root Persona"' , CLING_REPO_DIR_NAME )
177190
178191 applyPatches (patches )
179192
180193 syncTagAndPush (rootSyncTag , rootHashes [0 ])
181194
182- if __name__ == '__main__' :
195+
196+ if __name__ == "__main__" :
183197 sys .exit (principal ())
0 commit comments