1#!/usr/bin/env python
2import os
3import sys
4
5from _jb_utils import FileChangesTracker, jb_escape_output
6from fix_getpass import fixGetpass
7from pycharm_run_utils import adjust_django_sys_path
8
9try:
10    from runpy import run_module
11except ImportError:
12    from runpy_compat import run_module
13
14adjust_django_sys_path()
15base_path = sys.argv.pop()
16
17manage_file = os.getenv('PYCHARM_DJANGO_MANAGE_MODULE')
18track_files_pattern = os.environ.get('PYCHARM_TRACK_FILES_PATTERN', None)
19if not manage_file:
20    manage_file = 'manage'
21
22
23class _PseudoTTY(object):
24    """
25    Wraps stdin to return "true" for isatty() to fool
26    """
27
28    def __init__(self, underlying):
29        self.__underlying = underlying
30
31    def __getattr__(self, name):
32        return getattr(self.__underlying, name)
33
34    def isatty(self):
35        return True
36
37
38if __name__ == "__main__":
39    fixGetpass()
40    command = sys.argv[1]
41    if command in ["syncdb", "createsuperuser"]:  # List of commands that need stdin to be cheated
42        sys.stdin = _PseudoTTY(sys.stdin)
43
44
45    def run_command():
46        run_module(manage_file, None, '__main__', True)
47
48
49    if track_files_pattern:
50        print("Tracking file by folder pattern: ", track_files_pattern)
51        file_changes_tracker = FileChangesTracker(os.getcwd(), track_files_pattern.split(":"))
52        run_command()
53        # Report files affected/created by commands. This info is used on Java side.
54        changed_files = list(file_changes_tracker.get_changed_files())
55        if changed_files:
56            print("\n" + jb_escape_output(",".join(changed_files)))
57    else:
58        print("File tracking disabled")
59        run_command()
60