summaryrefslogtreecommitdiff
path: root/python/helpers/pydev/pydev_runfiles_coverage.py
blob: 55bec062ed90df9087cfa4d041b4d69c7d655dfc (plain)
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
76
import os.path
import sys
from pydevd_constants import Null


#=======================================================================================================================
# GetCoverageFiles
#=======================================================================================================================
def GetCoverageFiles(coverage_output_dir, number_of_files):
    base_dir = coverage_output_dir
    ret = []
    i = 0
    while len(ret) < number_of_files:
        while True:
            f = os.path.join(base_dir, '.coverage.%s' % i)
            i += 1
            if not os.path.exists(f):
                ret.append(f)
                break #Break only inner for.
    return ret


#=======================================================================================================================
# StartCoverageSupport
#=======================================================================================================================
def StartCoverageSupport(configuration):
    return StartCoverageSupportFromParams(
        configuration.coverage_output_dir, 
        configuration.coverage_output_file, 
        configuration.jobs, 
        configuration.coverage_include, 
    )
    

#=======================================================================================================================
# StartCoverageSupportFromParams
#=======================================================================================================================
def StartCoverageSupportFromParams(coverage_output_dir, coverage_output_file, jobs, coverage_include):
    coverage_files = []
    coverage_instance = Null()
    if coverage_output_dir or coverage_output_file:
        try:
            import coverage #@UnresolvedImport
        except:
            sys.stderr.write('Error: coverage module could not be imported\n')
            sys.stderr.write('Please make sure that the coverage module (http://nedbatchelder.com/code/coverage/)\n')
            sys.stderr.write('is properly installed in your interpreter: %s\n' % (sys.executable,))
            
            import traceback;traceback.print_exc()
        else:
            if coverage_output_dir:
                if not os.path.exists(coverage_output_dir):
                    sys.stderr.write('Error: directory for coverage output (%s) does not exist.\n' % (coverage_output_dir,))
                    
                elif not os.path.isdir(coverage_output_dir):
                    sys.stderr.write('Error: expected (%s) to be a directory.\n' % (coverage_output_dir,))
                    
                else:
                    n = jobs
                    if n <= 0:
                        n += 1
                    n += 1 #Add 1 more for the current process (which will do the initial import).
                    coverage_files = GetCoverageFiles(coverage_output_dir, n)
                    os.environ['COVERAGE_FILE'] = coverage_files.pop(0)
                    
                    coverage_instance = coverage.coverage(source=[coverage_include])
                    coverage_instance.start()
                    
            elif coverage_output_file:
                #Client of parallel run.
                os.environ['COVERAGE_FILE'] = coverage_output_file
                coverage_instance = coverage.coverage(source=[coverage_include])
                coverage_instance.start()
                
    return coverage_files, coverage_instance