File: utils.py

package info (click to toggle)
python-handy-archives 0.2.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,300 kB
  • sloc: python: 4,153; makefile: 5
file content (75 lines) | stat: -rw-r--r-- 1,427 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# stdlib
import os
import unittest
from typing import Callable, no_type_check


@no_type_check
def requires_gzip(reason="requires gzip"):
	try:
		# stdlib
		import gzip
	except ImportError:
		gzip = None
	return unittest.skipUnless(gzip, reason)


@no_type_check
def requires_zlib(reason="requires zlib"):
	try:
		# stdlib
		import zlib
	except ImportError:
		zlib = None
	return unittest.skipUnless(zlib, reason)


@no_type_check
def requires_bz2(reason="requires bz2"):
	try:
		# stdlib
		import bz2
	except ImportError:
		bz2 = None
	return unittest.skipUnless(bz2, reason)


@no_type_check
def requires_lzma(reason="requires lzma"):
	try:
		# stdlib
		import lzma
	except ImportError:
		lzma = None
	return unittest.skipUnless(lzma, reason)


def skip_unless_symlink(test: Callable):
	"""
	Skip decorator for tests that require symlinks.
	"""

	msg = "Requires functional symlink implementation"
	return test if CAN_SYMLINK else unittest.skip(msg)(test)


# Filename used for testing
if os.name == "java":
	# Jython disallows @ in module names
	TESTFN = "$test"
else:
	TESTFN = "@test"

# Disambiguate TESTFN for parallel testing, while letting it remain a valid
# module name.
TESTFN = f"{TESTFN}_{os.getpid()}_tmp"

symlink_path = TESTFN + "can_symlink"

try:
	os.symlink(TESTFN, symlink_path)
	CAN_SYMLINK = True
except (OSError, NotImplementedError, AttributeError):
	CAN_SYMLINK = False
else:
	os.remove(symlink_path)