forked from lexiforest/curl_cffi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
89 lines (71 loc) · 2.26 KB
/
build.py
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
import json
import os
import platform
import shutil
import struct
from pathlib import Path
from urllib.request import urlretrieve
from cffi import FFI
# this is the upstream libcurl-impersonate version
__version__ = "0.6.0"
def detect_arch():
with open(Path(__file__).parent.parent / "libs.json") as f:
archs = json.loads(f.read())
uname = platform.uname()
pointer_size = struct.calcsize("P") * 8
for arch in archs:
if (
arch["system"] == uname.system
and arch["machine"] == uname.machine
and arch["pointer_size"] == pointer_size
):
arch["libdir"] = os.path.expanduser(arch["libdir"])
return arch
raise Exception(f"Unsupported arch: {uname}")
arch = detect_arch()
def download_so():
if (Path(arch["libdir"]) / arch["so_name"]).exists():
print(".so files alreay downloaded.")
return
file = "libcurl-impersonate.tar.gz"
url = (
f"https://github.com/yifeikong/curl-impersonate/releases/download/"
f"v{__version__}/libcurl-impersonate-v{__version__}"
f".{arch['so_arch']}-{arch['sysname']}.tar.gz"
)
print(f"Downloading libcurl-impersonate-chrome from {url}...")
urlretrieve(url, file)
print("Unpacking downloaded files...")
os.makedirs(arch["libdir"], exist_ok=True)
shutil.unpack_archive(file, arch["libdir"])
if arch["system"] == "Windows":
shutil.copy2(f"{arch['libdir']}/libcurl.dll", "curl_cffi")
ffibuilder = FFI()
system = platform.system()
root_dir = Path(__file__).parent.parent
download_so()
ffibuilder.set_source(
"curl_cffi._wrapper",
"""
#include "shim.h"
""",
# FIXME from `curl-impersonate`
libraries=["curl-impersonate-chrome"] if system != "Windows" else ["libcurl"],
library_dirs=[arch["libdir"]],
source_extension=".c",
include_dirs=[
str(root_dir / "include"),
str(root_dir / "ffi"),
],
sources=[
str(root_dir / "ffi/shim.c"),
],
extra_compile_args=(
["-Wno-implicit-function-declaration"] if system == "Darwin" else []
),
)
with open(root_dir / "ffi/cdef.c") as f:
cdef_content = f.read()
ffibuilder.cdef(cdef_content)
if __name__ == "__main__":
ffibuilder.compile(verbose=False)