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 socket 9 10# Use a unix abstract domain socket: 11# https://man7.org/linux/man-pages/man7/unix.7.html#:~:text=abstract: 12SOCKET_ADDRESS = '\0chromium_build_server_socket' 13BUILD_SERVER_ENV_VARIABLE = 'INVOKED_BY_BUILD_SERVER' 14 15 16def MaybeRunCommand(name, argv, stamp_file, force): 17 """Returns True if the command was successfully sent to the build server.""" 18 19 # When the build server runs a command, it sets this environment variable. 20 # This prevents infinite recursion where the script sends a request to the 21 # build server, then the build server runs the script, and then the script 22 # sends another request to the build server. 23 if BUILD_SERVER_ENV_VARIABLE in os.environ: 24 return False 25 with contextlib.closing(socket.socket(socket.AF_UNIX)) as sock: 26 try: 27 sock.connect(SOCKET_ADDRESS) 28 sock.sendall( 29 json.dumps({ 30 'name': name, 31 'cmd': argv, 32 'cwd': os.getcwd(), 33 'stamp_file': stamp_file, 34 }).encode('utf8')) 35 except socket.error as e: 36 # [Errno 111] Connection refused. Either the server has not been started 37 # or the server is not currently accepting new connections. 38 if e.errno == 111: 39 if force: 40 raise RuntimeError( 41 '\n\nBuild server is not running and ' 42 'android_static_analysis="build_server" is set.\nPlease run ' 43 'this command in a separate terminal:\n\n' 44 '$ build/android/fast_local_dev_server.py\n\n') from None 45 return False 46 raise e 47 return True 48