From 2cf909c01ef03ca0ee2201072d5e5d5a45c9c5f5 Mon Sep 17 00:00:00 2001 From: Al Date: Tue, 17 Mar 2015 18:39:08 -0400 Subject: [PATCH] [utils] script utils --- scripts/geodata/file_utils.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/scripts/geodata/file_utils.py b/scripts/geodata/file_utils.py index 3cef7cb1..da632ec9 100644 --- a/scripts/geodata/file_utils.py +++ b/scripts/geodata/file_utils.py @@ -2,6 +2,10 @@ import os import subprocess +def download_file(url, dest_dir): + return subprocess.call(['wget', url, '-O', dest_dir, '--quiet']) == 0 + + def unzip_file(filename, dest_dir): subprocess.call(['unzip', '-o', filename, '-d', dest_dir]) @@ -13,3 +17,16 @@ def remove_file(filename): def ensure_dir(d): if not os.path.exists(d): os.makedirs(d) + + +class cd: + """Context manager for changing the current working directory""" + def __init__(self, path): + self.path = path + + def __enter__(self): + self.saved_path = os.getcwd() + os.chdir(self.path) + + def __exit__(self, etype, value, traceback): + os.chdir(self.saved_path)