Ver código fonte

No longer have to type PYTHONPATH=. to use celeryconfig in current dir.

This is accomplished by the default loader ensuring that the current directory
is in sys.path when loading the config module. sys.path is reset to its
original state after loading.

Adding cwd to sys.path without the user knowing may be a security issue,
as this means someone can drop a python module in the users directory that
executes arbitrary commands. This was the original reason not to do this,
but if done *only when loading the config module*, this means that this
behvavior will only apply to the modules imported in the config module,
which I think is a good compromise (certainly better than just explictly
setting PYTHONPATH=. anyway)
Ask Solem 14 anos atrás
pai
commit
c069f7d6a8

+ 3 - 3
Changelog

@@ -18,7 +18,7 @@
     If you've already hit this problem you may have to delete the
     declaration::
 
-        $ PYTHONPATH=. camqadm exchange.delete celerycrq
+        $ camqadm exchange.delete celerycrq
 
     or::
 
@@ -324,7 +324,7 @@ Backward incompatible changes
     If you've already used celery with this backend chances are you
     have to delete the previous declaration::
 
-        $ PYTHONPATH=. camqadm exchange.delete celeryresults
+        $ camqadm exchange.delete celeryresults
 
 * Now uses pickle instead of cPickle on Python versions <= 2.5
 
@@ -723,7 +723,7 @@ News
   If you've already used the AMQP backend this means you have to
   delete the previous definitions::
 
-      $ PYTHONPATH=. camqadm exchange.delete celeryresults
+      $ camqadm exchange.delete celeryresults
 
   or::
 

+ 0 - 4
bin/celerybeat

@@ -1,8 +1,4 @@
 #!/usr/bin/env python
-import sys
-if not '' in sys.path:
-    sys.path.insert(0, '')
-
 from celery.bin import celerybeat
 
 if __name__ == "__main__":

+ 0 - 3
bin/celeryd

@@ -1,7 +1,4 @@
 #!/usr/bin/env python
-import sys
-if not '' in sys.path:
-    sys.path.insert(0, '')
 from celery.bin import celeryd
 
 WINDOWS_MESSAGE = """

+ 0 - 4
bin/celeryev

@@ -1,8 +1,4 @@
 #!/usr/bin/env python
-import sys
-if not '' in sys.path:
-    sys.path.insert(0, '')
-
 from celery.bin import celeryev
 
 if __name__ == "__main__":

+ 24 - 5
celery/loaders/default.py

@@ -1,4 +1,5 @@
 import os
+import sys
 import warnings
 from importlib import import_module
 
@@ -54,20 +55,38 @@ class Loader(BaseLoader):
                              list(settings.INSTALLED_APPS))
         settings.INSTALLED_APPS = tuple(installed_apps)
         settings.CELERY_TASK_ERROR_WHITELIST = tuple(
-                getattr(import_module(mod), clas)
-                for fqn in settings.CELERY_TASK_ERROR_WHITELIST
-                for mod, clas in (fqn.rsplit('.', 1),)
-                )
+                getattr(import_module(mod), cls)
+                    for fqn in settings.CELERY_TASK_ERROR_WHITELIST
+                        for mod, cls in (fqn.rsplit('.', 1),))
 
         return settings
 
+    def import_from_cwd(self, module, imp=import_module):
+        """Import module, but make sure it finds modules
+        located in the current directory.
+
+        Modules located in the current directory has
+        precedence over modules located in ``sys.path``.
+        """
+        cwd = os.getcwd()
+        if cwd in sys.path:
+            return imp(module)
+        sys.path.insert(0, cwd)
+        try:
+            return imp(module)
+        finally:
+            try:
+                sys.path.remove(cwd)
+            except ValueError:
+                pass
+
     def read_configuration(self):
         """Read configuration from ``celeryconfig.py`` and configure
         celery and Django so it can be used by regular Python."""
         configname = os.environ.get("CELERY_CONFIG_MODULE",
                                     DEFAULT_CONFIG_MODULE)
         try:
-            celeryconfig = import_module(configname)
+            celeryconfig = self.import_from_cwd(configname)
         except ImportError:
             warnings.warn("No celeryconfig.py module found! Please make "
                           "sure it exists and is available to Python.",

+ 2 - 2
docs/getting-started/first-steps-with-celery.rst

@@ -86,7 +86,7 @@ Running the celery worker server
 To test we will run the worker server in the foreground, so we can
 see what's going on in the terminal::
 
-    $ PYTHONPATH="." celeryd --loglevel=INFO
+    $ celeryd --loglevel=INFO
 
 However, in production you probably want to run the worker in the
 background as a daemon. To do this you need to use to tools provided
@@ -95,7 +95,7 @@ by your platform, or something like `supervisord`_.
 For a complete listing of the command line options available, use the
 help command::
 
-    $  PYTHONPATH="." celeryd --help
+    $  celeryd --help
 
 For info on how to run celery as standalone daemon, see 
 :doc:`daemon mode reference<../cookbook/daemonizing>`