-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2a7f31
commit cc1a910
Showing
7 changed files
with
135 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 | ||
# For details: https://github.com/gaogaotiantian/coredumpy/blob/master/NOTICE.txt | ||
|
||
|
||
import datetime | ||
import os | ||
|
||
|
||
def get_dump_filename(frame, path, directory): | ||
if path is not None: | ||
if directory is not None: | ||
raise ValueError("Cannot specify both path and directory") | ||
if callable(path): | ||
return os.path.abspath(path()) | ||
return os.path.abspath(path) | ||
|
||
funcname = os.path.basename(frame.f_code.co_name) | ||
d = datetime.datetime.now() | ||
filename = f"coredumpy_{funcname}_{d.strftime('%Y%m%d_%H%M%S_%f')}.dump" | ||
|
||
if directory is None: | ||
return os.path.abspath(filename) | ||
|
||
return os.path.abspath(os.path.join(directory, filename)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 | ||
# For details: https://github.com/gaogaotiantian/coredumpy/blob/master/NOTICE.txt | ||
|
||
import os | ||
|
||
from coredumpy.utils import get_dump_filename | ||
|
||
from .base import TestBase | ||
|
||
|
||
class TestUtils(TestBase): | ||
def test_get_dump_filename(self): | ||
class FakeFrame: | ||
def __init__(self, name): | ||
self.f_code = FakeCode(name) | ||
|
||
class FakeCode: | ||
def __init__(self, name): | ||
self.co_name = name | ||
|
||
frame = FakeFrame("test_get_dump_filename") | ||
filename = get_dump_filename(frame, None, None) | ||
self.assertTrue(filename.startswith("/")) | ||
self.assertIn("test_get_dump_filename", filename) | ||
|
||
filename = get_dump_filename(frame, "test.dump", None) | ||
self.assertEqual(filename, os.path.abspath("test.dump")) | ||
|
||
filename = get_dump_filename(frame, lambda: "test.dump", None) | ||
self.assertEqual(filename, os.path.abspath("test.dump")) | ||
|
||
filename = get_dump_filename(frame, None, "dir") | ||
self.assertTrue(filename.startswith("/")) | ||
self.assertIn("test_get_dump_filename", filename) | ||
self.assertIn("dir", filename) | ||
|
||
with self.assertRaises(ValueError): | ||
filename = get_dump_filename(frame, "test.dump", "dir") |