| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Builds an SDK (in a specified target directory) from the (current) source | 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.""" | 7 repository (which should not be dirty) and a given "specification" file.""" |
| 8 | 8 |
| 9 | 9 |
| 10 import argparse | 10 import argparse |
| 11 from pylib.errors import FatalError | 11 from pylib.errors import FatalError |
| 12 from pylib.git import Git, IsGitTreeDirty, SanityCheckGit, GitLsFiles | 12 from pylib.git import Git, IsGitTreeDirty, SanityCheckGit, GitLsFiles |
| 13 import os | 13 import os |
| 14 import shutil | 14 import shutil |
| 15 import sys | 15 import sys |
| 16 | 16 |
| 17 | 17 |
| 18 def _MakeDirs(*args, **kwargs): | 18 def _MakeDirs(*args, **kwargs): |
| 19 """Like |os.makedirs()|, but ignores |OSError| exceptions (it assumes that | 19 """Like |os.makedirs()|, but ignores |OSError| exceptions (it assumes that |
| 20 these are due to the directory already existing).""" | 20 these are due to the directory already existing).""" |
| 21 try: | 21 try: |
| 22 os.makedirs(*args, **kwargs) | 22 os.makedirs(*args, **kwargs) |
| 23 except OSError: | 23 except OSError: |
| 24 pass | 24 pass |
| 25 | 25 |
| 26 | 26 |
| 27 def _CopyHelper(source_file, dest_file): |
| 28 """Copies |source_file| to |dest_file|. If |source_file| is user-executable, |
| 29 it will set |dest_file|'s mode to 0755 (user: rwx, group/other: rx); |
| 30 otherwise, it will set it to 0644 (user: rw, group/other: r).""" |
| 31 shutil.copy(source_file, dest_file) |
| 32 if os.stat(source_file).st_mode & 0100: |
| 33 os.chmod(dest_file, 0755) |
| 34 else: |
| 35 os.chmod(dest_file, 0644) |
| 36 |
| 37 |
| 27 def _CopyDir(source_path, dest_path, **kwargs): | 38 def _CopyDir(source_path, dest_path, **kwargs): |
| 28 """Copies directories from the source git repository (the current working | 39 """Copies directories from the source git repository (the current working |
| 29 directory should be the root of this repository) to the destination path. | 40 directory should be the root of this repository) to the destination path. |
| 30 |source_path| and the keyword arguments are as for the arguments of | 41 |source_path| and the keyword arguments are as for the arguments of |
| 31 |GitLsFiles|. |dest_path| should be the "root" destination path. Note that a | 42 |GitLsFiles|. |dest_path| should be the "root" destination path. Note that a |
| 32 file such as <source_path>/foo/bar/baz.quux is copied to | 43 file such as <source_path>/foo/bar/baz.quux is copied to |
| 33 <dest_path>/foo/bar/baz.quux.""" | 44 <dest_path>/foo/bar/baz.quux.""" |
| 34 | 45 |
| 35 # Normalize the source path. Note that this strips any trailing '/'. | 46 # Normalize the source path. Note that this strips any trailing '/'. |
| 36 source_path = os.path.normpath(source_path) | 47 source_path = os.path.normpath(source_path) |
| 37 source_files = GitLsFiles(source_path, **kwargs) | 48 source_files = GitLsFiles(source_path, **kwargs) |
| 38 for source_file in source_files: | 49 for source_file in source_files: |
| 39 rel_path = source_file[len(source_path) + 1:] | 50 rel_path = source_file[len(source_path) + 1:] |
| 40 dest_file = os.path.join(dest_path, rel_path) | 51 dest_file = os.path.join(dest_path, rel_path) |
| 41 _MakeDirs(os.path.dirname(dest_file)) | 52 _MakeDirs(os.path.dirname(dest_file)) |
| 42 shutil.copy(source_file, dest_file) | 53 _CopyHelper(source_file, dest_file) |
| 43 | 54 |
| 44 | 55 |
| 45 def _CopyFiles(source_files, dest_path): | 56 def _CopyFiles(source_files, dest_path): |
| 46 """Copies a given source file or files from the source git repository to the | 57 """Copies a given source file or files from the source git repository to the |
| 47 given destination path (the current working directory should be the root of | 58 given destination path (the current working directory should be the root of |
| 48 this repository) |source_files| should either be a relative path to a single | 59 this repository) |source_files| should either be a relative path to a single |
| 49 file in the source git repository or an iterable of such paths; note that this | 60 file in the source git repository or an iterable of such paths; note that this |
| 50 does not check that files are actually in the git repository (i.e., are | 61 does not check that files are actually in the git repository (i.e., are |
| 51 tracked).""" | 62 tracked).""" |
| 52 | 63 |
| 53 if type(source_files) is str: | 64 if type(source_files) is str: |
| 54 source_files = [source_files] | 65 source_files = [source_files] |
| 55 _MakeDirs(dest_path) | 66 _MakeDirs(dest_path) |
| 56 for source_file in source_files: | 67 for source_file in source_files: |
| 57 shutil.copy(source_file, | 68 _CopyHelper(source_file, |
| 58 os.path.join(dest_path, os.path.basename(source_file))) | 69 os.path.join(dest_path, os.path.basename(source_file))) |
| 59 | 70 |
| 60 | 71 |
| 61 def _GitGetRevision(): | 72 def _GitGetRevision(): |
| 62 """Returns the HEAD revision of the source git repository (the current working | 73 """Returns the HEAD revision of the source git repository (the current working |
| 63 directory should be the root of this repository).""" | 74 directory should be the root of this repository).""" |
| 64 | 75 |
| 65 return Git("rev-parse", "HEAD").strip() | 76 return Git("rev-parse", "HEAD").strip() |
| 66 | 77 |
| 67 | 78 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 | 110 |
| 100 # CD to the "src" directory (we should currently be in src/sdk_build). | 111 # CD to the "src" directory (we should currently be in src/sdk_build). |
| 101 src_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) | 112 src_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) |
| 102 os.chdir(src_dir) | 113 os.chdir(src_dir) |
| 103 | 114 |
| 104 SanityCheckGit() | 115 SanityCheckGit() |
| 105 | 116 |
| 106 if not args.allow_dirty_tree and IsGitTreeDirty(): | 117 if not args.allow_dirty_tree and IsGitTreeDirty(): |
| 107 FatalError("tree appears to be dirty") | 118 FatalError("tree appears to be dirty") |
| 108 | 119 |
| 120 # Set the umask, so that files/directories we create will be world- (and |
| 121 # group-) readable (and executable, for directories). |
| 122 os.umask(0022) |
| 123 |
| 109 try: | 124 try: |
| 110 os.mkdir(target_dir) | 125 os.mkdir(target_dir) |
| 111 except OSError: | 126 except OSError: |
| 112 FatalError("failed to create target directory %s" % target_dir) | 127 FatalError("failed to create target directory %s" % target_dir) |
| 113 | 128 |
| 114 # Wrappers to actually write things to the target directory: | 129 # Wrappers to actually write things to the target directory: |
| 115 | 130 |
| 116 def CopyDirToTargetDir(source_path, rel_dest_path, **kwargs): | 131 def CopyDirToTargetDir(source_path, rel_dest_path, **kwargs): |
| 117 return _CopyDir(source_path, os.path.join(target_dir, rel_dest_path), | 132 return _CopyDir(source_path, os.path.join(target_dir, rel_dest_path), |
| 118 **kwargs) | 133 **kwargs) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 131 "GitLsFiles": GitLsFiles, | 146 "GitLsFiles": GitLsFiles, |
| 132 "ReadFile": _ReadFile, | 147 "ReadFile": _ReadFile, |
| 133 "WriteFile": WriteFileToTargetDir} | 148 "WriteFile": WriteFileToTargetDir} |
| 134 exec args.sdk_spec_file in execution_globals | 149 exec args.sdk_spec_file in execution_globals |
| 135 | 150 |
| 136 return 0 | 151 return 0 |
| 137 | 152 |
| 138 | 153 |
| 139 if __name__ == "__main__": | 154 if __name__ == "__main__": |
| 140 sys.exit(main()) | 155 sys.exit(main()) |
| OLD | NEW |