]> git.phdru.name Git - ppu.git/commitdiff
Version 0.4.0: Add script which.py 0.4.0
authorOleg Broytman <phd@phdru.name>
Sun, 4 Jun 2017 19:18:28 +0000 (22:18 +0300)
committerOleg Broytman <phd@phdru.name>
Sun, 4 Jun 2017 19:18:28 +0000 (22:18 +0300)
TODO
docs/index.rst
docs/news.rst
ppu/find_executable.py [new file with mode: 0755]
scripts/which.py [new file with mode: 0755]
setup.cfg
setup.py
tests/test_which.py [new file with mode: 0755]

diff --git a/TODO b/TODO
index b40bc8d843c4fd2bdaad2905a74beb0f5b5709cf..0afd4eb33d4b7d1d26f60095f486bd5267a33dbb 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,6 +1,3 @@
-Add which.py.
-
-
 Extend remove-old-files.py: add option -v/--verbose to report every removed file.
 
 
index 4605aaa1e8a1fba8488542ed89aee2b0b4768eca..645a859a83c62ceaf15391a00fa367e194671671 100644 (file)
@@ -54,6 +54,15 @@ Usage::
 
     rm.py file1 [file2...]
 
+which.py
+~~~~~~~~
+
+Find a program in PATH and print full path to it.
+
+Usage::
+
+    which.py program
+
 Indices and tables
 ==================
 
index 9d15512c2859ec0d8cb0466b9eff8e12d5b98573..1948545349bcf385b5dade1cd7268d223a6a1635 100644 (file)
@@ -6,6 +6,10 @@ Version 0.4.0 (2017-06-??)
 
 * Add package 'ppu'.
 
+* Add module ppu/find_executable.py.
+
+* Add script which.py.
+
 Version 0.3.2 (2017-05-01)
 --------------------------
 
diff --git a/ppu/find_executable.py b/ppu/find_executable.py
new file mode 100755 (executable)
index 0000000..0c4720c
--- /dev/null
@@ -0,0 +1,48 @@
+#!/usr/bin/env python
+
+
+# https://gist.github.com/4368898
+# Public domain code by anatoly techtonik <techtonik@gmail.com>
+# AKA Linux `which` and Windows `where`
+
+
+import os
+import sys
+
+
+def find_executable(executable, path=None):
+    """Find if 'executable' can be run. Looks for it in 'path'
+    (string that lists directories separated by 'os.pathsep';
+    defaults to os.environ['PATH']). Checks for all executable
+    extensions. Returns full path or None if no command is found.
+    """
+    if path is None:
+        path = os.environ['PATH']
+    paths = path.split(os.pathsep)
+    extlist = ['']
+    if os.name == 'os2':
+        (base, ext) = os.path.splitext(executable)
+        # executable files on OS/2 can have an arbitrary extension, but
+        # .exe is automatically appended if no dot is present in the name
+        if not ext:
+            executable = executable + ".exe"
+    elif sys.platform == 'win32':
+        pathext = os.environ['PATHEXT'].lower().split(os.pathsep)
+        (base, ext) = os.path.splitext(executable)
+        if ext.lower() not in pathext:
+            extlist = pathext
+    for ext in extlist:
+        execname = executable + ext
+        if os.path.isfile(execname):
+            return execname
+        else:
+            for p in paths:
+                f = os.path.join(p, execname)
+                if os.path.isfile(f):
+                    return f
+    else:
+        return None
+
+
+if __name__ == '__main__':
+    print(find_executable('git'))
diff --git a/scripts/which.py b/scripts/which.py
new file mode 100755 (executable)
index 0000000..f2daae2
--- /dev/null
@@ -0,0 +1,11 @@
+#! /usr/bin/env python
+
+import sys
+from ppu.find_executable import find_executable
+
+if len(sys.argv) != 2:
+    sys.exit("Usage: %s program" % sys.argv[0])
+
+program = find_executable(sys.argv[1])
+if program:
+    print(program)
index d84280f9298ae87ecddb7fe37b49c9b0b8629fdb..708bf5e4044d63124ea3ffd0297e65eab9cab314 100644 (file)
--- a/setup.cfg
+++ b/setup.cfg
@@ -2,7 +2,6 @@
 universal = 1
 
 [easy_install]
-find_links = http://phdru.name/Software/Python/
 optimize = 2
 
 [egg_info]
index 99b5c17ceeaebd3ba97d97fbc57a8e836d8617f4..fba415a2ee3fc5327bb7a60ff07d39e1ce2b3a5f 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -1,7 +1,5 @@
 #!/usr/bin/env python
 
-import sys
-
 try:
     from setuptools import setup
 except ImportError:
@@ -35,5 +33,6 @@ setup(name='ppu',
       packages=['ppu'],
       scripts=[
           'scripts/cmp.py', 'scripts/remove-old-files.py', 'scripts/rm.py',
+          'scripts/which.py',
       ],
       )
diff --git a/tests/test_which.py b/tests/test_which.py
new file mode 100755 (executable)
index 0000000..5de5a64
--- /dev/null
@@ -0,0 +1,22 @@
+#! /usr/bin/env python
+
+import os
+import subprocess
+import sys
+from ppu_tu import setup, teardown, find_in_path  # noqa
+
+
+tmp_dir = None
+
+test_prog_path = find_in_path('which.py')
+if not test_prog_path:
+    sys.exit("Cannot find which.py in %s" % os.environ["PATH"])
+
+
+def test_which():
+    assert subprocess.check_output(
+        [sys.executable, test_prog_path, "which.py"],
+        universal_newlines=True).strip() == test_prog_path
+    assert subprocess.check_output(
+        [sys.executable, test_prog_path, "WhoWhereWhenceWhichWhereIs.py"],
+        universal_newlines=True).strip() == ''