OLD | NEW |
1 # -*- coding: utf-8; mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-
offset: 4 -*- vim:fenc=utf-8:ft=python:et:sw=4:ts=4:sts=4 | 1 # -*- coding: utf-8; mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-
offset: 4 -*- vim:fenc=utf-8:ft=python:et:sw=4:ts=4:sts=4 |
2 # Copyright (c) 2003-2013 LOGILAB S.A. (Paris, FRANCE). | 2 # Copyright (c) 2003-2013 LOGILAB S.A. (Paris, FRANCE). |
3 # http://www.logilab.fr/ -- mailto:contact@logilab.fr | 3 # http://www.logilab.fr/ -- mailto:contact@logilab.fr |
4 # | 4 # |
5 # This program is free software; you can redistribute it and/or modify it under | 5 # This program is free software; you can redistribute it and/or modify it under |
6 # the terms of the GNU General Public License as published by the Free Software | 6 # the terms of the GNU General Public License as published by the Free Software |
7 # Foundation; either version 2 of the License, or (at your option) any later | 7 # Foundation; either version 2 of the License, or (at your option) any later |
8 # version. | 8 # version. |
9 # | 9 # |
10 # This program is distributed in the hope that it will be useful, but WITHOUT | 10 # This program is distributed in the hope that it will be useful, but WITHOUT |
(...skipping 27 matching lines...) Expand all Loading... |
38 | 38 |
39 pylint a/c/y.py | 39 pylint a/c/y.py |
40 | 40 |
41 - As this script will be invoked by emacs within the directory of the file | 41 - As this script will be invoked by emacs within the directory of the file |
42 we are checking we need to go out of it to avoid these false positives. | 42 we are checking we need to go out of it to avoid these false positives. |
43 | 43 |
44 | 44 |
45 You may also use py_run to run pylint with desired options and get back (or not) | 45 You may also use py_run to run pylint with desired options and get back (or not) |
46 its output. | 46 its output. |
47 """ | 47 """ |
| 48 from __future__ import print_function |
48 | 49 |
49 import sys, os | 50 import sys, os |
50 import os.path as osp | 51 import os.path as osp |
51 from subprocess import Popen, PIPE | 52 from subprocess import Popen, PIPE |
52 | 53 |
53 def _get_env(): | 54 def _get_env(): |
54 '''Extracts the environment PYTHONPATH and appends the current sys.path to | 55 '''Extracts the environment PYTHONPATH and appends the current sys.path to |
55 those.''' | 56 those.''' |
56 env = dict(os.environ) | 57 env = dict(os.environ) |
57 env['PYTHONPATH'] = os.pathsep.join(sys.path) | 58 env['PYTHONPATH'] = os.pathsep.join(sys.path) |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 | 96 |
96 for line in process.stdout: | 97 for line in process.stdout: |
97 # remove pylintrc warning | 98 # remove pylintrc warning |
98 if line.startswith("No config file found"): | 99 if line.startswith("No config file found"): |
99 continue | 100 continue |
100 | 101 |
101 # modify the file name thats output to reverse the path traversal we mad
e | 102 # modify the file name thats output to reverse the path traversal we mad
e |
102 parts = line.split(":") | 103 parts = line.split(":") |
103 if parts and parts[0] == child_path: | 104 if parts and parts[0] == child_path: |
104 line = ":".join([filename] + parts[1:]) | 105 line = ":".join([filename] + parts[1:]) |
105 print line, | 106 print(line, end=' ') |
106 | 107 |
107 process.wait() | 108 process.wait() |
108 return process.returncode | 109 return process.returncode |
109 | 110 |
110 | 111 |
111 def py_run(command_options='', return_std=False, stdout=None, stderr=None, | 112 def py_run(command_options='', return_std=False, stdout=None, stderr=None, |
112 script='epylint'): | 113 script='epylint'): |
113 """Run pylint from python | 114 """Run pylint from python |
114 | 115 |
115 ``command_options`` is a string containing ``pylint`` command line options; | 116 ``command_options`` is a string containing ``pylint`` command line options; |
116 ``return_std`` (boolean) indicates return of created standart output | 117 ``return_std`` (boolean) indicates return of created standard output |
117 and error (see below); | 118 and error (see below); |
118 ``stdout`` and ``stderr`` are 'file-like' objects in which standart output | 119 ``stdout`` and ``stderr`` are 'file-like' objects in which standard output |
119 could be written. | 120 could be written. |
120 | 121 |
121 Calling agent is responsible for stdout/err management (creation, close). | 122 Calling agent is responsible for stdout/err management (creation, close). |
122 Default standart output and error are those from sys, | 123 Default standard output and error are those from sys, |
123 or standalone ones (``subprocess.PIPE``) are used | 124 or standalone ones (``subprocess.PIPE``) are used |
124 if they are not set and ``return_std``. | 125 if they are not set and ``return_std``. |
125 | 126 |
126 If ``return_std`` is set to ``True``, this function returns a 2-uple | 127 If ``return_std`` is set to ``True``, this function returns a 2-uple |
127 containing standart output and error related to created process, | 128 containing standard output and error related to created process, |
128 as follows: ``(stdout, stderr)``. | 129 as follows: ``(stdout, stderr)``. |
129 | 130 |
130 A trivial usage could be as follows: | 131 A trivial usage could be as follows: |
131 >>> py_run( '--version') | 132 >>> py_run( '--version') |
132 No config file found, using default configuration | 133 No config file found, using default configuration |
133 pylint 0.18.1, | 134 pylint 0.18.1, |
134 ... | 135 ... |
135 | 136 |
136 To silently run Pylint on a module, and get its standart output and error: | 137 To silently run Pylint on a module, and get its standard output and error: |
137 >>> (pylint_stdout, pylint_stderr) = py_run( 'module_name.py', True) | 138 >>> (pylint_stdout, pylint_stderr) = py_run( 'module_name.py', True) |
138 """ | 139 """ |
139 # Create command line to call pylint | 140 # Create command line to call pylint |
140 if os.name == 'nt': | 141 if os.name == 'nt': |
141 script += '.bat' | 142 script += '.bat' |
142 command_line = script + ' ' + command_options | 143 command_line = script + ' ' + command_options |
143 # Providing standart output and/or error if not set | 144 # Providing standard output and/or error if not set |
144 if stdout is None: | 145 if stdout is None: |
145 if return_std: | 146 if return_std: |
146 stdout = PIPE | 147 stdout = PIPE |
147 else: | 148 else: |
148 stdout = sys.stdout | 149 stdout = sys.stdout |
149 if stderr is None: | 150 if stderr is None: |
150 if return_std: | 151 if return_std: |
151 stderr = PIPE | 152 stderr = PIPE |
152 else: | 153 else: |
153 stderr = sys.stderr | 154 stderr = sys.stderr |
154 # Call pylint in a subprocess | 155 # Call pylint in a subprocess |
155 p = Popen(command_line, shell=True, stdout=stdout, stderr=stderr, | 156 p = Popen(command_line, shell=True, stdout=stdout, stderr=stderr, |
156 env=_get_env(), universal_newlines=True) | 157 env=_get_env(), universal_newlines=True) |
157 p.wait() | 158 p.wait() |
158 # Return standart output and error | 159 # Return standard output and error |
159 if return_std: | 160 if return_std: |
160 return (p.stdout, p.stderr) | 161 return (p.stdout, p.stderr) |
161 | 162 |
162 | 163 |
163 def Run(): | 164 def Run(): |
164 if len(sys.argv) == 1: | 165 if len(sys.argv) == 1: |
165 print "Usage: %s <filename> [options]" % sys.argv[0] | 166 print("Usage: %s <filename> [options]" % sys.argv[0]) |
166 sys.exit(1) | 167 sys.exit(1) |
167 elif not osp.exists(sys.argv[1]): | 168 elif not osp.exists(sys.argv[1]): |
168 print "%s does not exist" % sys.argv[1] | 169 print("%s does not exist" % sys.argv[1]) |
169 sys.exit(1) | 170 sys.exit(1) |
170 else: | 171 else: |
171 sys.exit(lint(sys.argv[1], sys.argv[2:])) | 172 sys.exit(lint(sys.argv[1], sys.argv[2:])) |
172 | 173 |
173 | 174 |
174 if __name__ == '__main__': | 175 if __name__ == '__main__': |
175 Run() | 176 Run() |
OLD | NEW |