OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """ Set of basic operations/utilities that are used by the build. """ | 5 """ Set of basic operations/utilities that are used by the build. """ |
6 | 6 |
7 import copy | 7 import copy |
8 import errno | 8 import errno |
9 import fnmatch | 9 import fnmatch |
10 import glob | 10 import glob |
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 if not os.path.isdir(dest_dir): | 468 if not os.path.isdir(dest_dir): |
469 raise PathNotFound('Unable to find dir %s' % dest_dir) | 469 raise PathNotFound('Unable to find dir %s' % dest_dir) |
470 src_file = os.path.basename(src_path) | 470 src_file = os.path.basename(src_path) |
471 if dest_fn: | 471 if dest_fn: |
472 shutil.copy(src_path, os.path.join(dest_dir, dest_fn)) | 472 shutil.copy(src_path, os.path.join(dest_dir, dest_fn)) |
473 else: | 473 else: |
474 shutil.copy(src_path, os.path.join(dest_dir, src_file)) | 474 shutil.copy(src_path, os.path.join(dest_dir, src_file)) |
475 | 475 |
476 | 476 |
477 def MakeZip(output_dir, archive_name, file_list, file_relative_dir, | 477 def MakeZip(output_dir, archive_name, file_list, file_relative_dir, |
478 raise_error=True, remove_archive_directory=True): | 478 raise_error=True, remove_archive_directory=True, path_filter=None): |
479 """Packs files into a new zip archive. | 479 """Packs files into a new zip archive. |
480 | 480 |
481 Files are first copied into a directory within the output_dir named for | 481 Files are first copied into a directory within the output_dir named for |
482 the archive_name, which will be created if necessary and emptied if it | 482 the archive_name, which will be created if necessary and emptied if it |
483 already exists. The files are then then packed using archive names | 483 already exists. The files are then then packed using archive names |
484 relative to the output_dir. That is, if the zipfile is unpacked in place, | 484 relative to the output_dir. That is, if the zipfile is unpacked in place, |
485 it will create a directory identical to the new archiev_name directory, in | 485 it will create a directory identical to the new archiev_name directory, in |
486 the output_dir. The zip file will be named as the archive_name, plus | 486 the output_dir. The zip file will be named as the archive_name, plus |
487 '.zip'. | 487 '.zip'. |
488 | 488 |
489 Args: | 489 Args: |
490 output_dir: Absolute path to the directory in which the archive is to | 490 output_dir: Absolute path to the directory in which the archive is to |
491 be created. | 491 be created. |
492 archive_dir: Subdirectory of output_dir holding files to be added to | 492 archive_dir: Subdirectory of output_dir holding files to be added to |
493 the new zipfile. | 493 the new zipfile. |
494 file_list: List of paths to files or subdirectories, relative to the | 494 file_list: List of paths to files or subdirectories, relative to the |
495 file_relative_dir. | 495 file_relative_dir. |
496 file_relative_dir: Absolute path to the directory containing the files | 496 file_relative_dir: Absolute path to the directory containing the files |
497 and subdirectories in the file_list. | 497 and subdirectories in the file_list. |
498 raise_error: Whether to raise a PathNotFound error if one of the files in | 498 raise_error: Whether to raise a PathNotFound error if one of the files in |
499 the list is not found. | 499 the list is not found. |
500 remove_archive_directory: Whether to remove the archive staging directory | 500 remove_archive_directory: Whether to remove the archive staging directory |
501 before copying files over to it. | 501 before copying files over to it. |
| 502 path_filter: A function f(path) -> path. For each file to add, transform |
| 503 the path with the path_filter function, and read data from the resulting |
| 504 file instead. i.e. foo.exe could actually contain data from foo.asan.exe. |
502 | 505 |
503 Returns: | 506 Returns: |
504 A tuple consisting of (archive_dir, zip_file_path), where archive_dir | 507 A tuple consisting of (archive_dir, zip_file_path), where archive_dir |
505 is the full path to the newly created archive_name subdirectory. | 508 is the full path to the newly created archive_name subdirectory. |
506 | 509 |
507 Raises: | 510 Raises: |
508 PathNotFound if any of the files in the list is not found, unless | 511 PathNotFound if any of the files in the list is not found, unless |
509 raise_error is False, in which case the error will be ignored. | 512 raise_error is False, in which case the error will be ignored. |
510 """ | 513 """ |
511 | 514 |
512 # Collect files into the archive directory. | 515 # Collect files into the archive directory. |
513 archive_dir = os.path.join(output_dir, archive_name) | 516 archive_dir = os.path.join(output_dir, archive_name) |
514 if remove_archive_directory and os.path.exists(archive_dir): | 517 if remove_archive_directory and os.path.exists(archive_dir): |
515 # Move it even if it's not a directory as expected. This can happen with | 518 # Move it even if it's not a directory as expected. This can happen with |
516 # FILES.cfg archive creation where we create an archive staging directory | 519 # FILES.cfg archive creation where we create an archive staging directory |
517 # that is the same name as the ultimate archive name. | 520 # that is the same name as the ultimate archive name. |
518 if not os.path.isdir(archive_dir): | 521 if not os.path.isdir(archive_dir): |
519 print 'Moving old "%s" file to create same name directory.' % archive_dir | 522 print 'Moving old "%s" file to create same name directory.' % archive_dir |
520 previous_archive_file = '%s.old' % archive_dir | 523 previous_archive_file = '%s.old' % archive_dir |
521 MoveFile(archive_dir, previous_archive_file) | 524 MoveFile(archive_dir, previous_archive_file) |
522 else: | 525 else: |
523 RemoveDirectory(archive_dir) | 526 RemoveDirectory(archive_dir) |
524 MaybeMakeDirectory(archive_dir) | 527 MaybeMakeDirectory(archive_dir) |
525 for needed_file in file_list: | 528 for needed_file in file_list: |
526 needed_file = needed_file.rstrip() | 529 needed_file = needed_file.rstrip() |
527 # These paths are relative to the file_relative_dir. We need to copy | 530 # These paths are relative to the file_relative_dir. We need to copy |
528 # them over maintaining the relative directories, where applicable. | 531 # them over maintaining the relative directories, where applicable. |
529 src_path = os.path.join(file_relative_dir, needed_file) | 532 src_path = os.path.join(file_relative_dir, needed_file) |
| 533 if path_filter: |
| 534 new_src_path = path_filter(src_path) |
| 535 if new_src_path is None: |
| 536 print 'Skipping %s' % src_path |
| 537 continue |
| 538 if new_src_path != src_path: |
| 539 print 'Replacing contents of %s with %s' % (src_path, new_src_path) |
| 540 src_path = new_src_path |
530 dirname, basename = os.path.split(needed_file) | 541 dirname, basename = os.path.split(needed_file) |
531 try: | 542 try: |
532 if os.path.isdir(src_path): | 543 if os.path.isdir(src_path): |
533 shutil.copytree(src_path, os.path.join(archive_dir, needed_file), | 544 shutil.copytree(src_path, os.path.join(archive_dir, needed_file), |
534 symlinks=True) | 545 symlinks=True) |
535 elif dirname != '' and basename != '': | 546 elif dirname != '' and basename != '': |
536 dest_dir = os.path.join(archive_dir, dirname) | 547 dest_dir = os.path.join(archive_dir, dirname) |
537 MaybeMakeDirectory(dest_dir) | 548 MaybeMakeDirectory(dest_dir) |
538 CopyFileToDir(src_path, dest_dir) | 549 CopyFileToDir(src_path, dest_dir, basename) |
539 else: | 550 else: |
540 CopyFileToDir(src_path, archive_dir) | 551 CopyFileToDir(src_path, archive_dir, basename) |
541 except PathNotFound: | 552 except PathNotFound: |
542 if raise_error: | 553 if raise_error: |
543 raise | 554 raise |
544 | 555 |
545 # Pack the zip file. | 556 # Pack the zip file. |
546 output_file = '%s.zip' % archive_dir | 557 output_file = '%s.zip' % archive_dir |
547 previous_file = '%s_old.zip' % archive_dir | 558 previous_file = '%s_old.zip' % archive_dir |
548 MoveFile(output_file, previous_file) | 559 MoveFile(output_file, previous_file) |
549 # On Windows we use the python zip module; on Linux and Mac, we use the zip | 560 # On Windows we use the python zip module; on Linux and Mac, we use the zip |
550 # command as it will handle links and file bits (executable). Which is much | 561 # command as it will handle links and file bits (executable). Which is much |
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1117 conn.close() | 1128 conn.close() |
1118 | 1129 |
1119 return rev, 'ok' | 1130 return rev, 'ok' |
1120 | 1131 |
1121 | 1132 |
1122 def AbsoluteCanonicalPath(*path): | 1133 def AbsoluteCanonicalPath(*path): |
1123 """Return the most canonical path Python can provide.""" | 1134 """Return the most canonical path Python can provide.""" |
1124 | 1135 |
1125 file_path = os.path.join(*path) | 1136 file_path = os.path.join(*path) |
1126 return os.path.realpath(os.path.abspath(os.path.expanduser(file_path))) | 1137 return os.path.realpath(os.path.abspath(os.path.expanduser(file_path))) |
OLD | NEW |