Commit 7c46d1e1 authored by David Danier's avatar David Danier

Merge branch 'dev/pylint' into 'master'

Introduce pylint

See merge request build/b5!6
parents 0eadab6a 467128b9
......@@ -12,6 +12,7 @@ packaging = ">=16.0"
[dev-packages]
"e1839a8" = {path = ".", editable = true}
pylint = ">=2.3.1"
[requires]
python_version = "3.7"
This diff is collapsed.
import argparse
import os
import termcolor
import sys
from .lib.state import State
from .lib.module import load_module
import termcolor
from .exceptions import B5ExecutionError
from .lib.module import load_module
from .lib.state import State
def main():
......@@ -37,9 +38,9 @@ def main():
raise B5ExecutionError('b5-execute is not intended to be called directly!')
state = State.load(open(args.state_file, 'rb'))
if not 'modules' in state.config:
if 'modules' not in state.config:
raise B5ExecutionError('No modules defined')
if not args.module in state.config['modules']:
if args.module not in state.config['modules']:
raise B5ExecutionError('Module not available')
module = load_module(state, args.module)
......@@ -49,6 +50,6 @@ def main():
os.chdir(state.run_path)
method(state, args.args)
except B5ExecutionError as e:
termcolor.cprint(str(e), 'red')
except B5ExecutionError as error:
termcolor.cprint(str(error), 'red')
sys.exit(1)
import argparse
import os
import re
import shutil
import termcolor
import subprocess
import sys
import re
from .exceptions import B5ExecutionError
import termcolor
from .exceptions import B5ExecutionError
NON_URL_SKELETON = re.compile('^[A-Za-z0-9_-]+$')
......@@ -56,7 +56,7 @@ def main():
full_path = os.path.realpath(os.path.join(os.getcwd(), path))
os.makedirs(full_path, exist_ok=True)
if len(os.listdir(full_path)) > 0:
if os.listdir(full_path):
raise B5ExecutionError('Cannot init an existing directory if not empty')
_run_cmd(['git', 'clone', skeleton_url, full_path], 'Could not clone skeleton repository, see above')
......@@ -80,6 +80,6 @@ def main():
termcolor.cprint('Successful initialized {path}'.format(path=path), 'green')
termcolor.cprint(' skeleton used: {skeleton_url}'.format(skeleton_url=skeleton_url), 'green')
termcolor.cprint(' project path: {full_path}'.format(full_path=full_path), 'green')
except B5ExecutionError as e:
termcolor.cprint(str(e), 'red')
except B5ExecutionError as error:
termcolor.cprint(str(error), 'red')
sys.exit(1)
import yaml
import os
import yaml
from .version import ensure_config_version
from ..exceptions import B5ExecutionError
......@@ -15,8 +16,8 @@ def find_configs(state, configs):
'config': config,
'path': config_path,
})
#if not found_configs:
# raise B5ExecutionError('No config found, tried %s inside %s' % (', '.join(configs), run_path))
# if not found_configs:
# raise B5ExecutionError('No config found, tried %s inside %s' % (', '.join(configs), run_path))
return found_configs
......@@ -40,7 +41,6 @@ def merge_config(cur_config, new_config):
return result_config
def validate_config(config):
if 'version' in config:
ensure_config_version(config['version'])
......@@ -51,8 +51,8 @@ def load_config(state):
configfiles = state.configfiles
config = {}
for configfile in configfiles:
fh = open(configfile['path'], 'r')
file_config = yaml.safe_load(fh)
file_handle = open(configfile['path'], 'r')
file_config = yaml.safe_load(file_handle)
if not isinstance(file_config, dict):
file_config = {}
config = merge_config(config, file_config)
......
......@@ -4,14 +4,14 @@ from importlib import import_module
def import_string(dotted_path):
try:
module_path, class_name = dotted_path.rsplit('.', 1)
except ValueError as err:
except ValueError:
raise ImportError("%s doesn't look like a module path" % dotted_path)
module = import_module(module_path)
try:
return getattr(module, class_name)
except AttributeError as err:
except AttributeError:
raise ImportError('Module "%s" does not define a "%s" attribute/class' % (
module_path, class_name)
)
\ No newline at end of file
)
from ..modules import MODULES
from ..exceptions import B5ExecutionError
from .importutils import import_string
from ..exceptions import B5ExecutionError
from ..modules import MODULES
def load_module(state, module_key):
if not 'modules' in state.config or not module_key in state.config['modules']:
if 'modules' not in state.config or module_key not in state.config['modules']:
raise RuntimeError('Module %s is not defined in config' % module_key)
module_config = state.config['modules'][module_key]
......@@ -17,8 +17,10 @@ def load_module(state, module_key):
module_import_path = module_class_key
if module_class_key in MODULES:
module_import_path = MODULES[module_class_key]
if not '.' in module_import_path:
raise B5ExecutionError('Module seems not to be valid (key=%s/import=%s), please check config' % (module_key, module_import_path))
if '.' not in module_import_path:
raise B5ExecutionError('Module seems not to be valid (key=%s/import=%s), please check config' %
(module_key, module_import_path)
)
try:
module_class = import_string(module_import_path)
......
import os
import re
import shlex
import tempfile
import os
from .module import load_module
RE_KEY_ESCAPE = re.compile('[^a-zA-Z0-9]+')
CONFIG_SUB = '%s_%s'
CONFIG_KEYS = '%s_KEYS'
def modules_script_source(state):
script = []
if 'modules' in state.config:
......@@ -15,12 +21,6 @@ def modules_script_source(state):
def config_script_source(config, prefix='CONFIG'):
import re
RE_KEY_ESCAPE = re.compile('[^a-zA-Z0-9]+')
CONFIG_SUB = '%s_%s'
CONFIG_KEYS = '%s_KEYS'
def _gen_config(config_node, prefix):
script = []
......@@ -85,7 +85,6 @@ def construct_script_source(state):
# run_path and parse Taskfile's
script.append('cd %s\n' % shlex.quote(state.run_path))
for taskfile in state.taskfiles:
#script.append('source %s\n' % shlex.quote(taskfile['path']))
script.append(open(taskfile['path'], 'r').read())
return '\n'.join(script)
......@@ -108,16 +107,16 @@ fi
)
class StoredScriptSource(object):
class StoredScriptSource:
def __init__(self, state, source):
self.state = state
self.source = source
self.fh = tempfile.NamedTemporaryFile(suffix='b5-compiled', delete=False)
self.fh.write(self.source.encode('utf-8'))
self.fh.close()
self.file_handle = tempfile.NamedTemporaryFile(suffix='b5-compiled', delete=False)
self.file_handle.write(self.source.encode('utf-8'))
self.file_handle.close()
def close(self):
os.unlink(self.fh.name)
os.unlink(self.file_handle.name)
def __enter__(self):
return self
......@@ -127,4 +126,4 @@ class StoredScriptSource(object):
@property
def name(self):
return self.fh.name
return self.file_handle.name
import yaml
import tempfile
import os
import tempfile
import yaml
class StoredState(object):
class StoredState:
def __init__(self, state):
self.state = state
if not self.state.stored_name is None:
if self.state.stored_name is not None:
raise RuntimeError('You may only store the state once')
self.fh = tempfile.NamedTemporaryFile(suffix='b5-state', mode='w', encoding='utf-8', delete=False)
self.file_handle = tempfile.NamedTemporaryFile(suffix='b5-state', mode='w', encoding='utf-8', delete=False)
self.state.stored_name = self.name
yaml.dump({
key: getattr(self.state, key)
for key in state.KEYS
}, self.fh, default_flow_style=False)
self.fh.close()
}, self.file_handle, default_flow_style=False)
self.file_handle.close()
def close(self):
os.unlink(self.fh.name)
os.unlink(self.file_handle.name)
self.state.stored_name = None
def __enter__(self):
......@@ -29,10 +30,10 @@ class StoredState(object):
@property
def name(self):
return self.fh.name
return self.file_handle.name
class State(object):
class State:
KEYS = ('project_path', 'run_path', 'taskfiles', 'configfiles', 'config', 'args', 'stored_name')
taskfiles = []
......@@ -44,7 +45,7 @@ class State(object):
if not hasattr(self, key):
setattr(self, key, None)
for key in kwargs:
if not key in self.KEYS:
if key not in self.KEYS:
raise RuntimeError('Key %s is not a valid state attribute' % key)
setattr(self, key, kwargs[key])
......@@ -52,5 +53,5 @@ class State(object):
return StoredState(self)
@classmethod
def load(cls, fh):
return cls(**yaml.safe_load(fh))
def load(cls, file_handle):
return cls(**yaml.safe_load(file_handle))
......@@ -16,4 +16,3 @@ def find_taskfiles(state, taskfiles):
if not found_taskfiles:
raise B5ExecutionError('No Taskfiles found, tried %s inside %s' % (', '.join(taskfiles), run_path))
return found_taskfiles
......@@ -5,14 +5,14 @@ import sys
import termcolor
from . import VERSION
from .exceptions import B5ExecutionError
from .lib.config import find_configs
from .lib.config import load_config
from .lib.detect import detect_project_path, DETECT
from .lib.taskfile import find_taskfiles
from .lib.config import find_configs
from .lib.script import StoredScriptSource, construct_script_source, construct_script_run
from .lib.state import State
from . import VERSION
from .lib.taskfile import find_taskfiles
def main():
......@@ -73,11 +73,11 @@ def main():
# State vars
state = State(
project_path = args.project_path,
run_path = None,
taskfiles = [],
config = {},
args = vars(args)
project_path=args.project_path,
run_path=None,
taskfiles=[],
config={},
args=vars(args)
)
# Find project dir
......@@ -112,12 +112,10 @@ def main():
construct_script_run(state),
])
with StoredScriptSource(state, script_source) as source:
# print(source.source)
# return
if state.run_path:
os.chdir(state.run_path)
try:
result = subprocess.run(
subprocess.run(
[
args.shell,
source.name,
......@@ -132,7 +130,6 @@ def main():
termcolor.cprint('Task execution failed, see above', color='red')
sys.exit(1)
_print('Task exited ok', color='green')
#print(result)
except B5ExecutionError as e:
termcolor.cprint(str(e), 'red')
except B5ExecutionError as error:
termcolor.cprint(str(error), 'red')
sys.exit(1)
from ..lib.config import merge_config
import re
from ..lib.config import merge_config
CONFIG_PREFIX_RE = re.compile('[^A-Z0-9]')
MODULES = {
......@@ -15,7 +15,7 @@ MODULES = {
}
class BaseModule(object):
class BaseModule:
DEFAULT_CONFIG = {}
def __init__(self, name, config, state, **kwargs):
......
......@@ -50,7 +50,6 @@ class ComposerModule(BaseModule):
)
'''.format(
vendor_path=shlex.quote(self.config['vendor_path']),
base_path=shlex.quote(self.config['base_path']),
)))
script.append(self._script_function_source('composer', '''
......
......@@ -52,7 +52,6 @@ class PipenvModule(BaseModule):
base_path=shlex.quote(self.config['base_path']),
pipenv_bin=shlex.quote(self.config['pipenv_bin']),
install_dev='--dev' if self.config['install_dev'] else '',
name=self.name,
)))
script.append(self._script_function_source('update', '''
......
......@@ -85,18 +85,18 @@ class TemplateModule(BaseModule):
'output_file': output_file if output_file else '-',
},
)
except jinja2.UndefinedError as e:
except jinja2.UndefinedError as error:
termcolor.cprint('Template could not be rendered (%s), error message' % args.template_file, color='red')
termcolor.cprint(e.message, color='yellow')
termcolor.cprint(error.message, color='yellow')
sys.exit(1)
if output_file:
try:
with open(output_file, 'w') as fh:
fh.write(rendered)
except IOError as e:
with open(output_file, 'w') as file_handle:
file_handle.write(rendered)
except IOError as error:
termcolor.cprint('Template output could not be saved (%s)' % args.output_file, color='red')
termcolor.cprint(e.message, color='yellow')
termcolor.cprint(str(error), color='yellow')
sys.exit(1)
else:
print(rendered)
......
......@@ -66,7 +66,6 @@ class VirtualenvModule(BaseModule):
"$@"
)
'''.format(
base_path=shlex.quote(self.config['base_path']),
activate_path=shlex.quote(os.path.join(
self.config['env_path'],
'bin',
......
......@@ -35,4 +35,11 @@ task:pypi:release() {
python3 setup.py sdist && \
twine upload dist/*
)
}
\ No newline at end of file
}
task:lint() {
(
cd .. && \
pipenv:run pylint --rcfile=pylintrc --exit-zero b5/
)
}
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment