run pylint3 and fixed report
This commit adds a pylint configuration and fixed all found issues. Signed-off-by: Claudius Heine <ch@denx.de>
This commit is contained in:
committed by
Daniel Wagner
parent
6bc8e08459
commit
33a21c8d0d
138
kas/libkas.py
138
kas/libkas.py
@@ -19,6 +19,9 @@
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
"""
|
||||
This module contains the core implementation of kas.
|
||||
"""
|
||||
|
||||
import re
|
||||
import os
|
||||
@@ -33,38 +36,58 @@ __copyright__ = 'Copyright (c) Siemens AG, 2017'
|
||||
|
||||
|
||||
class LogOutput:
|
||||
"""
|
||||
Handles the log output of executed applications
|
||||
"""
|
||||
def __init__(self, live):
|
||||
self.live = live
|
||||
self.stdout = []
|
||||
self.stderr = []
|
||||
|
||||
def log_stdout(self, line):
|
||||
"""
|
||||
This method is called when a line over stdout is received.
|
||||
"""
|
||||
if self.live:
|
||||
logging.info(line.strip())
|
||||
self.stdout.append(line)
|
||||
|
||||
def log_stderr(self, line):
|
||||
"""
|
||||
This method is called when a line over stderr is received.
|
||||
"""
|
||||
if self.live:
|
||||
logging.error(line.strip())
|
||||
self.stderr.append(line)
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def _read_stream(stream, cb):
|
||||
def _read_stream(stream, callback):
|
||||
"""
|
||||
This asynchronious method reads from the output stream of the
|
||||
application and transfers each line to the callback function.
|
||||
"""
|
||||
while True:
|
||||
line = yield from stream.readline()
|
||||
try:
|
||||
line = line.decode('utf-8')
|
||||
except:
|
||||
logging.warning('Could not decode line from stream - ignore it')
|
||||
except UnicodeDecodeError as err:
|
||||
logging.warning('Could not decode line from stream, ignore it: %s',
|
||||
err)
|
||||
if line:
|
||||
cb(line)
|
||||
callback(line)
|
||||
else:
|
||||
break
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def _stream_subprocess(cmd, cwd, env, shell, stdout_cb, stderr_cb):
|
||||
"""
|
||||
This function starts the subprocess, sets up the output stream
|
||||
handlers and waits until the process has existed
|
||||
"""
|
||||
# pylint: disable=too-many-arguments
|
||||
|
||||
if shell:
|
||||
process = yield from asyncio.create_subprocess_shell(
|
||||
cmd,
|
||||
@@ -89,14 +112,18 @@ def _stream_subprocess(cmd, cwd, env, shell, stdout_cb, stderr_cb):
|
||||
return ret
|
||||
|
||||
|
||||
def run_cmd(cmd, cwd, env={}, fail=True, shell=False, liveupdate=True):
|
||||
rc = 0
|
||||
stdout = []
|
||||
stderr = []
|
||||
def run_cmd(cmd, cwd, env=None, fail=True, shell=False, liveupdate=True):
|
||||
"""
|
||||
Starts a command.
|
||||
"""
|
||||
# pylint: disable=too-many-arguments
|
||||
|
||||
env = env or {}
|
||||
retc = 0
|
||||
cmdstr = cmd
|
||||
if not shell:
|
||||
cmdstr = ' '.join(cmd)
|
||||
logging.info('{}$ {}'.format(cwd, cmdstr))
|
||||
logging.info('%s$ %s', cwd, cmdstr)
|
||||
|
||||
logo = LogOutput(liveupdate)
|
||||
if asyncio.get_event_loop().is_closed():
|
||||
@@ -105,22 +132,25 @@ def run_cmd(cmd, cwd, env={}, fail=True, shell=False, liveupdate=True):
|
||||
else:
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
rc = loop.run_until_complete(
|
||||
retc = loop.run_until_complete(
|
||||
_stream_subprocess(cmd, cwd, env, shell,
|
||||
logo.log_stdout, logo.log_stderr))
|
||||
loop.close()
|
||||
|
||||
if rc and fail:
|
||||
if retc and fail:
|
||||
msg = 'Command "{cwd}$ {cmd}" failed\n'.format(cwd=cwd, cmd=cmdstr)
|
||||
for line in logo.stderr:
|
||||
msg += line
|
||||
logging.error(msg)
|
||||
sys.exit(rc)
|
||||
sys.exit(retc)
|
||||
|
||||
return (rc, ''.join(logo.stdout))
|
||||
return (retc, ''.join(logo.stdout))
|
||||
|
||||
|
||||
def find_program(paths, name):
|
||||
"""
|
||||
Find a file within the paths array and returns its path.
|
||||
"""
|
||||
for path in paths.split(os.pathsep):
|
||||
prg = os.path.join(path, name)
|
||||
if os.path.isfile(prg):
|
||||
@@ -129,6 +159,10 @@ def find_program(paths, name):
|
||||
|
||||
|
||||
def get_oe_environ(config, build_dir):
|
||||
"""
|
||||
Create the openembedded environment variables.
|
||||
"""
|
||||
# pylint: disable=too-many-locals
|
||||
# nasty side effect function: running oe-init-build-env also
|
||||
# creates the conf directory
|
||||
|
||||
@@ -142,19 +176,19 @@ def get_oe_environ(config, build_dir):
|
||||
sys.exit(1)
|
||||
|
||||
get_bb_env_file = tempfile.mktemp()
|
||||
with open(get_bb_env_file, 'w') as f:
|
||||
with open(get_bb_env_file, 'w') as fds:
|
||||
script = """#!/bin/bash
|
||||
source oe-init-build-env $1 > /dev/null 2>&1
|
||||
env
|
||||
"""
|
||||
f.write(script)
|
||||
fds.write(script)
|
||||
os.chmod(get_bb_env_file, 0o775)
|
||||
|
||||
env = {}
|
||||
env['PATH'] = '/bin:/usr/bin'
|
||||
|
||||
(rc, output) = run_cmd([get_bb_env_file, build_dir],
|
||||
cwd=oe_path, env=env, liveupdate=False)
|
||||
(_, output) = run_cmd([get_bb_env_file, build_dir],
|
||||
cwd=oe_path, env=env, liveupdate=False)
|
||||
|
||||
os.remove(get_bb_env_file)
|
||||
|
||||
@@ -163,65 +197,77 @@ def get_oe_environ(config, build_dir):
|
||||
try:
|
||||
(key, val) = line.split('=', 1)
|
||||
env[key] = val
|
||||
except:
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
vars = ['SSTATE_DIR', 'DL_DIR', 'TMPDIR']
|
||||
env_vars = ['SSTATE_DIR', 'DL_DIR', 'TMPDIR']
|
||||
if 'BB_ENV_EXTRAWHITE' in env:
|
||||
ew = env['BB_ENV_EXTRAWHITE'] + ' '.join(vars)
|
||||
env.update({'BB_ENV_EXTRAWHITE': ew})
|
||||
extra_white = env['BB_ENV_EXTRAWHITE'] + ' '.join(env_vars)
|
||||
env.update({'BB_ENV_EXTRAWHITE': extra_white})
|
||||
|
||||
vars.extend(['SSH_AGENT_PID', 'SSH_AUTH_SOCK',
|
||||
'SHELL', 'TERM'])
|
||||
env_vars.extend(['SSH_AGENT_PID', 'SSH_AUTH_SOCK',
|
||||
'SHELL', 'TERM'])
|
||||
|
||||
for v in vars:
|
||||
if v in os.environ:
|
||||
env[v] = os.environ[v]
|
||||
for env_var in env_vars:
|
||||
if env_var in os.environ:
|
||||
env[env_var] = os.environ[env_var]
|
||||
|
||||
return env
|
||||
|
||||
|
||||
def ssh_add_key(env, key):
|
||||
p = Popen(['/usr/bin/ssh-add', '-'], stdin=PIPE, stdout=None,
|
||||
stderr=PIPE, env=env)
|
||||
error = p.communicate(input=str.encode(key))[1]
|
||||
if p.returncode and error:
|
||||
logging.error('failed to add ssh key: {}'.format(error))
|
||||
"""
|
||||
Add ssh key to the ssh-agent
|
||||
"""
|
||||
process = Popen(['/usr/bin/ssh-add', '-'], stdin=PIPE, stdout=None,
|
||||
stderr=PIPE, env=env)
|
||||
(_, error) = process.communicate(input=str.encode(key))
|
||||
if process.returncode and error:
|
||||
logging.error('failed to add ssh key: %s', error)
|
||||
|
||||
|
||||
def ssh_cleanup_agent(config):
|
||||
"""Removes the identities and stop the ssh-agent instance """
|
||||
"""
|
||||
Removes the identities and stop the ssh-agent instance
|
||||
"""
|
||||
# remove the identities
|
||||
p = Popen(['/usr/bin/ssh-add', '-D'], env=config.environ)
|
||||
p.wait()
|
||||
if p.returncode != 0:
|
||||
process = Popen(['/usr/bin/ssh-add', '-D'], env=config.environ)
|
||||
process.wait()
|
||||
if process.returncode != 0:
|
||||
logging.error('failed to delete SSH identities')
|
||||
|
||||
# stop the ssh-agent
|
||||
p = Popen(['/usr/bin/ssh-agent', '-k'], env=config.environ)
|
||||
p.wait()
|
||||
if p.returncode != 0:
|
||||
process = Popen(['/usr/bin/ssh-agent', '-k'], env=config.environ)
|
||||
process.wait()
|
||||
if process.returncode != 0:
|
||||
logging.error('failed to stop SSH agent')
|
||||
|
||||
|
||||
def ssh_setup_agent(config, envkeys=['SSH_PRIVATE_KEY']):
|
||||
def ssh_setup_agent(config, envkeys=None):
|
||||
"""
|
||||
Starts the ssh-agent
|
||||
"""
|
||||
envkeys = envkeys or ['SSH_PRIVATE_KEY']
|
||||
output = os.popen('/usr/bin/ssh-agent -s').readlines()
|
||||
for line in output:
|
||||
matches = re.search("(\S+)\=(\S+)\;", line)
|
||||
matches = re.search(r"(\S+)\=(\S+)\;", line)
|
||||
if matches:
|
||||
config.environ[matches.group(1)] = matches.group(2)
|
||||
|
||||
for ek in envkeys:
|
||||
key = os.environ.get(ek)
|
||||
for envkey in envkeys:
|
||||
key = os.environ.get(envkey)
|
||||
if key:
|
||||
ssh_add_key(config.environ, key)
|
||||
else:
|
||||
logging.warning('{} is missing'.format(ek))
|
||||
logging.warning('%s is missing', envkey)
|
||||
|
||||
|
||||
def ssh_no_host_key_check(config):
|
||||
def ssh_no_host_key_check(_):
|
||||
"""
|
||||
Disables ssh host key check
|
||||
"""
|
||||
home = os.path.expanduser('~')
|
||||
if not os.path.exists(home + '/.ssh'):
|
||||
os.mkdir(home + '/.ssh')
|
||||
with open(home + '/.ssh/config', 'w') as f:
|
||||
f.write('Host *\n\tStrictHostKeyChecking no\n\n')
|
||||
with open(home + '/.ssh/config', 'w') as fds:
|
||||
fds.write('Host *\n\tStrictHostKeyChecking no\n\n')
|
||||
|
||||
Reference in New Issue
Block a user