blob: 42f8e4c6d879cec4dcc561ccca93f8244c14dd01 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:37 +00001#!/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"""
8Builds applications in debug mode:
9- Copies the module directories into their destinations.
10- Copies app.html as-is.
11"""
12
13from os import path
14from os.path import join
15import os
16import shutil
17import sys
18
19import modular_build
20
21
22def 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 Fisherc2873072019-07-25 00:38:44 +000028 application_names = argv[1:input_path_flag_index]
Blink Reformat4c46d092018-04-07 15:32:37 +000029 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 Fisherc2873072019-07-25 00:38:44 +000033 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 Reformat4c46d092018-04-07 15:32:37 +000038
39
Jeff Fisherc2873072019-07-25 00:38:44 +000040def symlink_or_copy_file(src, dest, safe=False):
Blink Reformat4c46d092018-04-07 15:32:37 +000041 if safe and path.exists(dest):
42 os.remove(dest)
Jeff Fisherc2873072019-07-25 00:38:44 +000043 if hasattr(os, 'symlink'):
44 os.symlink(src, dest)
45 else:
46 shutil.copy(src, dest)
Blink Reformat4c46d092018-04-07 15:32:37 +000047
48
Jeff Fisherc2873072019-07-25 00:38:44 +000049def symlink_or_copy_dir(src, dest):
Blink Reformat4c46d092018-04-07 15:32:37 +000050 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 Fisherc2873072019-07-25 00:38:44 +000059 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>
66class 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 Reformat4c46d092018-04-07 15:32:37 +000090
91
92if __name__ == '__main__':
93 sys.exit(main(sys.argv))