|
27 | 27 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
28 | 28 | # MA 02110-1301, USA.
|
29 | 29 | #
|
| 30 | +# is_docker based on https://github.com/jaraco/jaraco.docker |
| 31 | +# Copyright Jason R. Coombs |
| 32 | +# | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 33 | +# | of this software and associated documentation files (the "Software"), to deal |
| 34 | +# | in the Software without restriction, including without limitation the rights |
| 35 | +# | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 36 | +# | copies of the Software, and to permit persons to whom the Software is |
| 37 | +# | furnished to do so, subject to the following conditions: |
| 38 | +# | |
| 39 | +# | The above copyright notice and this permission notice shall be included in all |
| 40 | +# | copies or substantial portions of the Software. |
| 41 | +# | |
| 42 | +# | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 43 | +# | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 44 | +# | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 45 | +# | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 46 | +# | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 47 | +# | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE |
| 48 | +# | OR OTHER DEALINGS IN THE SOFTWARE. |
| 49 | +# |
30 | 50 |
|
31 | 51 | # stdlib
|
32 | 52 | import datetime
|
|
42 | 62 | # 3rd party
|
43 | 63 | import pytest # nodep
|
44 | 64 | from _pytest.mark import MarkDecorator # nodep
|
45 |
| -from jaraco.docker import is_docker # type: ignore # nodep |
46 | 65 | from pytest_regressions.file_regression import FileRegressionFixture # nodep
|
47 | 66 |
|
48 | 67 | # this package
|
@@ -357,6 +376,31 @@ def only_function(reason: str = default_reason.format("Only")) -> MarkDecorator:
|
357 | 376 | versionadded="1.5.0",
|
358 | 377 | )
|
359 | 378 |
|
| 379 | + |
| 380 | +def is_docker(): |
| 381 | + """ |
| 382 | + Is this current environment running in docker? |
| 383 | +
|
| 384 | + >>> type(is_docker()) |
| 385 | + <class 'bool'> |
| 386 | +
|
| 387 | + .. versionadded:: 0.6.0 |
| 388 | + """ |
| 389 | + |
| 390 | + if os.path.exists("/.dockerenv"): |
| 391 | + return True |
| 392 | + |
| 393 | + cgroup = PathPlus("/proc/self/cgroup") |
| 394 | + |
| 395 | + if cgroup.is_file(): |
| 396 | + try: |
| 397 | + return any("docker" in line for line in cgroup.read_lines()) |
| 398 | + except FileNotFoundError: |
| 399 | + return False |
| 400 | + |
| 401 | + return False |
| 402 | + |
| 403 | + |
360 | 404 | not_docker, only_docker = platform_boolean_factory(condition=is_docker(), platform="Docker", versionadded="1.5.0")
|
361 | 405 | not_docker.__doc__ = cast(str, not_docker.__doc__).replace("the current platform is", "running on")
|
362 | 406 | only_docker.__doc__ = cast(str, only_docker.__doc__).replace("the current platform is", "running on")
|
|
0 commit comments