summaryrefslogtreecommitdiff
path: root/util/meson_aux/skeletonmm-tarball.py
blob: 576b522c6530bde4f82e0ae9d0240e07ac7495ea (plain)
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
#!/usr/bin/env python3

# External command, intended to be called with run_command() or custom_target()
# in meson.build

#                              argv[1]           argv[2]       argv[3:]
# skeletonmm-tarball.py <output_file_or_check> <source_dir> <input_files...>

import os
import sys
import shutil
import tarfile

if sys.argv[1] == 'check':
  # Called from run_command() during setup or configuration.
  # Check which archive format can be used.
  # In order from most wanted to least wanted: .tar.xz, .tar.gz, .tar
  available_archive_formats = []
  for af in shutil.get_archive_formats():
    # Keep the formats in a list, skip the descriptions.
    available_archive_formats += [af[0]]
  if 'xztar' in available_archive_formats:
    suffix = '.tar.xz'
  elif 'gztar' in available_archive_formats:
    suffix = '.tar.gz'
  else: # Uncompressed tar format is always available.
    suffix = '.tar'
  print(suffix, end='') # stdout can be read in the meson.build file.
  sys.exit(0)

# Create an archive.
output_file = sys.argv[1]
source_dir = sys.argv[2]

if output_file.endswith('.xz'):
  mode = 'w:xz'
elif output_file.endswith('.gz'):
  mode = 'w:gz'
else:
  mode = 'w'

tar_file = tarfile.open(output_file, mode=mode)
os.chdir(source_dir) # Input filenames are relative to source_dir.
for file in sys.argv[3:]:
  tar_file.add(file)
tar_file.close()
# Errors raise exceptions. If an exception is raised, Meson+ninja will notice
# that the command failed, despite exit(0).
sys.exit(0)

# shutil.make_archive() might be an alternative, but it only archives
# whole directories. It's not useful, if you want to have full control
# of which files are archived.