Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

depend on phantomjs_bin to provide the phantomjs binaries #78

Merged
merged 5 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'fudge',
'nti.testing',
'zope.testrunner',
'nose >= 1.3.0'
jamadden marked this conversation as resolved.
Show resolved Hide resolved
]


Expand Down Expand Up @@ -70,6 +71,7 @@ def _read(fname):
'nti.wref',
'Paste',
'PasteDeploy',
'phantomjs-binary==2.1.3',
'PyPDF2',
'pyquery',
'pytz',
Expand Down
27 changes: 14 additions & 13 deletions src/nti/contentrendering/phantom.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
from __future__ import absolute_import

import os
import stat

import sys
import subprocess
import contextlib
from six.moves import urllib_parse

from phantomjs_bin import executable_path as phantomjs_exe

try:
from urllib.request import pathname2url
except ImportError:
Expand All @@ -27,6 +31,15 @@

logger = __import__('logging').getLogger(__name__)

# Make sure our phantomjs executable is, in fact, user executable
# https://github.com/jayjiahua/phantomjs-bin-pip/issues/1
st = os.stat(phantomjs_exe)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest deferring all this until it is used (e.g., inside run_phantom_on_page), and only if running fails.

  1. Import-time side-effects are generally bad; and
  2. Especially when they can fail; and
  3. This may not be necessary at all, better to let the OS tell us if it is.

if not st.st_mode & stat.S_IXUSR:
import warnings
warnings.warn('Phantomjs executable %s is not executable. Updating permissions' % (phantomjs_exe),
UserWarning, stacklevel=1)
os.chmod(phantomjs_exe, st.st_mode | stat.S_IXUSR)


def javascript_path(js_name):
"""
Expand All @@ -39,18 +52,6 @@ def javascript_path(js_name):
return resource_filename(__name__, js_name)


if not os.getenv('DATASERVER_DIR_IS_BUILDOUT'):
# Buildout puts it first on the path
import warnings
try:
warnings.warn("Using whatever phantomjs is on the PATH; supported version 2.1.1; version found at %s is %s"
% (subprocess.check_output(['which', 'phantomjs']).strip(),
subprocess.check_output(['phantomjs', '-v']).strip()),
UserWarning, stacklevel=1)
except subprocess.CalledProcessError:
warnings.warn("Phantomjs not found on the PATH")


class _PhantomProducedUnexpectedOutputError(subprocess.CalledProcessError):

def __str__(self):
Expand Down Expand Up @@ -105,7 +106,7 @@ def run_phantom_on_page(htmlFile, scriptName, args=(), key=_none_key,
# over a socket as opposed to stdout/stderr? As of 1.6, I think this is the
# recommended approach

process = ['phantomjs', scriptName, htmlFile]
process = [phantomjs_exe, scriptName, htmlFile]
process.extend(args)
__traceback_info__ = process
logger.debug("Executing %s", process)
Expand Down