OLD | NEW |
1 # Copyright (c) 2003-2013 LOGILAB S.A. (Paris, FRANCE). | |
2 # This program is free software; you can redistribute it and/or modify it under | 1 # This program is free software; you can redistribute it and/or modify it under |
3 # the terms of the GNU General Public License as published by the Free Software | 2 # the terms of the GNU General Public License as published by the Free Software |
4 # Foundation; either version 2 of the License, or (at your option) any later | 3 # Foundation; either version 2 of the License, or (at your option) any later |
5 # version. | 4 # version. |
6 # | 5 # |
7 # This program is distributed in the hope that it will be useful, but WITHOUT | 6 # This program is distributed in the hope that it will be useful, but WITHOUT |
8 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 7 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
9 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details | 8 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details |
10 # | 9 # |
11 # You should have received a copy of the GNU General Public License along with | 10 # You should have received a copy of the GNU General Public License along with |
12 # this program; if not, write to the Free Software Foundation, Inc., | 11 # this program; if not, write to the Free Software Foundation, Inc., |
13 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 12 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
14 """utilities for Pylint configuration : | 13 """ Copyright (c) 2003-2006 LOGILAB S.A. (Paris, FRANCE). |
| 14 http://www.logilab.fr/ -- mailto:contact@logilab.fr |
15 | 15 |
16 * pylintrc | 16 utilities for PyLint configuration : |
17 * pylint.d (PYLINTHOME) | 17 _ pylintrc |
| 18 _ pylint.d (PYLINT_HOME) |
18 """ | 19 """ |
19 from __future__ import with_statement | |
20 | 20 |
21 import pickle | 21 import pickle |
22 import os | 22 import os |
23 import sys | 23 import sys |
24 from os.path import exists, isfile, join, expanduser, abspath, dirname | 24 from os.path import exists, isfile, join, expanduser, abspath, dirname |
25 | 25 |
26 # pylint home is used to save old runs results ################################ | 26 # pylint home is used to save old runs results ################################ |
27 | 27 |
28 USER_HOME = expanduser('~') | 28 USER_HOME = expanduser('~') |
29 if 'PYLINTHOME' in os.environ: | 29 if 'PYLINTHOME' in os.environ: |
30 PYLINT_HOME = os.environ['PYLINTHOME'] | 30 PYLINT_HOME = os.environ['PYLINTHOME'] |
31 if USER_HOME == '~': | 31 if USER_HOME == '~': |
32 USER_HOME = dirname(PYLINT_HOME) | 32 USER_HOME = dirname(PYLINT_HOME) |
33 elif USER_HOME == '~': | 33 elif USER_HOME == '~': |
34 PYLINT_HOME = ".pylint.d" | 34 PYLINT_HOME = ".pylint.d" |
35 else: | 35 else: |
36 PYLINT_HOME = join(USER_HOME, '.pylint.d') | 36 PYLINT_HOME = join(USER_HOME, '.pylint.d') |
| 37 |
| 38 if not exists(PYLINT_HOME): |
| 39 try: |
| 40 os.mkdir(PYLINT_HOME) |
| 41 except OSError: |
| 42 print >> sys.stderr, 'Unable to create directory %s' % PYLINT_HOME |
37 | 43 |
38 def get_pdata_path(base_name, recurs): | 44 def get_pdata_path(base_name, recurs): |
39 """return the path of the file which should contain old search data for the | 45 """return the path of the file which should contain old search data for the |
40 given base_name with the given options values | 46 given base_name with the given options values |
41 """ | 47 """ |
42 base_name = base_name.replace(os.sep, '_') | 48 base_name = base_name.replace(os.sep, '_') |
43 return join(PYLINT_HOME, "%s%s%s"%(base_name, recurs, '.stats')) | 49 return join(PYLINT_HOME, "%s%s%s"%(base_name, recurs, '.stats')) |
44 | 50 |
45 def load_results(base): | 51 def load_results(base): |
46 """try to unpickle and return data from file if it exists and is not | 52 """try to unpickle and return data from file if it exists and is not |
47 corrupted | 53 corrupted |
48 | 54 |
49 return an empty dictionary if it doesn't exists | 55 return an empty dictionary if it doesn't exists |
50 """ | 56 """ |
51 data_file = get_pdata_path(base, 1) | 57 data_file = get_pdata_path(base, 1) |
52 try: | 58 try: |
53 with open(data_file, _PICK_LOAD) as stream: | 59 return pickle.load(open(data_file)) |
54 return pickle.load(stream) | |
55 except: | 60 except: |
56 return {} | 61 return {} |
57 | 62 |
58 if sys.version_info < (3, 0): | 63 if sys.version_info < (3, 0): |
59 _PICK_DUMP, _PICK_LOAD = 'w', 'r' | 64 _PICK_MOD = 'w' |
60 else: | 65 else: |
61 _PICK_DUMP, _PICK_LOAD = 'wb', 'rb' | 66 _PICK_MOD = 'wb' |
62 | 67 |
63 def save_results(results, base): | 68 def save_results(results, base): |
64 """pickle results""" | 69 """pickle results""" |
65 if not exists(PYLINT_HOME): | |
66 try: | |
67 os.mkdir(PYLINT_HOME) | |
68 except OSError: | |
69 print >> sys.stderr, 'Unable to create directory %s' % PYLINT_HOME | |
70 data_file = get_pdata_path(base, 1) | 70 data_file = get_pdata_path(base, 1) |
71 try: | 71 try: |
72 with open(data_file, _PICK_DUMP) as stream: | 72 pickle.dump(results, open(data_file, _PICK_MOD)) |
73 pickle.dump(results, stream) | |
74 except (IOError, OSError), ex: | 73 except (IOError, OSError), ex: |
75 print >> sys.stderr, 'Unable to create file %s: %s' % (data_file, ex) | 74 print >> sys.stderr, 'Unable to create file %s: %s' % (data_file, ex) |
76 | 75 |
77 # location of the configuration file ########################################## | 76 # location of the configuration file ########################################## |
78 | 77 |
79 | 78 |
80 def find_pylintrc(): | 79 def find_pylintrc(): |
81 """search the pylint rc file and return its path if it find it, else None | 80 """search the pylint rc file and return its path if it find it, else None |
82 """ | 81 """ |
83 # is there a pylint rc file in the current directory ? | 82 # is there a pylint rc file in the current directory ? |
84 if exists('pylintrc'): | 83 if exists('pylintrc'): |
85 return abspath('pylintrc') | 84 return abspath('pylintrc') |
86 if isfile('__init__.py'): | 85 if isfile('__init__.py'): |
87 curdir = abspath(os.getcwd()) | 86 curdir = abspath(os.getcwd()) |
88 while isfile(join(curdir, '__init__.py')): | 87 while isfile(join(curdir, '__init__.py')): |
89 curdir = abspath(join(curdir, '..')) | 88 curdir = abspath(join(curdir, '..')) |
90 if isfile(join(curdir, 'pylintrc')): | 89 if isfile(join(curdir, 'pylintrc')): |
91 return join(curdir, 'pylintrc') | 90 return join(curdir, 'pylintrc') |
92 if 'PYLINTRC' in os.environ and exists(os.environ['PYLINTRC']): | 91 if 'PYLINTRC' in os.environ and exists(os.environ['PYLINTRC']): |
93 pylintrc = os.environ['PYLINTRC'] | 92 pylintrc = os.environ['PYLINTRC'] |
94 else: | 93 else: |
95 user_home = expanduser('~') | 94 user_home = expanduser('~') |
96 if user_home == '~' or user_home == '/root': | 95 if user_home == '~' or user_home == '/root': |
97 pylintrc = ".pylintrc" | 96 pylintrc = ".pylintrc" |
98 else: | 97 else: |
99 pylintrc = join(user_home, '.pylintrc') | 98 pylintrc = join(user_home, '.pylintrc') |
100 if not isfile(pylintrc): | |
101 pylintrc = join(user_home, '.config', 'pylintrc') | |
102 if not isfile(pylintrc): | 99 if not isfile(pylintrc): |
103 if isfile('/etc/pylintrc'): | 100 if isfile('/etc/pylintrc'): |
104 pylintrc = '/etc/pylintrc' | 101 pylintrc = '/etc/pylintrc' |
105 else: | 102 else: |
106 pylintrc = None | 103 pylintrc = None |
107 return pylintrc | 104 return pylintrc |
108 | 105 |
109 PYLINTRC = find_pylintrc() | 106 PYLINTRC = find_pylintrc() |
110 | 107 |
111 ENV_HELP = ''' | 108 ENV_HELP = ''' |
112 The following environment variables are used: | 109 The following environment variables are used : |
113 * PYLINTHOME | 110 * PYLINTHOME |
114 Path to the directory where the persistent for the run will be stored. If | 111 path to the directory where data of persistent run will be stored. If not |
115 not found, it defaults to ~/.pylint.d/ or .pylint.d (in the current working | 112 found, it defaults to ~/.pylint.d/ or .pylint.d (in the current working |
116 directory). | 113 directory). |
117 * PYLINTRC | 114 * PYLINTRC |
118 Path to the configuration file. See the documentation for the method used | 115 path to the configuration file. If not found, it will use the first |
119 to search for configuration file. | 116 existent file in ~/.pylintrc, /etc/pylintrc. |
120 ''' % globals() | 117 ''' % globals() |
121 | 118 |
122 # evaluation messages ######################################################### | 119 # evaluation messages ######################################################### |
123 | 120 |
124 def get_note_message(note): | 121 def get_note_message(note): |
125 """return a message according to note | 122 """return a message according to note |
126 note is a float < 10 (10 is the highest note) | 123 note is a float < 10 (10 is the highest note) |
127 """ | 124 """ |
128 assert note <= 10, "Note is %.2f. Either you cheated, or pylint's \ | 125 assert note <= 10, "Note is %.2f. Either you cheated, or pylint's \ |
129 broken!" % note | 126 broken!" % note |
(...skipping 17 matching lines...) Expand all Loading... |
147 msg = 'If you commit now, people should not be making nasty \ | 144 msg = 'If you commit now, people should not be making nasty \ |
148 comments about you on c.l.py' | 145 comments about you on c.l.py' |
149 elif note < 9: | 146 elif note < 9: |
150 msg = 'That\'s pretty good. Good work mate.' | 147 msg = 'That\'s pretty good. Good work mate.' |
151 elif note < 10: | 148 elif note < 10: |
152 msg = 'So close to being perfect...' | 149 msg = 'So close to being perfect...' |
153 else: | 150 else: |
154 msg = 'Wow ! Now this deserves our uttermost respect.\nPlease send \ | 151 msg = 'Wow ! Now this deserves our uttermost respect.\nPlease send \ |
155 your code to python-projects@logilab.org' | 152 your code to python-projects@logilab.org' |
156 return msg | 153 return msg |
OLD | NEW |