Skip to content

Commit 974a386

Browse files
committed
add support for directory creation
1 parent 0014e48 commit 974a386

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

moonmen/filesystem/FileSystemManager.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ def getDiffDict(self, difference):
5555
param difference: L{watchdog.utils.dirsnapshot.DirectorySnapshotDiff}
5656
"""
5757
diffDict = {}
58+
print(difference.__dict__.items())
5859
for key, value in difference.__dict__.items():
5960
if key in ('_dirs_moved', '_files_moved'):
6061
diffDict[key[1:]] = list(starmap(
6162
lambda p1, p2: (relpath(p1, self.directoryPath),
6263
relpath(p2, self.directoryPath)), value))
6364
else:
6465
diffDict[key[1:]] = list(map(lambda p: relpath(p, self.directoryPath), value))
66+
print (diffDict)
6567
return diffDict
6668

6769
def syncBackupState(self):
@@ -83,7 +85,7 @@ def createDirectory(self, path):
8385
8486
param path : path of the directory to create
8587
"""
86-
os.makedirs(relpath(path, self.directoryPath), exist_ok=True)
88+
os.makedirs(relpath(self.directoryPath+'/'+path), exist_ok=True)
8789

8890
def deleteDirectory(self, path):
8991
"""

moonmen/synchronization/SynchronizationManager.py

+20
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ def getDeleteFileProcesses(self):
8686
if deletedFile not in (self.mySystemModififications['files_deleted'] + self.mySystemModififications['files_modified'] + self.mySystemModififications['files_created'] + self.mySystemMovedSource + self.mySystemMovedDestination):
8787
mySystemFileProcesses.append(('delete', deletedFile))
8888

89+
for deletedDirectory in self.mySystemModififications['dirs_deleted']:
90+
if deletedDirectory not in (self.otherSystemModifications['dirs_deleted'] + self.otherSystemModifications['dirs_modified'] + self.otherSystemModifications['dirs_created'] + self.otherSystemMovedSource + self.otherSystemMovedDestination):
91+
otherSystemFileProcesses.append(('deletedDirectory', deletedDirectory))
92+
93+
for deletedDirectory in self.otherSystemModifications['dirs_deleted']:
94+
if deletedDirectory not in (self.mySystemModififications['dirs_deleted'] + self.mySystemModififications['dirs_modified'] + self.mySystemModififications['dirs_created'] + self.mySystemMovedSource + self.mySystemMovedDestination):
95+
mySystemFileProcesses.append(('deletedDirectory', deletedDirectory))
96+
8997
return (mySystemFileProcesses, otherSystemFileProcesses)
9098

9199

@@ -105,6 +113,14 @@ def getCreateFileProcesses(self):
105113
if createdFile not in self.mySystemModififications['files_created']:
106114
mySystemFileProcesses.append(('create', createdFile))
107115

116+
for createdDirectory in self.mySystemModififications['dirs_created']:
117+
if createdDirectory not in self.otherSystemModifications['dirs_created']:
118+
otherSystemFileProcesses.append(('createDirectory', createdDirectory))
119+
120+
for createdDirectory in self.otherSystemModifications['dirs_created']:
121+
if createdDirectory not in self.mySystemModififications['dirs_created']:
122+
mySystemFileProcesses.append(('createDirectory', createdDirectory))
123+
108124

109125

110126
return (mySystemFileProcesses, otherSystemFileProcesses)
@@ -234,6 +250,10 @@ def synchronize(systemFileProcesses, contents, filesystemManager):
234250
filesystemManager.writeFile(systemFileProcess[1], contents[systemFileProcess[1]])
235251
elif systemFileProcess[0] == 'delete':
236252
filesystemManager.deleteFile(systemFileProcess[1])
253+
elif systemFileProcess[0] == 'deletedDirectory':
254+
filesystemManager.deleteDirectory(systemFileProcess[1])
255+
elif systemFileProcess[0] == 'createDirectory':
256+
filesystemManager.createDirectory(systemFileProcess[1])
237257
else:
238258
filesystemManager.moveFile(systemFileProcess[1][0], systemFileProcess[1][1])
239259

moonmen/test_synchronizermanager.py

+87
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,35 @@ def test_fileDeletedOnMyDevice(self):
3737
self.assertCountEqual(mySystemFileProcesses, expectedMySystemFileProcesses)
3838
self.assertCountEqual(otherSystemFileProcesses, expectedOtherSystemFileProcesses)
3939

40+
41+
def test_directoryDeletedOnMyDevice(self):
42+
mySystemModifications = {
43+
"dirs_created": [],
44+
"dirs_deleted": ['deleted_directory'],
45+
"dirs_modified": [],
46+
"dirs_moved": [],
47+
"files_created": [],
48+
"files_deleted": [],
49+
"files_modified": [],
50+
"files_moved": [],
51+
}
52+
otherSystemModifications = {
53+
"dirs_created": [],
54+
"dirs_deleted": [],
55+
"dirs_modified": [],
56+
"dirs_moved": [],
57+
"files_created": [],
58+
"files_deleted": [],
59+
"files_modified": [],
60+
"files_moved": [],
61+
}
62+
synchronizer = SynchronizationManager(mySystemModifications, otherSystemModifications)
63+
mySystemFileProcesses, otherSystemFileProcesses = synchronizer.getFileProcesses()
64+
expectedMySystemFileProcesses = []
65+
expectedOtherSystemFileProcesses = [('deletedDirectory', 'deleted_directory')]
66+
self.assertCountEqual(mySystemFileProcesses, expectedMySystemFileProcesses)
67+
self.assertCountEqual(otherSystemFileProcesses, expectedOtherSystemFileProcesses)
68+
4069
def test_fileDeletedOnOtherDevice(self):
4170
mySystemModifications = {
4271
"dirs_created": [],
@@ -65,6 +94,35 @@ def test_fileDeletedOnOtherDevice(self):
6594
self.assertCountEqual(otherSystemFileProcesses, expectedOtherSystemFileProcesses)
6695
self.assertCountEqual(mySystemFileProcesses, expectedMySystemFileProcesses)
6796

97+
98+
def test_directoryDeletedOnOtherDevice(self):
99+
mySystemModifications = {
100+
"dirs_created": [],
101+
"dirs_deleted": [],
102+
"dirs_modified": [],
103+
"dirs_moved": [],
104+
"files_created": [],
105+
"files_deleted": [],
106+
"files_modified": [],
107+
"files_moved": [],
108+
}
109+
otherSystemModifications = {
110+
"dirs_created": [],
111+
"dirs_deleted": ['deleted_directory'],
112+
"dirs_modified": [],
113+
"dirs_moved": [],
114+
"files_created": [],
115+
"files_deleted": [],
116+
"files_modified": [],
117+
"files_moved": [],
118+
}
119+
synchronizer = SynchronizationManager(mySystemModifications, otherSystemModifications)
120+
mySystemFileProcesses, otherSystemFileProcesses = synchronizer.getFileProcesses()
121+
expectedMySystemFileProcesses = [('deletedDirectory', 'deleted_directory')]
122+
expectedOtherSystemFileProcesses = []
123+
self.assertCountEqual(otherSystemFileProcesses, expectedOtherSystemFileProcesses)
124+
self.assertCountEqual(mySystemFileProcesses, expectedMySystemFileProcesses)
125+
68126
def test_fileDeletedOnBothDevices(self):
69127
mySystemModifications = {
70128
"dirs_created": [],
@@ -121,6 +179,35 @@ def test_fileCreatedOnMyDevice(self):
121179
self.assertCountEqual(mySystemFileProcesses, expectedMySystemFileProcesses,)
122180
self.assertCountEqual(otherSystemFileProcesses, expectedOtherSystemFileProcesses)
123181

182+
def test_directoryCreatedOnMyDevice(self):
183+
184+
mySystemModifications = {
185+
"dirs_created": ['created_directory'],
186+
"dirs_deleted": [],
187+
"dirs_modified": [],
188+
"dirs_moved": [],
189+
"files_created": [],
190+
"files_deleted": [],
191+
"files_modified": [],
192+
"files_moved": [],
193+
}
194+
otherSystemModifications = {
195+
"dirs_created": [],
196+
"dirs_deleted": [],
197+
"dirs_modified": [],
198+
"dirs_moved": [],
199+
"files_created": [],
200+
"files_deleted": [],
201+
"files_modified": [],
202+
"files_moved": [],
203+
}
204+
synchronizer = SynchronizationManager(mySystemModifications, otherSystemModifications)
205+
mySystemFileProcesses, otherSystemFileProcesses = synchronizer.getFileProcesses()
206+
expectedMySystemFileProcesses = []
207+
expectedOtherSystemFileProcesses = [('createDirectory', 'created_directory')]
208+
self.assertCountEqual(mySystemFileProcesses, expectedMySystemFileProcesses,)
209+
self.assertCountEqual(otherSystemFileProcesses, expectedOtherSystemFileProcesses)
210+
124211

125212
def test_fileCreatedOnOtherDevice(self):
126213
mySystemModifications = {

0 commit comments

Comments
 (0)