script_site_activation.py (2239B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 # This script is used by "test_site_activation.py" to verify how site activations 6 # affect the sys.path. 7 # The sys.path is printed in three stages: 8 # 1. Once at the beginning 9 # 2. Once after Mach site activation 10 # 3. Once after the command site activation 11 # The output of this script should be an ast-parsable list with three nested lists: one 12 # for each sys.path state. 13 # Note that virtualenv-creation output may need to be filtered out - it can be done by 14 # only ast-parsing the last line of text outputted by this script. 15 16 import os 17 import sys 18 from unittest.mock import patch 19 20 from mach.requirements import MachEnvRequirements, PthSpecifier 21 from mach.site import CommandSiteManager, MachSiteManager 22 23 24 def main(): 25 # Should be set by calling test 26 topsrcdir = os.environ["TOPSRCDIR"] 27 command_site = os.environ["COMMAND_SITE"] 28 mach_site_requirements = os.environ["MACH_SITE_PTH_REQUIREMENTS"] 29 command_site_requirements = os.environ["COMMAND_SITE_PTH_REQUIREMENTS"] 30 work_dir = os.environ["WORK_DIR"] 31 32 def resolve_requirements(topsrcdir, site_name): 33 req = MachEnvRequirements() 34 if site_name == "mach": 35 req.pth_requirements = [ 36 PthSpecifier(path) for path in mach_site_requirements.split(os.pathsep) 37 ] 38 else: 39 req.pth_requirements = [PthSpecifier(command_site_requirements)] 40 return req 41 42 with patch("mach.site.resolve_requirements", resolve_requirements): 43 initial_sys_path = sys.path.copy() 44 45 mach_site = MachSiteManager.from_environment( 46 topsrcdir, 47 lambda: work_dir, 48 ) 49 mach_site.activate() 50 mach_sys_path = sys.path.copy() 51 52 command_site = CommandSiteManager.from_environment( 53 topsrcdir, lambda: work_dir, command_site, work_dir 54 ) 55 command_site.activate() 56 command_sys_path = sys.path.copy() 57 print([ 58 initial_sys_path, 59 mach_sys_path, 60 command_sys_path, 61 ]) 62 63 64 if __name__ == "__main__": 65 main()