Created
September 5, 2015 16:04
-
-
Save schwehr/c143927ca25d03a10265 to your computer and use it in GitHub Desktop.
GDAL autotest2 gcore utilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2014 Google Inc. All Rights Reserved. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
"""Support for tests in gcore. | |
This supplements the helper code in gdrivers/gdrivers_util.py. | |
""" | |
import contextlib | |
import os | |
import shutil | |
import tempfile | |
from osgeo import gdal | |
# FIX: Import correctly | |
# from gflags import flags | |
FLAGS = flags.FLAGS | |
def GetTestFilePath(filename): | |
return os.path.join( | |
os.path.split(os.path.abspath(__file__))[0], | |
'testdata', | |
filename | |
) | |
@contextlib.contextmanager | |
def ErrorHandler(error_name): | |
handler = gdal.PushErrorHandler(error_name) | |
try: | |
yield handler | |
finally: | |
gdal.PopErrorHandler() | |
@contextlib.contextmanager | |
def TestTemporaryDirectory(keep=False, prefix=None): | |
tempdir_base = gdal.GetConfigOption('TMPDIR', tempfile.gettempdir()) | |
prefix = prefix or 'GdalAutotest2' | |
tempdir = tempfile.mkdtemp(prefix+'-', dir=tempdir_base) | |
try: | |
yield tempdir | |
finally: | |
if not keep: | |
shutil.rmtree(tempdir) | |
@contextlib.contextmanager | |
def GdalUnlinkWhenDone(filepath): | |
try: | |
yield | |
finally: | |
gdal.Unlink(filepath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment