File: meson_install_subdir.py

package info (click to toggle)
gimp 3.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 210,076 kB
  • sloc: ansic: 842,287; lisp: 10,761; python: 10,318; cpp: 7,238; perl: 4,355; sh: 1,043; xml: 963; yacc: 609; lex: 348; javascript: 150; makefile: 43
file content (99 lines) | stat: -rwxr-xr-x 3,090 bytes parent folder | download
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
#!/usr/bin/env python3

import sys, os
from pathlib import Path
import argparse

import json

import shutil


class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

def copytree(src, dst, symlinks=False, ignore=None):
    for item in os.listdir(src):
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if os.path.isdir(s):
            shutil.copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d)

class MesonStatus(metaclass = Singleton):
    def __init__(self):
        self.get_build_dir()
        self.get_meson_info()
        self.get_meson_installed()

    def get_build_dir(self):
        self.buildroot = None

        # Set up by meson.
        cwd = Path(os.environ['MESON_BUILD_ROOT'])

        if (cwd / 'meson-info').exists():
            self.buildroot = cwd

        if self.buildroot is None:
            print('Error: build directory was not detected. Are you in it ?')

    def get_meson_info(self):
        with open(self.buildroot / 'meson-info' / 'meson-info.json') as file:
            self.meson_info = json.load(file)
        self.sourceroot = Path(self.meson_info['directories']['source'])

    def get_meson_installed(self):
        info = self.meson_info['directories']['info']
        inst = self.meson_info['introspection']['information']['installed']['file']

        with open(Path(info) / inst) as file:
            self.installed_files = json.load(file)


def get_files_of_part(part_name):
    files_of_part = {}
    sourcedir = str(MesonStatus().sourceroot / part_name)
    builddir  = str(MesonStatus().buildroot  / part_name)
    for file, target in MesonStatus().installed_files.items():
        if file.startswith(sourcedir + '/') or file.startswith(builddir + '/'):
            files_of_part[file] = target

    return files_of_part

def install_files(files, verbose):
    warnings = []
    for file in sorted(files.keys()):
        target = files[file]
        if verbose: print(file + '  →  ' + target, end='\n')

        if os.path.isdir(file):
            copytree(file, target)
        if os.path.isfile(file):
            try:
                shutil.copy2(file, target)
            except Exception as e:
                warnings += [(file, e)]
    if len(warnings) > 0:
        sys.stderr.write("\n*** WARNING: *** Some file installation failed:\n")
        for (file, e) in warnings:
            sys.stderr.write("- {}: {}\n".format(file, e))

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('subdirs', nargs='+')
    parser.add_argument('--verbose', '-v', action='store_true')
    args = parser.parse_args()

    verbose = args.verbose

    for subdir in args.subdirs:
        files = get_files_of_part(subdir)
        if len(files) == 0:
            print('Error: subdir %s does not contain any installable file' % subdir)
        install_files(files, verbose)