1# Copyright 2017 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5"""Module for helper methods related to ResultInfo. 6""" 7 8import os 9 10 11def _get_file_stat(path): 12 """Get the os.stat of the file at the given path. 13 14 @param path: Path to the file. 15 @return: os.stat of the file. Return None if file doesn't exist. 16 """ 17 try: 18 return os.stat(path) 19 except OSError: 20 # File was deleted already. 21 return None 22 23 24def get_file_size(path): 25 """Get the size of the file in bytes for the given path. 26 27 @param path: Path to the file. 28 @return: Size in bytes for the given file. Return 0 if file doesn't exist. 29 """ 30 stat = _get_file_stat(path) 31 return stat.st_size if stat else 0 32 33 34def get_last_modification_time(path): 35 """Get the last modification time for the given path. 36 37 @param path: Path to the file. 38 @return: The last modification time of the given file as a unix timestamp 39 int, e.g., 1497896071 40 """ 41 stat = _get_file_stat(path) 42 return stat.st_mtime if stat else 0 43