Browse Source

added functions locate() and update_locate_database()

Fabian Peter Hammerle 8 years ago
parent
commit
178ea3d532
1 changed files with 25 additions and 0 deletions
  1. 25 0
      osex/__init__.py

+ 25 - 0
osex/__init__.py

@@ -40,3 +40,28 @@ def symlink(source, link_name, relative = False, override = False, backup = True
             os.symlink(source, link_name)
         else:
             raise OSError("link's path '%s' already exists." % link_name)
+
+def update_locate_database():
+    import subprocess
+    subprocess.call(['sudo', 'updatedb'])
+
+def locate(patterns, match_all = False, ignore_case = False, update_database = False):
+    if update_database:
+        update_locate_database()
+    if type(patterns) is not list:
+        patterns = [str(patterns)]
+    patterns = [str(p) for p in patterns]
+    options = []
+    if match_all:
+        options.append("--all")
+    if ignore_case:
+        options.append("--ignore-case")
+    import subprocess
+    try:
+        output = subprocess.check_output(["locate"] + options + patterns)
+    except subprocess.CalledProcessError as e:
+        if e.returncode == 1:
+            return []
+        else:
+            raise e
+    return output.strip().split('\n')