1# Copyright 2014 Altera Corporation. All Rights Reserved. 2# Author: John McGehee 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16""" 17Example module that is tested in :py:class`pyfakefs.example_test.TestExample`. 18This demonstrates the usage of the 19:py:class`pyfakefs.fake_filesystem_unittest.TestCase` base class. 20 21The modules related to file handling are bound to the respective fake modules: 22 23>>> os #doctest: +ELLIPSIS 24<pyfakefs.fake_filesystem.FakeOsModule object...> 25>>> os.path #doctest: +ELLIPSIS 26<pyfakefs.fake_filesystem.FakePathModule object...> 27>>> shutil #doctest: +ELLIPSIS 28<pyfakefs.fake_filesystem_shutil.FakeShutilModule object...> 29 30`open()` is an alias for `io.open()` and is bound to `FakeIoModule.open`. 31""" 32 33import glob 34import os 35import shutil 36 37try: 38 import scandir 39 has_scandir = True 40except ImportError: 41 scandir = None 42 has_scandir = False 43 44 45def create_file(path): 46 """Create the specified file and add some content to it. Use the `open()` 47 built in function. 48 49 For example, the following file operations occur in the fake file system. 50 In the real file system, we would not even have permission 51 to write `/test`: 52 53 >>> os.path.isdir('/test') 54 False 55 >>> os.mkdir('/test') 56 >>> os.path.isdir('/test') 57 True 58 >>> os.path.exists('/test/file.txt') 59 False 60 >>> create_file('/test/file.txt') 61 >>> os.path.exists('/test/file.txt') 62 True 63 >>> with open('/test/file.txt') as f: 64 ... f.readlines() 65 ["This is test file '/test/file.txt'.\\n", \ 66'It was created using open().\\n'] 67 """ 68 with open(path, 'w') as f: 69 f.write("This is test file '{0}'.\n".format(path)) 70 f.write("It was created using open().\n") 71 72 73def delete_file(path): 74 """Delete the specified file. 75 76 For example: 77 78 >>> os.mkdir('/test') 79 >>> os.path.exists('/test/file.txt') 80 False 81 >>> create_file('/test/file.txt') 82 >>> os.path.exists('/test/file.txt') 83 True 84 >>> delete_file('/test/file.txt') 85 >>> os.path.exists('/test/file.txt') 86 False 87 """ 88 os.remove(path) 89 90 91def path_exists(path): 92 """Return True if the specified file exists. 93 94 For example: 95 96 >>> path_exists('/test') 97 False 98 >>> os.mkdir('/test') 99 >>> path_exists('/test') 100 True 101 >>> 102 >>> path_exists('/test/file.txt') 103 False 104 >>> create_file('/test/file.txt') 105 >>> path_exists('/test/file.txt') 106 True 107 """ 108 return os.path.exists(path) 109 110 111def get_glob(glob_path): 112 r"""Return the list of paths matching the specified glob expression. 113 114 For example: 115 116 >>> os.mkdir('/test') 117 >>> create_file('/test/file1.txt') 118 >>> create_file('/test/file2.txt') 119 >>> file_names = sorted(get_glob('/test/file*.txt')) 120 >>> 121 >>> import sys 122 >>> if sys.platform.startswith('win'): 123 ... # Windows style path 124 ... file_names == [r'/test\file1.txt', r'/test\file2.txt'] 125 ... else: 126 ... # UNIX style path 127 ... file_names == ['/test/file1.txt', '/test/file2.txt'] 128 True 129 """ 130 return glob.glob(glob_path) 131 132 133def rm_tree(path): 134 """Delete the specified file hierarchy.""" 135 shutil.rmtree(path) 136 137 138def scan_dir(path): 139 """Return a list of directory entries for the given path.""" 140 if has_scandir: 141 return list(scandir.scandir(path)) 142 return list(os.scandir(path)) 143 144 145def file_contents(path): 146 """Return the contents of the given path as byte array.""" 147 with open(path, 'rb') as f: 148 return f.read() 149