]>
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 the --version commands.
9 # Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>
11 # This program and the accompanying materials
12 # are licensed and made available under the terms and conditions of the BSD License
13 # which accompanies this distribution. The full text of the license may be found at
14 # http://opensource.org/licenses/bsd-license.php
16 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 """ This program will update the BuildVersion.py and BuildVersion.h files used to set a tool's version value """
20 from __future__
import absolute_import
27 from argparse
import ArgumentParser
, SUPPRESS
28 from tempfile
import NamedTemporaryFile
29 from types
import IntType
, ListType
32 SYS_ENV_ERR
= "ERROR : %s system environment variable must be set prior to running this tool.\n"
34 __execname__
= "UpdateBuildVersions.py"
35 SVN_REVISION
= "$LastChangedRevision: 3 $"
36 SVN_REVISION
= SVN_REVISION
.replace("$LastChangedRevision:", "").replace("$", "").strip()
37 __copyright__
= "Copyright (c) 2014, Intel Corporation. All rights reserved."
38 VERSION_NUMBER
= "0.7.0"
39 __version__
= "Version %s.%s" % (VERSION_NUMBER
, SVN_REVISION
)
44 Parse the command-line options.
45 The options for this tool will be passed along to the MkBinPkg tool.
47 parser
= ArgumentParser(
48 usage
=("%s [options]" % __execname__
),
49 description
=__copyright__
,
50 conflict_handler
='resolve')
52 # Standard Tool Options
53 parser
.add_argument("--version", action
="version",
54 version
=__execname__
+ " " + __version__
)
55 parser
.add_argument("-s", "--silent", action
="store_true",
57 help="All output will be disabled, pass/fail determined by the exit code")
58 parser
.add_argument("-v", "--verbose", action
="store_true",
60 help="Enable verbose output")
61 # Tool specific options
62 parser
.add_argument("--revert", action
="store_true",
63 dest
="REVERT", default
=False,
64 help="Revert the BuildVersion files only")
65 parser
.add_argument("--svn-test", action
="store_true",
66 dest
="TEST_SVN", default
=False,
67 help="Test if the svn command is available")
68 parser
.add_argument("--svnFlag", action
="store_true",
69 dest
="HAVE_SVN", default
=False,
72 return(parser
.parse_args())
75 def ShellCommandResults(CmdLine
, Opt
):
76 """ Execute the comand, returning the output content """
77 file_list
= NamedTemporaryFile(delete
=False)
78 filename
= file_list
.name
83 subprocess
.check_call(args
=shlex
.split(CmdLine
), stderr
=subprocess
.STDOUT
, stdout
=file_list
)
84 except subprocess
.CalledProcessError
as err_val
:
87 sys
.stderr
.write("ERROR : %d : %s\n" % (err_val
.returncode
, err_val
.__str
__()))
88 if os
.path
.exists(filename
):
89 sys
.stderr
.write(" : Partial results may be in this file: %s\n" % filename
)
91 returnValue
= err_val
.returncode
93 except IOError as err_val
:
94 (errno
, strerror
) = err_val
.args
97 sys
.stderr
.write("I/O ERROR : %s : %s\n" % (str(errno
), strerror
))
98 sys
.stderr
.write("ERROR : this command failed : %s\n" % CmdLine
)
99 if os
.path
.exists(filename
):
100 sys
.stderr
.write(" : Partial results may be in this file: %s\n" % filename
)
104 except OSError as err_val
:
105 (errno
, strerror
) = err_val
.args
108 sys
.stderr
.write("OS ERROR : %s : %s\n" % (str(errno
), strerror
))
109 sys
.stderr
.write("ERROR : this command failed : %s\n" % CmdLine
)
110 if os
.path
.exists(filename
):
111 sys
.stderr
.write(" : Partial results may be in this file: %s\n" % filename
)
115 except KeyboardInterrupt:
118 sys
.stderr
.write("ERROR : Command terminated by user : %s\n" % CmdLine
)
119 if os
.path
.exists(filename
):
120 sys
.stderr
.write(" : Partial results may be in this file: %s\n" % filename
)
125 if not file_list
.closed
:
127 os
.fsync(file_list
.fileno())
130 if os
.path
.exists(filename
):
131 fd_
= open(filename
, 'r')
132 Results
= fd_
.readlines()
142 def UpdateBuildVersionPython(Rev
, UserModified
, opts
):
143 """ This routine will update the BuildVersion.h files in the C source tree """
144 for SubDir
in ["Common", "UPT"]:
145 PyPath
= os
.path
.join(os
.environ
['BASE_TOOLS_PATH'], "Source", "Python", SubDir
)
146 BuildVersionPy
= os
.path
.join(PyPath
, "BuildVersion.py")
147 fd_
= open(os
.path
.normpath(BuildVersionPy
), 'r')
148 contents
= fd_
.readlines()
150 if opts
.HAVE_SVN
is False:
151 BuildVersionOrig
= os
.path
.join(PyPath
, "orig_BuildVersion.py")
152 fd_
= open (BuildVersionOrig
, 'w')
153 for line
in contents
:
158 for line
in contents
:
159 if line
.strip().startswith("gBUILD_VERSION"):
160 new_line
= "gBUILD_VERSION = \"Developer Build based on Revision: %s\"" % Rev
162 new_line
= "gBUILD_VERSION = \"Developer Build based on Revision: %s with Modified Sources\"" % Rev
163 new_content
.append(new_line
)
165 new_content
.append(line
)
167 fd_
= open(os
.path
.normpath(BuildVersionPy
), 'w')
168 for line
in new_content
:
173 def UpdateBuildVersionH(Rev
, UserModified
, opts
):
174 """ This routine will update the BuildVersion.h files in the C source tree """
175 CPath
= os
.path
.join(os
.environ
['BASE_TOOLS_PATH'], "Source", "C", "Include", "Common")
176 BuildVersionH
= os
.path
.join(CPath
, "BuildVersion.h")
177 fd_
= open(os
.path
.normpath(BuildVersionH
), 'r')
178 contents
= fd_
.readlines()
180 if opts
.HAVE_SVN
is False:
181 BuildVersionOrig
= os
.path
.join(CPath
, "orig_BuildVersion.h")
182 fd_
= open(BuildVersionOrig
, 'w')
183 for line
in contents
:
189 for line
in contents
:
190 if line
.strip().startswith("#define"):
191 new_line
= "#define __BUILD_VERSION \"Developer Build based on Revision: %s\"" % Rev
193 new_line
= "#define __BUILD_VERSION \"Developer Build based on Revision: %s with Modified Sources\"" % \
195 new_content
.append(new_line
)
197 new_content
.append(line
)
199 fd_
= open(os
.path
.normpath(BuildVersionH
), 'w')
200 for line
in new_content
:
205 def RevertCmd(Filename
, Opt
):
206 """ This is the shell command that does the SVN revert """
207 CmdLine
= "svn revert %s" % Filename
.replace("\\", "/").strip()
209 subprocess
.check_output(args
=shlex
.split(CmdLine
))
210 except subprocess
.CalledProcessError
as err_val
:
212 sys
.stderr
.write("Subprocess ERROR : %s\n" % err_val
)
215 except IOError as err_val
:
216 (errno
, strerror
) = err_val
.args
218 sys
.stderr
.write("I/O ERROR : %d : %s\n" % (str(errno
), strerror
))
219 sys
.stderr
.write("ERROR : this command failed : %s\n" % CmdLine
)
222 except OSError as err_val
:
223 (errno
, strerror
) = err_val
.args
225 sys
.stderr
.write("OS ERROR : %d : %s\n" % (str(errno
), strerror
))
226 sys
.stderr
.write("ERROR : this command failed : %s\n" % CmdLine
)
229 except KeyboardInterrupt:
231 sys
.stderr
.write("ERROR : Command terminated by user : %s\n" % CmdLine
)
235 sys
.stdout
.write("Reverted this file: %s\n" % Filename
)
239 def GetSvnRevision(opts
):
240 """ Get the current revision of the BaseTools/Source tree, and check if any of the files have been modified """
244 if opts
.HAVE_SVN
is False:
245 sys
.stderr
.write("WARNING: the svn command-line tool is not available.\n")
246 return (Revision
, Modified
)
248 SrcPath
= os
.path
.join(os
.environ
['BASE_TOOLS_PATH'], "Source")
249 # Check if there are modified files.
253 StatusCmd
= "svn st -v --depth infinity --non-interactive"
254 contents
= ShellCommandResults(StatusCmd
, opts
)
256 if isinstance(contents
, ListType
):
257 for line
in contents
:
258 if line
.startswith("M "):
262 # Get the repository revision of BaseTools/Source
263 InfoCmd
= "svn info %s" % SrcPath
.replace("\\", "/").strip()
265 contents
= ShellCommandResults(InfoCmd
, opts
)
266 if isinstance(contents
, IntType
):
268 for line
in contents
:
270 if line
.startswith("Revision:"):
271 Revision
= line
.replace("Revision:", "").strip()
274 return (Revision
, Modified
)
279 This routine will return True if an svn --version command succeeds, or False if it fails.
280 If it failed, SVN is not available.
282 OriginalSilent
= opts
.silent
284 VerCmd
= "svn --version"
285 contents
= ShellCommandResults(VerCmd
, opts
)
286 opts
.silent
= OriginalSilent
287 if isinstance(contents
, IntType
):
289 sys
.stdout
.write("SVN does not appear to be available.\n")
294 sys
.stdout
.write("Found %s" % contents
[0])
299 def CopyOrig(Src
, Dest
, Opt
):
300 """ Overwrite the Dest File with the Src File content """
303 contents
= fd_
.readlines()
305 fd_
= open(Dest
, 'w')
306 for line
in contents
:
312 sys
.stderr
.write("Unable to restore this file: %s\n" % Dest
)
318 sys
.stdout
.write("Restored this file: %s\n" % Src
)
324 def CheckOriginals(Opts
):
326 If SVN was not available, then the tools may have made copies of the original BuildVersion.* files using
327 orig_BuildVersion.* for the name. If they exist, replace the existing BuildVersion.* file with the corresponding
328 orig_BuildVersion.* file.
329 Returns 0 if this succeeds, or 1 if the copy function fails. It will also return 0 if the orig_BuildVersion.* file
332 CPath
= os
.path
.join(os
.environ
['BASE_TOOLS_PATH'], "Source", "C", "Include", "Common")
333 BuildVersionH
= os
.path
.join(CPath
, "BuildVersion.h")
334 OrigBuildVersionH
= os
.path
.join(CPath
, "orig_BuildVersion.h")
335 if not os
.path
.exists(OrigBuildVersionH
):
337 if CopyOrig(OrigBuildVersionH
, BuildVersionH
, Opts
):
339 for SubDir
in ["Common", "UPT"]:
340 PyPath
= os
.path
.join(os
.environ
['BASE_TOOLS_PATH'], "Source", "Python", SubDir
)
341 BuildVersionPy
= os
.path
.join(PyPath
, "BuildVersion.h")
342 OrigBuildVersionPy
= os
.path
.join(PyPath
, "orig_BuildVersion.h")
343 if not os
.path
.exists(OrigBuildVersionPy
):
345 if CopyOrig(OrigBuildVersionPy
, BuildVersionPy
, Opts
):
351 def RevertBuildVersionFiles(opts
):
353 This routine will attempt to perform an SVN --revert on each of the BuildVersion.* files
355 if not opts
.HAVE_SVN
:
356 if CheckOriginals(opts
):
360 BuildVersionH
= os
.path
.join(os
.environ
['BASE_TOOLS_PATH'], "Source", "C", "Include", "Common", "BuildVersion.h")
361 RevertCmd(BuildVersionH
, opts
)
362 for SubDir
in ["Common", "UPT"]:
363 BuildVersionPy
= os
.path
.join(os
.environ
['BASE_TOOLS_PATH'], "Source", "Python", SubDir
, "BuildVersion.py")
364 RevertCmd(BuildVersionPy
, opts
)
366 def UpdateRevisionFiles():
367 """ Main routine that will update the BuildVersion.py and BuildVersion.h files."""
368 options
= ParseOptions()
369 # Check the working environment
370 if "WORKSPACE" not in os
.environ
.keys():
371 sys
.stderr
.write(SYS_ENV_ERR
% 'WORKSPACE')
373 if 'BASE_TOOLS_PATH' not in os
.environ
.keys():
374 sys
.stderr
.write(SYS_ENV_ERR
% 'BASE_TOOLS_PATH')
376 if not os
.path
.exists(os
.environ
['BASE_TOOLS_PATH']):
377 sys
.stderr
.write("Unable to locate the %s directory." % os
.environ
['BASE_TOOLS_PATH'])
381 options
.HAVE_SVN
= CheckSvn(options
)
383 return (not options
.HAVE_SVN
)
384 # done processing the option, now use the option.HAVE_SVN as a flag. True = Have it, False = Don't have it.
386 # Just revert the tools an exit
387 RevertBuildVersionFiles(options
)
389 # Revert any changes in the BuildVersion.* files before setting them again.
390 RevertBuildVersionFiles(options
)
391 Revision
, Modified
= GetSvnRevision(options
)
393 sys
.stdout
.write("Revision: %s is Modified: %s\n" % (Revision
, Modified
))
395 UpdateBuildVersionH(Revision
, Modified
, options
)
396 UpdateBuildVersionPython(Revision
, Modified
, options
)
401 if __name__
== "__main__":
402 sys
.exit(UpdateRevisionFiles())