Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 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 | Builds applications in debug mode: |
| 9 | - Copies the module directories into their destinations. |
| 10 | - Copies app.html as-is. |
| 11 | """ |
| 12 | |
| 13 | from os import path |
| 14 | from os.path import join |
| 15 | import os |
| 16 | import shutil |
| 17 | import sys |
| 18 | |
| 19 | import modular_build |
| 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] |
Jeff Fisher | c287307 | 2019-07-25 00:38:44 +0000 | [diff] [blame] | 28 | application_names = argv[1:input_path_flag_index] |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 29 | except: |
| 30 | print('Usage: %s app_1 app_2 ... app_N --input_path <input_path> --output_path <output_path>' % argv[0]) |
| 31 | raise |
| 32 | |
Jeff Fisher | c287307 | 2019-07-25 00:38:44 +0000 | [diff] [blame] | 33 | loader = modular_build.DescriptorLoader(input_path) |
| 34 | for app in application_names: |
| 35 | descriptors = loader.load_application(app) |
| 36 | builder = DebugBuilder(app, descriptors, input_path, output_path) |
| 37 | builder.build_app() |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 38 | |
| 39 | |
Jeff Fisher | c287307 | 2019-07-25 00:38:44 +0000 | [diff] [blame] | 40 | def symlink_or_copy_file(src, dest, safe=False): |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 41 | if safe and path.exists(dest): |
| 42 | os.remove(dest) |
Jeff Fisher | c287307 | 2019-07-25 00:38:44 +0000 | [diff] [blame] | 43 | if hasattr(os, 'symlink'): |
| 44 | os.symlink(src, dest) |
| 45 | else: |
| 46 | shutil.copy(src, dest) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 47 | |
| 48 | |
Jeff Fisher | c287307 | 2019-07-25 00:38:44 +0000 | [diff] [blame] | 49 | def symlink_or_copy_dir(src, dest): |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 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) |
Jeff Fisher | c287307 | 2019-07-25 00:38:44 +0000 | [diff] [blame] | 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 | |
| 68 | def __init__(self, application_name, descriptors, application_dir, output_dir): |
| 69 | self.application_name = application_name |
| 70 | self.descriptors = descriptors |
| 71 | self.application_dir = application_dir |
| 72 | self.output_dir = output_dir |
| 73 | |
| 74 | def app_file(self, extension): |
| 75 | return self.application_name + '.' + extension |
| 76 | |
| 77 | def build_app(self): |
| 78 | if self.descriptors.has_html: |
| 79 | self._build_html() |
| 80 | for filename in os.listdir(self.application_dir): |
| 81 | src = join(os.getcwd(), self.application_dir, filename) |
| 82 | if os.path.isdir(src): |
| 83 | symlink_or_copy_dir(src, join(self.output_dir, filename)) |
| 84 | else: |
| 85 | symlink_or_copy_file(src, join(self.output_dir, filename), safe=True) |
| 86 | |
| 87 | def _build_html(self): |
| 88 | html_name = self.app_file('html') |
| 89 | symlink_or_copy_file(join(os.getcwd(), self.application_dir, html_name), join(self.output_dir, html_name), True) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | if __name__ == '__main__': |
| 93 | sys.exit(main(sys.argv)) |