]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Scripts/UpdateBuildVersions.py
2 # Update build revisions of the tools when performing a developer build
4 # This script will modife the C/Include/Common/BuildVersion.h file and the two
5 # Python scripts, Python/Common/BuildVersion.py and Python/UPT/BuildVersion.py.
6 # If SVN is available, the tool will obtain the current checked out version of
7 # the source tree for including the --version commands.
9 # Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>
11 # SPDX-License-Identifier: BSD-2-Clause-Patent
13 """ This program will update the BuildVersion.py and BuildVersion.h files used to set a tool's version value """
14 from __future__
import absolute_import
21 from argparse
import ArgumentParser
, SUPPRESS
22 from tempfile
import NamedTemporaryFile
23 from types
import IntType
, ListType
26 SYS_ENV_ERR
= "ERROR : %s system environment variable must be set prior to running this tool.\n"
28 __execname__
= "UpdateBuildVersions.py"
29 SVN_REVISION
= "$LastChangedRevision: 3 $"
30 SVN_REVISION
= SVN_REVISION
.replace("$LastChangedRevision:", "").replace("$", "").strip()
31 __copyright__
= "Copyright (c) 2014, Intel Corporation. All rights reserved."
32 VERSION_NUMBER
= "0.7.0"
33 __version__
= "Version %s.%s" % (VERSION_NUMBER
, SVN_REVISION
)
38 Parse the command-line options.
39 The options for this tool will be passed along to the MkBinPkg tool.
41 parser
= ArgumentParser(
42 usage
=("%s [options]" % __execname__
),
43 description
=__copyright__
,
44 conflict_handler
='resolve')
46 # Standard Tool Options
47 parser
.add_argument("--version", action
="version",
48 version
=__execname__
+ " " + __version__
)
49 parser
.add_argument("-s", "--silent", action
="store_true",
51 help="All output will be disabled, pass/fail determined by the exit code")
52 parser
.add_argument("-v", "--verbose", action
="store_true",
54 help="Enable verbose output")
55 # Tool specific options
56 parser
.add_argument("--revert", action
="store_true",
57 dest
="REVERT", default
=False,
58 help="Revert the BuildVersion files only")
59 parser
.add_argument("--svn-test", action
="store_true",
60 dest
="TEST_SVN", default
=False,
61 help="Test if the svn command is available")
62 parser
.add_argument("--svnFlag", action
="store_true",
63 dest
="HAVE_SVN", default
=False,
66 return(parser
.parse_args())
69 def ShellCommandResults(CmdLine
, Opt
):
70 """ Execute the command, returning the output content """
71 file_list
= NamedTemporaryFile(delete
=False)
72 filename
= file_list
.name
77 subprocess
.check_call(args
=shlex
.split(CmdLine
), stderr
=subprocess
.STDOUT
, stdout
=file_list
)
78 except subprocess
.CalledProcessError
as err_val
:
81 sys
.stderr
.write("ERROR : %d : %s\n" % (err_val
.returncode
, err_val
.__str
__()))
82 if os
.path
.exists(filename
):
83 sys
.stderr
.write(" : Partial results may be in this file: %s\n" % filename
)
85 returnValue
= err_val
.returncode
87 except IOError as err_val
:
88 (errno
, strerror
) = err_val
.args
91 sys
.stderr
.write("I/O ERROR : %s : %s\n" % (str(errno
), strerror
))
92 sys
.stderr
.write("ERROR : this command failed : %s\n" % CmdLine
)
93 if os
.path
.exists(filename
):
94 sys
.stderr
.write(" : Partial results may be in this file: %s\n" % filename
)
98 except OSError as err_val
:
99 (errno
, strerror
) = err_val
.args
102 sys
.stderr
.write("OS ERROR : %s : %s\n" % (str(errno
), strerror
))
103 sys
.stderr
.write("ERROR : this command failed : %s\n" % CmdLine
)
104 if os
.path
.exists(filename
):
105 sys
.stderr
.write(" : Partial results may be in this file: %s\n" % filename
)
109 except KeyboardInterrupt:
112 sys
.stderr
.write("ERROR : Command terminated by user : %s\n" % CmdLine
)
113 if os
.path
.exists(filename
):
114 sys
.stderr
.write(" : Partial results may be in this file: %s\n" % filename
)
119 if not file_list
.closed
:
121 os
.fsync(file_list
.fileno())
124 if os
.path
.exists(filename
):
125 fd_
= open(filename
, 'r')
126 Results
= fd_
.readlines()
136 def UpdateBuildVersionPython(Rev
, UserModified
, opts
):
137 """ This routine will update the BuildVersion.h files in the C source tree """
138 for SubDir
in ["Common", "UPT"]:
139 PyPath
= os
.path
.join(os
.environ
['BASE_TOOLS_PATH'], "Source", "Python", SubDir
)
140 BuildVersionPy
= os
.path
.join(PyPath
, "BuildVersion.py")
141 fd_
= open(os
.path
.normpath(BuildVersionPy
), 'r')
142 contents
= fd_
.readlines()
144 if opts
.HAVE_SVN
is False:
145 BuildVersionOrig
= os
.path
.join(PyPath
, "orig_BuildVersion.py")
146 fd_
= open (BuildVersionOrig
, 'w')
147 for line
in contents
:
152 for line
in contents
:
153 if line
.strip().startswith("gBUILD_VERSION"):
154 new_line
= "gBUILD_VERSION = \"Developer Build based on Revision: %s\"" % Rev
156 new_line
= "gBUILD_VERSION = \"Developer Build based on Revision: %s with Modified Sources\"" % Rev
157 new_content
.append(new_line
)
159 new_content
.append(line
)
161 fd_
= open(os
.path
.normpath(BuildVersionPy
), 'w')
162 for line
in new_content
:
167 def UpdateBuildVersionH(Rev
, UserModified
, opts
):
168 """ This routine will update the BuildVersion.h files in the C source tree """
169 CPath
= os
.path
.join(os
.environ
['BASE_TOOLS_PATH'], "Source", "C", "Include", "Common")
170 BuildVersionH
= os
.path
.join(CPath
, "BuildVersion.h")
171 fd_
= open(os
.path
.normpath(BuildVersionH
), 'r')
172 contents
= fd_
.readlines()
174 if opts
.HAVE_SVN
is False:
175 BuildVersionOrig
= os
.path
.join(CPath
, "orig_BuildVersion.h")
176 fd_
= open(BuildVersionOrig
, 'w')
177 for line
in contents
:
183 for line
in contents
:
184 if line
.strip().startswith("#define"):
185 new_line
= "#define __BUILD_VERSION \"Developer Build based on Revision: %s\"" % Rev
187 new_line
= "#define __BUILD_VERSION \"Developer Build based on Revision: %s with Modified Sources\"" % \
189 new_content
.append(new_line
)
191 new_content
.append(line
)
193 fd_
= open(os
.path
.normpath(BuildVersionH
), 'w')
194 for line
in new_content
:
199 def RevertCmd(Filename
, Opt
):
200 """ This is the shell command that does the SVN revert """
201 CmdLine
= "svn revert %s" % Filename
.replace("\\", "/").strip()
203 subprocess
.check_output(args
=shlex
.split(CmdLine
))
204 except subprocess
.CalledProcessError
as err_val
:
206 sys
.stderr
.write("Subprocess ERROR : %s\n" % err_val
)
209 except IOError as err_val
:
210 (errno
, strerror
) = err_val
.args
212 sys
.stderr
.write("I/O ERROR : %d : %s\n" % (str(errno
), strerror
))
213 sys
.stderr
.write("ERROR : this command failed : %s\n" % CmdLine
)
216 except OSError as err_val
:
217 (errno
, strerror
) = err_val
.args
219 sys
.stderr
.write("OS ERROR : %d : %s\n" % (str(errno
), strerror
))
220 sys
.stderr
.write("ERROR : this command failed : %s\n" % CmdLine
)
223 except KeyboardInterrupt:
225 sys
.stderr
.write("ERROR : Command terminated by user : %s\n" % CmdLine
)
229 sys
.stdout
.write("Reverted this file: %s\n" % Filename
)
233 def GetSvnRevision(opts
):
234 """ Get the current revision of the BaseTools/Source tree, and check if any of the files have been modified """
238 if opts
.HAVE_SVN
is False:
239 sys
.stderr
.write("WARNING: the svn command-line tool is not available.\n")
240 return (Revision
, Modified
)
242 SrcPath
= os
.path
.join(os
.environ
['BASE_TOOLS_PATH'], "Source")
243 # Check if there are modified files.
247 StatusCmd
= "svn st -v --depth infinity --non-interactive"
248 contents
= ShellCommandResults(StatusCmd
, opts
)
250 if isinstance(contents
, ListType
):
251 for line
in contents
:
252 if line
.startswith("M "):
256 # Get the repository revision of BaseTools/Source
257 InfoCmd
= "svn info %s" % SrcPath
.replace("\\", "/").strip()
259 contents
= ShellCommandResults(InfoCmd
, opts
)
260 if isinstance(contents
, IntType
):
262 for line
in contents
:
264 if line
.startswith("Revision:"):
265 Revision
= line
.replace("Revision:", "").strip()
268 return (Revision
, Modified
)
273 This routine will return True if an svn --version command succeeds, or False if it fails.
274 If it failed, SVN is not available.
276 OriginalSilent
= opts
.silent
278 VerCmd
= "svn --version"
279 contents
= ShellCommandResults(VerCmd
, opts
)
280 opts
.silent
= OriginalSilent
281 if isinstance(contents
, IntType
):
283 sys
.stdout
.write("SVN does not appear to be available.\n")
288 sys
.stdout
.write("Found %s" % contents
[0])
293 def CopyOrig(Src
, Dest
, Opt
):
294 """ Overwrite the Dest File with the Src File content """
297 contents
= fd_
.readlines()
299 fd_
= open(Dest
, 'w')
300 for line
in contents
:
306 sys
.stderr
.write("Unable to restore this file: %s\n" % Dest
)
312 sys
.stdout
.write("Restored this file: %s\n" % Src
)
318 def CheckOriginals(Opts
):
320 If SVN was not available, then the tools may have made copies of the original BuildVersion.* files using
321 orig_BuildVersion.* for the name. If they exist, replace the existing BuildVersion.* file with the corresponding
322 orig_BuildVersion.* file.
323 Returns 0 if this succeeds, or 1 if the copy function fails. It will also return 0 if the orig_BuildVersion.* file
326 CPath
= os
.path
.join(os
.environ
['BASE_TOOLS_PATH'], "Source", "C", "Include", "Common")
327 BuildVersionH
= os
.path
.join(CPath
, "BuildVersion.h")
328 OrigBuildVersionH
= os
.path
.join(CPath
, "orig_BuildVersion.h")
329 if not os
.path
.exists(OrigBuildVersionH
):
331 if CopyOrig(OrigBuildVersionH
, BuildVersionH
, Opts
):
333 for SubDir
in ["Common", "UPT"]:
334 PyPath
= os
.path
.join(os
.environ
['BASE_TOOLS_PATH'], "Source", "Python", SubDir
)
335 BuildVersionPy
= os
.path
.join(PyPath
, "BuildVersion.h")
336 OrigBuildVersionPy
= os
.path
.join(PyPath
, "orig_BuildVersion.h")
337 if not os
.path
.exists(OrigBuildVersionPy
):
339 if CopyOrig(OrigBuildVersionPy
, BuildVersionPy
, Opts
):
345 def RevertBuildVersionFiles(opts
):
347 This routine will attempt to perform an SVN --revert on each of the BuildVersion.* files
349 if not opts
.HAVE_SVN
:
350 if CheckOriginals(opts
):
354 BuildVersionH
= os
.path
.join(os
.environ
['BASE_TOOLS_PATH'], "Source", "C", "Include", "Common", "BuildVersion.h")
355 RevertCmd(BuildVersionH
, opts
)
356 for SubDir
in ["Common", "UPT"]:
357 BuildVersionPy
= os
.path
.join(os
.environ
['BASE_TOOLS_PATH'], "Source", "Python", SubDir
, "BuildVersion.py")
358 RevertCmd(BuildVersionPy
, opts
)
360 def UpdateRevisionFiles():
361 """ Main routine that will update the BuildVersion.py and BuildVersion.h files."""
362 options
= ParseOptions()
363 # Check the working environment
364 if "WORKSPACE" not in os
.environ
.keys():
365 sys
.stderr
.write(SYS_ENV_ERR
% 'WORKSPACE')
367 if 'BASE_TOOLS_PATH' not in os
.environ
.keys():
368 sys
.stderr
.write(SYS_ENV_ERR
% 'BASE_TOOLS_PATH')
370 if not os
.path
.exists(os
.environ
['BASE_TOOLS_PATH']):
371 sys
.stderr
.write("Unable to locate the %s directory." % os
.environ
['BASE_TOOLS_PATH'])
375 options
.HAVE_SVN
= CheckSvn(options
)
377 return (not options
.HAVE_SVN
)
378 # done processing the option, now use the option.HAVE_SVN as a flag. True = Have it, False = Don't have it.
380 # Just revert the tools an exit
381 RevertBuildVersionFiles(options
)
383 # Revert any changes in the BuildVersion.* files before setting them again.
384 RevertBuildVersionFiles(options
)
385 Revision
, Modified
= GetSvnRevision(options
)
387 sys
.stdout
.write("Revision: %s is Modified: %s\n" % (Revision
, Modified
))
389 UpdateBuildVersionH(Revision
, Modified
, options
)
390 UpdateBuildVersionPython(Revision
, Modified
, options
)
395 if __name__
== "__main__":
396 sys
.exit(UpdateRevisionFiles())