summaryrefslogtreecommitdiffstats
path: root/python/mozbuild/mozbuild/action/file_generate_wrapper.py
blob: b6c030bbf6ae4e8a03a292f24fe6c288971db4dc (plain)
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import json
import os
import subprocess
import sys
from pathlib import Path

import buildconfig


def action(fh, script, target_dir, *args):
    fh.close()
    os.unlink(fh.name)

    args = list(args)
    objdir = Path.cwd()
    topsrcdir = Path(buildconfig.topsrcdir)

    def make_absolute(base_path, p):
        return Path(base_path) / Path(p.lstrip("/"))

    try:
        abs_target_dir = str(make_absolute(objdir, target_dir))
        abs_script = make_absolute(topsrcdir, script)
        script = [str(abs_script)]
        if abs_script.suffix == ".py":
            script = [sys.executable] + script
        subprocess.check_call(script + args, cwd=abs_target_dir)
    except Exception:
        relative = os.path.relpath(__file__, topsrcdir)
        print(
            "%s:action caught exception. params=%s\n"
            % (relative, json.dumps([script, target_dir] + args, indent=2))
        )
        raise