1# Copyright 2021 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import contextlib 6import json 7import os 8import pathlib 9import socket 10 11# Use a unix abstract domain socket: 12# https://man7.org/linux/man-pages/man7/unix.7.html#:~:text=abstract: 13SOCKET_ADDRESS = '\0chromium_build_server_socket' 14BUILD_SERVER_ENV_VARIABLE = 'INVOKED_BY_BUILD_SERVER' 15 16 17def MaybeRunCommand(name, argv, stamp_file, force): 18 """Returns True if the command was successfully sent to the build server.""" 19 20 # When the build server runs a command, it sets this environment variable. 21 # This prevents infinite recursion where the script sends a request to the 22 # build server, then the build server runs the script, and then the script 23 # sends another request to the build server. 24 if BUILD_SERVER_ENV_VARIABLE in os.environ: 25 return False 26 with contextlib.closing(socket.socket(socket.AF_UNIX)) as sock: 27 try: 28 sock.connect(SOCKET_ADDRESS) 29 sock.sendall( 30 json.dumps({ 31 'name': name, 32 'cmd': argv, 33 'cwd': os.getcwd(), 34 'stamp_file': stamp_file, 35 }).encode('utf8')) 36 except socket.error as e: 37 # [Errno 111] Connection refused. Either the server has not been started 38 # or the server is not currently accepting new connections. 39 if e.errno == 111: 40 if force: 41 raise RuntimeError( 42 '\n\nBuild server is not running and ' 43 'android_static_analysis="build_server" is set.\nPlease run ' 44 'this command in a separate terminal:\n\n' 45 '$ build/android/fast_local_dev_server.py\n\n') from None 46 return False 47 raise e 48 49 # Siso needs the stamp file to be created in order for the build step to 50 # complete. If the task fails when the build server runs it, the build server 51 # will delete the stamp file so that it will be run again next build. 52 pathlib.Path(stamp_file).touch() 53 return True 54