|
1 | 1 | # -- coding:utf-8 -- |
2 | 2 |
|
3 | 3 | import datetime |
4 | | -# import logging |
5 | | -import os |
6 | | -import platform |
7 | | -import time |
8 | | -import sys |
| 4 | +import logging |
9 | 5 | import subprocess |
| 6 | +import time |
10 | 7 |
|
11 | 8 | from com.dvsnier.directory.common_dir import generate_complex_file_name |
12 | 9 |
|
13 | 10 |
|
14 | 11 | def execute(cmds, quiet=True): |
15 | 12 | 'the process execute that a command' |
| 13 | + content = '' |
| 14 | + stdouts = None |
16 | 15 | start = time.time() |
17 | | - p = subprocess.Popen(cmds[0], stdout=subprocess.PIPE, shell=True) |
18 | | - # logging.debug('the current sub process pid(cwd: %s, ppid: %s, id: %s%d).' % |
19 | | - # (p.pid, os.getppid(), type(p), id(p))) |
20 | | - processes = [p] |
21 | | - for x in cmds[1:]: |
22 | | - p = subprocess.Popen(x, stdin=p.stdout, stdout=subprocess.PIPE, shell=True) |
23 | | - # logging.debug( |
24 | | - # 'the current sub process pid(cwd: %s, ppid: %s, id: %s%d).' % |
25 | | - # (p.pid, os.getppid(), type(p), id(p))) |
26 | | - # logging.debug(type(p), id(p)) |
27 | | - processes.append(p) |
28 | | - # logging.debug('the current run process pid(cwd: %s, ppid: %s, id: %s%d).' % |
29 | | - # (p.pid, os.getppid(), type(p), id(p))) |
30 | | - # logging.debug(type(p), id(p)) |
31 | | - output = p.communicate()[0] |
32 | | - for p in processes: |
33 | | - p.wait() |
| 16 | + with subprocess.Popen(cmds[0], stdout=subprocess.PIPE, shell=True) as p: |
| 17 | + # logging.debug('the current sub process pid(cwd: %s, ppid: %s, id: %s%d).' % |
| 18 | + # (p.pid, os.getppid(), type(p), id(p))) |
| 19 | + processes = [p] |
| 20 | + for x in cmds[1:]: |
| 21 | + # |
| 22 | + # the airticle link reference: |
| 23 | + # |
| 24 | + # 1. https://docs.python.org/2.7/library/subprocess.html#subprocess.Popen.communicate |
| 25 | + # 2. https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate |
| 26 | + # 3. https://stackoverflow.com/questions/58649679/resourcewarning-unclosed-file-io-bufferedreader-name-4 |
| 27 | + # 4. https://python.readthedocs.io/en/stable/library/subprocess.html |
| 28 | + # |
| 29 | + p = subprocess.Popen(x, stdin=p.stdout, stdout=subprocess.PIPE, bufsize=1024, shell=True) |
| 30 | + # logging.debug( |
| 31 | + # 'the current sub process pid(cwd: %s, ppid: %s, id: %s%d).' % |
| 32 | + # (p.pid, os.getppid(), type(p), id(p))) |
| 33 | + # logging.debug(type(p), id(p)) |
| 34 | + processes.append(p) |
| 35 | + # logging.debug('the current run process pid(cwd: %s, ppid: %s, id: %s%d).' % |
| 36 | + # (p.pid, os.getppid(), type(p), id(p))) |
| 37 | + # logging.debug(type(p), id(p)) |
| 38 | + stdouts, stderrs = p.communicate() |
| 39 | + if stderrs: |
| 40 | + logging.error(stderrs) |
| 41 | + for p in processes: |
| 42 | + p.kill() |
| 43 | + p.wait() |
34 | 44 |
|
35 | 45 | end = time.time() |
36 | 46 | if not quiet: |
37 | | - if platform.system() == 'Linux' and os.isatty(1): |
38 | | - print('\r'), |
39 | 47 | msg = '[%.5f] -> %s' % (end - start, ' | '.join(cmds)) |
40 | | - print(msg) |
41 | | - content = None |
42 | | - if sys.version_info.major > 2: |
43 | | - content = str(output.rstrip(bytes('\n', encoding='utf-8')), encoding='utf-8') |
44 | | - else: |
45 | | - content = output.rstrip('\n') |
46 | | - # logging.debug('the current run process pid(cwd: %s, ppid: %s, id: %s%d).' % |
47 | | - # (p.pid, os.getppid(), type(p), id(p))) |
| 48 | + logging.debug(msg) |
| 49 | + if stdouts: |
| 50 | + if isinstance(stdouts, str): |
| 51 | + content = str(stdouts).rstrip('\n') |
| 52 | + elif isinstance(stdouts, bytes): |
| 53 | + content = str(stdouts.rstrip(bytes('\n', encoding='utf-8')), encoding='utf-8') |
| 54 | + else: |
| 55 | + content = '' |
| 56 | + # logging.debug('the current run process pid(cwd: %s, ppid: %s, id: %s%d).' % |
| 57 | + # (p.pid, os.getppid(), type(p), id(p))) |
48 | 58 | return content |
49 | 59 |
|
50 | 60 |
|
|
0 commit comments