OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # -*- coding: UTF-8 -*- |
| 3 # |
| 4 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 5 # Use of this source code is governed by a BSD-style license that can be |
| 6 # found in the LICENSE file. |
| 7 |
| 8 """ |
| 9 Builds applications in debug mode: |
| 10 - Copies the module directories into their destinations. |
| 11 - Copies app.html as-is. |
| 12 """ |
| 13 |
| 14 from os import path |
| 15 from os.path import join |
| 16 import modular_build |
| 17 import os |
| 18 import shutil |
| 19 import sys |
| 20 |
| 21 |
| 22 def main(argv): |
| 23 try: |
| 24 input_path_flag_index = argv.index('--input_path') |
| 25 input_path = argv[input_path_flag_index + 1] |
| 26 output_path_flag_index = argv.index('--output_path') |
| 27 output_path = argv[output_path_flag_index + 1] |
| 28 application_names = argv[1:input_path_flag_index] |
| 29 except: |
| 30 print('Usage: %s app_1 app_2 ... app_N --input_path <input_path> --outpu
t_path <output_path>' % argv[0]) |
| 31 raise |
| 32 |
| 33 loader = modular_build.DescriptorLoader(input_path) |
| 34 for app in application_names: |
| 35 descriptors = loader.load_application(app + '.json') |
| 36 builder = DebugBuilder(app, descriptors, input_path, output_path) |
| 37 builder.build_app() |
| 38 |
| 39 |
| 40 def symlink_or_copy_file(src, dest, safe=False): |
| 41 if safe and path.exists(dest): |
| 42 os.remove(dest) |
| 43 if hasattr(os, 'symlink'): |
| 44 os.symlink(src, dest) |
| 45 else: |
| 46 shutil.copy(src, dest) |
| 47 |
| 48 |
| 49 def symlink_or_copy_dir(src, dest): |
| 50 if path.exists(dest): |
| 51 shutil.rmtree(dest) |
| 52 for src_dir, dirs, files in os.walk(src): |
| 53 subpath = path.relpath(src_dir, src) |
| 54 dest_dir = path.normpath(join(dest, subpath)) |
| 55 os.mkdir(dest_dir) |
| 56 for name in files: |
| 57 src_name = join(os.getcwd(), src_dir, name) |
| 58 dest_name = join(dest_dir, name) |
| 59 symlink_or_copy_file(src_name, dest_name) |
| 60 |
| 61 |
| 62 # Outputs: |
| 63 # <app_name>.html as-is |
| 64 # <app_name>.js as-is |
| 65 # <module_name>/<all_files> |
| 66 class DebugBuilder(object): |
| 67 def __init__(self, application_name, descriptors, application_dir, output_di
r): |
| 68 self.application_name = application_name |
| 69 self.descriptors = descriptors |
| 70 self.application_dir = application_dir |
| 71 self.output_dir = output_dir |
| 72 |
| 73 def app_file(self, extension): |
| 74 return self.application_name + '.' + extension |
| 75 |
| 76 def build_app(self): |
| 77 if self.descriptors.has_html: |
| 78 self._build_html() |
| 79 for filename in os.listdir(self.application_dir): |
| 80 src = join(os.getcwd(), self.application_dir, filename) |
| 81 if os.path.isdir(src): |
| 82 symlink_or_copy_dir(src, join(self.output_dir, filename)) |
| 83 else: |
| 84 symlink_or_copy_file(src, join(self.output_dir, filename), safe=
True) |
| 85 |
| 86 def _build_html(self): |
| 87 html_name = self.app_file('html') |
| 88 symlink_or_copy_file(join(os.getcwd(), self.application_dir, html_name),
join(self.output_dir, html_name), True) |
| 89 |
| 90 |
| 91 if __name__ == '__main__': |
| 92 sys.exit(main(sys.argv)) |
OLD | NEW |