From 78638a131663681fc9a89b246446978abb196325 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 4 Jun 2017 22:18:28 +0300 Subject: [PATCH] Version 0.4.0: Add script which.py --- TODO | 3 --- docs/index.rst | 9 ++++++++ docs/news.rst | 4 ++++ ppu/find_executable.py | 48 ++++++++++++++++++++++++++++++++++++++++++ scripts/which.py | 11 ++++++++++ setup.cfg | 1 - setup.py | 3 +-- tests/test_which.py | 22 +++++++++++++++++++ 8 files changed, 95 insertions(+), 6 deletions(-) create mode 100755 ppu/find_executable.py create mode 100755 scripts/which.py create mode 100755 tests/test_which.py diff --git a/TODO b/TODO index b40bc8d..0afd4eb 100644 --- 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. diff --git a/docs/index.rst b/docs/index.rst index 4605aaa..645a859 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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 ================== diff --git a/docs/news.rst b/docs/news.rst index 9d15512..1948545 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -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 index 0000000..0c4720c --- /dev/null +++ b/ppu/find_executable.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python + + +# https://gist.github.com/4368898 +# Public domain code by anatoly techtonik +# 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 index 0000000..f2daae2 --- /dev/null +++ b/scripts/which.py @@ -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) diff --git a/setup.cfg b/setup.cfg index d84280f..708bf5e 100644 --- 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] diff --git a/setup.py b/setup.py index 99b5c17..fba415a 100755 --- 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 index 0000000..5de5a64 --- /dev/null +++ b/tests/test_which.py @@ -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() == '' -- 2.39.2