spack/lib/spack/spack/util/filesystem.py

59 lines
1.4 KiB
Python
Raw Normal View History

2013-10-08 09:54:58 +08:00
import os
import re
import shutil
import errno
from contextlib import contextmanager, closing
import spack.tty as tty
from spack.util.compression import ALLOWED_ARCHIVE_TYPES
def install(src, dest):
"""Manually install a file to a particular location."""
tty.info("Installing %s to %s" % (src, dest))
shutil.copy(src, dest)
@contextmanager
def working_dir(dirname):
orig_dir = os.getcwd()
os.chdir(dirname)
yield
os.chdir(orig_dir)
def mkdirp(*paths):
for path in paths:
if not os.path.exists(path):
os.makedirs(path)
elif not os.path.isdir(path):
raise OSError(errno.EEXIST, "File alredy exists", path)
def new_path(prefix, *args):
path = str(prefix)
2013-10-08 09:54:58 +08:00
for elt in args:
path = os.path.join(path, str(elt))
if re.search(r'\s', path):
tty.die("Invalid path: '%s'. Use a path without whitespace." % path)
return path
def ancestor(dir, n=1):
"""Get the nth ancestor of a directory."""
parent = os.path.abspath(dir)
for i in range(n):
parent = os.path.dirname(parent)
return parent
def stem(path):
"""Get the part of a path that does not include its compressed
type extension."""
for type in ALLOWED_ARCHIVE_TYPES:
suffix = r'\.%s$' % type
if re.search(suffix, path):
return re.sub(suffix, "", path)
return path