-
-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathgenerate_consts.py
108 lines (93 loc) · 3.64 KB
/
generate_consts.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import platform
import subprocess
import sys
CONST_FILE = "curl_cffi/const.py"
CURL_VERSION = sys.argv[1]
uname = platform.uname()
print("extract consts from curl.h")
with open(CONST_FILE, "w") as f:
f.write("# This file is automatically generated, do not modify it directly.\n\n")
f.write("from enum import IntEnum\n\n\n")
f.write("class CurlOpt(IntEnum):\n")
f.write(' """``CULROPT_`` constancs extracted from libcurl,\n')
f.write(' see: https://curl.se/libcurl/c/curl_easy_setopt.html"""\n\n')
cmd = rf"""
echo '#include "{CURL_VERSION}/include/curl/curl.h"' | gcc -E - | grep -i "CURLOPT_.\+ =" | sed "s/ CURLOPT_/ /g" | sed "s/,//g"
""" # noqa E501
output = subprocess.check_output(cmd, shell=True)
f.write(output.decode())
f.write(
"""
if locals().get("WRITEDATA"):
FILE = locals().get("WRITEDATA")
if locals().get("READDATA"):
INFILE = locals().get("READDATA")
if locals().get("HEADERDATA"):
WRITEHEADER = locals().get("HEADERDATA")\n\n
"""
)
f.write("class CurlInfo(IntEnum):\n")
f.write(' """``CURLINFO_`` constancs extracted from libcurl,\n')
f.write(' see: https://curl.se/libcurl/c/curl_easy_getinfo.html"""\n\n')
cmd = rf"""
echo '#include "{CURL_VERSION}/include/curl/curl.h"' | gcc -E - | grep -i "CURLINFO_.\+ =" | sed "s/ CURLINFO_/ /g" | sed "s/,//g"
""" # noqa E501
output = subprocess.check_output(cmd, shell=True)
f.write(output.decode())
f.write(
"""
if locals().get("RESPONSE_CODE"):
HTTP_CODE = locals().get("RESPONSE_CODE")\n\n
"""
)
f.write("class CurlMOpt(IntEnum):\n")
f.write(' """``CURLMOPT_`` constancs extracted from libcurl,\n')
f.write(' see: https://curl.se/libcurl/c/curl_multi_setopt.html"""\n\n')
cmd = rf"""
echo '#include "{CURL_VERSION}/include/curl/curl.h"' | gcc -E - | grep -i "CURLMOPT_.\+ =" | sed "s/ CURLMOPT_/ /g" | sed "s/,//g"
""" # noqa E501
output = subprocess.check_output(cmd, shell=True)
f.write(output.decode())
f.write("\n\n")
f.write("class CurlECode(IntEnum):\n")
f.write(' """``CURLECODE_`` constancs extracted from libcurl,\n')
f.write(' see: https://curl.se/libcurl/c/libcurl-errors.html"""\n\n')
cmd = rf"""
echo '#include "{CURL_VERSION}/include/curl/curl.h"' | gcc -E - | grep -i CURLE_ | sed "s/[, ][=0]*//g" | sed "s/CURLE_/ /g" | awk '{{print $0 " = " NR-1}}'
""" # noqa E501
output = subprocess.check_output(cmd, shell=True)
f.write(output.decode())
f.write("\n")
# These lines are not easy to be extracted automatically
f.write(
'''
class CurlHttpVersion(IntEnum):
"""``CURL_HTTP_VERSION`` constants extracted from libcurl, see comments for details."""
NONE = 0
V1_0 = 1 # please use HTTP 1.0 in the request */
V1_1 = 2 # please use HTTP 1.1 in the request */
V2_0 = 3 # please use HTTP 2 in the request */
V2TLS = 4 # use version 2 for HTTPS, version 1.1 for HTTP */
V2_PRIOR_KNOWLEDGE = 5 # please use HTTP 2 without HTTP/1.1 Upgrade */
V3 = 30 # Makes use of explicit HTTP/3 without fallback.
class CurlWsFlag(IntEnum):
"""``CURL_WS_FLAG`` constants extracted from libcurl, see comments for details."""
TEXT = 1 << 0
BINARY = 1 << 1
CONT = 1 << 2
CLOSE = 1 << 3
PING = 1 << 4
OFFSET = 1 << 5
class CurlSslVersion(IntEnum):
"""``CURL_SSLVERSION`` constants extracted from libcurl, see comments for details."""
DEFAULT = 0
TLSv1 = 1
SSLv2 = 2
SSLv3 = 3
TLSv1_0 = 4
TLSv1_1 = 5
TLSv1_2 = 6
TLSv1_3 = 7
MAX_DEFAULT = 1 << 16
'''
)