Resources: use expanded archive name by default (#11688)
For resources, it is desirable to use the expanded archive name of the resource as the name of the directory when adding it to the root staging area. #11528 established 'spack-src' as the universal directory where source files are placed, which also affected the behavior of resources managed with Stages. This adds a new property ('srcdir') to Stage to remember the name of the expanded source directory, and uses this as the default name when placing a resource directory in the root staging area. This also: * Ensures that downloaded sources are archived using the expanded archive name (otherwise Spack will not be able to determine the original directory name when using a cached archive). * Updates working_dir context manager to guarantee restoration of original working directory when an exception occurs * Adds a "temp_cwd" context manager which creates a temporary directory and sets it as the working directory
This commit is contained in:
		@@ -455,8 +455,10 @@ def working_dir(dirname, **kwargs):
 | 
			
		||||
 | 
			
		||||
    orig_dir = os.getcwd()
 | 
			
		||||
    os.chdir(dirname)
 | 
			
		||||
    yield
 | 
			
		||||
    os.chdir(orig_dir)
 | 
			
		||||
    try:
 | 
			
		||||
        yield
 | 
			
		||||
    finally:
 | 
			
		||||
        os.chdir(orig_dir)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@contextmanager
 | 
			
		||||
@@ -605,6 +607,36 @@ def ancestor(dir, n=1):
 | 
			
		||||
    return parent
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_single_file(directory):
 | 
			
		||||
    fnames = os.listdir(directory)
 | 
			
		||||
    if len(fnames) != 1:
 | 
			
		||||
        raise ValueError("Expected exactly 1 file, got {0}"
 | 
			
		||||
                         .format(str(len(fnames))))
 | 
			
		||||
    return fnames[0]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@contextmanager
 | 
			
		||||
def temp_cwd():
 | 
			
		||||
    tmp_dir = tempfile.mkdtemp()
 | 
			
		||||
    try:
 | 
			
		||||
        with working_dir(tmp_dir):
 | 
			
		||||
            yield tmp_dir
 | 
			
		||||
    finally:
 | 
			
		||||
        shutil.rmtree(tmp_dir)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@contextmanager
 | 
			
		||||
def temp_rename(orig_path, temp_path):
 | 
			
		||||
    same_path = os.path.realpath(orig_path) == os.path.realpath(temp_path)
 | 
			
		||||
    if not same_path:
 | 
			
		||||
        shutil.move(orig_path, temp_path)
 | 
			
		||||
    try:
 | 
			
		||||
        yield
 | 
			
		||||
    finally:
 | 
			
		||||
        if not same_path:
 | 
			
		||||
            shutil.move(temp_path, orig_path)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def can_access(file_name):
 | 
			
		||||
    """True if we have read/write access to the file."""
 | 
			
		||||
    return os.access(file_name, os.R_OK | os.W_OK)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user