New python function
Lars Ivar Igesund <[email protected]> Thu, 22 Apr 2004 09:51:57 +0100
| Newsgroups | gmane.comp.tools.aap.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi I've made a new python function by modifying program_path. My version is a bit simpler and is called file_path. It can find any kind of file, whether it is executable or not. I don't know if it should be added as-is, but the new function is very useful in conf operations where you're looking for libraries, etc. Lars Ivar Igesund
recpython.diff
(text/plain, 1.5 KB)
Index: RecPython.py
===================================================================
RCS file: /cvsroot/a-a-p/Exec/RecPython.py,v
retrieving revision 1.38
diff -u -r1.38 RecPython.py
--- RecPython.py 8 Mar 2004 20:55:52 -0000 1.38
+++ RecPython.py 22 Apr 2004 07:31:45 -0000
@@ -363,6 +363,35 @@
return action
+def file_path(name, path = None, skip = None):
+ """Return the full name of a file "name" if it can be found in the
+ search path. None otherwise.
+ If "path" is given, use it as the search path (string of items
+ separated with os.pathsep).
+ If "skip" is given, skip this directory."""
+ # Decide on the list of directories to examine.
+ if not path is None:
+ envpath = path
+ elif os.environ.has_key('PATH'):
+ envpath = os.environ['PATH']
+ else:
+ envpath = os.defpath
+ if not type(envpath) is types.ListType:
+ dirlist = string.split(envpath, os.pathsep)
+ # When using a default path also include our own directory and its "bin"
+ # subdirectory.
+ if path is None and Global.aap_rootdir:
+ dirlist = [ Global.aap_rootdir, Global.aap_bindir ] + dirlist
+
+ for dir in dirlist:
+ if skip and os.path.abspath(dir) == os.path.abspath(skip):
+ continue
+ if os.path.isfile(fullname):
+ return fullname
+
+ return None
+
+
def program_path(name, path = None, pathext = None, skip = None):
"""Return the full name of a program "name" if it can be found in the
search path. None otherwise.
pydoc.diff
(text/plain, 2.6 KB)
Index: ref-python.sgml
===================================================================
RCS file: /cvsroot/a-a-p/Exec/doc/ref-python.sgml,v
retrieving revision 1.16
diff -u -r1.16 ref-python.sgml
--- ref-python.sgml 8 Mar 2004 20:58:38 -0000 1.16
+++ ref-python.sgml 22 Apr 2004 07:32:40 -0000
@@ -135,6 +135,10 @@
<entry> Return the contents of a file as a string. </entry>
</row>
<row>
+ <entry><link linkend="python-file-path"> file_path() </link></entry>
+ <entry> Search for a file and return the full path. </entry>
+ </row>
+ <row>
<entry><link linkend="python-program-path"> program_path() </link></entry>
<entry> Search for a program and return the full path. </entry>
</row>
@@ -674,6 +678,59 @@
</listitem>
</varlistentry>
+ <varlistentry id='python-file-path'>
+ <term><cmdsynopsis>
+ <command>file_path(name, path = None, skip = None)</command>
+ </cmdsynopsis></term>
+ <listitem>
+ <para>
+ Returns the path for file "name". This uses the
+ $PATH environment variable or os.defpath if it isn't
+ set.
+ </para>
+ <para>
+ Additionally, the directory where &Aap; is installed
+ and the "bin" subdirectory are searched. This finds
+ tools supplied with &Aap; and installed packages.
+ This is not done when the optional "path" argument is
+ supplied.
+ </para>
+ <para>
+ Returns the first file found.
+ Returns None when "name" could not be found.
+ </para>
+ <para>
+ Optional arguments:
+ <informaltable frame='none'>
+ <tgroup cols='2'>
+ <colspec colwidth="100"/>
+ <tbody>
+ <row>
+ <entry>path</entry>
+ <entry>search path to use instead of $PATH;
+ when a string items are separated with
+ os.pathsep</entry>
+ </row>
+ <row>
+ <entry>skip</entry>
+ <entry>name of directory to skip, "name" is not found in this
+ directory</entry>
+ </row>
+ </tbody>
+ </tgroup>
+ </informaltable>
+ </para>
+ <para>
+ Example, search for file "opengl32.dll":
+ </para>
+ <para>
+ <programlisting>
+ p = `file_path("opengl32.dll")`
+</programlisting>
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry id='python-program-path'>
<term><cmdsynopsis>