OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Builds an SDK (in a specified target directory) from the (current) source |
| 7 repository (which should not be dirty) and a given "specification" file.""" |
| 8 |
| 9 |
| 10 import argparse |
| 11 from pylib.errors import FatalError |
| 12 from pylib.git import Git, IsGitTreeDirty, SanityCheckGit, GitLsFiles |
| 13 import os |
| 14 import shutil |
| 15 import sys |
| 16 |
| 17 |
| 18 def _CopyFiles(source_path, dest_path, **kwargs): |
| 19 """Copies files from the source git repository (the current working directory |
| 20 should be the root of this repository) to the destination path. |source_path| |
| 21 and the keyword arguments are as for the arguments of |GitLsFiles|. |
| 22 |dest_path| should be the "root" destination path. Note that a file such as |
| 23 <source_path>/foo/bar/baz.quux is copied to <dest_path>/foo/bar/baz.quux.""" |
| 24 |
| 25 # Normalize the source path. Note that this strips any trailing '/'. |
| 26 source_path = os.path.normpath(source_path) |
| 27 source_files = GitLsFiles(source_path, **kwargs) |
| 28 for source_file in source_files: |
| 29 rel_path = source_file[len(source_path) + 1:] |
| 30 dest_file = os.path.join(dest_path, rel_path) |
| 31 try: |
| 32 os.makedirs(os.path.dirname(dest_file)) |
| 33 except OSError: |
| 34 pass |
| 35 shutil.copyfile(source_file, dest_file) |
| 36 |
| 37 |
| 38 def main(): |
| 39 parser = argparse.ArgumentParser( |
| 40 description="Constructs an SDK from a specification") |
| 41 parser.add_argument("--allow-dirty-tree", dest="allow_dirty_tree", |
| 42 action="store_true", |
| 43 help="proceed even if the source tree is dirty") |
| 44 parser.add_argument("sdk_spec_file", metavar="sdk_spec_file.sdk", |
| 45 type=argparse.FileType("rb"), |
| 46 help="spec file for the SDK to build") |
| 47 parser.add_argument("target_dir", |
| 48 help="target directory (must not already exist)") |
| 49 args = parser.parse_args() |
| 50 |
| 51 target_dir = os.path.abspath(args.target_dir) |
| 52 |
| 53 # CD to the "src" directory (we should currently be in src/sdk_build). |
| 54 src_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) |
| 55 os.chdir(src_dir) |
| 56 |
| 57 SanityCheckGit() |
| 58 |
| 59 if not args.allow_dirty_tree and IsGitTreeDirty(): |
| 60 FatalError("tree appears to be dirty") |
| 61 |
| 62 try: |
| 63 os.mkdir(target_dir) |
| 64 except OSError: |
| 65 FatalError("failed to create target directory %s" % target_dir) |
| 66 |
| 67 def CopyFilesToTargetDir(source_path, rel_dest_path, **kwargs): |
| 68 return _CopyFiles(source_path, os.path.join(target_dir, rel_dest_path), |
| 69 **kwargs) |
| 70 |
| 71 execution_globals = {"CopyFiles": CopyFilesToTargetDir, |
| 72 "FatalError": FatalError, |
| 73 "GitLsFiles": GitLsFiles} |
| 74 exec args.sdk_spec_file in execution_globals |
| 75 |
| 76 return 0 |
| 77 |
| 78 |
| 79 if __name__ == "__main__": |
| 80 sys.exit(main()) |
OLD | NEW |