cross-platform Popen with hidden window

This commit is contained in:
Alan Hamlett 2018-09-30 21:24:32 -07:00
parent d76e5ca0e5
commit 0fa25af0ec
3 changed files with 21 additions and 7 deletions

View file

@ -7,7 +7,6 @@ from wakatime.packages.requests.models import Response
import logging import logging
import os import os
import platform
import shutil import shutil
import tempfile import tempfile
import time import time
@ -219,9 +218,8 @@ class ProjectTestCase(TestCase):
stderr = '' stderr = ''
mock_popen.return_value = DynamicIterable((stdout, stderr), max_calls=1) mock_popen.return_value = DynamicIterable((stdout, stderr), max_calls=1)
expected = None if platform.system() == 'Windows' else 'svn'
self.shared( self.shared(
expected_project=expected, expected_project='svn',
entity='projects/svn/afolder/emptyfile.txt', entity='projects/svn/afolder/emptyfile.txt',
) )

View file

@ -11,11 +11,14 @@
import codecs import codecs
import platform
import subprocess
import sys import sys
is_py2 = (sys.version_info[0] == 2) is_py2 = (sys.version_info[0] == 2)
is_py3 = (sys.version_info[0] == 3) is_py3 = (sys.version_info[0] == 3)
is_win = platform.system() == 'Windows'
if is_py2: # pragma: nocover if is_py2: # pragma: nocover
@ -98,3 +101,18 @@ try:
from .packages import simplejson as json from .packages import simplejson as json
except (ImportError, SyntaxError): # pragma: nocover except (ImportError, SyntaxError): # pragma: nocover
import json import json
class Popen(subprocess.Popen):
"""Patched Popen to prevent opening cmd window on Windows platform."""
def __init__(self, *args, **kwargs):
startupinfo = kwargs.get('startupinfo')
if is_win or True:
try:
startupinfo = startupinfo or subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
except AttributeError:
pass
kwargs['startupinfo'] = startupinfo
super(Popen, self).__init__(*args, **kwargs)

View file

@ -12,10 +12,10 @@
import logging import logging
import os import os
import platform import platform
from subprocess import Popen, PIPE from subprocess import PIPE
from .base import BaseProject from .base import BaseProject
from ..compat import u, open from ..compat import u, open, Popen
try: try:
from collections import OrderedDict from collections import OrderedDict
except ImportError: # pragma: nocover except ImportError: # pragma: nocover
@ -86,8 +86,6 @@ class Subversion(BaseProject):
return info return info
def _find_project_base(self, path, found=False): def _find_project_base(self, path, found=False):
if platform.system() == 'Windows':
return False # pragma: nocover
path = os.path.realpath(path) path = os.path.realpath(path)
if os.path.isfile(path): if os.path.isfile(path):
path = os.path.split(path)[0] path = os.path.split(path)[0]