Index: scripts/slave/recipe_modules/file/api.py |
diff --git a/scripts/slave/recipe_modules/file/api.py b/scripts/slave/recipe_modules/file/api.py |
index 56482112c9a99a5734ae68e5c9e7ff708dd0d7b6..11135feda78857195df51f39da977b465bdf1a53 100644 |
--- a/scripts/slave/recipe_modules/file/api.py |
+++ b/scripts/slave/recipe_modules/file/api.py |
@@ -98,3 +98,94 @@ class FileApi(recipe_api.RecipeApi): |
**kwargs |
) |
+ def listdir(self, name, path, step_test_data=None): |
+ """Wrapper for os.listdir.""" |
+ return self.m.python.inline('listdir %s' % name, |
+ """ |
+ import json, os, sys |
+ if os.path.exists(sys.argv[1]) and os.path.isdir(sys.argv[1]): |
+ with open(sys.argv[2], 'w') as f: |
+ json.dump(os.listdir(sys.argv[1]), f) |
+ """, |
+ args=[path, self.m.json.output()], |
+ step_test_data=(step_test_data or |
+ self.test_api.listdir(['file 1', 'file 2'])), |
+ ).json.output |
+ |
+ def makedirs(self, name, path, mode=0777): |
+ """ |
+ Like os.makedirs, except that if the directory exists, then there is no |
+ error. |
+ """ |
+ self.m.path.assert_absolute(path) |
+ self.m.python.inline( |
+ 'makedirs ' + name, |
+ """ |
+ import sys, os |
+ path = sys.argv[1] |
+ mode = int(sys.argv[2]) |
+ if not os.path.isdir(path): |
+ if os.path.exists(path): |
+ print "%s exists but is not a dir" % path |
+ sys.exit(1) |
+ os.makedirs(path, mode) |
+ """, |
+ args=[path, str(mode)], |
+ ) |
+ self.m.path.mock_add_paths(path) |
+ |
+ def rmtree(self, name, path): |
+ """Wrapper for chromium_utils.RemoveDirectory.""" |
+ self.m.path.assert_absolute(path) |
+ self.m.python.inline( |
+ 'rmtree ' + name, |
+ """ |
+ import os, sys |
+ from common import chromium_utils |
+ |
+ if os.path.exists(sys.argv[1]): |
+ chromium_utils.RemoveDirectory(sys.argv[1]) |
+ """, |
+ args=[path], |
+ ) |
+ |
+ def rmcontents(self, name, path): |
+ """ |
+ Similar to rmtree, but removes only contents not the directory. |
+ |
+ This is useful e.g. when removing contents of current working directory. |
+ Deleting current working directory makes all further getcwd calls fail |
+ until chdir is called. chdir would be tricky in recipes, so we provide |
+ a call that doesn't delete the directory itself. |
+ """ |
+ self.m.path.assert_absolute(path) |
+ self.m.python.inline( |
+ 'rmcontents ' + name, |
+ """ |
+ import os, sys |
+ from common import chromium_utils |
+ |
+ for p in [os.path.join(sys.argv[1], x) for x in os.listdir(sys.argv[1])]: |
+ if os.path.isdir(p): |
+ chromium_utils.RemoveDirectory(p) |
+ else: |
+ os.unlink(p) |
+ """, |
+ args=[path], |
+ ) |
+ |
+ def rmwildcard(self, pattern, path, **kwargs): |
+ """ |
+ Removes all files in the subtree of path matching the glob pattern. |
+ """ |
+ self.m.path.assert_absolute(path) |
+ self.m.python.inline( |
+ 'rmwildcard %s in %s' % (pattern, path), |
+ """ |
+ import sys |
+ from common import chromium_utils |
+ |
+ chromium_utils.RemoveFilesWildcards(sys.argv[1], root=sys.argv[2]) |
+ """, |
+ args=[pattern,path], |
+ **kwargs) |