gen_package.py 4.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/usr/bin/env python
#
# Copyright 2013 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
""" Genrate a Fuchsia FAR Archive from an asset manifest and a signing key.
"""

import argparse
import collections
import json
import os
import subprocess
import sys

16 17
from gather_flutter_runner_artifacts import CreateMetaPackage

18

19 20 21 22 23 24 25 26
# Generates the manifest and returns the file.
def GenerateManifest(package_dir):
  full_paths = []
  for root, dirs, files in os.walk(package_dir):
    for f in files:
      common_prefix = os.path.commonprefix([root, package_dir])
      rel_path = os.path.relpath(os.path.join(root, f), common_prefix)
      from_package = os.path.abspath(os.path.join(package_dir, rel_path))
27 28
      assert from_package, 'Failed to create from_package for %s' % os.path.join(
          root, f)
29 30 31 32 33 34 35 36 37 38
      full_paths.append('%s=%s' % (rel_path, from_package))
  parent_dir = os.path.abspath(os.path.join(package_dir, os.pardir))
  manifest_file_name = os.path.basename(package_dir) + '.manifest'
  manifest_path = os.path.join(parent_dir, manifest_file_name)
  with open(manifest_path, 'w') as f:
    for item in full_paths:
      f.write("%s\n" % item)
  return manifest_path


39
def main():
40
  parser = argparse.ArgumentParser()
41 42

  parser.add_argument('--pm-bin', dest='pm_bin', action='store', required=True)
43 44 45 46 47
  parser.add_argument(
      '--package-dir', dest='package_dir', action='store', required=True)
  parser.add_argument(
      '--signing-key', dest='signing_key', action='store', required=True)
  parser.add_argument(
48 49 50
      '--manifest-file', dest='manifest_file', action='store', required=False)
  parser.add_argument(
      '--far-name', dest='far_name', action='store', required=False)
51 52 53 54 55 56

  args = parser.parse_args()

  assert os.path.exists(args.pm_bin)
  assert os.path.exists(args.package_dir)
  assert os.path.exists(args.signing_key)
57 58 59

  pkg_dir = args.package_dir
  if not os.path.exists(os.path.join(pkg_dir, 'meta', 'package')):
60
    CreateMetaPackage(pkg_dir, args.far_name)
61

62 63 64 65
  output_dir = os.path.abspath(pkg_dir + '_out')
  if not os.path.exists(output_dir):
    os.makedirs(output_dir)

66 67 68 69 70 71
  manifest_file = None
  if args.manifest_file is not None:
    assert os.path.exists(args.manifest_file)
    manifest_file = args.manifest_file
  else:
    manifest_file = GenerateManifest(args.package_dir)
72

73
  strace_out = os.path.join(output_dir, 'strace_out')
74

75
  pm_command_base = [
76 77 78 79
      'strace',
      '-f',
      '-o',
      strace_out,
80 81
      args.pm_bin,
      '-o',
82
      output_dir,
83 84 85
      '-k',
      args.signing_key,
      '-m',
86
      manifest_file,
87 88
  ]

D
Dan Field 已提交
89 90
  # Build and then archive the package
  # Use check_output so if anything goes wrong we get the output.
91
  try:
92 93
    pm_commands = [
        ['build'],
94 95 96 97
        [
            'archive', '--output=' +
            os.path.join(os.path.dirname(output_dir), args.far_name + "-0")
        ],
98 99 100
    ]
    for pm_command in pm_commands:
      pm_command_args = pm_command_base + pm_command
101 102
      sys.stderr.write("===== Running %s\n" % pm_command_args)
      subprocess.check_output(pm_command_args)
103
  except subprocess.CalledProcessError as e:
104 105 106
    print(
        '==================== Manifest contents ========================================='
    )
D
Dan Field 已提交
107
    with open(manifest_file, 'r') as manifest:
108
      sys.stdout.write(manifest.read())
109 110 111
    print(
        '==================== End manifest contents ====================================='
    )
112 113
    meta_contents_path = os.path.join(output_dir, 'meta', 'contents')
    if os.path.exists(meta_contents_path):
114 115 116
      print(
          '==================== meta/contents ============================================='
      )
117 118
      with open(meta_contents_path, 'r') as meta_contents:
        sys.stdout.write(meta_contents.read())
119 120 121 122 123 124
      print(
          '==================== End meta/contents ========================================='
      )
    print(
        '==================== Strace output ============================================='
    )
125 126
    with open(strace_out, 'r') as strace:
      sys.stdout.write(strace.read())
127 128 129
    print(
        '==================== End strace output ========================================='
    )
130
    raise
131 132 133

  return 0

134

135 136
if __name__ == '__main__':
  sys.exit(main())