File: tasks.py

package info (click to toggle)
python-libzim 2.1.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,660 kB
  • sloc: python: 1,193; cpp: 394; makefile: 5
file content (131 lines) | stat: -rw-r--r-- 3,655 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3


"""
A description file for invoke (https://www.pyinvoke.org/)
"""

import inspect
import pathlib
import platform
import re
import urllib.request

# temp local fix for https://github.com/pyinvoke/invoke/issues/891
if not hasattr(inspect, "getargspec"):
    inspect.getargspec = inspect.getfullargspec

from invoke import task


@task
def download_libzim(c, version="8.1.0"):
    """download C++ libzim binary"""

    if platform.machine() != "x86_64" or platform.system() not in ("Linux", "Darwin"):
        raise NotImplementedError(f"Platform {platform.platform()} not supported")

    is_nightly = re.match(r"^\d{4}-\d{2}-\d{2}$", version)

    if not is_nightly and not re.match(r"^\d\.\d\.\d$", version):
        raise ValueError(
            f"Unrecognised version {version}. "
            "Must be either a x.x.x release or a Y-M-D date to use a nightly"
        )

    fname = pathlib.Path(
        "libzim_{os}-x86_64-{version}.tar.gz".format(
            os={"Linux": "linux", "Darwin": "macos"}.get(platform.system()),
            version=version,
        )
    )
    url = (
        f"https://download.openzim.org/nightly/{version}/{fname.name}"
        if is_nightly
        else f"https://download.openzim.org/release/libzim/{fname.name}"
    )
    print("Downloading from", url)

    with urllib.request.urlopen(url) as response, open(fname, "wb") as fh:  # nosec
        fh.write(response.read())

    # extract and remove archive
    c.run(f"tar -xvf {fname.name}")
    c.run(f"rm -vf {fname.name}")

    # remove previous install from target directories
    c.run("find ./include -not -name 'README.md' -not -name 'include' | xargs rm -rf")
    c.run("find ./lib -not -name 'README.md' -not -name 'lib' | xargs rm -rf")

    # move extracted bins and headers to expected places
    dname = fname.with_suffix("").stem
    c.run(f"mv -v {dname}/include/* ./include/")
    if platform.system() == "Linux":
        c.run(f"mv -v {dname}/lib/x86_64-linux-gnu/* ./lib/")
        c.run(f"rmdir {dname}/lib/x86_64-linux-gnu")
    else:
        c.run(f"mv -v {dname}/lib/* ./lib/")
    # remove extracted folder (should be empty)
    c.run(f"rmdir {dname}/lib {dname}/include/ {dname}")

    # add link for version-less dylib and link from root
    if platform.system() == "Darwin":
        c.run(f"ln -svf ./lib/libzim.{version[0]}.dylib .")
        c.run(f"ln -svf ./libzim.{version[0]}.dylib lib/libzim.dylib")
    else:
        c.run(f"ln -svf ./lib/libzim.so.{version[0]} .")
        c.run(f"ln -svf ./libzim.so.{version[0]} lib/libzim.so")


@task
def build_ext(c):
    c.run("PROFILE=1 python setup.py build_ext -i")


@task
def test(c):
    c.run("python -m pytest --color=yes --ff -x .")


@task
def coverage(c):
    c.run(
        "python -m pytest --color=yes "
        "--cov=libzim --cov-config=.coveragerc "
        "--cov-report=term --cov-report term-missing ."
    )


@task
def clean(c):
    c.run("rm -rf build")
    c.run("rm *.so")


@task
def install_dev(c):
    c.run("pip install -r requirements-dev.txt")


@task
def check(c):
    c.run("isort --check-only .")
    c.run("black --check .")
    c.run('echo "one pass for show-stopper syntax errors or undefined names"')
    c.run("flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics")
    c.run('echo "one pass for small stylistic things"')
    c.run("flake8 . --count --statistics")


@task
def lint(c):
    c.run("isort .")
    c.run("black .")
    c.run("flake8 .")


if __name__ == "__main__":
    print(
        "This file is not intended to be directly run.\n"
        "Install invoke and run the `invoke` command line."
    )