blob: 63ab93793f423c8226c32ad40f54ddf289340c6c [file] [log] [blame]
#!/usr/bin/env python
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Calculates the current Build ID and writes it to 'build.id'."""
import argparse
import logging
import os
import sys
import textwrap
import gyp_utils
_SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
_BUILD_ID_PATH = gyp_utils.BUILD_ID_PATH
# Return values used by main().
RETVAL_SUCCESS = 0
RETVAL_ERROR = 1
def main():
logging.basicConfig(level=logging.WARNING, format='%(message)s')
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description=textwrap.dedent(__doc__))
parser.add_argument(
'--delete',
'-d',
action='store_true',
default=False,
help='Delete build.id file.')
options = parser.parse_args()
# Update the build id to the latest, even if one is already set.
try:
os.unlink(_BUILD_ID_PATH)
except OSError:
pass
if options.delete:
return 0
build_id = gyp_utils.GetBuildNumber()
if not build_id:
logging.error('Unable to retrieve build id.')
return RETVAL_ERROR
try:
with open(_BUILD_ID_PATH, 'w') as build_id_file:
build_id_file.write('{0}'.format(build_id))
except RuntimeError as e:
logging.error(e)
return RETVAL_ERROR
return RETVAL_SUCCESS
if __name__ == '__main__':
sys.exit(main())