Permalink
Browse files

Merge pull request #1547 from cherrypy/bugfix/daemon-as-a-module

Don't chdir when running as a daemon
  • Loading branch information...
2 parents db152f6 + 9bcf3fb commit 2ce092b62e167349e987d2c0a26b2defa4e94804 @jaraco jaraco committed on GitHub Jan 11, 2017
Showing with 77 additions and 13 deletions.
  1. +3 −2 cherrypy/__main__.py
  2. +0 −6 cherrypy/cherryd
  3. +0 −1 cherrypy/process/plugins.py
  4. +52 −1 cherrypy/process/wspbus.py
  5. +1 −1 cherrypy/scaffold/__init__.py
  6. +20 −1 cherrypy/test/helper.py
  7. +1 −1 setup.py
@@ -1,4 +1,5 @@
-import cherrypy.daemon
+from cherrypy.daemon import run
+
if __name__ == '__main__':
- cherrypy.daemon.run()
+ run()
View
@@ -1,6 +0,0 @@
-#! /usr/bin/env python
-
-import cherrypy.daemon
-
-if __name__ == '__main__':
- cherrypy.daemon.run()
@@ -409,7 +409,6 @@ def start(self):
sys.exit('%s: fork #2 failed: (%d) %s\n'
% (sys.argv[0], exc.errno, exc.strerror))
- os.chdir('/')
os.umask(0)
si = open(self.stdin, 'r')
@@ -447,6 +447,57 @@ def _get_true_argv():
argc = ctypes.c_int()
ctypes.pythonapi.Py_GetArgcArgv(ctypes.byref(argc), ctypes.byref(argv))
+
+ _argv = argv[:argc.value]
+
+ # The code below is trying to correctly handle special cases.
+ # `-c`'s argument interpreted by Python itself becomes `-c` as
+ # well. Same applies to `-m`. This snippet is trying to survive
+ # at least the case with `-m`
+ # Ref: https://github.com/cherrypy/cherrypy/issues/1545
+ # Ref: python/cpython@418baf9
+ argv_len, is_command, is_module = len(_argv), False, False
+
+ try:
+ m_ind = _argv.index('-m')
+ if m_ind < argv_len - 1 and _argv[m_ind + 1] in ('-c', '-m'):
+ """
+ In some older Python versions `-m`'s argument may be
+ substituted with `-c`, not `-m`
+ """
+ is_module = True
+ except (IndexError, ValueError):
+ m_ind = None
+
+ try:
+ c_ind = _argv.index('-c')
+ if m_ind < argv_len - 1 and _argv[c_ind + 1] == '-c':
+ is_command = True
+ except (IndexError, ValueError):
+ c_ind = None
+
+ if is_module:
+ """It's containing `-m -m` sequence of arguments"""
+ if is_command and c_ind < m_ind:
+ """There's `-c -c` before `-m`"""
+ raise RuntimeError(
+ "Cannot reconstruct command from '-c'. Ref: "
+ 'https://github.com/cherrypy/cherrypy/issues/1545')
+ # Survive module argument here
+ original_module = sys.argv[0]
+ if not os.access(original_module, os.R_OK):
+ """There's no such module exist"""
+ raise AttributeError(
+ "{} doesn't seem to be a module "
+ "accessible by current user".format(original_module))
+ del _argv[m_ind:m_ind + 2] # remove `-m -m`
+ # ... and substitute it with the original module path:
+ _argv.insert(m_ind, original_module)
+ elif is_command:
+ """It's containing just `-c -c` sequence of arguments"""
+ raise RuntimeError(
+ "Cannot reconstruct command from '-c'. "
+ 'Ref: https://github.com/cherrypy/cherrypy/issues/1545')
except AttributeError:
"""It looks Py_GetArgcArgv is completely absent in some environments
@@ -459,7 +510,7 @@ def _get_true_argv():
"""
raise NotImplementedError
else:
- return argv[:argc.value]
+ return _argv
@staticmethod
def _extend_pythonpath(env):
@@ -8,7 +8,7 @@
Even before any tweaking, this should serve a few demonstration pages.
Change to this directory and run:
- ../cherryd -c site.conf
+ cherryd -c site.conf
"""
@@ -473,10 +473,29 @@ def start(self, imports=None):
cherrypy._cpserver.wait_for_free_port(self.host, self.port)
args = [
- os.path.join(thisdir, '..', 'cherryd'),
+ '-m',
+ 'cherrypy.__main__', # __main__ is needed for `-m` in Python 2.6
'-c', self.config_file,
'-p', self.pid_file,
]
+ """
+ Command for running cherryd server with autoreload enabled
+
+ Using
+
+ ```
+ ['-c',
+ "__requires__ = 'CherryPy'; \
+ import pkg_resources, re, sys; \
+ sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]); \
+ sys.exit(\
+ pkg_resources.load_entry_point(\
+ 'CherryPy', 'console_scripts', 'cherryd')())"]
+ ```
+
+ doesn't work as it's impossible to reconstruct the `-c`'s contents.
+ Ref: https://github.com/cherrypy/cherrypy/issues/1545
+ """
if not isinstance(imports, (list, tuple)):
imports = [imports]
View
@@ -107,7 +107,7 @@
url=url,
license=cp_license,
packages=packages,
- scripts=scripts,
+ entry_points={"console_scripts": ["cherryd = cherrypy.__main__:run"]},
include_package_data=True,
install_requires=install_requires,
extras_require=extras_require,

0 comments on commit 2ce092b

Please sign in to comment.