/
apply_patches.py
155 lines (137 loc) · 4.95 KB
/
apply_patches.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
if sys.version_info.major < 3 or sys.version_info.minor < 6:
raise RuntimeError('Python 3.6+ is required for this script. You have: {}.{}'.format(
sys.version_info.major, sys.version_info.minor))
import argparse
import os
import re
import shutil
import subprocess
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent / 'core' / 'utils'))
import downloads
import domain_substitution
import prune_binaries
import patches
import save_original_files
import var_replace
import patch_made_files_remover
import set_version
from distutils.dir_util import copy_tree
from _common import ENCODING, USE_REGISTRY, ExtractorEnum, get_logger
sys.path.pop(0)
_ROOT_DIR = Path(__file__).resolve().parent
_PATCH_BIN_RELPATH = Path('third_party/git/usr/bin/patch.exe')
# Set common variables
source_tree = _ROOT_DIR / 'build' / 'src'
core_dir = _ROOT_DIR / 'core'
downloads_cache = _ROOT_DIR / 'build' / 'downloads_cache'
domsubcache = _ROOT_DIR / 'build' / 'domsubcache.tar.gz'
urlsubcache = _ROOT_DIR / 'build' / 'urlsubcache.tar.gz'
exesubcache = _ROOT_DIR / 'build' / 'exesubcache.tar.gz'
saved_patches = _ROOT_DIR / 'build' / 'patches'
temp_patches = _ROOT_DIR / 'build' /'temp_patches'
"""CLI Entrypoint"""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'--disable-ssl-verification',
action='store_true',
help='Disables SSL verification for downloading')
parser.add_argument(
'--7z-path',
dest='sevenz_path',
default=USE_REGISTRY,
help=('Command or path to 7-Zip\'s "7z" binary. If "_use_registry" is '
'specified, determine the path from the registry. Default: %(default)s'))
parser.add_argument(
'--winrar-path',
dest='winrar_path',
default=USE_REGISTRY,
help=('Command or path to WinRAR\'s "winrar.exe" binary. If "_use_registry" is '
'specified, determine the path from the registry. Default: %(default)s'))
args = parser.parse_args()
def _get_vcvars_path(name='64'):
"""
Returns the path to the corresponding vcvars*.bat path
As of VS 2017, name can be one of: 32, 64, all, amd64_x86, x86_amd64
"""
vswhere_exe = '%ProgramFiles(x86)%\\Microsoft Visual Studio\\Installer\\vswhere.exe'
result = subprocess.run(
'"{}" -latest -property installationPath'.format(vswhere_exe),
shell=True,
check=True,
stdout=subprocess.PIPE,
universal_newlines=True)
vcvars_path = Path(result.stdout.strip(), 'VC/Auxiliary/Build/vcvars{}.bat'.format(name))
if not vcvars_path.exists():
raise RuntimeError(
'Could not find vcvars batch script in expected location: {}'.format(vcvars_path))
return vcvars_path
def _run_build_process(*args, **kwargs):
"""
Runs the subprocess with the correct environment variables for building
"""
# Add call to set VC variables
cmd_input = ['call "%s" >nul' % _get_vcvars_path()]
cmd_input.append('set DEPOT_TOOLS_WIN_TOOLCHAIN=0')
cmd_input.append(' '.join(map('"{}"'.format, args)))
cmd_input.append('exit\n')
subprocess.run(('cmd.exe', '/k'),
input='\n'.join(cmd_input),
check=True,
encoding=ENCODING,
**kwargs)
def _copy_files(src, dst = source_tree):
if os.path.isdir(src):
copy_tree(str(src), str(dst))
else:
print(src + " doesnt exists")
def _make_tmp_paths():
"""Creates TMP and TEMP variable dirs so ninja won't fail"""
tmp_path = Path(os.environ['TMP'])
if not tmp_path.exists():
tmp_path.mkdir()
tmp_path = Path(os.environ['TEMP'])
if not tmp_path.exists():
tmp_path.mkdir()
def main():
# Setup environment
source_tree.mkdir(parents=True, exist_ok=True)
downloads_cache.mkdir(parents=True, exist_ok=True)
_make_tmp_paths()
series_files_list = [
_ROOT_DIR / 'core' / 'patches',
_ROOT_DIR / 'patches',
_ROOT_DIR / 'core' / 'patches' / 'core' / 'breeze'
]
patches.combine_patches(
series_files_list,
temp_patches
)
var_replace.main()
if saved_patches.exists():
patches.compare_new_patches(
saved_patches,
temp_patches,
source_tree,
patch_bin_path=(source_tree / _PATCH_BIN_RELPATH)
)
else:
print('No previous patches found\n')
print('Applying all!\n')
patch_made_files_remover.main()
_copy_files("build/unpatched")
save_original_files.main()
# apply patches
patches.apply_patches(
patches.generate_patches_from_series(temp_patches, resolve=True),
source_tree,
patch_bin_path=(source_tree / _PATCH_BIN_RELPATH)
)
_copy_files(src = temp_patches, dst = saved_patches)
shutil.rmtree(temp_patches)
set_version.update_version(source_tree, core_dir / 'breeze_version.txt')
if __name__ == '__main__':
main()