| 
									
										
										
										
											2017-06-05 16:02:39 +03:00
										 |  |  | # Copyright (c) 2014-present PlatformIO <contact@platformio.org> | 
					
						
							| 
									
										
										
										
											2015-11-18 17:16:17 +02:00
										 |  |  | # | 
					
						
							|  |  |  | # Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | # you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | # You may obtain a copy of the License at | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #    http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | # See the License for the specific language governing permissions and | 
					
						
							|  |  |  | # limitations under the License. | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | from glob import glob | 
					
						
							| 
									
										
										
										
											2016-08-05 18:43:20 +03:00
										 |  |  | from os import getenv, makedirs, remove | 
					
						
							| 
									
										
										
										
											2019-10-17 20:57:40 +03:00
										 |  |  | from os.path import abspath, basename, isdir, isfile, join | 
					
						
							| 
									
										
										
										
											2016-08-05 18:43:20 +03:00
										 |  |  | from shutil import copyfile, copytree | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  | from tempfile import mkdtemp | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import click | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 19:44:37 +03:00
										 |  |  | from platformio import app, fs | 
					
						
							| 
									
										
										
										
											2016-09-14 19:06:22 +03:00
										 |  |  | from platformio.commands.init import cli as cmd_init | 
					
						
							|  |  |  | from platformio.commands.init import validate_boards | 
					
						
							| 
									
										
										
										
											2019-10-23 16:05:27 +03:00
										 |  |  | from platformio.commands.run.command import cli as cmd_run | 
					
						
							| 
									
										
										
										
											2019-05-17 13:18:15 +03:00
										 |  |  | from platformio.compat import glob_escape | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  | from platformio.exception import CIBuildEnvsEmpty | 
					
						
							| 
									
										
										
										
											2019-05-07 17:51:50 +03:00
										 |  |  | from platformio.project.config import ProjectConfig | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-18 12:18:27 +03:00
										 |  |  | def validate_path(ctx, param, value):  # pylint: disable=unused-argument | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  |     invalid_path = None | 
					
						
							| 
									
										
										
										
											2015-06-19 00:10:50 +03:00
										 |  |  |     value = list(value) | 
					
						
							|  |  |  |     for i, p in enumerate(value): | 
					
						
							|  |  |  |         if p.startswith("~"): | 
					
						
							| 
									
										
										
										
											2019-10-17 20:57:40 +03:00
										 |  |  |             value[i] = fs.expanduser(p) | 
					
						
							| 
									
										
										
										
											2015-06-19 00:10:50 +03:00
										 |  |  |         value[i] = abspath(value[i]) | 
					
						
							|  |  |  |         if not glob(value[i]): | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  |             invalid_path = p | 
					
						
							|  |  |  |             break | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         assert invalid_path is None | 
					
						
							|  |  |  |         return value | 
					
						
							|  |  |  |     except AssertionError: | 
					
						
							|  |  |  |         raise click.BadParameter("Found invalid path: %s" % invalid_path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @click.command("ci", short_help="Continuous Integration") | 
					
						
							|  |  |  | @click.argument("src", nargs=-1, callback=validate_path) | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  | @click.option("-l", "--lib", multiple=True, callback=validate_path, metavar="DIRECTORY") | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  | @click.option("--exclude", multiple=True) | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  | @click.option("-b", "--board", multiple=True, metavar="ID", callback=validate_boards) | 
					
						
							|  |  |  | @click.option( | 
					
						
							|  |  |  |     "--build-dir", | 
					
						
							|  |  |  |     default=mkdtemp, | 
					
						
							|  |  |  |     type=click.Path(file_okay=False, dir_okay=True, writable=True, resolve_path=True), | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  | @click.option("--keep-build-dir", is_flag=True) | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  | @click.option( | 
					
						
							|  |  |  |     "-c", | 
					
						
							|  |  |  |     "--project-conf", | 
					
						
							|  |  |  |     type=click.Path( | 
					
						
							|  |  |  |         exists=True, file_okay=True, dir_okay=False, readable=True, resolve_path=True | 
					
						
							|  |  |  |     ), | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-09-01 15:14:38 +03:00
										 |  |  | @click.option("-O", "--project-option", multiple=True) | 
					
						
							| 
									
										
										
										
											2016-07-22 18:02:04 +03:00
										 |  |  | @click.option("-v", "--verbose", is_flag=True) | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  | @click.pass_context | 
					
						
							| 
									
										
										
										
											2019-02-24 11:45:14 +02:00
										 |  |  | def cli(  # pylint: disable=too-many-arguments, too-many-branches | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |     ctx, | 
					
						
							|  |  |  |     src, | 
					
						
							|  |  |  |     lib, | 
					
						
							|  |  |  |     exclude, | 
					
						
							|  |  |  |     board, | 
					
						
							|  |  |  |     build_dir, | 
					
						
							|  |  |  |     keep_build_dir, | 
					
						
							|  |  |  |     project_conf, | 
					
						
							|  |  |  |     project_option, | 
					
						
							|  |  |  |     verbose, | 
					
						
							|  |  |  | ): | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-18 12:18:27 +03:00
										 |  |  |     if not src and getenv("PLATFORMIO_CI_SRC"): | 
					
						
							|  |  |  |         src = validate_path(ctx, None, getenv("PLATFORMIO_CI_SRC").split(":")) | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  |     if not src: | 
					
						
							|  |  |  |         raise click.BadParameter("Missing argument 'src'") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         app.set_session_var("force_option", True) | 
					
						
							| 
									
										
										
										
											2019-02-23 18:39:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if not keep_build_dir and isdir(build_dir): | 
					
						
							| 
									
										
										
										
											2019-08-12 19:44:37 +03:00
										 |  |  |             fs.rmtree(build_dir) | 
					
						
							| 
									
										
										
										
											2019-02-23 18:39:54 +02:00
										 |  |  |         if not isdir(build_dir): | 
					
						
							|  |  |  |             makedirs(build_dir) | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-27 20:51:55 +03:00
										 |  |  |         for dir_name, patterns in dict(lib=lib, src=src).items(): | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  |             if not patterns: | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             contents = [] | 
					
						
							|  |  |  |             for p in patterns: | 
					
						
							|  |  |  |                 contents += glob(p) | 
					
						
							|  |  |  |             _copy_contents(join(build_dir, dir_name), contents) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if project_conf and isfile(project_conf): | 
					
						
							| 
									
										
										
										
											2016-07-11 22:40:37 +03:00
										 |  |  |             _copy_project_conf(build_dir, project_conf) | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  |         elif not board: | 
					
						
							|  |  |  |             raise CIBuildEnvsEmpty() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if exclude: | 
					
						
							|  |  |  |             _exclude_contents(build_dir, exclude) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # initialise project | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         ctx.invoke( | 
					
						
							|  |  |  |             cmd_init, project_dir=build_dir, board=board, project_option=project_option | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # process project | 
					
						
							| 
									
										
										
										
											2015-05-15 22:40:29 +02:00
										 |  |  |         ctx.invoke(cmd_run, project_dir=build_dir, verbose=verbose) | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  |     finally: | 
					
						
							|  |  |  |         if not keep_build_dir: | 
					
						
							| 
									
										
										
										
											2019-08-12 19:44:37 +03:00
										 |  |  |             fs.rmtree(build_dir) | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _copy_contents(dst_dir, contents): | 
					
						
							| 
									
										
										
										
											2016-08-03 23:38:20 +03:00
										 |  |  |     items = {"dirs": set(), "files": set()} | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for path in contents: | 
					
						
							|  |  |  |         if isdir(path): | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |             items["dirs"].add(path) | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  |         elif isfile(path): | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |             items["files"].add(path) | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     dst_dir_name = basename(dst_dir) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |     if dst_dir_name == "src" and len(items["dirs"]) == 1: | 
					
						
							|  |  |  |         copytree(list(items["dirs"]).pop(), dst_dir, symlinks=True) | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2019-03-14 20:12:18 +02:00
										 |  |  |         if not isdir(dst_dir): | 
					
						
							|  |  |  |             makedirs(dst_dir) | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         for d in items["dirs"]: | 
					
						
							| 
									
										
										
										
											2016-03-21 16:47:24 +02:00
										 |  |  |             copytree(d, join(dst_dir, basename(d)), symlinks=True) | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |     if not items["files"]: | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  |         return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if dst_dir_name == "lib": | 
					
						
							|  |  |  |         dst_dir = join(dst_dir, mkdtemp(dir=dst_dir)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |     for f in items["files"]: | 
					
						
							| 
									
										
										
										
											2019-03-22 21:24:43 +02:00
										 |  |  |         dst_file = join(dst_dir, basename(f)) | 
					
						
							|  |  |  |         if f == dst_file: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         copyfile(f, dst_file) | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _exclude_contents(dst_dir, patterns): | 
					
						
							|  |  |  |     contents = [] | 
					
						
							|  |  |  |     for p in patterns: | 
					
						
							| 
									
										
										
										
											2019-05-17 13:18:15 +03:00
										 |  |  |         contents += glob(join(glob_escape(dst_dir), p)) | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  |     for path in contents: | 
					
						
							| 
									
										
										
										
											2015-05-15 13:56:19 +02:00
										 |  |  |         path = abspath(path) | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  |         if isdir(path): | 
					
						
							| 
									
										
										
										
											2019-08-12 19:44:37 +03:00
										 |  |  |             fs.rmtree(path) | 
					
						
							| 
									
										
										
										
											2015-05-07 16:20:53 +01:00
										 |  |  |         elif isfile(path): | 
					
						
							|  |  |  |             remove(path) | 
					
						
							| 
									
										
										
										
											2016-07-11 22:40:37 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _copy_project_conf(build_dir, project_conf): | 
					
						
							| 
									
										
										
										
											2019-05-07 17:51:50 +03:00
										 |  |  |     config = ProjectConfig(project_conf, parse_extra=False) | 
					
						
							| 
									
										
										
										
											2016-09-17 23:46:53 +03:00
										 |  |  |     if config.has_section("platformio"): | 
					
						
							|  |  |  |         config.remove_section("platformio") | 
					
						
							| 
									
										
										
										
											2019-05-07 19:57:24 +03:00
										 |  |  |     config.save(join(build_dir, "platformio.ini")) |