Fix remote coverage execution
[idea/community.git] / python / helpers / coverage_runner / run_coverage.py
1 """Coverage.py's main entrypoint."""
2
3 import os
4 import sys
5
6 bundled_coverage_path = os.getenv('BUNDLED_COVERAGE_PATH')
7 if bundled_coverage_path:
8     sys_path_backup = sys.path
9     sys.path = [p for p in sys.path if p != bundled_coverage_path]
10     from coverage.cmdline import main
11     sys.path = sys_path_backup
12 else:
13     from coverage.cmdline import main
14
15 coverage_file = os.getenv('PYCHARM_COVERAGE_FILE')
16
17 coverage_file = coverage_file[0:-len(".coverage")]
18
19 run_cov = os.getenv('PYCHARM_RUN_COVERAGE')
20 if os.getenv('CREATE_TEMP_COVERAGE_FILE'):
21     line = 'LOG: PyCharm: File mapping:%s\t%s\n'
22     import tempfile
23     (h, new_cov_file) = tempfile.mkstemp(prefix='pycharm-coverage')
24     print(line%(coverage_file + ".coverage", new_cov_file + ".coverage"))
25     print(line%(coverage_file + '.syspath.txt', new_cov_file + '.syspath.txt'))
26     print(line%(coverage_file + '.xml', new_cov_file + '.xml'))
27     coverage_file = new_cov_file
28
29 if coverage_file:
30     os.environ['COVERAGE_FILE'] = coverage_file + ".coverage"
31
32 if run_cov:
33     a_file = open(coverage_file + '.syspath.txt', mode='w')
34     a_file.write(os.getcwd()+"\n")
35     for path in sys.path: a_file.write(path + "\n")
36     a_file.close()
37
38 argv = []
39 for arg in sys.argv:
40     if arg.startswith('-m'):
41         argv.append('-m')
42         argv.append(arg[2:])
43     else:
44         argv.append(arg)
45 sys.argv = argv
46
47 cwd = os.getcwd()
48
49 try:
50     main()
51 finally:
52     if run_cov:
53         os.chdir(cwd)
54         main(["xml", "-o", coverage_file + ".xml", "--ignore-errors"])