Skip to content

Call python scripts as argument to virtualenv python command #1559

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

Merged
merged 2 commits into from
Aug 12, 2015
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
7 changes: 6 additions & 1 deletion readthedocs/doc_builder/backends/mkdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,19 @@ def append_conf(self, **kwargs):
def build(self, **kwargs):
checkout_path = self.project.checkout_path(self.version.slug)
build_command = [
'python',
self.project.venv_bin(version=self.version.slug, bin='mkdocs'),
self.builder,
'--clean',
'--site-dir', self.build_dir,
]
if self.use_theme:
build_command.extend(['--theme', 'readthedocs'])
cmd_ret = self.run(*build_command, cwd=checkout_path)
cmd_ret = self.run(
*build_command,
cwd=checkout_path,
bin_path=self.project.venv_bin(version=self.version.slug, bin=None)
)
return cmd_ret.successful


Expand Down
12 changes: 9 additions & 3 deletions readthedocs/doc_builder/backends/sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def build(self, **kwargs):
self.clean()
project = self.project
build_command = [
'python',
project.venv_bin(version=self.version.slug, bin='sphinx-build'),
'-T'
]
Expand All @@ -138,8 +139,11 @@ def build(self, **kwargs):
'.',
self.sphinx_build_dir
])
cmd_ret = self.run(*build_command,
cwd=project.conf_dir(self.version.slug))
cmd_ret = self.run(
*build_command,
cwd=project.conf_dir(self.version.slug),
bin_path=project.venv_bin(version=self.version.slug, bin=None)
)
return cmd_ret.successful


Expand Down Expand Up @@ -232,13 +236,15 @@ def build(self, **kwargs):

# Default to this so we can return it always.
self.run(
'python',
self.project.venv_bin(version=self.version.slug, bin='sphinx-build'),
'-b', 'latex',
'-D', 'language={lang}'.format(lang=self.project.language),
'-d', '_build/doctrees',
'.',
'_build/latex',
cwd=cwd
cwd=cwd,
bin_path=self.project.venv_bin(version=self.version.slug, bin=None)
)
latex_cwd = os.path.join(cwd, '_build', 'latex')
tex_files = glob(os.path.join(latex_cwd, '*.tex'))
Expand Down
8 changes: 7 additions & 1 deletion readthedocs/doc_builder/environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class BuildCommand(object):

# TODO add short name here for reporting
def __init__(self, command, cwd=None, shell=False, environment=None,
combine_output=True, input_data=None, build_env=None):
combine_output=True, input_data=None, build_env=None,
bin_path=None):
self.command = command
self.shell = shell
if cwd is None:
Expand All @@ -55,6 +56,7 @@ def __init__(self, command, cwd=None, shell=False, environment=None,
self.environment.update(environment)
self.combine_output = combine_output
self.build_env = build_env
self.bin_path = bin_path
self.status = None
self.input_data = input_data
self.output = None
Expand Down Expand Up @@ -91,6 +93,10 @@ def run(self):
del environment['DJANGO_SETTINGS_MODULE']
if 'PYTHONPATH' in environment:
del environment['PYTHONPATH']
if self.bin_path is not None:
env_paths = environment.get('PATH', '').split(':')
env_paths.insert(0, self.bin_path)
environment['PATH'] = ':'.join(env_paths)

try:
proc = subprocess.Popen(
Expand Down
8 changes: 8 additions & 0 deletions readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,14 @@ def single_version_symlink_path(self):
#

def venv_bin(self, version=LATEST, bin='python'):
Copy link
Member

Choose a reason for hiding this comment

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

Might make sense to just kill the python default here, so that we don't have to pass None everywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd prefer this, yes. I didn't want to have to refactor all of the calls, but searching again it looks like there aren't any calling python this way anyways.

"""Return path to the virtualenv bin path, or a specific binary

By default, return the path to the ``python`` binary in the virtual
environment path. If ``bin`` is :py:data:`None`, then return the path to
the virtual env path.
"""
if bin is None:
bin = ''
return os.path.join(self.venv_path(version), 'bin', bin)

def full_doc_path(self, version=LATEST):
Expand Down
26 changes: 18 additions & 8 deletions readthedocs/projects/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ def setup_environment(self):
]

cmd = [
'python',
self.project.venv_bin(version=self.version.slug, bin='pip'),
'install',
'--use-wheel',
Expand All @@ -280,7 +281,10 @@ def setup_environment(self):
# --system-site-packages is used)
cmd.append('-I')
cmd.extend(requirements)
self.build_env.run(*cmd)
self.build_env.run(
*cmd,
bin_path=self.project.venv_bin(version=self.version.slug, bin=None)
)

# Handle requirements
requirements_file_path = self.project.requirements_file
Expand All @@ -298,11 +302,14 @@ def setup_environment(self):

if requirements_file_path:
self.build_env.run(
'python',
self.project.venv_bin(version=self.version.slug, bin='pip'),
'install',
'--exists-action=w',
'-r{0}'.format(requirements_file_path),
cwd=checkout_path
cwd=checkout_path,
bin_path=self.project.venv_bin(version=self.version.slug,
bin=None)
)

# Handle setup.py
Expand All @@ -311,21 +318,24 @@ def setup_environment(self):
if os.path.isfile(setup_path):
if getattr(settings, 'USE_PIP_INSTALL', False):
self.build_env.run(
self.project.venv_bin(version=self.version.slug,
bin='pip'),
'python',
self.project.venv_bin(version=self.version.slug, bin='pip'),
'install',
'--ignore-installed',
'.',
cwd=checkout_path
cwd=checkout_path,
bin_path=self.project.venv_bin(version=self.version.slug,
bin=None)
)
else:
self.build_env.run(
self.project.venv_bin(version=self.version.slug,
bin='python'),
'python',
'setup.py',
'install',
'--force',
cwd=checkout_path
cwd=checkout_path,
bin_path=self.project.venv_bin(version=self.version.slug,
bin=None)
)

def build_docs(self):
Expand Down
3 changes: 2 additions & 1 deletion readthedocs/rtd_tests/tests/test_builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def test_build(self):
# Get command and check first part of command list is a call to sphinx
self.assertEqual(self.mocks.popen.call_count, 1)
cmd = self.mocks.popen.call_args_list[0][0]
self.assertRegexpMatches(cmd[0][0], r'sphinx-build')
self.assertRegexpMatches(cmd[0][0], r'python')
self.assertRegexpMatches(cmd[0][1], r'sphinx-build')

def test_build_respects_pdf_flag(self):
'''Build output format control'''
Expand Down