libkas: make get_build_environ more exception safe
Eliminate manual call to os.remove by using tempfile context manager. The 'get bb env' script has to be created in a directory (rather than using a NamedTemporaryFile directly, for instance), since otherwise you'll get a "Text file busy" error when trying to execute the script. Signed-off-by: Chris Laplante <chris.laplante@agilent.com> Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
This commit is contained in:
		
				
					committed by
					
						
						Jan Kiszka
					
				
			
			
				
	
			
			
			
						parent
						
							22f7c0b03d
						
					
				
				
					commit
					9518dc4e35
				
			@@ -30,6 +30,7 @@ import logging
 | 
				
			|||||||
import tempfile
 | 
					import tempfile
 | 
				
			||||||
import asyncio
 | 
					import asyncio
 | 
				
			||||||
import errno
 | 
					import errno
 | 
				
			||||||
 | 
					import pathlib
 | 
				
			||||||
from subprocess import Popen, PIPE
 | 
					from subprocess import Popen, PIPE
 | 
				
			||||||
from .context import get_context
 | 
					from .context import get_context
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -215,24 +216,23 @@ def get_build_environ():
 | 
				
			|||||||
        logging.error('Did not find any init-build-env script')
 | 
					        logging.error('Did not find any init-build-env script')
 | 
				
			||||||
        sys.exit(1)
 | 
					        sys.exit(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    get_bb_env_file = tempfile.mktemp()
 | 
					    with tempfile.TemporaryDirectory() as temp_dir:
 | 
				
			||||||
    with open(get_bb_env_file, 'w') as fds:
 | 
					 | 
				
			||||||
        script = """#!/bin/bash
 | 
					        script = """#!/bin/bash
 | 
				
			||||||
        set -e
 | 
					        set -e
 | 
				
			||||||
        source %s $1 > /dev/null
 | 
					        source %s $1 > /dev/null
 | 
				
			||||||
        env
 | 
					        env
 | 
				
			||||||
        """ % init_script
 | 
					        """ % init_script
 | 
				
			||||||
        fds.write(script)
 | 
					
 | 
				
			||||||
    os.chmod(get_bb_env_file, 0o775)
 | 
					        get_bb_env_file = pathlib.Path(temp_dir) / "get_bb_env"
 | 
				
			||||||
 | 
					        get_bb_env_file.write_text(script)
 | 
				
			||||||
 | 
					        get_bb_env_file.chmod(0o775)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        env = {}
 | 
					        env = {}
 | 
				
			||||||
        env['PATH'] = '/usr/sbin:/usr/bin:/sbin:/bin'
 | 
					        env['PATH'] = '/usr/sbin:/usr/bin:/sbin:/bin'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    (_, output) = run_cmd([get_bb_env_file, get_context().build_dir],
 | 
					        (_, output) = run_cmd([str(get_bb_env_file), get_context().build_dir],
 | 
				
			||||||
                              cwd=init_repo.path, env=env, liveupdate=False)
 | 
					                              cwd=init_repo.path, env=env, liveupdate=False)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    os.remove(get_bb_env_file)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    env = {}
 | 
					    env = {}
 | 
				
			||||||
    for line in output.splitlines():
 | 
					    for line in output.splitlines():
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user