Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
create_archive.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5#
6# This python script creates a tar archive and a C++ source file which contains
7# the tar archive as an array of bytes.
8
9import os
10import sys
11from os.path import join, splitext
12import time
13from optparse import OptionParser
14from datetime import date
15import tarfile
16import tempfile
17import gzip
18
19
20def CreateTarArchive(tar_path, client_root, compress, files):
21 mode_string = 'w'
22 tar = tarfile.open(tar_path, mode=mode_string)
23 for input_file_name in files:
24 # Chop off client_root.
25 archive_file_name = input_file_name[len(client_root):]
26 # Replace back slash with forward slash. So we do not have Windows paths.
27 archive_file_name = archive_file_name.replace("\\", "/")
28 # Open input file and add it to the archive.
29 with open(input_file_name, 'rb') as input_file:
30 tarInfo = tarfile.TarInfo(name=archive_file_name)
31 input_file.seek(0, 2)
32 tarInfo.size = input_file.tell()
33 tarInfo.mtime = 0 # For deterministic builds.
34 input_file.seek(0)
35 tar.addfile(tarInfo, fileobj=input_file)
36 tar.close()
37 if compress:
38 with open(tar_path, "rb") as fin:
39 uncompressed = fin.read()
40 with open(tar_path, "wb") as fout:
41 # mtime=0 for deterministic builds.
42 gz = gzip.GzipFile(fileobj=fout, mode="wb", filename="", mtime=0)
43 gz.write(uncompressed)
44 gz.close()
45
46
47def MakeArchive(options):
48 if not options.client_root:
49 sys.stderr.write('--client_root not specified')
50 return -1
51
52 files = []
53 for dirname, dirnames, filenames in os.walk(options.client_root):
54 # strip out all dot files.
55 filenames = [f for f in filenames if not f[0] == '.']
56 dirnames[:] = [d for d in dirnames if not d[0] == '.']
57 for f in filenames:
58 src_path = os.path.join(dirname, f)
59 if (os.path.isdir(src_path)):
60 continue
61 files.append(src_path)
62
63 # Ensure consistent file ordering for reproducible builds.
64 files.sort()
65
66 # Write out archive.
67 CreateTarArchive(options.tar_output, options.client_root, options.compress,
68 files)
69 return 0
70
71
73 output_file,
74 outer_namespace,
75 inner_namespace,
76 name,
77 tar_archive,
78):
79 with open(output_file, 'w') as out:
80 out.write('''
81// Copyright (c) %d, the Dart project authors. Please see the AUTHORS file
82// for details. All rights reserved. Use of this source code is governed by a
83// BSD-style license that can be found in the LICENSE file.
84
85''' % date.today().year)
86 out.write('''
87
88#include <stdint.h>
89
90''')
91 out.write('namespace %s {\n' % outer_namespace)
92 if inner_namespace != None:
93 out.write('namespace %s {\n' % inner_namespace)
94 out.write('\n\n')
95 # Write the byte contents of the archive as a comma separated list of
96 # integers, one integer for each byte.
97 out.write('static const uint8_t %s_[] = {\n' % name)
98 line = ' '
99 lineCounter = 0
100 for byte in tar_archive:
101 line += r" %d," % (byte if isinstance(byte, int) else ord(byte))
102 lineCounter += 1
103 if lineCounter == 10:
104 out.write(line + '\n')
105 line = ' '
106 lineCounter = 0
107 if lineCounter != 0:
108 out.write(line + '\n')
109 out.write('};\n')
110 out.write('\nunsigned int %s_len = %d;\n' % (name, len(tar_archive)))
111 out.write('\nconst uint8_t* %s = %s_;\n\n' % (name, name))
112 if inner_namespace != None:
113 out.write('} // namespace %s\n' % inner_namespace)
114 out.write('} // namespace %s\n' % outer_namespace)
115
116
117def MakeCCFile(options):
118 if not options.output:
119 sys.stderr.write('--output not specified\n')
120 return -1
121 if not options.name:
122 sys.stderr.write('--name not specified\n')
123 return -1
124 if not options.tar_input:
125 sys.stderr.write('--tar_input not specified\n')
126 return -1
127
128 # Read it back in.
129 with open(options.tar_input, 'rb') as tar_file:
130 tar_archive = tar_file.read()
131
132 # Write CC file.
133 WriteCCFile(options.output, options.outer_namespace,
134 options.inner_namespace, options.name, tar_archive)
135 return 0
136
137
138def Main(args):
139 try:
140 # Parse input.
141 parser = OptionParser()
142 parser.add_option(
143 "--output", action="store", type="string", help="output file name")
144 parser.add_option("--tar_input",\
145 action="store", type="string",
146 help="input tar archive")
147 parser.add_option(
148 "--tar_output",
149 action="store",
150 type="string",
151 help="tar output file name")
152 parser.add_option(
153 "--outer_namespace",
154 action="store",
155 type="string",
156 help="outer C++ namespace",
157 default="dart")
158 parser.add_option(
159 "--inner_namespace",
160 action="store",
161 type="string",
162 help="inner C++ namespace",
163 default="bin")
164 parser.add_option(
165 "--name",
166 action="store",
167 type="string",
168 help="name of tar archive symbol")
169 parser.add_option("--compress", action="store_true", default=False)
170 parser.add_option(
171 "--client_root",
172 action="store",
173 type="string",
174 help="root directory client resources")
175
176 (options, args) = parser.parse_args()
177
178 if options.tar_output:
179 return MakeArchive(options)
180 else:
181 return MakeCCFile(options)
182
183 except Exception as inst:
184 sys.stderr.write('create_archive.py exception\n')
185 sys.stderr.write(str(inst))
186 sys.stderr.write('\n')
187 return -1
188
189
190if __name__ == '__main__':
191 sys.exit(Main(sys.argv))
WriteCCFile(output_file, outer_namespace, inner_namespace, name, tar_archive)
CreateTarArchive(tar_path, client_root, compress, files)
MakeArchive(options)