forked from ConservationInternational/trends.earth-schemas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pavement.py
146 lines (118 loc) · 3.8 KB
/
pavement.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
# -*- coding: utf-8 -*-
import os
import sys
import re
import fnmatch
import json
import shutil
import subprocess
from tempfile import mkstemp
from paver.easy import *
from paver.doctools import html
options(
source_dir=path('.'),
)
# Function to find and replace in a file
def _replace(file_path, regex, subst):
#Create temp file
fh, abs_path = mkstemp()
with os.fdopen(fh, 'w') as new_file:
with open(file_path) as old_file:
for line in old_file:
new_file.write(regex.sub(subst, line))
os.remove(file_path)
shutil.move(abs_path, file_path)
@task
@cmdopts([
('version=', 'v', 'Version to set'),
])
def set_version(options):
v = getattr(options, 'version', False)
if not v or not re.match("[0-9]+[.][0-9]+", v):
print('Must specify a valid version (example: 0.36)')
return
# Set in setup.py
print('Setting version to {} in setup.py'.format(v))
setup_regex = re.compile("^([ ]*version=[ ]*')[0-9]+[.][0-9]+")
_replace('setup.py', setup_regex, '\g<1>' + v)
# Set in __init__.py
print('Setting version to {} in __init__.py'.format(v))
init_regex = re.compile('^(__version__[ ]*=[ ]*["\'])[0-9]+[.][0-9]+')
_replace(os.path.join(options.source_dir, '__init__.py'), init_regex, '\g<1>' + v)
@task
@consume_args
def pep8(args):
"""Check code for PEP8 violations"""
try:
import pep8
except:
error('pep8 not found! Run "pip install autopep8".')
sys.exit(1)
# Errors to ignore
ignore = ['E203', 'E121', 'E122', 'E123', 'E124', 'E125', 'E126', 'E127',
'E128', 'E402']
styleguide = pep8.StyleGuide(ignore=ignore,
exclude=['*/ext-libs/*', '*/ext-src/*'],
repeat=True, max_line_length=79,
parse_argv=args)
styleguide.input_dir(options.plugin.source_dir)
info('===== PEP8 SUMMARY =====')
styleguide.options.report.print_statistics()
@task
@consume_args
def autopep8(args):
"""Format code according to PEP8
"""
try:
import autopep8
except:
error('autopep8 not found! Run "paver install_devtools".')
sys.exit(1)
if any(x not in args for x in ['-i', '--in-place']):
args.append('-i')
args.append('--ignore=E261,E265,E402,E501')
args.insert(0, 'dummy')
cmd_args = autopep8.parse_args(args)
excludes = ('ext-lib', 'ext-src')
for p in path('.').walk():
if any(exclude in p for exclude in excludes):
continue
if p.fnmatch('*.py'):
autopep8.fix_file(p, options=cmd_args)
@task
@consume_args
def pylint(args):
"""Check code for errors and coding standard violations"""
try:
from pylint import lint
except:
error('pylint not found! Run "paver install_devtools".')
sys.exit(1)
if not 'rcfile' in args:
args.append('--rcfile=pylintrc')
args.append(options.plugin.source_dir)
lint.Run(args)
################################################
# Below is based on pb_tool:
# https://github.com/g-sherman/plugin_build_tool
def check_path(app):
""" Adapted from StackExchange:
http://stackoverflow.com/questions/377017
"""
def is_exe(fpath):
return os.path.exists(fpath) and os.access(fpath, os.X_OK)
def ext_candidates(fpath):
yield fpath
for ext in os.environ.get("PATHEXT", "").split(os.pathsep):
yield fpath + ext
fpath, fname = os.path.split(app)
if fpath:
if is_exe(app):
return app
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, app)
for candidate in ext_candidates(exe_file):
if is_exe(candidate):
return candidate
return None