Browse Source

def dpkg_search & dpkg_which; alias dpkg-which

Fabian Peter Hammerle 6 years ago
parent
commit
43be01fab8
1 changed files with 16 additions and 0 deletions
  1. 16 0
      rc.xsh

+ 16 - 0
rc.xsh

@@ -44,6 +44,21 @@ if shutil.which('gpgconf'):
 # wrapper for termite required when launching termite from ranger:
 $TERMCMD = os.path.join(os.path.dirname(__file__), 'ranger-termite-termcmd')
 
+def dpkg_search(path_search_pattern):
+    assert isinstance(path_search_pattern, str)
+    return re.findall(
+        '^(\S+): (.*)$\n',
+        $(dpkg --search @(path_search_pattern)),
+        flags=re.MULTILINE,
+    )
+
+def dpkg_which(cmd):
+    cmd_path = shutil.which(cmd)
+    assert cmd_path, 'cmd {!r} not found'.format(cmd)
+    matches = dpkg_search(cmd_path)
+    assert len(matches) in [0, 1]
+    return matches[0] if len(matches) == 1 else None
+
 def locate(*patterns, match_all=True, ignore_case=True):
     params = []
     if match_all:
@@ -57,6 +72,7 @@ def timestamp_iso():
     # the system local timezone for the target timezone
     return dt.datetime.now().astimezone().strftime('%Y%m%dT%H%M%S%z')
 
+aliases['dpkg-which'] = lambda args: str(dpkg_which(args[0]))
 aliases['g'] = ['git']
 aliases['ll'] = ['ls', '-l', '--all', '--indicator-style=slash',
                  '--human-readable', '--time-style=long-iso', '--color=auto']