1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# ----------------------------------------------------------------------------- |
3
|
|
|
# Copyright (c) 2015 Yann Lanthony |
4
|
|
|
# Copyright (c) 2017-2018 Spyder Project Contributors |
5
|
|
|
# |
6
|
|
|
# Licensed under the terms of the MIT License |
7
|
|
|
# (See LICENSE.txt for details) |
8
|
|
|
# ----------------------------------------------------------------------------- |
9
|
|
|
"""Contains the fallback implementation of the Watcher api.""" |
10
|
|
|
|
11
|
|
|
# yapf: disable |
12
|
|
|
|
13
|
|
|
from __future__ import absolute_import, print_function |
14
|
|
|
|
15
|
|
|
# Standard library imports |
16
|
|
|
import os |
17
|
|
|
|
18
|
|
|
# Local imports |
19
|
|
|
from qtsass.importers import norm_path |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
# yapf: enable |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
def take(dir_or_file, depth=3): |
26
|
|
|
"""Return a dict mapping files and folders to their mtimes.""" |
27
|
|
|
if os.path.isfile(dir_or_file): |
28
|
|
|
path = norm_path(dir_or_file) |
29
|
|
|
return {path: os.path.getmtime(path)} |
30
|
|
|
|
31
|
|
|
if not os.path.isdir(dir_or_file): |
32
|
|
|
return {} |
33
|
|
|
|
34
|
|
|
snapshot = {} |
35
|
|
|
base_depth = len(norm_path(dir_or_file).split('/')) |
36
|
|
|
|
37
|
|
|
for root, subdirs, files in os.walk(dir_or_file): |
38
|
|
|
|
39
|
|
|
path = norm_path(root) |
40
|
|
|
if len(path.split('/')) - base_depth == depth: |
41
|
|
|
subdirs[:] = [] |
42
|
|
|
|
43
|
|
|
snapshot[path] = os.path.getmtime(path) |
44
|
|
|
for f in files: |
45
|
|
|
path = norm_path(root, f) |
46
|
|
|
snapshot[path] = os.path.getmtime(path) |
47
|
|
|
|
48
|
|
|
return snapshot |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def diff(prev_snapshot, next_snapshot): |
52
|
|
|
"""Return a dict containing changes between two snapshots.""" |
53
|
|
|
changes = {} |
54
|
|
|
for path in set(prev_snapshot.keys()) | set(next_snapshot.keys()): |
55
|
|
|
if path in prev_snapshot and path not in next_snapshot: |
56
|
|
|
changes[path] = 'Deleted' |
57
|
|
|
elif path not in prev_snapshot and path in next_snapshot: |
58
|
|
|
changes[path] = 'Created' |
59
|
|
|
else: |
60
|
|
|
prev_mtime = prev_snapshot[path] |
61
|
|
|
next_mtime = next_snapshot[path] |
62
|
|
|
if next_mtime > prev_mtime: |
63
|
|
|
changes[path] = 'Changed' |
64
|
|
|
return changes |
65
|
|
|
|