Rizin
unix-like reverse engineering framework and cli tools
check_meson_subproject.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # SPDX-FileCopyrightText: 2021 ret2libc <sirmy15@gmail.com>
4 # SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
5 # SPDX-License-Identifier: LGPL-3.0-only
6 #
7 # This script is necessary to make sure people notice a subproject has been
8 # changed and need to be updated. Meson does not warn you now (0.56.0)
9 
10 """ Portable python script to check if subproject is up-to-date and warn if not """
11 
12 import filecmp
13 import os
14 import sys
15 
16 subproject = sys.argv[1]
17 meson_root = os.environ["MESON_SOURCE_ROOT"]
18 
19 subproject_filename = os.path.join(meson_root, "subprojects", subproject + ".wrap")
20 
21 try:
22  with open(subproject_filename, "r", encoding="utf8") as f:
23  is_wrap_git = False
24  revision = None
25  directory = subproject
26  patch_directory = subproject
27  for l in f:
28  if "wrap-git" in l:
29  is_wrap_git = True
30  elif "wrap-file" in l:
31  is_wrap_file = True
32  elif l.startswith("revision"):
33  revision = l.split("=")[1].strip()
34  elif l.startswith("directory"):
35  directory = l.split("=")[1].strip()
36  elif l.startswith("patch_directory"):
37  patch_directory = l.split("=")[1].strip()
38 
39  if is_wrap_git:
40  if not revision:
41  sys.exit(0)
42 
43  subproject_dir = os.path.join(meson_root, "subprojects", directory)
44  subproject_git_dir = os.path.join(subproject_dir, ".git")
45  if os.path.isdir(subproject_dir) and os.path.isdir(subproject_git_dir):
46  with open(
47  os.path.join(subproject_git_dir, "HEAD"), "r", encoding="utf8"
48  ) as f:
49  head = f.read().strip()
50  # when using a branch name, head is 'refs/heads/<branch>'
51  if head != revision and revision not in head:
52  sys.exit(1)
53 
54  if not patch_directory:
55  sys.exit(0)
56 
57  subproject_dir = os.path.join(meson_root, "subprojects", directory)
58  patch_subproject_dir = os.path.join(
59  meson_root, "subprojects", "packagefiles", patch_directory
60  )
61  if os.path.isdir(patch_subproject_dir) and os.path.isdir(subproject_dir):
62  for root, dirs, files in os.walk(patch_subproject_dir, topdown=False):
63  for name in files:
64  subproject_f = os.path.join(root, name)
65  subproject_p_f = subproject_f.replace(
66  patch_subproject_dir, subproject_dir
67  )
68  if not os.path.isfile(subproject_f):
69  sys.exit(2)
70 
71  if not filecmp.cmp(subproject_p_f, subproject_f):
72  sys.exit(3)
73 
74  sys.exit(0)
75 except FileNotFoundError:
76  sys.exit(0)