util.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. """Utility functions and classes used by nose internally.
  2. """
  3. import inspect
  4. import itertools
  5. import logging
  6. import stat
  7. import os
  8. import re
  9. import sys
  10. import types
  11. import unittest
  12. from nose.pyversion import ClassType, TypeType, isgenerator, ismethod
  13. log = logging.getLogger('nose')
  14. ident_re = re.compile(r'^[A-Za-z_][A-Za-z0-9_.]*$')
  15. class_types = (ClassType, TypeType)
  16. skip_pattern = r"(?:\.svn)|(?:[^.]+\.py[co])|(?:.*~)|(?:.*\$py\.class)|(?:__pycache__)"
  17. try:
  18. set()
  19. set = set # make from nose.util import set happy
  20. except NameError:
  21. try:
  22. from sets import Set as set
  23. except ImportError:
  24. pass
  25. def ls_tree(dir_path="",
  26. skip_pattern=skip_pattern,
  27. indent="|-- ", branch_indent="| ",
  28. last_indent="`-- ", last_branch_indent=" "):
  29. # TODO: empty directories look like non-directory files
  30. return "\n".join(_ls_tree_lines(dir_path, skip_pattern,
  31. indent, branch_indent,
  32. last_indent, last_branch_indent))
  33. def _ls_tree_lines(dir_path, skip_pattern,
  34. indent, branch_indent, last_indent, last_branch_indent):
  35. if dir_path == "":
  36. dir_path = os.getcwd()
  37. lines = []
  38. names = os.listdir(dir_path)
  39. names.sort()
  40. dirs, nondirs = [], []
  41. for name in names:
  42. if re.match(skip_pattern, name):
  43. continue
  44. if os.path.isdir(os.path.join(dir_path, name)):
  45. dirs.append(name)
  46. else:
  47. nondirs.append(name)
  48. # list non-directories first
  49. entries = list(itertools.chain([(name, False) for name in nondirs],
  50. [(name, True) for name in dirs]))
  51. def ls_entry(name, is_dir, ind, branch_ind):
  52. if not is_dir:
  53. yield ind + name
  54. else:
  55. path = os.path.join(dir_path, name)
  56. if not os.path.islink(path):
  57. yield ind + name
  58. subtree = _ls_tree_lines(path, skip_pattern,
  59. indent, branch_indent,
  60. last_indent, last_branch_indent)
  61. for x in subtree:
  62. yield branch_ind + x
  63. for name, is_dir in entries[:-1]:
  64. for line in ls_entry(name, is_dir, indent, branch_indent):
  65. yield line
  66. if entries:
  67. name, is_dir = entries[-1]
  68. for line in ls_entry(name, is_dir, last_indent, last_branch_indent):
  69. yield line
  70. def absdir(path):
  71. """Return absolute, normalized path to directory, if it exists; None
  72. otherwise.
  73. """
  74. if not os.path.isabs(path):
  75. path = os.path.normpath(os.path.abspath(os.path.join(os.getcwd(),
  76. path)))
  77. if path is None or not os.path.isdir(path):
  78. return None
  79. return path
  80. def absfile(path, where=None):
  81. """Return absolute, normalized path to file (optionally in directory
  82. where), or None if the file can't be found either in where or the current
  83. working directory.
  84. """
  85. orig = path
  86. if where is None:
  87. where = os.getcwd()
  88. if isinstance(where, list) or isinstance(where, tuple):
  89. for maybe_path in where:
  90. maybe_abs = absfile(path, maybe_path)
  91. if maybe_abs is not None:
  92. return maybe_abs
  93. return None
  94. if not os.path.isabs(path):
  95. path = os.path.normpath(os.path.abspath(os.path.join(where, path)))
  96. if path is None or not os.path.exists(path):
  97. if where != os.getcwd():
  98. # try the cwd instead
  99. path = os.path.normpath(os.path.abspath(os.path.join(os.getcwd(),
  100. orig)))
  101. if path is None or not os.path.exists(path):
  102. return None
  103. if os.path.isdir(path):
  104. # might want an __init__.py from pacakge
  105. init = os.path.join(path,'__init__.py')
  106. if os.path.isfile(init):
  107. return init
  108. elif os.path.isfile(path):
  109. return path
  110. return None
  111. def anyp(predicate, iterable):
  112. for item in iterable:
  113. if predicate(item):
  114. return True
  115. return False
  116. def file_like(name):
  117. """A name is file-like if it is a path that exists, or it has a
  118. directory part, or it ends in .py, or it isn't a legal python
  119. identifier.
  120. """
  121. return (os.path.exists(name)
  122. or os.path.dirname(name)
  123. or name.endswith('.py')
  124. or not ident_re.match(os.path.splitext(name)[0]))
  125. def func_lineno(func):
  126. """Get the line number of a function. First looks for
  127. compat_co_firstlineno, then func_code.co_first_lineno.
  128. """
  129. try:
  130. return func.compat_co_firstlineno
  131. except AttributeError:
  132. try:
  133. return func.func_code.co_firstlineno
  134. except AttributeError:
  135. return -1
  136. def isclass(obj):
  137. """Is obj a class? Inspect's isclass is too liberal and returns True
  138. for objects that can't be subclasses of anything.
  139. """
  140. obj_type = type(obj)
  141. return obj_type in class_types or issubclass(obj_type, type)
  142. # backwards compat (issue #64)
  143. is_generator = isgenerator
  144. def ispackage(path):
  145. """
  146. Is this path a package directory?
  147. >>> ispackage('nose')
  148. True
  149. >>> ispackage('unit_tests')
  150. False
  151. >>> ispackage('nose/plugins')
  152. True
  153. >>> ispackage('nose/loader.py')
  154. False
  155. """
  156. if os.path.isdir(path):
  157. # at least the end of the path must be a legal python identifier
  158. # and __init__.py[co] must exist
  159. end = os.path.basename(path)
  160. if ident_re.match(end):
  161. for init in ('__init__.py', '__init__.pyc', '__init__.pyo'):
  162. if os.path.isfile(os.path.join(path, init)):
  163. return True
  164. if sys.platform.startswith('java') and \
  165. os.path.isfile(os.path.join(path, '__init__$py.class')):
  166. return True
  167. return False
  168. def isproperty(obj):
  169. """
  170. Is this a property?
  171. >>> class Foo:
  172. ... def got(self):
  173. ... return 2
  174. ... def get(self):
  175. ... return 1
  176. ... get = property(get)
  177. >>> isproperty(Foo.got)
  178. False
  179. >>> isproperty(Foo.get)
  180. True
  181. """
  182. return type(obj) == property
  183. def getfilename(package, relativeTo=None):
  184. """Find the python source file for a package, relative to a
  185. particular directory (defaults to current working directory if not
  186. given).
  187. """
  188. if relativeTo is None:
  189. relativeTo = os.getcwd()
  190. path = os.path.join(relativeTo, os.sep.join(package.split('.')))
  191. if os.path.exists(path + '/__init__.py'):
  192. return path
  193. filename = path + '.py'
  194. if os.path.exists(filename):
  195. return filename
  196. return None
  197. def getpackage(filename):
  198. """
  199. Find the full dotted package name for a given python source file
  200. name. Returns None if the file is not a python source file.
  201. >>> getpackage('foo.py')
  202. 'foo'
  203. >>> getpackage('biff/baf.py')
  204. 'baf'
  205. >>> getpackage('nose/util.py')
  206. 'nose.util'
  207. Works for directories too.
  208. >>> getpackage('nose')
  209. 'nose'
  210. >>> getpackage('nose/plugins')
  211. 'nose.plugins'
  212. And __init__ files stuck onto directories
  213. >>> getpackage('nose/plugins/__init__.py')
  214. 'nose.plugins'
  215. Absolute paths also work.
  216. >>> path = os.path.abspath(os.path.join('nose', 'plugins'))
  217. >>> getpackage(path)
  218. 'nose.plugins'
  219. """
  220. src_file = src(filename)
  221. if (os.path.isdir(src_file) or not src_file.endswith('.py')) and not ispackage(src_file):
  222. return None
  223. base, ext = os.path.splitext(os.path.basename(src_file))
  224. if base == '__init__':
  225. mod_parts = []
  226. else:
  227. mod_parts = [base]
  228. path, part = os.path.split(os.path.split(src_file)[0])
  229. while part:
  230. if ispackage(os.path.join(path, part)):
  231. mod_parts.append(part)
  232. else:
  233. break
  234. path, part = os.path.split(path)
  235. mod_parts.reverse()
  236. return '.'.join(mod_parts)
  237. def ln(label):
  238. """Draw a 70-char-wide divider, with label in the middle.
  239. >>> ln('hello there')
  240. '---------------------------- hello there -----------------------------'
  241. """
  242. label_len = len(label) + 2
  243. chunk = (70 - label_len) // 2
  244. out = '%s %s %s' % ('-' * chunk, label, '-' * chunk)
  245. pad = 70 - len(out)
  246. if pad > 0:
  247. out = out + ('-' * pad)
  248. return out
  249. def resolve_name(name, module=None):
  250. """Resolve a dotted name to a module and its parts. This is stolen
  251. wholesale from unittest.TestLoader.loadTestByName.
  252. >>> resolve_name('nose.util') #doctest: +ELLIPSIS
  253. <module 'nose.util' from...>
  254. >>> resolve_name('nose.util.resolve_name') #doctest: +ELLIPSIS
  255. <function resolve_name at...>
  256. """
  257. parts = name.split('.')
  258. parts_copy = parts[:]
  259. if module is None:
  260. while parts_copy:
  261. try:
  262. log.debug("__import__ %s", name)
  263. module = __import__('.'.join(parts_copy))
  264. break
  265. except ImportError:
  266. del parts_copy[-1]
  267. if not parts_copy:
  268. raise
  269. parts = parts[1:]
  270. obj = module
  271. log.debug("resolve: %s, %s, %s, %s", parts, name, obj, module)
  272. for part in parts:
  273. obj = getattr(obj, part)
  274. return obj
  275. def split_test_name(test):
  276. """Split a test name into a 3-tuple containing file, module, and callable
  277. names, any of which (but not all) may be blank.
  278. Test names are in the form:
  279. file_or_module:callable
  280. Either side of the : may be dotted. To change the splitting behavior, you
  281. can alter nose.util.split_test_re.
  282. """
  283. norm = os.path.normpath
  284. file_or_mod = test
  285. fn = None
  286. if not ':' in test:
  287. # only a file or mod part
  288. if file_like(test):
  289. return (norm(test), None, None)
  290. else:
  291. return (None, test, None)
  292. # could be path|mod:callable, or a : in the file path someplace
  293. head, tail = os.path.split(test)
  294. if not head:
  295. # this is a case like 'foo:bar' -- generally a module
  296. # name followed by a callable, but also may be a windows
  297. # drive letter followed by a path
  298. try:
  299. file_or_mod, fn = test.split(':')
  300. if file_like(fn):
  301. # must be a funny path
  302. file_or_mod, fn = test, None
  303. except ValueError:
  304. # more than one : in the test
  305. # this is a case like c:\some\path.py:a_test
  306. parts = test.split(':')
  307. if len(parts[0]) == 1:
  308. file_or_mod, fn = ':'.join(parts[:-1]), parts[-1]
  309. else:
  310. # nonsense like foo:bar:baz
  311. raise ValueError("Test name '%s' could not be parsed. Please "
  312. "format test names as path:callable or "
  313. "module:callable." % (test,))
  314. elif not tail:
  315. # this is a case like 'foo:bar/'
  316. # : must be part of the file path, so ignore it
  317. file_or_mod = test
  318. else:
  319. if ':' in tail:
  320. file_part, fn = tail.split(':')
  321. else:
  322. file_part = tail
  323. file_or_mod = os.sep.join([head, file_part])
  324. if file_or_mod:
  325. if file_like(file_or_mod):
  326. return (norm(file_or_mod), None, fn)
  327. else:
  328. return (None, file_or_mod, fn)
  329. else:
  330. return (None, None, fn)
  331. split_test_name.__test__ = False # do not collect
  332. def test_address(test):
  333. """Find the test address for a test, which may be a module, filename,
  334. class, method or function.
  335. """
  336. if hasattr(test, "address"):
  337. return test.address()
  338. # type-based polymorphism sucks in general, but I believe is
  339. # appropriate here
  340. t = type(test)
  341. file = module = call = None
  342. if t == types.ModuleType:
  343. file = getattr(test, '__file__', None)
  344. module = getattr(test, '__name__', None)
  345. return (src(file), module, call)
  346. if t == types.FunctionType or issubclass(t, type) or t == types.ClassType:
  347. module = getattr(test, '__module__', None)
  348. if module is not None:
  349. m = sys.modules[module]
  350. file = getattr(m, '__file__', None)
  351. if file is not None:
  352. file = os.path.abspath(file)
  353. call = getattr(test, '__name__', None)
  354. return (src(file), module, call)
  355. if t == types.MethodType:
  356. cls_adr = test_address(test.im_class)
  357. return (src(cls_adr[0]), cls_adr[1],
  358. "%s.%s" % (cls_adr[2], test.__name__))
  359. # handle unittest.TestCase instances
  360. if isinstance(test, unittest.TestCase):
  361. if (hasattr(test, '_FunctionTestCase__testFunc') # pre 2.7
  362. or hasattr(test, '_testFunc')): # 2.7
  363. # unittest FunctionTestCase
  364. try:
  365. return test_address(test._FunctionTestCase__testFunc)
  366. except AttributeError:
  367. return test_address(test._testFunc)
  368. # regular unittest.TestCase
  369. cls_adr = test_address(test.__class__)
  370. # 2.5 compat: __testMethodName changed to _testMethodName
  371. try:
  372. method_name = test._TestCase__testMethodName
  373. except AttributeError:
  374. method_name = test._testMethodName
  375. return (src(cls_adr[0]), cls_adr[1],
  376. "%s.%s" % (cls_adr[2], method_name))
  377. if (hasattr(test, '__class__') and
  378. test.__class__.__module__ not in ('__builtin__', 'builtins')):
  379. return test_address(test.__class__)
  380. raise TypeError("I don't know what %s is (%s)" % (test, t))
  381. test_address.__test__ = False # do not collect
  382. def try_run(obj, names):
  383. """Given a list of possible method names, try to run them with the
  384. provided object. Keep going until something works. Used to run
  385. setup/teardown methods for module, package, and function tests.
  386. """
  387. for name in names:
  388. func = getattr(obj, name, None)
  389. if func is not None:
  390. if type(obj) == types.ModuleType:
  391. # py.test compatibility
  392. if isinstance(func, types.FunctionType):
  393. args, varargs, varkw, defaults = \
  394. inspect.getargspec(func)
  395. else:
  396. # Not a function. If it's callable, call it anyway
  397. if hasattr(func, '__call__') and not inspect.ismethod(func):
  398. func = func.__call__
  399. try:
  400. args, varargs, varkw, defaults = \
  401. inspect.getargspec(func)
  402. args.pop(0) # pop the self off
  403. except TypeError:
  404. raise TypeError("Attribute %s of %r is not a python "
  405. "function. Only functions or callables"
  406. " may be used as fixtures." %
  407. (name, obj))
  408. if len(args):
  409. log.debug("call fixture %s.%s(%s)", obj, name, obj)
  410. return func(obj)
  411. log.debug("call fixture %s.%s", obj, name)
  412. return func()
  413. def src(filename):
  414. """Find the python source file for a .pyc, .pyo or $py.class file on
  415. jython. Returns the filename provided if it is not a python source
  416. file.
  417. """
  418. if filename is None:
  419. return filename
  420. if sys.platform.startswith('java') and filename.endswith('$py.class'):
  421. return '.'.join((filename[:-9], 'py'))
  422. base, ext = os.path.splitext(filename)
  423. if ext in ('.pyc', '.pyo', '.py'):
  424. return '.'.join((base, 'py'))
  425. return filename
  426. def regex_last_key(regex):
  427. """Sort key function factory that puts items that match a
  428. regular expression last.
  429. >>> from nose.config import Config
  430. >>> from nose.pyversion import sort_list
  431. >>> c = Config()
  432. >>> regex = c.testMatch
  433. >>> entries = ['.', '..', 'a_test', 'src', 'lib', 'test', 'foo.py']
  434. >>> sort_list(entries, regex_last_key(regex))
  435. >>> entries
  436. ['.', '..', 'foo.py', 'lib', 'src', 'a_test', 'test']
  437. """
  438. def k(obj):
  439. if regex.search(obj):
  440. return (1, obj)
  441. return (0, obj)
  442. return k
  443. def tolist(val):
  444. """Convert a value that may be a list or a (possibly comma-separated)
  445. string into a list. The exception: None is returned as None, not [None].
  446. >>> tolist(["one", "two"])
  447. ['one', 'two']
  448. >>> tolist("hello")
  449. ['hello']
  450. >>> tolist("separate,values, with, commas, spaces , are ,ok")
  451. ['separate', 'values', 'with', 'commas', 'spaces', 'are', 'ok']
  452. """
  453. if val is None:
  454. return None
  455. try:
  456. # might already be a list
  457. val.extend([])
  458. return val
  459. except AttributeError:
  460. pass
  461. # might be a string
  462. try:
  463. return re.split(r'\s*,\s*', val)
  464. except TypeError:
  465. # who knows...
  466. return list(val)
  467. class odict(dict):
  468. """Simple ordered dict implementation, based on:
  469. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747
  470. """
  471. def __init__(self, *arg, **kw):
  472. self._keys = []
  473. super(odict, self).__init__(*arg, **kw)
  474. def __delitem__(self, key):
  475. super(odict, self).__delitem__(key)
  476. self._keys.remove(key)
  477. def __setitem__(self, key, item):
  478. super(odict, self).__setitem__(key, item)
  479. if key not in self._keys:
  480. self._keys.append(key)
  481. def __str__(self):
  482. return "{%s}" % ', '.join(["%r: %r" % (k, v) for k, v in self.items()])
  483. def clear(self):
  484. super(odict, self).clear()
  485. self._keys = []
  486. def copy(self):
  487. d = super(odict, self).copy()
  488. d._keys = self._keys[:]
  489. return d
  490. def items(self):
  491. return zip(self._keys, self.values())
  492. def keys(self):
  493. return self._keys[:]
  494. def setdefault(self, key, failobj=None):
  495. item = super(odict, self).setdefault(key, failobj)
  496. if key not in self._keys:
  497. self._keys.append(key)
  498. return item
  499. def update(self, dict):
  500. super(odict, self).update(dict)
  501. for key in dict.keys():
  502. if key not in self._keys:
  503. self._keys.append(key)
  504. def values(self):
  505. return map(self.get, self._keys)
  506. def transplant_func(func, module):
  507. """
  508. Make a function imported from module A appear as if it is located
  509. in module B.
  510. >>> from pprint import pprint
  511. >>> pprint.__module__
  512. 'pprint'
  513. >>> pp = transplant_func(pprint, __name__)
  514. >>> pp.__module__
  515. 'nose.util'
  516. The original function is not modified.
  517. >>> pprint.__module__
  518. 'pprint'
  519. Calling the transplanted function calls the original.
  520. >>> pp([1, 2])
  521. [1, 2]
  522. >>> pprint([1,2])
  523. [1, 2]
  524. """
  525. from nose.tools import make_decorator
  526. if isgenerator(func):
  527. def newfunc(*arg, **kw):
  528. for v in func(*arg, **kw):
  529. yield v
  530. else:
  531. def newfunc(*arg, **kw):
  532. return func(*arg, **kw)
  533. newfunc = make_decorator(func)(newfunc)
  534. newfunc.__module__ = module
  535. return newfunc
  536. def transplant_class(cls, module):
  537. """
  538. Make a class appear to reside in `module`, rather than the module in which
  539. it is actually defined.
  540. >>> from nose.failure import Failure
  541. >>> Failure.__module__
  542. 'nose.failure'
  543. >>> Nf = transplant_class(Failure, __name__)
  544. >>> Nf.__module__
  545. 'nose.util'
  546. >>> Nf.__name__
  547. 'Failure'
  548. """
  549. class C(cls):
  550. pass
  551. C.__module__ = module
  552. C.__name__ = cls.__name__
  553. return C
  554. def safe_str(val, encoding='utf-8'):
  555. try:
  556. return str(val)
  557. except UnicodeEncodeError:
  558. if isinstance(val, Exception):
  559. return ' '.join([safe_str(arg, encoding)
  560. for arg in val])
  561. return unicode(val).encode(encoding)
  562. def is_executable(file):
  563. if not os.path.exists(file):
  564. return False
  565. st = os.stat(file)
  566. return bool(st.st_mode & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH))
  567. if __name__ == '__main__':
  568. import doctest
  569. doctest.testmod()