Files
tobiko/tools/setup_infrared.py
Federico Ressi 86441d7aac Update roles requiring python
Change-Id: Iae5cd4affc3e7d148221e55768fbdf6d28907e54
2020-04-03 11:10:38 +00:00

116 lines
3.3 KiB
Python

# Copyright 2018 Red Hat
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import absolute_import
import logging
import os
import subprocess
import sys
LOG = logging.getLogger(__name__)
def main():
setup_logging()
add_tobiko_plugin()
import_workspace()
copy_ansible_inventory()
def setup_logging(level=logging.DEBUG):
logging.basicConfig(
level=level,
stream=sys.stderr,
format='%(name)-s: %(levelname)-7s %(asctime)-15s | %(message)s')
def add_tobiko_plugin():
add_plugin('tobiko', os.environ.get('IR_TOBIKO_PLUGIN'))
def add_plugin(name, path):
if path:
path = normalize_path(path)
if os.path.isdir(path):
remove_plugin(name)
execute('ir plugin add "{}"', path)
else:
LOG.debug("Plug-in '%s' directory not found: '%s'", name, path)
else:
LOG.debug("Plug-in '%s' path not specified", name)
def remove_plugin(name):
try:
execute('ir plugin remove "{}"', name)
except subprocess.CalledProcessError:
return False
else:
return True
def import_workspace(filename=None):
filename = filename or os.environ.get('IR_WORKSPACE_FILE')
if filename:
filename = normalize_path(filename)
if os.path.isfile(filename):
try:
execute('ir workspace import "{}"', filename)
except subprocess.CalledProcessError:
# If file was already imported before we checkout to its
# workspace
workspace = name_from_path(filename)
execute('ir workspace checkout "{}"', workspace)
else:
LOG.debug("No such workspace file: '%s'", filename)
else:
LOG.debug('Workspace file not specified')
def copy_ansible_inventory(filename=None):
filename = filename or os.environ.get('ANSIBLE_INVENTORY')
if filename:
if os.path.isfile(filename):
destination = execute('ir workspace inventory')
if not os.path.exists(os.path.basename(destination)):
os.makedirs(os.path.basename(destination))
execute('cp {} {}', filename, destination)
else:
LOG.debug('Inventary file not found: %r', filename)
else:
LOG.debug('Ansible inventory file not specified')
def normalize_path(path):
return os.path.realpath(os.path.expanduser(path))
def execute(command, *args, **kwargs):
if args or kwargs:
command = command.format(*args, **kwargs)
LOG.info('%s', command)
return subprocess.check_output(command, shell=True,
universal_newlines=True)
def name_from_path(path):
return os.path.splitext(os.path.basename(path))[0]
if __name__ == '__main__':
LOG = logging.getLogger(name_from_path(__file__))
main()