Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # | |
| 3 # Copyright 2014 The Chromium Authors. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | |
| 5 # found in the LICENSE file. | |
| 6 | |
| 7 """Archives a set of dart packages""" | |
|
Cutch
2015/05/15 22:21:14
This is a slightly modified clone of mojo/public/t
| |
| 8 | |
| 9 import ast | |
| 10 import optparse | |
| 11 import os | |
| 12 import sys | |
| 13 import zipfile | |
| 14 | |
| 15 def IsPackagesPath(path): | |
| 16 return path.startswith('packages/') | |
| 17 | |
| 18 def IsMojomPath(path): | |
| 19 return path.startswith('mojom/lib/') | |
| 20 | |
| 21 def IsMojomDartFile(path): | |
| 22 return path.endswith('.mojom.dart') | |
| 23 | |
| 24 # Strips off mojom/lib/ returning module/interface.mojom.dart | |
| 25 def MojomDartRelativePath(path): | |
| 26 assert IsMojomPath(path) | |
| 27 assert IsMojomDartFile(path) | |
| 28 return os.path.relpath(path, 'mojom/lib/') | |
| 29 | |
| 30 # Line is a line from pubspec.yaml | |
| 31 def PackageName(line): | |
| 32 assert line.startswith("name:") | |
| 33 return line.split(":")[1].strip() | |
| 34 | |
| 35 # pubspec_contents is the contents of a pubspec.yaml file, returns the package | |
| 36 # name. | |
| 37 def FindPackageName(pubspec_contents): | |
| 38 for line in pubspec_contents.splitlines(): | |
| 39 if line.startswith("name:"): | |
| 40 return PackageName(line) | |
| 41 | |
| 42 # Returns true if path is in lib/. | |
| 43 def IsPathInLib(path): | |
| 44 return path.startswith("lib/") | |
| 45 | |
| 46 # Strips off lib/ | |
| 47 def PackageRelativePath(path): | |
| 48 return os.path.relpath(path, "lib/") | |
| 49 | |
| 50 def DoZip(inputs, zip_inputs, output, base_dir): | |
| 51 files = [] | |
| 52 with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as outfile: | |
| 53 # Loose file inputs (package source files) | |
| 54 for f in inputs: | |
| 55 file_name = os.path.relpath(f, base_dir) | |
| 56 # We should never see a packages/ path here. | |
| 57 assert not IsPackagesPath(file_name) | |
| 58 # We should never see a .mojom.dart path here. | |
| 59 files.append(file_name) | |
| 60 outfile.write(f, file_name) | |
| 61 # zip file inputs (other packages) | |
| 62 for zf_name in zip_inputs: | |
| 63 with zipfile.ZipFile(zf_name, 'r') as zf: | |
| 64 # Attempt to sniff package_name. If this fails, we are processing a zip | |
| 65 # file with mojom.dart bindings or a packages/ dump. | |
| 66 package_name = None | |
| 67 try: | |
| 68 with zf.open("pubspec.yaml") as pubspec_file: | |
| 69 package_name = FindPackageName(pubspec_file.read()) | |
| 70 except KeyError: | |
| 71 pass | |
| 72 | |
| 73 # Iterate over all files in zip file. | |
| 74 for f in zf.namelist(): | |
| 75 | |
| 76 # Copy any direct mojom dependencies into mojom/ | |
| 77 if IsMojomPath(f): | |
| 78 mojom_dep_copy = os.path.join("lib/mojom/", | |
| 79 MojomDartRelativePath(f)) | |
| 80 if mojom_dep_copy not in files: | |
| 81 files.append(mojom_dep_copy) | |
| 82 with zf.open(f) as zff: | |
| 83 outfile.writestr(mojom_dep_copy, zff.read()) | |
| 84 | |
| 85 # Rewrite output file name, if it isn't a packages/ path. | |
| 86 output_name = None | |
| 87 if not IsPackagesPath(f): | |
| 88 if IsMojomDartFile(f) and IsMojomPath(f): | |
| 89 # Place mojom/lib/*.mojom.dart files into packages/mojom/ | |
| 90 output_name = os.path.join("packages/mojom/", | |
| 91 MojomDartRelativePath(f)) | |
| 92 else: | |
| 93 # We are processing a package, it must have a package name. | |
| 94 assert not (package_name is None) | |
| 95 package_path = os.path.join("packages/", package_name) | |
| 96 if IsPathInLib(f): | |
| 97 output_name = os.path.join(package_path, PackageRelativePath(f)) | |
| 98 else: | |
| 99 output_name = f; | |
| 100 | |
| 101 if output_name is None: | |
| 102 continue | |
| 103 | |
| 104 if output_name not in files: | |
| 105 files.append(output_name) | |
| 106 with zf.open(f) as zff: | |
| 107 outfile.writestr(output_name, zff.read()) | |
| 108 | |
| 109 | |
| 110 def main(): | |
| 111 parser = optparse.OptionParser() | |
| 112 | |
| 113 parser.add_option('--inputs', help='List of files to archive.') | |
| 114 parser.add_option('--link-inputs', | |
| 115 help='List of files to archive. Symbolic links are resolved.') | |
| 116 parser.add_option('--zip-inputs', help='List of zip files to re-archive.') | |
| 117 parser.add_option('--output', help='Path to output archive.') | |
| 118 parser.add_option('--base-dir', | |
| 119 help='If provided, the paths in the archive will be ' | |
| 120 'relative to this directory', default='.') | |
| 121 options, _ = parser.parse_args() | |
| 122 | |
| 123 inputs = [] | |
| 124 if (options.inputs): | |
| 125 inputs = ast.literal_eval(options.inputs) | |
| 126 zip_inputs = [] | |
| 127 if options.zip_inputs: | |
| 128 zip_inputs = ast.literal_eval(options.zip_inputs) | |
| 129 output = options.output | |
| 130 base_dir = options.base_dir | |
| 131 | |
| 132 DoZip(inputs, zip_inputs, output, base_dir) | |
| 133 | |
| 134 if __name__ == '__main__': | |
| 135 sys.exit(main()) | |
| OLD | NEW |