OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 import fnmatch | 6 import fnmatch |
7 import glob | 7 import glob |
8 import optparse | 8 import optparse |
9 import os | 9 import os |
10 import posixpath | 10 import posixpath |
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
462 print ' %s: %s (stored 0%%)' % (operation, zip_path) | 462 print ' %s: %s (stored 0%%)' % (operation, zip_path) |
463 elif zip_info.compress_type == zipfile.ZIP_DEFLATED: | 463 elif zip_info.compress_type == zipfile.ZIP_DEFLATED: |
464 print ' %s: %s (deflated %d%%)' % (operation, zip_path, | 464 print ' %s: %s (deflated %d%%)' % (operation, zip_path, |
465 100 - zip_info.compress_size * 100 / zip_info.file_size) | 465 100 - zip_info.compress_size * 100 / zip_info.file_size) |
466 finally: | 466 finally: |
467 zip_stream.close() | 467 zip_stream.close() |
468 | 468 |
469 return 0 | 469 return 0 |
470 | 470 |
471 | 471 |
472 def Which(args): | |
473 """A Unix style which. | |
474 | |
475 Looks for all arguments in the PATH environment variable, and prints their | |
476 path if they are executable files. | |
477 | |
478 Note: If you pass an argument with a path to which, it will just test if it | |
479 is executable, not if it is in the path. | |
480 """ | |
481 parser = optparse.OptionParser(usage='usage: which args...') | |
482 _, files = parser.parse_args(args) | |
483 if files == 0: | |
Sam Clegg
2013/02/09 01:28:14
if not files:
binji
2013/02/09 01:44:43
Done. Whoops.
| |
484 return | |
Sam Clegg
2013/02/09 01:28:14
parser.error("no files specified")?
No need for r
binji
2013/02/09 01:44:43
It's not an error to run which without arguments.
Sam Clegg
2013/02/09 01:49:28
Ha! At least it should return non-zero here then.
binji
2013/02/09 02:31:44
Done.
| |
485 | |
486 env_path = os.environ.get('PATH', '') | |
487 paths = env_path.split(os.pathsep) | |
488 | |
489 def IsExecutableFile(path): | |
490 return os.path.isfile(path) and os.access(path, os.X_OK) | |
491 | |
492 retval = 0 | |
493 for filename in files: | |
494 if os.path.sep in filename: | |
495 if IsExecutableFile(filename): | |
496 print filename | |
497 continue | |
498 | |
499 for path in paths: | |
500 filepath = os.path.join(path, filename) | |
501 if IsExecutableFile(filepath): | |
502 print os.path.abspath(os.path.join(path, filename)) | |
Sam Clegg
2013/02/09 01:28:14
Might want to also append try adding .com .bat and
binji
2013/02/09 01:44:43
Not needed in this CL, we can add it later if desi
| |
503 break | |
504 else: | |
505 retval = 1 | |
506 return retval | |
507 | |
508 | |
472 FuncMap = { | 509 FuncMap = { |
473 'cp': Copy, | 510 'cp': Copy, |
474 'mkdir': Mkdir, | 511 'mkdir': Mkdir, |
475 'mv': Move, | 512 'mv': Move, |
476 'rm': Remove, | 513 'rm': Remove, |
477 'zip': Zip, | 514 'zip': Zip, |
515 'which': Which, | |
478 } | 516 } |
479 | 517 |
480 | 518 |
481 def main(args): | 519 def main(args): |
482 if not args: | 520 if not args: |
483 print 'No command specified' | 521 print 'No command specified' |
484 print 'Available commands: %s' % ' '.join(FuncMap) | 522 print 'Available commands: %s' % ' '.join(FuncMap) |
485 return 1 | 523 return 1 |
486 func = FuncMap.get(args[0]) | 524 func = FuncMap.get(args[0]) |
487 if not func: | 525 if not func: |
488 print 'Do not recognize command: ' + args[0] | 526 print 'Do not recognize command: ' + args[0] |
489 print 'Available commands: %s' % ' '.join(FuncMap) | 527 print 'Available commands: %s' % ' '.join(FuncMap) |
490 return 1 | 528 return 1 |
491 return func(args[1:]) | 529 return func(args[1:]) |
492 | 530 |
493 if __name__ == '__main__': | 531 if __name__ == '__main__': |
494 sys.exit(main(sys.argv[1:])) | 532 sys.exit(main(sys.argv[1:])) |
OLD | NEW |