1#!/usr/local/bin/python3.8 2# -*- coding: utf-8 -*- 3# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab 4 5# Copyright (c) 2014 Kevin B. Hendricks, John Schember, and Doug Massay 6# All rights reserved. 7# 8# Redistribution and use in source and binary forms, with or without modification, 9# are permitted provided that the following conditions are met: 10# 11# 1. Redistributions of source code must retain the above copyright notice, this list of 12# conditions and the following disclaimer. 13# 14# 2. Redistributions in binary form must reproduce the above copyright notice, this list 15# of conditions and the following disclaimer in the documentation and/or other materials 16# provided with the distribution. 17# 18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 19# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 21# SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 23# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 26# WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28from __future__ import unicode_literals, division, absolute_import, print_function 29from .compatibility_utils import PY2, text_type, binary_type 30 31import sys 32import os 33 34# utility routines to convert all paths to be full unicode 35 36# Under Python 2, if a bytestring, try to convert it to unicode using sys.getfilesystemencoding 37# Under Python 3, if bytes, try to convert it to unicode using os.fsencode() to decode it 38 39# Mac OS X and Windows will happily support full unicode paths 40# Linux can support full unicode paths but allows arbitrary byte paths which may be inconsistent with unicode 41 42fsencoding = sys.getfilesystemencoding() 43 44def pathof(s, enc=fsencoding): 45 if s is None: 46 return None 47 if isinstance(s, text_type): 48 return s 49 if isinstance(s, binary_type): 50 try: 51 return s.decode(enc) 52 except: 53 pass 54 return s 55 56def exists(s): 57 return os.path.exists(pathof(s)) 58 59def isfile(s): 60 return os.path.isfile(pathof(s)) 61 62def isdir(s): 63 return os.path.isdir(pathof(s)) 64 65def mkdir(s): 66 return os.mkdir(pathof(s)) 67 68def listdir(s): 69 rv = [] 70 for file in os.listdir(pathof(s)): 71 rv.append(pathof(file)) 72 return rv 73 74def getcwd(): 75 if PY2: 76 return os.getcwdu() 77 return os.getcwd() 78 79def walk(top): 80 top = pathof(top) 81 rv = [] 82 for base, dnames, names in os.walk(top): 83 base = pathof(base) 84 for name in names: 85 name = pathof(name) 86 rv.append(relpath(os.path.join(base, name), top)) 87 return rv 88 89def relpath(path, start=None): 90 return os.path.relpath(pathof(path) , pathof(start)) 91 92def abspath(path): 93 return os.path.abspath(pathof(path)) 94