• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2Copyright (c) 2022 iSoftStone Device Co., Ltd.
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7   http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14"""
15import os
16import patch
17import sys
18from subprocess import Popen, PIPE
19
20def clean_code(patch_set):
21  print("----------- clean code ----------")
22  for git_path in patch_set.keys():
23    print(git_path)
24    p = Popen("git restore .", shell=True, stdout=PIPE, cwd=git_path)
25    p.wait()
26    p = Popen("git clean -fd", shell=True, stdout=PIPE, cwd=git_path)
27    p.wait()
28
29def patch():
30  print("----------patch code to harmony--------")
31  patch_set = get_file_set(patch_path)
32  if not patch_set:
33    print("---- [fasle] no patch to do ----")
34    sys.exit(1)
35  #检查每个补丁路径是否正确
36  if not check_gits_exit(patch_set.keys()):
37    return False
38  clean_code(patch_set)
39  patch_code_to_harmony(patch_set)
40  print("---- [success] patch code to harmony -----")
41
42def patch_code_to_harmony(patch_set):
43  for git_path in patch_set.keys():
44    if not patch_set[git_path]:
45      continue
46    patch_path = patch_set[git_path]
47    p = Popen(f"git apply {patch_path}/*.patch ",shell=True, stdout=PIPE, cwd=git_path)
48    p.wait()
49
50def push_source():
51  print("----------push source to harmony-------")
52  source_set = get_file_set(code_source_path)
53  if not source_set:
54    print("---- [false] no resource ----")
55    return False
56  push_source_to_harmony(source_set)
57
58def push_source_to_harmony(source_set):
59  for git_path in source_set.keys():
60    if not source_set[git_path]:
61      continue
62    source_path = source_set[git_path]
63    p = Popen(f"cp -a {source_path}/* {git_path}/", shell=True, stdout=PIPE)
64    p.wait()
65    print("---- [success] push source to harmony ----")
66
67def check_gits_exit(git_paths: list):
68  for path in git_paths:
69    git_path = os.path.join(path, ".git")
70    if not os.path.isdir(git_path):
71      print(f"---- [false] not found {git_path} ----")
72      return False
73  return True
74
75def get_file_set(path):
76  file_set = {}
77  if not os.path.isdir(path):
78    print(f"not fount {path}, please check")
79    sys.exit(1)
80  git_dir_list = []
81  for root, dirs, files in os.walk(path):
82    for dir in dirs:
83      dir_path = os.path.join(root, dir)
84      if os.path.isdir(dir_path):
85        git_path = os.path.join(root_path, str(dir).replace("-", "/"))
86        file_set[git_path] = dir_path
87    break
88  return file_set
89
90def copy_new_file(src_path, dest_path):
91  if not os.path.isdir(dest_path):
92    print(f"[error] not found {dest_path}, please cheak!")
93    return false
94  if not os.path.exists(src_path):
95    print(f"[error] not fount {src_path}, please check!")
96    return false
97  p = Popen(f"cp -a {src_path} {dest_path}/", shell=True, stdout=PIPE)
98  p.wait()
99
100
101if __name__ == "__main__":
102
103  support_platforms = ["t507", "r818"]
104  curent_path = os.getcwd()
105  root_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(curent_path))))))
106  print(root_path)
107  allwinner_path = os.path.join(root_path, "device", "soc", 'allwinner')
108
109
110  if len(sys.argv) <= 1:
111    print("[error] please selete ic only support 'T507' or 'R818' now")
112    print("[sample] 'python3 patch.py R818'")
113    sys.exit(1)
114  current_ic = str(sys.argv[-1]).lower()
115  print(f"ic name is {current_ic}")
116  if current_ic not in support_platforms:
117    print(f"[error] {current_ic} not support")
118    sys.exit(1)
119
120  ic_path = os.path.join(allwinner_path, current_ic)
121  patch_path = os.path.join(ic_path, 'patches', 'harmony')
122
123  file_source_path = os.path.join(ic_path, 'patches', 'file')
124  json_name = current_ic + '.json'
125  patch()
126
127
128
129