diff options
Diffstat (limited to 'subcmds')
| -rw-r--r-- | subcmds/sync.py | 34 |
1 files changed, 15 insertions, 19 deletions
diff --git a/subcmds/sync.py b/subcmds/sync.py index 324f15b5..68a076df 100644 --- a/subcmds/sync.py +++ b/subcmds/sync.py | |||
| @@ -1805,44 +1805,40 @@ class _FetchTimes(object): | |||
| 1805 | 1805 | ||
| 1806 | def __init__(self, manifest): | 1806 | def __init__(self, manifest): |
| 1807 | self._path = os.path.join(manifest.repodir, ".repo_fetchtimes.json") | 1807 | self._path = os.path.join(manifest.repodir, ".repo_fetchtimes.json") |
| 1808 | self._times = None | 1808 | self._saved = None |
| 1809 | self._seen = set() | 1809 | self._seen = {} |
| 1810 | 1810 | ||
| 1811 | def Get(self, project): | 1811 | def Get(self, project): |
| 1812 | self._Load() | 1812 | self._Load() |
| 1813 | return self._times.get(project.name, _ONE_DAY_S) | 1813 | return self._saved.get(project.name, _ONE_DAY_S) |
| 1814 | 1814 | ||
| 1815 | def Set(self, project, t): | 1815 | def Set(self, project, t): |
| 1816 | self._Load() | ||
| 1817 | name = project.name | 1816 | name = project.name |
| 1818 | old = self._times.get(name, t) | 1817 | |
| 1819 | self._seen.add(name) | 1818 | # For shared projects, save the longest time. |
| 1820 | a = self._ALPHA | 1819 | self._seen[name] = max(self._seen.get(name, 0), t) |
| 1821 | self._times[name] = (a * t) + ((1 - a) * old) | ||
| 1822 | 1820 | ||
| 1823 | def _Load(self): | 1821 | def _Load(self): |
| 1824 | if self._times is None: | 1822 | if self._saved is None: |
| 1825 | try: | 1823 | try: |
| 1826 | with open(self._path) as f: | 1824 | with open(self._path) as f: |
| 1827 | self._times = json.load(f) | 1825 | self._saved = json.load(f) |
| 1828 | except (IOError, ValueError): | 1826 | except (IOError, ValueError): |
| 1829 | platform_utils.remove(self._path, missing_ok=True) | 1827 | platform_utils.remove(self._path, missing_ok=True) |
| 1830 | self._times = {} | 1828 | self._saved = {} |
| 1831 | 1829 | ||
| 1832 | def Save(self): | 1830 | def Save(self): |
| 1833 | if self._times is None: | 1831 | if self._saved is None: |
| 1834 | return | 1832 | return |
| 1835 | 1833 | ||
| 1836 | to_delete = [] | 1834 | for name, t in self._seen.items(): |
| 1837 | for name in self._times: | 1835 | # Keep a moving average across the previous/current sync runs. |
| 1838 | if name not in self._seen: | 1836 | old = self._saved.get(name, t) |
| 1839 | to_delete.append(name) | 1837 | self._seen[name] = (self._ALPHA * t) + ((1 - self._ALPHA) * old) |
| 1840 | for name in to_delete: | ||
| 1841 | del self._times[name] | ||
| 1842 | 1838 | ||
| 1843 | try: | 1839 | try: |
| 1844 | with open(self._path, "w") as f: | 1840 | with open(self._path, "w") as f: |
| 1845 | json.dump(self._times, f, indent=2) | 1841 | json.dump(self._seen, f, indent=2) |
| 1846 | except (IOError, TypeError): | 1842 | except (IOError, TypeError): |
| 1847 | platform_utils.remove(self._path, missing_ok=True) | 1843 | platform_utils.remove(self._path, missing_ok=True) |
| 1848 | 1844 | ||
