PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/telemetry/third_party/pyfakefs/pyfakefs/example.py

https://gitlab.com/jonnialva90/iridium-browser
Python | 121 lines | 74 code | 3 blank | 44 comment | 0 complexity | d08283fd5468d7ec274383673dd42e34 MD5 | raw file
  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. Example module that is tested in :py:class`pyfakefs.example_test.TestExample`.
  17. This demonstrates the usage of the
  18. :py:class`pyfakefs.fake_filesystem_unittest.TestCase` base class.
  19. The modules related to file handling are bound to the respective fake modules:
  20. >>> os #doctest: +ELLIPSIS
  21. <fake_filesystem.FakeOsModule object...>
  22. >>> os.path #doctest: +ELLIPSIS
  23. <fake_filesystem.FakePathModule object...>
  24. >>> glob #doctest: +ELLIPSIS
  25. <fake_filesystem_glob.FakeGlobModule object...>
  26. >>> shutil #doctest: +ELLIPSIS
  27. <fake_filesystem_shutil.FakeShutilModule object...>
  28. The `open()` built-in is bound to the fake `open()`:
  29. >>> open #doctest: +ELLIPSIS
  30. <fake_filesystem.FakeFileOpen object...>
  31. In Python 2 the `file()` built-in is also bound to the fake `open()`. `file()`
  32. was eliminated in Python 3.
  33. """
  34. import os
  35. import glob
  36. import shutil
  37. def create_file(path):
  38. '''Create the specified file and add some content to it. Use the `open()`
  39. built in function.
  40. For example, the following file operations occur in the fake file system.
  41. In the real file system, we would not even have permission to write `/test`:
  42. >>> os.path.isdir('/test')
  43. False
  44. >>> os.mkdir('/test')
  45. >>> os.path.isdir('/test')
  46. True
  47. >>> os.path.exists('/test/file.txt')
  48. False
  49. >>> create_file('/test/file.txt')
  50. >>> os.path.exists('/test/file.txt')
  51. True
  52. >>> with open('/test/file.txt') as f:
  53. ... f.readlines()
  54. ["This is test file '/test/file.txt'.\\n", 'It was created using the open() function.\\n']
  55. '''
  56. with open(path, 'w') as f:
  57. f.write("This is test file '{}'.\n".format(path))
  58. f.write("It was created using the open() function.\n")
  59. def delete_file(path):
  60. '''Delete the specified file.
  61. For example:
  62. >>> os.mkdir('/test')
  63. >>> os.path.exists('/test/file.txt')
  64. False
  65. >>> create_file('/test/file.txt')
  66. >>> os.path.exists('/test/file.txt')
  67. True
  68. >>> delete_file('/test/file.txt')
  69. >>> os.path.exists('/test/file.txt')
  70. False
  71. '''
  72. os.remove(path)
  73. def path_exists(path):
  74. '''Return True if the specified file exists.
  75. For example:
  76. >>> path_exists('/test')
  77. False
  78. >>> os.mkdir('/test')
  79. >>> path_exists('/test')
  80. True
  81. >>>
  82. >>> path_exists('/test/file.txt')
  83. False
  84. >>> create_file('/test/file.txt')
  85. >>> path_exists('/test/file.txt')
  86. True
  87. '''
  88. return os.path.exists(path)
  89. def get_glob(glob_path):
  90. '''Return the list of paths matching the specified glob expression.
  91. For example:
  92. >>> os.mkdir('/test')
  93. >>> create_file('/test/file1.txt')
  94. >>> create_file('/test/file2.txt')
  95. >>> get_glob('/test/file*.txt')
  96. ['/test/file1.txt', '/test/file2.txt']
  97. '''
  98. return glob.glob(glob_path)
  99. def rm_tree(path):
  100. '''Delete the specified file hierarchy.'''
  101. shutil.rmtree(path)