Blame bin/install_megadrivers.py

Packit Service 5cb53b
#! /usr/libexec/platform-python
Packit 8f2243
# encoding=utf-8
Packit 8f2243
# Copyright 2017-2018 Intel Corporation
Packit 8f2243
Packit 8f2243
# Permission is hereby granted, free of charge, to any person obtaining a copy
Packit 8f2243
# of this software and associated documentation files (the "Software"), to deal
Packit 8f2243
# in the Software without restriction, including without limitation the rights
Packit 8f2243
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Packit 8f2243
# copies of the Software, and to permit persons to whom the Software is
Packit 8f2243
# furnished to do so, subject to the following conditions:
Packit 8f2243
Packit 8f2243
# The above copyright notice and this permission notice shall be included in
Packit 8f2243
# all copies or substantial portions of the Software.
Packit 8f2243
Packit 8f2243
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit 8f2243
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit 8f2243
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit 8f2243
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit 8f2243
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Packit 8f2243
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit 8f2243
# SOFTWARE.
Packit 8f2243
Packit 8f2243
"""Script to install megadriver symlinks for meson."""
Packit 8f2243
Packit 8f2243
from __future__ import print_function
Packit 8f2243
import argparse
Packit 8f2243
import os
Packit 8f2243
Packit 8f2243
Packit 8f2243
def main():
Packit 8f2243
    parser = argparse.ArgumentParser()
Packit 8f2243
    parser.add_argument('megadriver')
Packit 8f2243
    parser.add_argument('libdir')
Packit 8f2243
    parser.add_argument('drivers', nargs='+')
Packit 8f2243
    args = parser.parse_args()
Packit 8f2243
Packit 8f2243
    if os.path.isabs(args.libdir):
Packit 8f2243
        destdir = os.environ.get('DESTDIR')
Packit 8f2243
        if destdir:
Packit 8f2243
            to = os.path.join(destdir, args.libdir[1:])
Packit 8f2243
        else:
Packit 8f2243
            to = args.libdir
Packit 8f2243
    else:
Packit 8f2243
        to = os.path.join(os.environ['MESON_INSTALL_DESTDIR_PREFIX'], args.libdir)
Packit 8f2243
Packit 8f2243
    master = os.path.join(to, os.path.basename(args.megadriver))
Packit 8f2243
Packit 8f2243
    if not os.path.exists(to):
Packit 8f2243
        if os.path.lexists(to):
Packit 8f2243
            os.unlink(to)
Packit 8f2243
        os.makedirs(to)
Packit 8f2243
Packit 8f2243
    for driver in args.drivers:
Packit 8f2243
        abs_driver = os.path.join(to, driver)
Packit 8f2243
Packit 8f2243
        if os.path.lexists(abs_driver):
Packit 8f2243
            os.unlink(abs_driver)
Packit 8f2243
        print('installing {} to {}'.format(args.megadriver, abs_driver))
Packit 8f2243
        os.link(master, abs_driver)
Packit 8f2243
Packit 8f2243
        try:
Packit 8f2243
            ret = os.getcwd()
Packit 8f2243
            os.chdir(to)
Packit 8f2243
Packit 8f2243
            name, ext = os.path.splitext(driver)
Packit 8f2243
            while ext != '.so':
Packit 8f2243
                if os.path.lexists(name):
Packit 8f2243
                    os.unlink(name)
Packit 8f2243
                os.symlink(driver, name)
Packit 8f2243
                name, ext = os.path.splitext(name)
Packit 8f2243
        finally:
Packit 8f2243
            os.chdir(ret)
Packit 8f2243
Packit 8f2243
    # Remove meson-created master .so and symlinks
Packit 8f2243
    os.unlink(master)
Packit 8f2243
    name, ext = os.path.splitext(master)
Packit 8f2243
    while ext != '.so':
Packit 8f2243
        if os.path.lexists(name):
Packit 8f2243
            os.unlink(name)
Packit 8f2243
        name, ext = os.path.splitext(name)
Packit 8f2243
Packit 8f2243
Packit 8f2243
if __name__ == '__main__':
Packit 8f2243
    main()