File: build.py

package info (click to toggle)
httpie 3.2.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,904 kB
  • sloc: python: 13,760; xml: 162; makefile: 141; ruby: 79; sh: 32
file content (109 lines) | stat: -rw-r--r-- 2,767 bytes parent folder | download | duplicates (2)
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
100
101
102
103
104
105
106
107
108
109
import stat
import subprocess
from pathlib import Path
from typing import Iterator, Tuple

BUILD_DIR = Path(__file__).parent
HTTPIE_DIR = BUILD_DIR.parent.parent.parent

EXTRAS_DIR = HTTPIE_DIR / 'extras'
MAN_PAGES_DIR = EXTRAS_DIR /  'man'

SCRIPT_DIR = BUILD_DIR / Path('scripts')
HOOKS_DIR = SCRIPT_DIR / 'hooks'

DIST_DIR = BUILD_DIR / 'dist'

TARGET_SCRIPTS = {
    SCRIPT_DIR / 'http_cli.py': [],
    SCRIPT_DIR / 'httpie_cli.py': ['--hidden-import=pip'],
}


def build_binaries() -> Iterator[Tuple[str, Path]]:
    for target_script, extra_args in TARGET_SCRIPTS.items():
        subprocess.check_call(
            [
                'pyinstaller',
                '--onefile',
                '--noupx',
                '-p',
                HTTPIE_DIR,
                '--additional-hooks-dir',
                HOOKS_DIR,
                *extra_args,
                target_script,
            ]
        )

    for executable_path in DIST_DIR.iterdir():
        if executable_path.suffix:
            continue
        stat_r = executable_path.stat()
        executable_path.chmod(stat_r.st_mode | stat.S_IEXEC)
        yield executable_path.stem, executable_path


def build_packages(http_binary: Path, httpie_binary: Path) -> None:
    import httpie

    # Mapping of src_file -> dst_file
    files = [
        (http_binary, '/usr/bin/http'),
        (http_binary, '/usr/bin/https'),
        (httpie_binary, '/usr/bin/httpie'),
    ]
    files.extend(
        (man_page, f'/usr/share/man/man1/{man_page.name}')
        for man_page in MAN_PAGES_DIR.glob('*.1')
    )

    # A list of additional dependencies
    deps = [
        'python3 >= 3.7',
        'python3-pip'
    ]

    processed_deps = [
        f'--depends={dep}'
        for dep in deps
    ]
    processed_files = [
        '='.join([str(src.resolve()), dst]) for src, dst in files
    ]
    for target in ['deb', 'rpm']:
        subprocess.check_call(
            [
                'fpm',
                '--force',
                '-s',
                'dir',
                '-t',
                target,
                '--name',
                'httpie',
                '--version',
                httpie.__version__,
                '--description',
                httpie.__doc__.strip(),
                '--license',
                httpie.__licence__,
                *processed_deps,
                *processed_files,
            ],
            cwd=DIST_DIR,
        )


def main():
    binaries = dict(build_binaries())
    build_packages(binaries['http_cli'], binaries['httpie_cli'])

    # Rename http_cli/httpie_cli to http/httpie
    binaries['http_cli'].rename(DIST_DIR / 'http')
    binaries['httpie_cli'].rename(DIST_DIR / 'httpie')



if __name__ == '__main__':
    main()