Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2017 The LUCI Authors. All rights reserved. | 2 # Copyright 2017 The LUCI Authors. All rights reserved. |
| 3 # Use of this source code is governed under the Apache License, Version 2.0 | 3 # Use of this source code is governed under the Apache License, Version 2.0 |
| 4 # that can be found in the LICENSE file. | 4 # that can be found in the LICENSE file. |
| 5 | 5 |
| 6 """Utility exporting basic filesystem operations. | 6 """Utility exporting basic filesystem operations. |
| 7 | 7 |
| 8 This file was cut from "scripts/common/chromium_utils.py" at: | 8 This file was cut from "scripts/common/chromium_utils.py" at: |
| 9 91310531c31fa645256b4fb5d44b460c42b3e151 | 9 91310531c31fa645256b4fb5d44b460c42b3e151 |
| 10 """ | 10 """ |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 243 func=lambda opts: print('\n'.join(sorted(os.listdir(opts.source))))) | 243 func=lambda opts: print('\n'.join(sorted(os.listdir(opts.source))))) |
| 244 | 244 |
| 245 # Subcommand: ensure-directory | 245 # Subcommand: ensure-directory |
| 246 subparser = subparsers.add_parser('ensure-directory', | 246 subparser = subparsers.add_parser('ensure-directory', |
| 247 help='Ensures that the given path is a directory.') | 247 help='Ensures that the given path is a directory.') |
| 248 subparser.add_argument('--mode', help='The octal mode of the directory.', | 248 subparser.add_argument('--mode', help='The octal mode of the directory.', |
| 249 type=lambda s: int(s, 8)) | 249 type=lambda s: int(s, 8)) |
| 250 subparser.add_argument('dest', help='The dir to ensure.') | 250 subparser.add_argument('dest', help='The dir to ensure.') |
| 251 subparser.set_defaults(func=lambda opts: _EnsureDir(opts.mode, opts.dest)) | 251 subparser.set_defaults(func=lambda opts: _EnsureDir(opts.mode, opts.dest)) |
| 252 | 252 |
| 253 # Subcommand: filesize | |
| 254 subparser = subparsers.add_parser('filesize', | |
| 255 help='Prints a list for sizes in bytes (1 per line) for each given file') | |
| 256 subparser.add_argument('file', nargs='+', help='Path to a file') | |
| 257 subparser.set_defaults( | |
| 258 func=lambda opts: print('\n'.join(str(os.stat(f).st_size) | |
| 259 for f in opts.file))) | |
|
Michael Achenbach
2017/08/07 07:37:40
nit: The for loop should be indented on the level
tandrii(chromium)
2017/08/07 10:59:49
Done.
| |
| 260 | |
| 253 # Parse arguments. | 261 # Parse arguments. |
| 254 opts = parser.parse_args(args) | 262 opts = parser.parse_args(args) |
| 255 | 263 |
| 256 # Actually do the thing. | 264 # Actually do the thing. |
| 257 data = { | 265 data = { |
| 258 'ok': False, | 266 'ok': False, |
| 259 'errno_name': '', | 267 'errno_name': '', |
| 260 'message': '', | 268 'message': '', |
| 261 } | 269 } |
| 262 try: | 270 try: |
| 263 opts.func(opts) | 271 opts.func(opts) |
| 264 data['ok'] = True | 272 data['ok'] = True |
| 265 except OSError as e: | 273 except OSError as e: |
| 266 data['errno_name'] = errno.errorcode[e.errno] | 274 data['errno_name'] = errno.errorcode[e.errno] |
| 267 data['message'] = str(e) | 275 data['message'] = str(e) |
| 268 except shutil.Error as e: | 276 except shutil.Error as e: |
| 269 data['message'] = e.message | 277 data['message'] = e.message |
| 270 except Exception as e: | 278 except Exception as e: |
| 271 data['message'] = 'UNKNOWN: %s' % e | 279 data['message'] = 'UNKNOWN: %s' % e |
| 272 | 280 |
| 273 with opts.json_output: | 281 with opts.json_output: |
| 274 json.dump(data, opts.json_output) | 282 json.dump(data, opts.json_output) |
| 275 | 283 |
| 276 return 0 | 284 return 0 |
| 277 | 285 |
| 278 | 286 |
| 279 if __name__ == '__main__': | 287 if __name__ == '__main__': |
| 280 sys.exit(main(sys.argv[1:])) | 288 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |