#!/usr/bin/env python
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 2; coding: utf-8 -*-
#
# This file is part of Déjà Dup.
# For copyright information, see AUTHORS.
#
# Déjà Dup is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Déjà Dup is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Déjà Dup.  If not, see <http://www.gnu.org/licenses/>.

# Test how well we handle user mucking with files while we back up

import sys
import os
sys.path.insert(0, sys.path[0]+'/..')
import base
import ldtp
import time
import re

last_written = 'a'

def rewrite(name, size):
  global last_written
  f = open(name, 'wb')
  string = ('{:%s<%d}' % (last_written, size)).format('')
  f.write(string)
  f.close()
  last_written = chr(ord(last_written) + 1) if last_written != 'z' else 'a'

def keep_writing(destdir, sourcename):
  count = 0
  rewrite(sourcename, 30 * 1024 * 1024)
  assert ldtp.waittillguiexist('frmBackUp')
  while ldtp.guiexist('frmBackUp'):
    rewrite(sourcename, 30 * 1024 * 1024)
    count += 1
    assert count < 200

def wait_for_volume(destdir):
  count = 0
  while len(os.listdir(destdir)) < 2 and count < 200:
    time.sleep(1)
    count += 1

def writing():
  global last_written
  last_written = 'a'
  base.setup()
  sourcedir = base.get_temp_name('source')
  os.makedirs(sourcedir)
  sourcefile = os.path.join(sourcedir, 'file')
  base.backup_simple(backend='file', includes=[sourcedir], finish=False)

  destdir = base.get_temp_name('local')
  keep_writing(destdir, sourcefile)

  restoredir = base.get_temp_name('restore')
  restorefile = os.path.join(restoredir, './'+sourcefile)
  base.restore_simple(restoredir)

  # make sure that whatever version of the file got written, got written complete
  f = open(restorefile, 'rb')
  string = f.read()
  f.close()
  assert re.match('^%c+$' % string[0], string), string

def pause_before():
  global last_written
  last_written = 'a'
  base.setup()
  sourcedir = base.get_temp_name('source')
  os.makedirs(sourcedir)

  rewrite(os.path.join(sourcedir, 'a'), 20)
  rewrite(os.path.join(sourcedir, 'b'), 30 * 1024 * 1024)
  rewrite(os.path.join(sourcedir, 'c'), 30)

  base.backup_simple(backend='file', includes=[sourcedir], finish=False)

  destdir = base.get_temp_name('local')
  wait_for_volume(destdir)
  ldtp.click('frmBackUp', 'btnResumeLater')

  rewrite(os.path.join(sourcedir, 'a'), 20)

  base.backup_simple()

  restoredir = base.get_temp_name('restore')
  restorefile = os.path.join(restoredir, './'+sourcedir, 'a')
  base.restore_simple(restoredir)

  f = open(restorefile, 'rb')
  string = f.read()
  f.close()
  assert len(string) == 20, string
  assert re.match('^d+$', string), string

def pause_middle():
  global last_written
  last_written = 'a'
  base.setup()
  sourcedir = base.get_temp_name('source')
  os.makedirs(sourcedir)

  rewrite(os.path.join(sourcedir, 'a'), 20)
  rewrite(os.path.join(sourcedir, 'b'), 30 * 1024 * 1024)
  rewrite(os.path.join(sourcedir, 'c'), 30)

  base.backup_simple(backend='file', includes=[sourcedir], finish=False)

  destdir = base.get_temp_name('local')
  wait_for_volume(destdir)
  ldtp.click('frmBackUp', 'btnResumeLater')

  rewrite(os.path.join(sourcedir, 'b'), 30 * 1024 * 1024)

  base.backup_simple()

  restoredir = base.get_temp_name('restore')
  restorefile = os.path.join(restoredir, './'+sourcedir, 'b')
  base.restore_simple(restoredir)

  f = open(restorefile, 'rb')
  string = f.read()
  f.close()
  assert len(string) == 30 * 1024 * 1024, string[0:1024]
  assert re.match('^d+$', string), string[0:1024]

base.run(writing)
base.run(pause_before)
base.run(pause_middle)

