OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 from slave import recipe_api | 5 from slave import recipe_api |
6 | 6 |
7 | 7 |
8 class FileApi(recipe_api.RecipeApi): | 8 class FileApi(recipe_api.RecipeApi): |
9 """FileApi contains helper functions for reading and writing files.""" | 9 """FileApi contains helper functions for reading and writing files.""" |
10 | 10 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 name, | 91 name, |
92 """ | 92 """ |
93 import os | 93 import os |
94 import sys | 94 import sys |
95 os.remove(sys.argv[1]) | 95 os.remove(sys.argv[1]) |
96 """, | 96 """, |
97 args=[path], | 97 args=[path], |
98 **kwargs | 98 **kwargs |
99 ) | 99 ) |
100 | 100 |
| 101 def listdir(self, name, path, step_test_data=None): |
| 102 """Wrapper for os.listdir.""" |
| 103 return self.m.python.inline('listdir %s' % name, |
| 104 """ |
| 105 import json, os, sys |
| 106 if os.path.exists(sys.argv[1]) and os.path.isdir(sys.argv[1]): |
| 107 with open(sys.argv[2], 'w') as f: |
| 108 json.dump(os.listdir(sys.argv[1]), f) |
| 109 """, |
| 110 args=[path, self.m.json.output()], |
| 111 step_test_data=(step_test_data or |
| 112 self.test_api.listdir(['file 1', 'file 2'])), |
| 113 ).json.output |
| 114 |
| 115 def makedirs(self, name, path, mode=0777): |
| 116 """ |
| 117 Like os.makedirs, except that if the directory exists, then there is no |
| 118 error. |
| 119 """ |
| 120 self.m.path.assert_absolute(path) |
| 121 self.m.python.inline( |
| 122 'makedirs ' + name, |
| 123 """ |
| 124 import sys, os |
| 125 path = sys.argv[1] |
| 126 mode = int(sys.argv[2]) |
| 127 if not os.path.isdir(path): |
| 128 if os.path.exists(path): |
| 129 print "%s exists but is not a dir" % path |
| 130 sys.exit(1) |
| 131 os.makedirs(path, mode) |
| 132 """, |
| 133 args=[path, str(mode)], |
| 134 ) |
| 135 self.m.path.mock_add_paths(path) |
| 136 |
| 137 def rmtree(self, name, path): |
| 138 """Wrapper for chromium_utils.RemoveDirectory.""" |
| 139 self.m.path.assert_absolute(path) |
| 140 self.m.python.inline( |
| 141 'rmtree ' + name, |
| 142 """ |
| 143 import os, sys |
| 144 from common import chromium_utils |
| 145 |
| 146 if os.path.exists(sys.argv[1]): |
| 147 chromium_utils.RemoveDirectory(sys.argv[1]) |
| 148 """, |
| 149 args=[path], |
| 150 ) |
| 151 |
| 152 def rmcontents(self, name, path): |
| 153 """ |
| 154 Similar to rmtree, but removes only contents not the directory. |
| 155 |
| 156 This is useful e.g. when removing contents of current working directory. |
| 157 Deleting current working directory makes all further getcwd calls fail |
| 158 until chdir is called. chdir would be tricky in recipes, so we provide |
| 159 a call that doesn't delete the directory itself. |
| 160 """ |
| 161 self.m.path.assert_absolute(path) |
| 162 self.m.python.inline( |
| 163 'rmcontents ' + name, |
| 164 """ |
| 165 import os, sys |
| 166 from common import chromium_utils |
| 167 |
| 168 for p in [os.path.join(sys.argv[1], x) for x in os.listdir(sys.argv[1])]: |
| 169 if os.path.isdir(p): |
| 170 chromium_utils.RemoveDirectory(p) |
| 171 else: |
| 172 os.unlink(p) |
| 173 """, |
| 174 args=[path], |
| 175 ) |
| 176 |
| 177 def rmwildcard(self, pattern, path, **kwargs): |
| 178 """ |
| 179 Removes all files in the subtree of path matching the glob pattern. |
| 180 """ |
| 181 self.m.path.assert_absolute(path) |
| 182 self.m.python.inline( |
| 183 'rmwildcard %s in %s' % (pattern, path), |
| 184 """ |
| 185 import sys |
| 186 from common import chromium_utils |
| 187 |
| 188 chromium_utils.RemoveFilesWildcards(sys.argv[1], root=sys.argv[2]) |
| 189 """, |
| 190 args=[pattern,path], |
| 191 **kwargs) |
OLD | NEW |