install_vadere_analysis_tool.py 2.63 KB
Newer Older
1
2
3
4
import getpass
import os
import re
import subprocess
5
import shutil
6
7
8
9
10
11
12


def install_package_if_needed(package_name='VadereAnalysisTool', search_path='Tools/VadereAnalysisTool'):
    try:

        make_package_cwd = os.path.abspath(search_path)

13
14
15
        if os.path.exists(os.path.join(search_path, "dist")):
            shutil.rmtree(os.path.join(search_path, "dist"))

16
17
18
19
20
21
22
23
        print("Build package {}...".format(package_name))
        p_make_package = subprocess.run(
            args=["python3", "setup.py",  "bdist_wheel"],
            cwd= make_package_cwd,
            timeout=10,
            check=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
24
25
26
27
28
29
        if p_make_package.stdout:
            print("StdOut: {}".format(p_make_package.stdout.decode('utf8')))
        if p_make_package.stderr:
            print("StdErr: {}".format(p_make_package.stderr.decode('utf8')))
        print("ReturnCode: {}".format(p_make_package.returncode))

30
31
        if p_make_package.returncode == 0:
            stdout = p_make_package.stdout.decode('utf8')
32
33
34
            dist_dir = os.path.join(search_path, "dist")
            wheel_files = [f for f in os.listdir(dist_dir) if f.endswith(".whl")]
            if len(wheel_files) > 0:
35
                dist_path = os.path.join("dist", wheel_files[0])
36
37
                user = getpass.getuser()

38
                print("\nInstall package {} locally for user {} ...".format(dist_path, user))
39
40
41
42
43
44
45
46
                p_install_package = subprocess.run(
                    args=["python3", "-m", "pip", "install", "--user",  dist_path],
                    cwd=make_package_cwd,
                    timeout=10,
                    check=True,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE)

47
48
49
50
51
52
                if p_install_package.stdout:
                    print("StdOut: {}".format(p_install_package.stdout.decode('utf8')))
                if p_install_package.stderr:
                    print("StdErr: {}".format(p_install_package.stderr.decode('utf8')))
                print("ReturnCode: {}".format(p_install_package.returncode))

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
            else:
                exit(1)
        else:
            exit(1)


    except subprocess.TimeoutExpired as exception:
        print("Timeout installing {} from path {}".format(package_name, search_path))

    except subprocess.CalledProcessError as exception:
        print("Error installing {} from path {}\n  err:{}".format(package_name,
                                                                  search_path,
                                                                  exception.stderr))


if __name__ == '__main__':
    install_package_if_needed()