From d3402eac1ca362e1db0b40eb677e7cfc74a3d2c9 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Tue, 26 Jun 2012 12:16:39 -0700 Subject: 1. Add a chdir context manager 2. Add a abs path joining function that will return the absolute path of a combined path (where applicable) --- cloudinit/util.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cloudinit/util.py b/cloudinit/util.py index 2f5ed275..f95a5d07 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -486,6 +486,16 @@ def mergedict(src, cand): return src +@contextlib.contextmanager +def chdir(ndir): + curr = os.getcwd() + try: + os.chdir(ndir) + yield ndir + finally: + os.chdir(curr) + + @contextlib.contextmanager def umask(n_msk): old = os.umask(n_msk) @@ -1285,6 +1295,10 @@ def subp(args, data=None, rcs=None, env=None, capture=True, shell=False): return (out, err) +def abs_join(*paths): + return os.path.abspath(os.path.join(*paths)) + + # shellify, takes a list of commands # for each entry in the list # if it is an array, shell protect it (with single ticks) -- cgit v1.2.3