PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/HLI_RNAseqQC_optimized/pymodules/python2.7/lib/python/ipython-2.2.0-py2.7.egg/IPython/html/tests/test_nbextensions.py

https://gitlab.com/pooja043/Globus_Docker_4
Python | 272 lines | 237 code | 16 blank | 19 comment | 8 complexity | ec8137c5c4ceb870d1a1f48d1448d6b7 MD5 | raw file
  1. # coding: utf-8
  2. """Test installation of notebook extensions"""
  3. #-----------------------------------------------------------------------------
  4. # Copyright (C) 2014 The IPython Development Team
  5. #
  6. # Distributed under the terms of the BSD License. The full license is in
  7. # the file COPYING, distributed as part of this software.
  8. #-----------------------------------------------------------------------------
  9. #-----------------------------------------------------------------------------
  10. # Imports
  11. #-----------------------------------------------------------------------------
  12. import glob
  13. import os
  14. import re
  15. import tarfile
  16. import zipfile
  17. from io import BytesIO
  18. from os.path import basename, join as pjoin
  19. from unittest import TestCase
  20. import IPython.testing.tools as tt
  21. import IPython.testing.decorators as dec
  22. from IPython.utils import py3compat
  23. from IPython.utils.tempdir import TemporaryDirectory
  24. from IPython.html import nbextensions
  25. from IPython.html.nbextensions import install_nbextension, check_nbextension
  26. #-----------------------------------------------------------------------------
  27. # Test functions
  28. #-----------------------------------------------------------------------------
  29. def touch(file, mtime=None):
  30. """ensure a file exists, and set its modification time
  31. returns the modification time of the file
  32. """
  33. open(file, 'a').close()
  34. # set explicit mtime
  35. if mtime:
  36. atime = os.stat(file).st_atime
  37. os.utime(file, (atime, mtime))
  38. return os.stat(file).st_mtime
  39. class TestInstallNBExtension(TestCase):
  40. def tempdir(self):
  41. td = TemporaryDirectory()
  42. self.tempdirs.append(td)
  43. return py3compat.cast_unicode(td.name)
  44. def setUp(self):
  45. self.tempdirs = []
  46. src = self.src = self.tempdir()
  47. self.files = files = [
  48. pjoin(u'ƒile'),
  49. pjoin(u'∂ir', u'ƒile1'),
  50. pjoin(u'∂ir', u'∂ir2', u'ƒile2'),
  51. ]
  52. for file in files:
  53. fullpath = os.path.join(self.src, file)
  54. parent = os.path.dirname(fullpath)
  55. if not os.path.exists(parent):
  56. os.makedirs(parent)
  57. touch(fullpath)
  58. self.ipdir = self.tempdir()
  59. self.save_get_ipython_dir = nbextensions.get_ipython_dir
  60. nbextensions.get_ipython_dir = lambda : self.ipdir
  61. def tearDown(self):
  62. for td in self.tempdirs:
  63. td.cleanup()
  64. nbextensions.get_ipython_dir = self.save_get_ipython_dir
  65. def assert_path_exists(self, path):
  66. if not os.path.exists(path):
  67. do_exist = os.listdir(os.path.dirname(path))
  68. self.fail(u"%s should exist (found %s)" % (path, do_exist))
  69. def assert_not_path_exists(self, path):
  70. if os.path.exists(path):
  71. self.fail(u"%s should not exist" % path)
  72. def assert_installed(self, relative_path, ipdir=None):
  73. self.assert_path_exists(
  74. pjoin(ipdir or self.ipdir, u'nbextensions', relative_path)
  75. )
  76. def assert_not_installed(self, relative_path, ipdir=None):
  77. self.assert_not_path_exists(
  78. pjoin(ipdir or self.ipdir, u'nbextensions', relative_path)
  79. )
  80. def test_create_ipython_dir(self):
  81. """install_nbextension when ipython_dir doesn't exist"""
  82. with TemporaryDirectory() as td:
  83. ipdir = pjoin(td, u'ipython')
  84. install_nbextension(self.src, ipython_dir=ipdir)
  85. self.assert_path_exists(ipdir)
  86. for file in self.files:
  87. self.assert_installed(
  88. pjoin(basename(self.src), file),
  89. ipdir
  90. )
  91. def test_create_nbextensions(self):
  92. with TemporaryDirectory() as ipdir:
  93. install_nbextension(self.src, ipython_dir=ipdir)
  94. self.assert_installed(
  95. pjoin(basename(self.src), u'ƒile'),
  96. ipdir
  97. )
  98. def test_single_file(self):
  99. file = self.files[0]
  100. install_nbextension(pjoin(self.src, file))
  101. self.assert_installed(file)
  102. def test_single_dir(self):
  103. d = u'∂ir'
  104. install_nbextension(pjoin(self.src, d))
  105. self.assert_installed(self.files[-1])
  106. def test_install_nbextension(self):
  107. install_nbextension(glob.glob(pjoin(self.src, '*')))
  108. for file in self.files:
  109. self.assert_installed(file)
  110. def test_overwrite_file(self):
  111. with TemporaryDirectory() as d:
  112. fname = u'ƒ.js'
  113. src = pjoin(d, fname)
  114. with open(src, 'w') as f:
  115. f.write('first')
  116. mtime = touch(src)
  117. dest = pjoin(self.ipdir, u'nbextensions', fname)
  118. install_nbextension(src)
  119. with open(src, 'w') as f:
  120. f.write('overwrite')
  121. mtime = touch(src, mtime - 100)
  122. install_nbextension(src, overwrite=True)
  123. with open(dest) as f:
  124. self.assertEqual(f.read(), 'overwrite')
  125. def test_overwrite_dir(self):
  126. with TemporaryDirectory() as src:
  127. # src = py3compat.cast_unicode_py2(src)
  128. base = basename(src)
  129. fname = u'ƒ.js'
  130. touch(pjoin(src, fname))
  131. install_nbextension(src)
  132. self.assert_installed(pjoin(base, fname))
  133. os.remove(pjoin(src, fname))
  134. fname2 = u'∂.js'
  135. touch(pjoin(src, fname2))
  136. install_nbextension(src, overwrite=True)
  137. self.assert_installed(pjoin(base, fname2))
  138. self.assert_not_installed(pjoin(base, fname))
  139. def test_update_file(self):
  140. with TemporaryDirectory() as d:
  141. fname = u'ƒ.js'
  142. src = pjoin(d, fname)
  143. with open(src, 'w') as f:
  144. f.write('first')
  145. mtime = touch(src)
  146. install_nbextension(src)
  147. self.assert_installed(fname)
  148. dest = pjoin(self.ipdir, u'nbextensions', fname)
  149. old_mtime = os.stat(dest).st_mtime
  150. with open(src, 'w') as f:
  151. f.write('overwrite')
  152. touch(src, mtime + 10)
  153. install_nbextension(src)
  154. with open(dest) as f:
  155. self.assertEqual(f.read(), 'overwrite')
  156. def test_skip_old_file(self):
  157. with TemporaryDirectory() as d:
  158. fname = u'ƒ.js'
  159. src = pjoin(d, fname)
  160. mtime = touch(src)
  161. install_nbextension(src)
  162. self.assert_installed(fname)
  163. dest = pjoin(self.ipdir, u'nbextensions', fname)
  164. old_mtime = os.stat(dest).st_mtime
  165. mtime = touch(src, mtime - 100)
  166. install_nbextension(src)
  167. new_mtime = os.stat(dest).st_mtime
  168. self.assertEqual(new_mtime, old_mtime)
  169. def test_quiet(self):
  170. with tt.AssertNotPrints(re.compile(r'.+')):
  171. install_nbextension(self.src, verbose=0)
  172. def test_install_zip(self):
  173. path = pjoin(self.src, "myjsext.zip")
  174. with zipfile.ZipFile(path, 'w') as f:
  175. f.writestr("a.js", b"b();")
  176. f.writestr("foo/a.js", b"foo();")
  177. install_nbextension(path)
  178. self.assert_installed("a.js")
  179. self.assert_installed(pjoin("foo", "a.js"))
  180. def test_install_tar(self):
  181. def _add_file(f, fname, buf):
  182. info = tarfile.TarInfo(fname)
  183. info.size = len(buf)
  184. f.addfile(info, BytesIO(buf))
  185. for i,ext in enumerate((".tar.gz", ".tgz", ".tar.bz2")):
  186. path = pjoin(self.src, "myjsext" + ext)
  187. with tarfile.open(path, 'w') as f:
  188. _add_file(f, "b%i.js" % i, b"b();")
  189. _add_file(f, "foo/b%i.js" % i, b"foo();")
  190. install_nbextension(path)
  191. self.assert_installed("b%i.js" % i)
  192. self.assert_installed(pjoin("foo", "b%i.js" % i))
  193. def test_install_url(self):
  194. def fake_urlretrieve(url, dest):
  195. touch(dest)
  196. save_urlretrieve = nbextensions.urlretrieve
  197. nbextensions.urlretrieve = fake_urlretrieve
  198. try:
  199. install_nbextension("http://example.com/path/to/foo.js")
  200. self.assert_installed("foo.js")
  201. install_nbextension("https://example.com/path/to/another/bar.js")
  202. self.assert_installed("bar.js")
  203. finally:
  204. nbextensions.urlretrieve = save_urlretrieve
  205. def test_check_nbextension(self):
  206. with TemporaryDirectory() as d:
  207. f = u'ƒ.js'
  208. src = pjoin(d, f)
  209. touch(src)
  210. install_nbextension(src)
  211. assert check_nbextension(f, self.ipdir)
  212. assert check_nbextension([f], self.ipdir)
  213. assert not check_nbextension([f, pjoin('dne', f)], self.ipdir)
  214. @dec.skip_win32
  215. def test_install_symlink(self):
  216. with TemporaryDirectory() as d:
  217. f = u'ƒ.js'
  218. src = pjoin(d, f)
  219. touch(src)
  220. install_nbextension(src, symlink=True)
  221. dest = pjoin(self.ipdir, u'nbextensions', f)
  222. assert os.path.islink(dest)
  223. link = os.readlink(dest)
  224. self.assertEqual(link, src)
  225. def test_install_symlink_bad(self):
  226. with self.assertRaises(ValueError):
  227. install_nbextension("http://example.com/foo.js", symlink=True)
  228. with TemporaryDirectory() as d:
  229. zf = u'ƒ.zip'
  230. zsrc = pjoin(d, zf)
  231. with zipfile.ZipFile(zsrc, 'w') as z:
  232. z.writestr("a.js", b"b();")
  233. with self.assertRaises(ValueError):
  234. install_nbextension(zsrc, symlink=True)