Skip to content

Commit

Permalink
[MRG+1]: Fixes for Windows (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner authored and lesteve committed Dec 12, 2016
1 parent 40349d7 commit fdbdf5a
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 36 deletions.
38 changes: 38 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

# CI on Windows via appveyor
# This file was based on Olivier Grisel's python-appveyor-demo

environment:

global:
PYTHON: "C:\\conda"
MINICONDA_VERSION: "latest"
CONDA_DEPENDENCIES: "numpy seaborn matplotlib mayavi sphinx pillow nose coverage"

matrix:
- PYTHON_VERSION: "2.7"
PYTHON_ARCH: "64"

platform:
-x64

install:
- "git clone git://github.com/astropy/ci-helpers.git"
- "powershell ci-helpers/appveyor/install-miniconda.ps1"
- "SET PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%"
- "activate test"

build: false # Not a C# project, build stuff at the test step instead.

test_script:
# Run the project tests
- nosetests.exe
# Build the docs
- python setup.py develop
- cd doc
# The Makefile doesn't work directly on Windows PowerShell because of
# slashes (but will work e.g. in Msys-git), so for now do these manually
# make html-noplot
- sphinx-build -D plot_gallery=0 -b html -d _build\doctrees . _build\html
# make html
- sphinx-build -b html -d _build\doctrees . _build\html
2 changes: 2 additions & 0 deletions sphinx_gallery/backreferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ def scan_used_functions(example_file, gallery_conf):
return backrefs


# XXX This figure:: uses a forward slash even on Windows, but the op.join's
# elsewhere will use backslashes...
THUMBNAIL_TEMPLATE = """
.. raw:: html
Expand Down
8 changes: 5 additions & 3 deletions sphinx_gallery/gen_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ def sumarize_failing_examples(app, exception):
return

gallery_conf = app.config.sphinx_gallery_conf
failing_examples = set(gallery_conf['failing_examples'])
expected_failing_examples = set(gallery_conf['expected_failing_examples'])
failing_examples = set([os.path.normpath(path) for path in
gallery_conf['failing_examples']])
expected_failing_examples = set([os.path.normpath(path) for path in
gallery_conf['expected_failing_examples']])

examples_expected_to_fail = failing_examples.intersection(
expected_failing_examples)
Expand All @@ -214,7 +216,7 @@ def sumarize_failing_examples(app, exception):
failing_examples)
if examples_not_expected_to_pass:
fail_msgs.append("Examples expected to fail, but not failling:\n" +
"Please remove this examples from\n" +
"Please remove these examples from\n" +
"sphinx_gallery_conf['expected_failing_examples']\n" +
"in your conf.py file"
"\n".join(examples_not_expected_to_pass))
Expand Down
6 changes: 4 additions & 2 deletions sphinx_gallery/gen_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def prefixed_lines():
from . import glr_path_static
from .backreferences import write_backreferences, _thumbnail_div
from .downloads import CODE_DOWNLOAD
from .py_source_parser import get_docstring_and_rest, split_code_and_text_blocks
from .py_source_parser import (get_docstring_and_rest,
split_code_and_text_blocks)

from .notebook import jupyter_notebook, text2string, save_notebook

Expand Down Expand Up @@ -170,7 +171,8 @@ def extract_thumbnail_number(text):

# check whether the user has specified a specific thumbnail image
pattr = re.compile(
r"^\s*#\s*sphinx_gallery_thumbnail_number\s*=\s*([0-9]+)\s*$", flags=re.MULTILINE)
r"^\s*#\s*sphinx_gallery_thumbnail_number\s*=\s*([0-9]+)\s*$",
flags=re.MULTILINE)
match = pattr.search(text)

if match is None:
Expand Down
2 changes: 2 additions & 0 deletions sphinx_gallery/py_source_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def get_docstring_and_rest(filename):
# "SyntaxError: encoding declaration in Unicode string"
with open(filename, 'rb') as f:
content = f.read()
# change from Windows format to UNIX for uniformity
content = content.replace(b'\r\n', b'\n')

node = ast.parse(content)
if not isinstance(node, ast.Module):
Expand Down
11 changes: 6 additions & 5 deletions sphinx_gallery/tests/test_backreferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Testing the rst files generator
"""
from __future__ import division, absolute_import, print_function
import os
import sphinx_gallery.backreferences as sg
from nose.tools import assert_equal

Expand All @@ -14,21 +15,21 @@ def test_thumbnail_div():

html_div = sg._thumbnail_div('fake_dir', 'test_file.py', 'test formating')

reference = """
reference = r"""
.. raw:: html
<div class="sphx-glr-thumbcontainer" tooltip="test formating">
.. only:: html
.. figure:: /fake_dir/images/thumb/sphx_glr_test_file_thumb.png
.. figure:: /fake_dir{0}images{0}thumb{0}sphx_glr_test_file_thumb.png
:ref:`sphx_glr_fake_dir_test_file.py`
.. raw:: html
</div>
"""
""".format(os.sep)

assert_equal(html_div, reference)

Expand All @@ -46,7 +47,7 @@ def test_backref_thumbnail_div():
.. only:: html
.. figure:: /fake_dir/images/thumb/sphx_glr_test_file_thumb.png
.. figure:: /fake_dir{0}images{0}thumb{0}sphx_glr_test_file_thumb.png
:ref:`sphx_glr_fake_dir_test_file.py`
Expand All @@ -57,7 +58,7 @@ def test_backref_thumbnail_div():
.. only:: not html
* :ref:`sphx_glr_fake_dir_test_file.py`
"""
""".format(os.sep)

assert_equal(html_div, reference)

Expand Down
20 changes: 11 additions & 9 deletions sphinx_gallery/tests/test_docs_resolv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Testing the rst files generator
"""
from __future__ import division, absolute_import, print_function
import os
import sphinx_gallery.docs_resolv as sg
import tempfile
import sys
Expand All @@ -16,19 +17,20 @@ def test_shelve():
retrieved after file is deleted"""
test_string = 'test information'
tmp_cache = tempfile.mkdtemp()
with tempfile.NamedTemporaryFile('w') as fid:
fid.write(test_string)
fid.seek(0)

with tempfile.NamedTemporaryFile('w', delete=False) as f:
f.write(test_string)
try:
# recovers data from temporary file and caches it in the shelve
file_data = sg.get_data(fid.name, tmp_cache)
# tests recovered data matches
assert_equal(file_data, test_string)
file_data = sg.get_data(f.name, tmp_cache)
finally:
os.remove(f.name)
# tests recovered data matches
assert_equal(file_data, test_string)

# test if cached data is available after temporary file has vanished
assert_equal(sg.get_data(fid.name, tmp_cache), test_string)
assert_equal(sg.get_data(f.name, tmp_cache), test_string)

# shelve keys need to be str in python 2, deal with unicode input
if sys.version_info[0] == 2:
unicode_name = unicode(fid.name)
unicode_name = unicode(f.name)
assert_equal(sg.get_data(unicode_name, tmp_cache), test_string)
42 changes: 28 additions & 14 deletions sphinx_gallery/tests/test_gen_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import re
import os
import shutil
import warnings
import zipfile

import nose
Expand Down Expand Up @@ -73,14 +74,16 @@ def test_bug_cases_of_notebook_syntax():
def test_direct_comment_after_docstring():
# For more details see
# https://github.com/sphinx-gallery/sphinx-gallery/pull/49
with tempfile.NamedTemporaryFile('w') as f:
with tempfile.NamedTemporaryFile('w', delete=False) as f:
f.write('\n'.join(['"Docstring"',
'# and now comes the module code',
'# with a second line of comment',
'x, y = 1, 2',
'']))
f.flush()
try:
result = sg.split_code_and_text_blocks(f.name)
finally:
os.remove(f.name)

expected_result = [
('text', 'Docstring'),
Expand All @@ -102,10 +105,12 @@ def test_codestr2rst():


def test_extract_intro():
with tempfile.NamedTemporaryFile('wb') as f:
with tempfile.NamedTemporaryFile('wb', delete=False) as f:
f.write('\n'.join(CONTENT).encode('utf-8'))
f.flush()
try:
result = sg.extract_intro(f.name)
finally:
os.remove(f.name)
assert_false('Docstring' in result)
assert_equal(
result,
Expand All @@ -116,9 +121,9 @@ def test_extract_intro():
def test_md5sums():
"""Test md5sum check functions work on know file content"""

with tempfile.NamedTemporaryFile('w') as f:
f.write('Local test\n')
f.flush()
with tempfile.NamedTemporaryFile('wb', delete=False) as f:
f.write(b'Local test\n')
try:
file_md5 = sg.get_md5sum(f.name)
# verify correct md5sum
assert_equal('ea8a570e9f3afc0a7c3f2a17a48b8047', file_md5)
Expand All @@ -127,8 +132,12 @@ def test_md5sums():
# Write md5sum to file to check is current
with open(f.name + '.md5', 'w') as file_checksum:
file_checksum.write(file_md5)
assert_true(sg.md5sum_is_current(f.name))
os.remove(f.name + '.md5')
try:
assert_true(sg.md5sum_is_current(f.name))
finally:
os.remove(f.name + '.md5')
finally:
os.remove(f.name)


def build_test_configuration(**kwargs):
Expand All @@ -154,8 +163,11 @@ def test_fail_example():
mode='w', encoding='utf-8') as f:
f.write('\n'.join(failing_code))

sg.generate_file_rst('raise.py', gallery_conf['gallery_dir'],
gallery_conf['examples_dir'], gallery_conf)
with warnings.catch_warnings(record=True) as w:
sg.generate_file_rst('raise.py', gallery_conf['gallery_dir'],
gallery_conf['examples_dir'], gallery_conf)
assert_equal(len(w), 1)
assert_true('not defined' in str(w[0].message))

# read rst file and check if it contains traceback output

Expand Down Expand Up @@ -207,12 +219,14 @@ def test_thumbnail_number():
'# sphinx_gallery_thumbnail_number=2',
'#sphinx_gallery_thumbnail_number = 2',
' # sphinx_gallery_thumbnail_number=2']:
with tempfile.NamedTemporaryFile('w') as f:
with tempfile.NamedTemporaryFile('w', delete=False) as f:
f.write('\n'.join(['"Docstring"',
test_str]))
f.flush()
try:
_, content = sg.get_docstring_and_rest(f.name)
thumbnail_number = sg.extract_thumbnail_number(content)
finally:
os.remove(f.name)
thumbnail_number = sg.extract_thumbnail_number(content)
assert_equal(thumbnail_number, 2)


Expand Down
9 changes: 6 additions & 3 deletions sphinx_gallery/tests/test_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@ def test_jupyter_notebook():
blocks = sg.split_code_and_text_blocks('tutorials/plot_parse.py')
example_nb = jupyter_notebook(blocks)

with tempfile.NamedTemporaryFile('w') as nb_file:
save_notebook(example_nb, nb_file.name)
with open(nb_file.name, "r") as fname:
with tempfile.NamedTemporaryFile('w', delete=False) as f:
save_notebook(example_nb, f.name)
try:
with open(f.name, "r") as fname:
assert_equal(json.load(fname), example_nb)
finally:
os.remove(f.name)

###############################################################################
# Notebook shell utility
Expand Down

0 comments on commit fdbdf5a

Please sign in to comment.