~time-drive-devel/time-drive/time-drive.sync

« back to all changes in this revision

Viewing changes to sync/__init__.py

  • Committer: Rob Oakes
  • Date: 2012-11-27 22:41:26 UTC
  • Revision ID: rob.oakes@oak-tree.us-20121127224126-pnofhrbj0vad4yzx
Added support for the deletion of remote files when they aren't present on the local disk. Worked to cleanup the downloadFile method so that it is better able to manage edge cases.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
# Time-Drive Imports
10
10
from settings import appSettings, collectionSettings
11
11
from local import SyncFile, createDataConnection, createDataTables, \
12
 
        getLocalFileRecord, getLocalFolderRecord
 
12
        getLocalFileRecord, getLocalFolderRecord, getDatabaseFolderContents
13
13
from remote import RemoteResource, getRemoteCacheRecord
14
14
from syncxml import parseManifest
15
15
from syncaction import checkMd5, updateRemoteCache, \
17
17
        replaceLocalFile, missingManifestFile, \
18
18
        mergeExistingFile, deleteRemoteFile, deleteLocalFile, \
19
19
        downloadRemoteAsConflict, updateLocalFolderRecord_FromRemote, \
20
 
        updateLocalFolderRecord_FromLocal
 
20
        updateLocalFolderRecord_FromLocal, updateDatabaseRecords, markDatabaseFileDeleted
21
21
from dateutils import *
22
22
from pathutils import *
23
23
 
428
428
        else: changelist_ = change_list
429
429
        folder_modified = 0
430
430
 
431
 
        # Retrieve reference to local
432
 
        if parent_dbfolder == None:
433
 
                top_dbfolder = getLocalFolderRecord(wd_, db_session)
434
 
                # Retrieve mdate and cdate for the directory
435
 
                top_dbfolder_cdate, top_dbfolder_mdate = getFileDates(wd_)
436
 
 
437
 
                # Compare database mdate with database mdate
438
 
                if top_dbfolder is not None:
439
 
 
440
 
                        # If there is a match, there are no changes to the directory
441
 
                        if (top_dbfolder_mdate == top_dbfolder.mdate):
442
 
                                logging.info('Top-level mdate matches for collection: ' \
443
 
                                        + str(collection_['Collection-Name']))
444
 
                                return changelist_
445
 
 
446
431
        # Compare files and folders, collect files that have been modified
447
432
 
448
433
        # Retrieve list of the folder contents
449
 
        # try: 
450
 
        wd_contents = os.listdir(wd_)
451
 
        # except OSError: wd_contents = []
 
434
        try: wd_contents = os.listdir(wd_)
 
435
        except OSError: wd_contents = []
452
436
 
453
437
        # Iterate through contents of directory, compare file information to database
454
438
        for resource_localname in wd_contents:
471
455
                                db_session, change_list = changelist_, parent_dbfolder=dbfolder_,
472
456
                                check_files = analyze_folder)
473
457
 
 
458
                        # Locate files that were removed on the local disk,
 
459
                        # but still exist in the database
 
460
                        dbfolder_foldercontents = getDatabaseFolderContents(
 
461
                                resource_localpath, db_session)
 
462
                        for dbfolder_file in dbfolder_foldercontents:
 
463
                                if not os.path.exists(dbfolder_file.full_path):
 
464
                                        changelist_.append(dbfolder_file.full_path)
 
465
 
474
466
                # Compare files in the directory to those in the database
475
467
                # Compares both the mdate and the md5 signature to detect files
476
468
                elif (os.path.isfile(resource_localpath) == True) & (check_files == True):
504
496
                        (parent_cdate, parent_mdate),
505
497
                        parent_dbfolder, db_session)
506
498
 
507
 
        # Update top level folder record
508
 
        if (parent_dbfolder == None):
509
 
                if len(changelist_) == 0:
510
 
                        logging.info('Top-Level Folder Record (Local): ' + wd_)
511
 
                        updateLocalFolderRecord_FromLocal(
512
 
                                wd_, (top_dbfolder_cdate, top_dbfolder_mdate),
513
 
                                top_dbfolder, db_session)
514
 
 
515
499
        # Save any pending changes to the database
516
500
        db_session.commit()
517
501