1#!/usr/bin/env python3
2
3# External command, intended to be called with meson.add_dist_script() in meson.build
4
5#                          argv[1]       argv[2]     argv[3:]
6# dist-build-scripts.py <root_src_dir> <script_dir> <no_dist>...
7
8# <script_dir> The directory with the build scripts, relative to <root_source_dir>.
9# <no_dist> Zero or more names (relative to MESON_DIST_ROOT) of files and
10#           directories that shall not be distributed.
11
12import os
13import sys
14import shutil
15
16dist_root = os.getenv('MESON_DIST_ROOT')
17src_script_dir = os.path.join(sys.argv[1], sys.argv[2])
18dist_script_dir = os.path.join(dist_root, sys.argv[2])
19
20# Create the distribution script directory, if it does not exist.
21os.makedirs(dist_script_dir, exist_ok=True)
22
23# Distribute files that mm-common-get has copied to src_script_dir.
24files = [
25  'check-dllexport-usage.py',
26  'dist-build-scripts.py',
27  'dist-changelog.py',
28  'doc-reference.py',
29  'generate-binding.py'
30]
31for file in files:
32  shutil.copy(os.path.join(src_script_dir, file), dist_script_dir)
33
34# Don't distribute .gitignore files.
35for dirpath, dirnames, filenames in os.walk(dist_root):
36  if '.gitignore' in filenames:
37    os.remove(os.path.join(dirpath, '.gitignore'))
38
39# Remove an empty MESON_DIST_ROOT/build directory.
40dist_build_dir = os.path.join(dist_root, 'build')
41if os.path.isdir(dist_build_dir):
42  try:
43    os.rmdir(dist_build_dir)
44  except OSError:
45    # Ignore the error, if not empty.
46    pass
47
48# Remove specified files and directories from the MESON_DIST_ROOT directory.
49for rel_path in sys.argv[3:]:
50  abs_path = os.path.join(dist_root, rel_path)
51  if os.path.isfile(abs_path):
52    os.remove(abs_path)
53  elif os.path.isdir(abs_path):
54    shutil.rmtree(abs_path, ignore_errors=True)
55  else:
56    # Ignore non-existent files and directories.
57    print('--- Info:', abs_path, 'not found, not removed.')
58