Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: mojo/public/tools/dart_package.py

Issue 1311803002: Dart: Removes dartzip (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Readme fixes Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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"""
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 |package_name|/lib/ returning module/interface.mojom.dart
25 def MojomDartRelativePath(package_name, path):
26 assert IsMojomDartFile(path)
27 return os.path.relpath(path, package_name + '/lib/')
28
29 # Line is a line from pubspec.yaml
30 def PackageName(line):
31 assert line.startswith("name:")
32 return line.split(":")[1].strip()
33
34 # pubspec_contents is the contents of a pubspec.yaml file, returns the package
35 # name.
36 def FindPackageName(pubspec_contents):
37 for line in pubspec_contents.splitlines():
38 if line.startswith("name:"):
39 return PackageName(line)
40
41 # Returns true if path is in lib/.
42 def IsPathInLib(path):
43 return path.startswith("lib/")
44
45 # Strips off lib/
46 def PackageRelativePath(path):
47 return os.path.relpath(path, "lib/")
48
49 def HasPubspec(paths):
50 for path in paths:
51 _, filename = os.path.split(path)
52 if 'pubspec.yaml' == filename:
53 return True
54 return False
55
56 def ReadPackageName(paths):
57 for path in paths:
58 _, filename = os.path.split(path)
59 if 'pubspec.yaml' == filename:
60 with open(path, 'r') as f:
61 return FindPackageName(f.read())
62 return None
63
64 def DoZip(inputs, zip_inputs, output, base_dir):
65 files = []
66 with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as outfile:
67 # Loose file inputs (package source files)
68 for f in inputs:
69 file_name = os.path.relpath(f, base_dir)
70 # We should never see a packages/ path here.
71 assert not IsPackagesPath(file_name)
72 files.append(file_name)
73 outfile.write(f, file_name)
74
75 if HasPubspec(inputs):
76 # We are writing out a package, write lib/ into packages/<package_name>
77 # so that package:<package_name>/ imports work within the package.
78 package_name = ReadPackageName(inputs)
79 assert not (package_name is None), "pubspec.yaml does not have a name"
80 package_path = os.path.join("packages/", package_name)
81 for f in inputs:
82 file_name = os.path.relpath(f, base_dir)
83 if IsPathInLib(file_name):
84 output_name = os.path.join(package_path,
85 PackageRelativePath(file_name))
86 if output_name not in files:
87 files.append(output_name)
88 outfile.write(f, output_name)
89
90 # zip file inputs (other packages)
91 for zf_name in zip_inputs:
92 with zipfile.ZipFile(zf_name, 'r') as zf:
93 # Attempt to sniff package_name. If this fails, we are processing a zip
94 # file with mojom.dart bindings or a packages/ dump.
95 package_name = None
96 try:
97 with zf.open("pubspec.yaml") as pubspec_file:
98 package_name = FindPackageName(pubspec_file.read())
99 except KeyError:
100 pass
101
102 # Iterate over all files in zip file.
103 for f in zf.namelist():
104
105 # Copy any direct mojom dependencies into mojom/
106 if IsMojomPath(f):
107 mojom_dep_copy = os.path.join("lib/mojom/",
108 MojomDartRelativePath("mojom", f))
109 if mojom_dep_copy not in files:
110 files.append(mojom_dep_copy)
111 with zf.open(f) as zff:
112 outfile.writestr(mojom_dep_copy, zff.read())
113 # Copy under lib/ as well.
114 mojom_dep_copy = os.path.join("lib/",
115 MojomDartRelativePath("mojom", f))
116 if mojom_dep_copy not in files:
117 files.append(mojom_dep_copy)
118 with zf.open(f) as zff:
119 outfile.writestr(mojom_dep_copy, zff.read())
120
121 # Rewrite output file name, if it isn't a packages/ path.
122 output_name = None
123 if not IsPackagesPath(f):
124 if IsMojomDartFile(f):
125 # Place $package/lib/*.mojom.dart files into packages/$package/
126 package = next(p for p in f.split(os.path.sep) if p)
127 output_name = os.path.join("packages/" + package + "/",
128 MojomDartRelativePath(package, f))
129 else:
130 # We are processing a package, it must have a package name.
131 assert not (package_name is None)
132 package_path = os.path.join("packages/", package_name)
133 if IsPathInLib(f):
134 output_name = os.path.join(package_path, PackageRelativePath(f))
135 else:
136 output_name = f;
137
138 if output_name is None:
139 continue
140
141 if output_name not in files:
142 files.append(output_name)
143 with zf.open(f) as zff:
144 outfile.writestr(output_name, zff.read())
145
146
147 def main():
148 parser = optparse.OptionParser()
149
150 parser.add_option('--inputs', help='List of files to archive.')
151 parser.add_option('--link-inputs',
152 help='List of files to archive. Symbolic links are resolved.')
153 parser.add_option('--zip-inputs', help='List of zip files to re-archive.')
154 parser.add_option('--output', help='Path to output archive.')
155 parser.add_option('--base-dir',
156 help='If provided, the paths in the archive will be '
157 'relative to this directory', default='.')
158 options, _ = parser.parse_args()
159
160 inputs = []
161 if (options.inputs):
162 inputs = ast.literal_eval(options.inputs)
163 zip_inputs = []
164 if options.zip_inputs:
165 zip_inputs = ast.literal_eval(options.zip_inputs)
166 output = options.output
167 base_dir = options.base_dir
168
169 DoZip(inputs, zip_inputs, output, base_dir)
170
171 if __name__ == '__main__':
172 sys.exit(main())
OLDNEW
« no previous file with comments | « mojo/public/tools/dart_list_packages_contents.py ('k') | mojo/public/tools/download_archiecture_independent_frameworks.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698