>
> Following the law of the least surprise, code that does what a human
> expects would be best. In this case, that means that code that does an
> upward search for the first parent directory that does NOT contain an
> __init__.py (starting from the dirname of the single .py file) would be
> what *I* would expect. ;)
>
> Note that the corner case that the .py file is isolated (a script) and not
> part of a package works too, since then dir containing the script is added.


Alright, that sounds good. I can't figure out how to attach a patch to
http://www.logilab.org/ticket/88218, so it's attached to this email.
diff -r f673b9738dcd lint.py
--- a/lint.py	Mon Jan 09 21:25:10 2012 +0100
+++ b/lint.py	Thu Feb 02 10:42:06 2012 -0700
@@ -866,7 +866,10 @@
             sys.exit(32)
         # insert current working directory to the python path to have a correct
         # behaviour
-        sys.path.insert(0, os.getcwd())
+        if len(args) == 1:
+            sys.path.insert(0, self._get_python_path(args[0]))
+        else:
+            sys.path.insert(0, os.getcwd())
         if self.linter.config.profile:
             print >> sys.stderr, '** profiled run'
             import cProfile, pstats
@@ -881,6 +884,16 @@
         if exit:
             sys.exit(self.linter.msg_status)
 
+    def _get_python_path(self, filepath):
+        dirname = os.path.dirname(os.path.realpath(
+                os.path.expanduser(filepath)))
+        while True:
+            if not os.path.exists(os.path.join(dirname, "__init__.py")):
+                return dirname
+            old_dirname = dirname
+            dirname = os.path.dirname(dirname)
+            if old_dirname == dirname: return os.getcwd()
+
     def cb_set_rcfile(self, name, value):
         """callback for option preprocessing (i.e. before optik parsing)"""
         self._rcfile = value
_______________________________________________
Python-Projects mailing list
[email protected]
http://lists.logilab.org/mailman/listinfo/python-projects

Reply via email to