forked from young9471/pymodbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_commands.py
executable file
·187 lines (157 loc) · 5.41 KB
/
setup_commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from distutils.core import Command
import sys, os, shutil
#---------------------------------------------------------------------------#
# Extra Commands
#---------------------------------------------------------------------------#
class BuildApiDocsCommand(Command):
''' Helper command to build the available api documents
This scans all the subdirectories under api and runs the
build.py script underneath trying to build the api
documentation for the given format.
'''
description = "build all the projects api documents"
user_options = []
def initialize_options(self):
''' options setup '''
if not os.path.exists('./build'):
os.mkdir('./build')
def finalize_options(self):
''' options teardown '''
pass
def run(self):
''' command runner '''
old_cwd = os.getcwd()
directories = (d for d in os.listdir('./doc/api') if not d.startswith('.'))
for entry in directories:
os.chdir('./doc/api/%s' % entry)
os.system('python build.py')
os.chdir(old_cwd)
class DeepCleanCommand(Command):
''' Helper command to return the directory to a completely
clean state.
'''
description = "clean everything that we don't want"
user_options = []
def initialize_options(self):
''' options setup '''
self.trash = ['build', 'dist', 'pymodbus.egg-info',
os.path.join(os.path.join('doc','sphinx'),'build'),
]
def finalize_options(self):
pass
def run(self):
''' command runner '''
self.__delete_pyc_files()
self.__delete_trash_dirs()
def __delete_trash_dirs(self):
''' remove all directories created in building '''
self.__delete_pyc_files()
for directory in self.trash:
if os.path.exists(directory):
shutil.rmtree(directory)
def __delete_pyc_files(self):
''' remove all python cache files '''
for root,dirs,files in os.walk('.'):
for file in files:
if file.endswith('.pyc'):
os.remove(os.path.join(root,file))
class LintCommand(Command):
''' Helper command to perform a lint scan of the
sourcecode and return the results.
'''
description = "perform a lint scan of the code"
user_options = []
def initialize_options(self):
''' options setup '''
if not os.path.exists('./build'):
os.mkdir('./build')
def finalize_options(self):
pass
def run(self):
''' command runner '''
scanners = [s for s in dir(self) if s.find('__try') >= 0]
for scanner in scanners:
if getattr(self, scanner)():
break
def __try_pyflakes(self):
try:
from pyflakes.scripts.pyflakes import main
sys.argv = '''pyflakes pymodbus'''.split()
main()
return True
except: return False
def __try_pychecker(self):
try:
import pychecker
sys.argv = '''pychecker pymodbus/*.py'''.split()
main()
return True
except: return False
def __try_pylint(self):
try:
import pylint
sys.argv = '''pylint pymodbus/*.py'''.split()
main()
return True
except: return False
class Python3Command(Command):
''' Helper command to scan for potential python 3
errors.
./setup.py scan_2to3 > build/diffs_2to3 build/report_2to3
'''
description = "perform 2to3 scan of the code"
user_options = []
def initialize_options(self):
''' options setup '''
if not os.path.exists('./build'):
os.mkdir('./build')
self.directories = ['pymodbus', 'test', 'examples']
def finalize_options(self):
pass
def run(self):
''' command runner '''
self.__run_python3()
def __run_python3(self):
try:
from lib2to3.main import main
sys.argv = ['2to3'] + self.directories
main("lib2to3.fixes")
return True
except: return False
class Pep8Command(Command):
''' Helper command to scan for potential pep8 violations
'''
description = "perform pep8 scan of the code"
user_options = []
def initialize_options(self):
''' options setup '''
if not os.path.exists('./build'):
os.mkdir('./build')
self.directories = ['pymodbus']
def finalize_options(self):
pass
def run(self):
''' command runner '''
self.__run_pep8()
def __run_pep8(self):
try:
from pep8 import _main as main
sys.argv = '''pep8 --repeat --count --statistics
'''.split() + self.directories
main()
return True
except: return False
#---------------------------------------------------------------------------#
# Command Configuration
#---------------------------------------------------------------------------#
command_classes = {
'deep_clean' : DeepCleanCommand,
'build_apidocs' : BuildApiDocsCommand,
'lint' : LintCommand,
'scan_2to3' : Python3Command,
'pep8' : Pep8Command,
}
#---------------------------------------------------------------------------#
# Export command list
#---------------------------------------------------------------------------#
__all__ = ['command_classes']