gen_package.py 4.5 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
      assert from_package, 'Failed to create from_package for %s' % os.path.join(root, f)
28 29 30 31 32 33 34 35 36 37
      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


38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
def CreateFarPackage(pm_bin, package_dir, signing_key, dst_dir):
  manifest_path = GenerateManifest(package_dir)

  pm_command_base = [
      pm_bin, '-m', manifest_path, '-k', signing_key, '-o', dst_dir
  ]

  # Build the package
  subprocess.check_output(pm_command_base + ['build'])

  # Archive the package
  subprocess.check_output(pm_command_base + ['archive'])

  return 0


54
def main():
55
  parser = argparse.ArgumentParser()
56 57

  parser.add_argument('--pm-bin', dest='pm_bin', action='store', required=True)
58 59 60 61 62
  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(
63 64 65
      '--manifest-file', dest='manifest_file', action='store', required=False)
  parser.add_argument(
      '--far-name', dest='far_name', action='store', required=False)
66 67 68 69 70 71

  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)
72 73 74

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

77 78 79 80
  output_dir = os.path.abspath(pkg_dir + '_out')
  if not os.path.exists(output_dir):
    os.makedirs(output_dir)

81 82 83 84 85 86
  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)
87

88
  strace_out = os.path.join(output_dir, 'strace_out')
89

90
  pm_command_base = [
91 92 93 94
      'strace',
      '-f',
      '-o',
      strace_out,
95 96
      args.pm_bin,
      '-o',
97
      output_dir,
98 99 100
      '-k',
      args.signing_key,
      '-m',
101
      manifest_file,
102 103
  ]

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

  return 0

134

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