Changeset 852:f883b3708318
- Timestamp:
- 2010-02-11 21:43:49 (6 months ago)
- Author:
- Stefan Schwarzer <sschwarzer@…>
- Branch:
- default
- Message:
-
Add a `_st_mtime_precision` value to stat result objects.
This contains the precision of the modification timestamp in
seconds. I plan to use this for a more robust decision in the
`upload_if_newer` and `download_if_newer` methods.
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r843
|
r852
|
|
| 1 | | # Copyright (C) 2003-2009, Stefan Schwarzer |
| | 1 | # Copyright (C) 2003-2010, Stefan Schwarzer |
| 2 | 2 | # All rights reserved. |
| 3 | 3 | # |
| … |
… |
|
| 280 | 280 | self.failIf(isinstance(exc_obj, ftp_error.FTPOSError)) |
| 281 | 281 | |
| 282 | | def test_lstat_one_file(self): |
| 283 | | """Test `lstat` for a file.""" |
| | 282 | def test_lstat_one_unix_file(self): |
| | 283 | """Test `lstat` for a file described in Unix-style format.""" |
| 284 | 284 | stat_result = self.stat.lstat('/home/sschwarzer/index.html') |
| 285 | 285 | self.assertEqual(oct(stat_result.st_mode), '0100644') |
| 286 | 286 | self.assertEqual(stat_result.st_size, 4604) |
| 287 | | |
| 288 | | def test_lstat_one_dir(self): |
| 289 | | """Test `lstat` for a directory.""" |
| | 287 | self.assertEqual(stat_result._st_mtime_precision, 60) |
| | 288 | |
| | 289 | def test_lstat_one_ms_file(self): |
| | 290 | """Test `lstat` for a file described in DOS-style format.""" |
| | 291 | stat_result = self.stat.lstat('/home/msformat/abcd.exe') |
| | 292 | self.assertEqual(stat_result._st_mtime_precision, 60) |
| | 293 | |
| | 294 | def test_lstat_one_unix_dir(self): |
| | 295 | """Test `lstat` for a directory described in Unix-style format.""" |
| 290 | 296 | stat_result = self.stat.lstat('/home/sschwarzer/scios2') |
| 291 | 297 | self.assertEqual(oct(stat_result.st_mode), '042755') |
| … |
… |
|
| 300 | 306 | stat_tuple_to_seconds((1999, 9, 20, 0, 0, 0))) |
| 301 | 307 | self.assertEqual(stat_result.st_ctime, None) |
| | 308 | self.assertEqual(stat_result._st_mtime_precision, 24*60*60) |
| 302 | 309 | self.failUnless(stat_result == |
| 303 | 310 | (17901, None, None, 6, '45854', '200', 512, None, |
| 304 | 311 | stat_tuple_to_seconds((1999, 9, 20, 0, 0, 0)), None)) |
| | 312 | |
| | 313 | def test_lstat_one_ms_dir(self): |
| | 314 | """Test `lstat` for a directory described in DOS-style format.""" |
| | 315 | stat_result = self.stat.lstat('/home/msformat/WindowsXP') |
| | 316 | self.assertEqual(stat_result._st_mtime_precision, 60) |
| 305 | 317 | |
| 306 | 318 | def test_lstat_via_stat_module(self): |
-
|
r843
|
r852
|
|
| 1 | | # Copyright (C) 2002-2008, Stefan Schwarzer |
| | 1 | # Copyright (C) 2002-2010, Stefan Schwarzer |
| 2 | 2 | # All rights reserved. |
| 3 | 3 | # |
| … |
… |
|
| 148 | 148 | |
| 149 | 149 | def parse_unix_time(self, month_abbreviation, day, year_or_time, |
| 150 | | time_shift): |
| | 150 | time_shift, with_precision=False): |
| 151 | 151 | """ |
| 152 | 152 | Return a floating point number, like from `time.mktime`, by |
| … |
… |
|
| 155 | 155 | "time on server" - "time on client" and is available as the |
| 156 | 156 | `time_shift` parameter in the `parse_line` interface. |
| | 157 | |
| | 158 | If `with_precision` is true (default: false), return a |
| | 159 | two-element tuple consisting of the floating point number as |
| | 160 | described in the previous paragraph and the precision of the |
| | 161 | time in seconds. This takes into account that, for example, a |
| | 162 | time string like "May 26 2005" has only a precision of one |
| | 163 | day. This information is important for the `upload_if_newer` |
| | 164 | and `download_if_newer` methods in the `FTPHost` class. |
| 157 | 165 | |
| 158 | 166 | Times in Unix-style directory listings typically have one of |
| … |
… |
|
| 176 | 184 | st_mtime = time.mktime( (year, month, day, |
| 177 | 185 | hour, minute, 0, 0, 0, -1) ) |
| | 186 | # precise up to a day |
| | 187 | st_mtime_precision = 24 * 60 * 60 |
| 178 | 188 | else: |
| 179 | 189 | # `year_or_time` is a time hh:mm |
| … |
… |
|
| 184 | 194 | st_mtime = time.mktime( (year, month, day, |
| 185 | 195 | hour, minute, 0, 0, 0, -1) ) |
| | 196 | # precise up to a minute |
| | 197 | st_mtime_precision = 60 |
| 186 | 198 | # rhs of comparison: transform client time to server time |
| 187 | 199 | # (as on the lhs), so both can be compared with respect |
| … |
… |
|
| 199 | 211 | st_mtime = time.mktime( (year-1, month, day, |
| 200 | 212 | hour, minute, 0, 0, 0, -1) ) |
| 201 | | return st_mtime |
| | 213 | if with_precision: |
| | 214 | return (st_mtime, st_mtime_precision) |
| | 215 | else: |
| | 216 | return st_mtime |
| 202 | 217 | |
| 203 | 218 | def parse_ms_time(self, date, time_, time_shift): |
| … |
… |
|
| 221 | 236 | # don't complain about unused `time_shift` argument |
| 222 | 237 | # pylint: disable-msg=W0613 |
| | 238 | # For the time being, I don't add a `with_precision` |
| | 239 | # parameter as in the Unix parser because the precision for |
| | 240 | # the DOS format is always a minute and can be set in |
| | 241 | # `MSParser.parse_line`. Should you find yourself needing |
| | 242 | # support for `with_precision` for a derived class, please |
| | 243 | # write a mail (see ftputil.txt/html). |
| 223 | 244 | try: |
| 224 | 245 | month, day, year = [int(part) for part in date.split('-')] |
| … |
… |
|
| 294 | 315 | st_atime = None |
| 295 | 316 | # st_mtime |
| 296 | | st_mtime = self.parse_unix_time(month, day, year_or_time, time_shift) |
| | 317 | st_mtime, st_mtime_precision = \ |
| | 318 | self.parse_unix_time(month, day, year_or_time, time_shift, |
| | 319 | with_precision=True) |
| 297 | 320 | # st_ctime |
| 298 | 321 | st_ctime = None |
| … |
… |
|
| 305 | 328 | (st_mode, st_ino, st_dev, st_nlink, st_uid, |
| 306 | 329 | st_gid, st_size, st_atime, st_mtime, st_ctime) ) |
| | 330 | stat_result._st_mtime_precision = st_mtime_precision |
| 307 | 331 | stat_result._st_name = st_name |
| 308 | 332 | stat_result._st_target = st_target |
| … |
… |
|
| 362 | 386 | stat_result._st_name = name |
| 363 | 387 | stat_result._st_target = None |
| | 388 | # mtime precision in seconds |
| | 389 | stat_result._st_mtime_precision = 60 |
| 364 | 390 | return stat_result |
| 365 | 391 | |