From ca1b25a3089a7db3d5a08c2a317a2f689f2ff34f Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sun, 19 Apr 2009 08:16:50 -0400 Subject: Annotated files are only created for source files relative to the current directory. When writing to a separate file, annotation file names include the directory hierarchy flattened, so that same-named files won't collide. --- coverage/codeunit.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'coverage/codeunit.py') diff --git a/coverage/codeunit.py b/coverage/codeunit.py index 96ed43b..3c3d30f 100644 --- a/coverage/codeunit.py +++ b/coverage/codeunit.py @@ -47,8 +47,11 @@ def code_unit_factory(morfs, file_locator, omit_prefixes=None): class CodeUnit: """Code unit: a filename or module. + Instance attributes: + `name` is a human-readable name for this code unit. `filename` is the os path from which we can read the source. + `relative` is a boolean. """ @@ -64,10 +67,26 @@ class CodeUnit: if hasattr(morf, '__name__'): n = morf.__name__ + self.relative = True else: n = os.path.splitext(morf)[0] - n = file_locator.relative_filename(n) + rel = file_locator.relative_filename(n) + self.relative = (rel != n) + n = rel self.name = n def __cmp__(self, other): return cmp(self.name, other.name) + + def flat_rootname(self): + """A base for a flat filename to correspond to this code unit. + + Useful for writing files about the code where you want all the files in + the same directory, but need to differentiate same-named files from + different directories. + + For example, the file a/b/c.py might return 'a_b_c' + + """ + root = os.path.splitdrive(os.path.splitext(self.name)[0])[1] + return root.replace('\\', '_').replace('/', '_') -- cgit v1.2.1