dist.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # Copyright 2017 The Meson development team
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. import lzma
  12. import os
  13. import shutil
  14. import subprocess
  15. import pickle
  16. import hashlib
  17. import tarfile, zipfile
  18. import tempfile
  19. from glob import glob
  20. from mesonbuild.environment import detect_ninja
  21. from mesonbuild.mesonlib import windows_proof_rmtree
  22. def create_hash(fname):
  23. hashname = fname + '.sha256sum'
  24. m = hashlib.sha256()
  25. m.update(open(fname, 'rb').read())
  26. with open(hashname, 'w') as f:
  27. f.write('%s %s\n' % (m.hexdigest(), os.path.basename(fname)))
  28. def create_zip(zipfilename, packaging_dir):
  29. prefix = os.path.dirname(packaging_dir)
  30. removelen = len(prefix) + 1
  31. with zipfile.ZipFile(zipfilename,
  32. 'w',
  33. compression=zipfile.ZIP_DEFLATED,
  34. allowZip64=True) as zf:
  35. zf.write(packaging_dir, packaging_dir[removelen:])
  36. for root, dirs, files in os.walk(packaging_dir):
  37. for d in dirs:
  38. dname = os.path.join(root, d)
  39. zf.write(dname, dname[removelen:])
  40. for f in files:
  41. fname = os.path.join(root, f)
  42. zf.write(fname, fname[removelen:])
  43. def del_gitfiles(dirname):
  44. for f in glob(os.path.join(dirname, '.git*')):
  45. if os.path.isdir(f) and not os.path.islink(f):
  46. windows_proof_rmtree(f)
  47. else:
  48. os.unlink(f)
  49. def process_submodules(dirname):
  50. module_file = os.path.join(dirname, '.gitmodules')
  51. if not os.path.exists(module_file):
  52. return
  53. subprocess.check_call(['git', 'submodule', 'update', '--init'], cwd=dirname)
  54. for line in open(module_file):
  55. line = line.strip()
  56. if '=' not in line:
  57. continue
  58. k, v = line.split('=', 1)
  59. k = k.strip()
  60. v = v.strip()
  61. if k != 'path':
  62. continue
  63. del_gitfiles(os.path.join(dirname, v))
  64. def create_dist_git(dist_name, src_root, bld_root, dist_sub):
  65. distdir = os.path.join(dist_sub, dist_name)
  66. if os.path.exists(distdir):
  67. shutil.rmtree(distdir)
  68. os.makedirs(distdir)
  69. subprocess.check_call(['git', 'clone', '--shared', src_root, distdir])
  70. process_submodules(distdir)
  71. del_gitfiles(distdir)
  72. xzname = distdir + '.tar.xz'
  73. # Should use shutil but it got xz support only in 3.5.
  74. with tarfile.open(xzname, 'w:xz') as tf:
  75. tf.add(distdir, dist_name)
  76. # Create only .tar.xz for now.
  77. # zipname = distdir + '.zip'
  78. # create_zip(zipname, distdir)
  79. shutil.rmtree(distdir)
  80. return (xzname, )
  81. def create_dist_hg(dist_name, src_root, bld_root, dist_sub):
  82. os.makedirs(dist_sub, exist_ok=True)
  83. tarname = os.path.join(dist_sub, dist_name + '.tar')
  84. xzname = tarname + '.xz'
  85. subprocess.check_call(['hg', 'archive', '-R', src_root, '-S', '-t', 'tar', tarname])
  86. with lzma.open(xzname, 'wb') as xf, open(tarname, 'rb') as tf:
  87. shutil.copyfileobj(tf, xf)
  88. os.unlink(tarname)
  89. # Create only .tar.xz for now.
  90. # zipname = os.path.join(dist_sub, dist_name + '.zip')
  91. # subprocess.check_call(['hg', 'archive', '-R', src_root, '-S', '-t', 'zip', zipname])
  92. return (xzname, )
  93. def check_dist(packagename, meson_command):
  94. print('Testing distribution package %s.' % packagename)
  95. unpackdir = tempfile.mkdtemp()
  96. builddir = tempfile.mkdtemp()
  97. installdir = tempfile.mkdtemp()
  98. ninja_bin = detect_ninja()
  99. try:
  100. tf = tarfile.open(packagename)
  101. tf.extractall(unpackdir)
  102. srcdir = glob(os.path.join(unpackdir, '*'))[0]
  103. if subprocess.call(meson_command + ['--backend=ninja', srcdir, builddir]) != 0:
  104. print('Running Meson on distribution package failed')
  105. return 1
  106. if subprocess.call([ninja_bin], cwd=builddir) != 0:
  107. print('Compiling the distribution package failed.')
  108. return 1
  109. if subprocess.call([ninja_bin, 'test'], cwd=builddir) != 0:
  110. print('Running unit tests on the distribution package failed.')
  111. return 1
  112. myenv = os.environ.copy()
  113. myenv['DESTDIR'] = installdir
  114. if subprocess.call([ninja_bin, 'install'], cwd=builddir, env=myenv) != 0:
  115. print('Installing the distribution package failed.')
  116. return 1
  117. finally:
  118. shutil.rmtree(unpackdir)
  119. shutil.rmtree(builddir)
  120. shutil.rmtree(installdir)
  121. print('Distribution package %s tested.' % packagename)
  122. return 0
  123. def run(args):
  124. src_root = args[0]
  125. bld_root = args[1]
  126. meson_command = args[2:]
  127. priv_dir = os.path.join(bld_root, 'meson-private')
  128. dist_sub = os.path.join(bld_root, 'meson-dist')
  129. buildfile = os.path.join(priv_dir, 'build.dat')
  130. build = pickle.load(open(buildfile, 'rb'))
  131. dist_name = build.project_name + '-' + build.project_version
  132. if os.path.isdir(os.path.join(src_root, '.git')):
  133. names = create_dist_git(dist_name, src_root, bld_root, dist_sub)
  134. elif os.path.isdir(os.path.join(src_root, '.hg')):
  135. names = create_dist_hg(dist_name, src_root, bld_root, dist_sub)
  136. else:
  137. print('Dist currently only works with Git or Mercurial repos.')
  138. return 1
  139. if names is None:
  140. return 1
  141. error_count = 0
  142. for name in names:
  143. rc = check_dist(name, meson_command) # Check only one.
  144. if rc == 0:
  145. create_hash(name)
  146. error_count += rc
  147. return 1 if error_count else 0