Skip to content

Commit 8117b98

Browse files
committed
Added app data unit test and fixed downloading of app data files
1 parent c41e3d4 commit 8117b98

5 files changed

Lines changed: 45 additions & 23 deletions

File tree

main.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import unicode_literals
22

3-
import pyperclip
43
import asyncio
54
import re
65
import os
@@ -10,7 +9,7 @@
109
from shutil import rmtree
1110
import traceback
1211

13-
from rimlink import generateStructure, compareStructures, getRimworldConfigArea, isAdmin
12+
from rimlink import generateStructure, compareStructures, AppDataStructure, isAdmin
1413

1514

1615

@@ -29,6 +28,7 @@ def validateIP(givenIp):
2928

3029

3130
def menu(prompt, validator, useClipboard=False):
31+
import pyperclip
3232
if useClipboard:
3333
input(prompt)
3434
result = pyperclip.paste()
@@ -58,9 +58,10 @@ def hangForever():
5858
while True:
5959
time.sleep(120)
6060

61-
def clientSyncFiles(to_delete, to_add, to_modify):
61+
def clientSyncFiles(to_delete, to_add, to_modify, **kwargs):
6262
to_delete.extend(to_modify)
6363
to_add.extend(to_modify)
64+
testing = kwargs.get("testing", None)
6465
del to_modify
6566
folders = []
6667
for delete in to_delete:
@@ -86,6 +87,9 @@ def clientSyncFiles(to_delete, to_add, to_modify):
8687

8788
i = 0
8889
for file_name in to_add:
90+
if testing:
91+
print("Downloaded {}".format(file_name))
92+
continue
8993
s = socket.socket()
9094
s.connect((IP_ADDRESS, PORT))
9195
Server.clientRecieveFile(s, file_name.path())
@@ -139,7 +143,7 @@ def client():
139143
s = socket.socket()
140144
s.connect((IP_ADDRESS, PORT))
141145
if sync_config:
142-
my_config = generateStructure(getRimworldConfigArea(), app_data=getRimworldConfigArea())
146+
my_config = generateStructure(AppDataStructure.getRimworldConfigArea(), app_data=AppDataStructure.getRimworldConfigArea())
143147
my_config_pickled = pickle.dumps(my_config)
144148
s.send(b"\02")
145149
Server.clientSendPickle(s, my_config_pickled)
@@ -281,7 +285,7 @@ async def _handle_client(self, r, w):
281285
async def run(self):
282286
print("Analyzing rimworld...")
283287
self.base_structure = generateStructure(".")
284-
self.base_app_data_structure = generateStructure(getRimworldConfigArea(), app_data=getRimworldConfigArea())
288+
self.base_app_data_structure = generateStructure(AppDataStructure.getRimworldConfigArea(), app_data=AppDataStructure.getRimworldConfigArea())
285289
print("Ready to receive connections on {}:{}".format(IP_ADDRESS, PORT))
286290
await asyncio.start_server(self._handle_client, IP_ADDRESS, PORT)
287291

rimlink.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ def isAdmin():
1515
is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
1616
return is_admin
1717

18-
def getRimworldConfigArea():
19-
roaming = os.getenv("APPDATA")
20-
app_data = "\\".join(roaming.split("\\")[:-1])
21-
return os.path.join(os.path.join(os.path.join(app_data, "LocalLow"), "Ludeon Studios"), "RimWorld by Ludeon Studios")
22-
2318
class FileFolder:
2419
def __init__(self, name, parent=None, **kwargs):
2520
assert isinstance(name, str)
@@ -64,12 +59,17 @@ def hashFile(givenFile):
6459
h = hashlib.sha256()
6560
b = bytearray(128*1024)
6661
mv = memoryview(b)
67-
try:
68-
with open(givenFile, 'rb', buffering=0) as f:
69-
for n in iter(lambda : f.readinto(mv), 0):
70-
h.update(mv[:n])
71-
except:
72-
return "permission_denied"
62+
if os.path.isdir(givenFile):
63+
return "folder"
64+
elif os.path.isfile(givenFile):
65+
try:
66+
with open(givenFile, 'rb', buffering=0) as f:
67+
for n in iter(lambda : f.readinto(mv), 0):
68+
h.update(mv[:n])
69+
except:
70+
return "permission_denied"
71+
else:
72+
raise Exception("{} does not exist".format(givenFile))
7373
return h.hexdigest()
7474

7575
def compareFiles(file1, file2):
@@ -86,14 +86,19 @@ def __init__(self, name, parent=None, **kwargs):
8686

8787
class AppDataStructure(HashStructure):
8888
def __init__(self, name, parent=None, **kwargs):
89-
self.app_data_path = kwargs['app_data']
9089
super(AppDataStructure, self).__init__(name, parent, **kwargs)
9190

9291
def path(self):
9392
if self.parent:
9493
return os.path.join(self.parent.path(), self.name)
9594
else:
96-
return self.app_data_path
95+
return AppDataStructure.getRimworldConfigArea()
96+
@staticmethod
97+
def getRimworldConfigArea():
98+
roaming = os.getenv("APPDATA")
99+
app_data = "\\".join(roaming.split("\\")[:-1])
100+
return os.path.join(os.path.join(os.path.join(app_data, "LocalLow"), "Ludeon Studios"), "RimWorld by Ludeon Studios")
101+
97102

98103
FILE_EXCEPTIONS = ["__pycache__", "Saves", "Scenarios", "MpReplays", "MpDesyncs", "Player.log", "Player-prev.log", ".gitignore", ".git", "rimlink.exe"]
99104
def generateStructure(relativePositionStart, parent=None, **kwargs):

test.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import unicode_literals
22

33
from rimlink import *
4+
from main import clientSyncFiles
45

56
import unittest
67
import os
@@ -142,7 +143,7 @@ def test_different_deep(self):
142143
self.assertEqual(results['modify'], [r"Interior\deep\hihi.txt"])
143144
self.assertEqual(results['add'], [])
144145
def test_app_data(self):
145-
TOP_LOCATION = getRimworldConfigArea()
146+
TOP_LOCATION = AppDataStructure.getRimworldConfigArea()
146147
INNER_LOCATION = os.path.join(TOP_LOCATION, "testing")
147148
try:
148149
os.mkdir(INNER_LOCATION)
@@ -156,12 +157,12 @@ def test_app_data(self):
156157
file = open(add_inner, "w")
157158
file.write("testing")
158159
file.close()
159-
base_structure = generateStructure(getRimworldConfigArea(), app_data=getRimworldConfigArea())
160+
base_structure = generateStructure(AppDataStructure.getRimworldConfigArea(), app_data=AppDataStructure.getRimworldConfigArea())
160161

161162
os.remove(add)
162163
os.remove(add_inner)
163164

164-
after_structure = generateStructure(getRimworldConfigArea(), app_data=getRimworldConfigArea())
165+
after_structure = generateStructure(AppDataStructure.getRimworldConfigArea(), app_data=AppDataStructure.getRimworldConfigArea())
165166

166167
results = compareStructures(base_structure, after_structure)
167168

@@ -177,9 +178,22 @@ def test_app_data(self):
177178
def test_different_app_data(self):
178179
APP_DATA_BASE = "test_files/FakeAppData1/"
179180
APP_DATA_DIFFERENT = "test_files/FakeAppData2/"
181+
182+
file_name = "different.txt"
183+
file = open(os.path.join(APP_DATA_BASE, file_name), "w")
184+
file.write("good")
185+
file.close()
186+
file = open(os.path.join(APP_DATA_DIFFERENT, file_name), "w")
187+
file.write("bad")
188+
file.close()
189+
AppDataStructure.getRimworldConfigArea = lambda : APP_DATA_BASE
180190
base_structure = generateStructure(APP_DATA_BASE, app_data=APP_DATA_BASE)
191+
AppDataStructure.getRimworldConfigArea = lambda : APP_DATA_DIFFERENT
181192
different_structure = generateStructure(APP_DATA_DIFFERENT, app_data=APP_DATA_DIFFERENT)
193+
182194
results = compareStructures(base_structure, different_structure)
195+
self.assertEqual(results['modify'][0].path(), 'test_files/FakeAppData2/different.txt')
196+
clientSyncFiles(results['delete'], results['add'], results['modify'], testing=True)
183197

184198

185199
class IsFileTest(unittest.TestCase):
@@ -196,7 +210,7 @@ def test_is_not_file(self):
196210
self.assertFalse(FileFolder("test_files\\RimworldBase\\empty").file)
197211

198212
def test_file_indirect(self):
199-
config_location = getRimworldConfigArea()
213+
config_location = AppDataStructure.getRimworldConfigArea()
200214
parent = generateStructure(config_location, app_data=config_location)
201215
self.assertFalse(parent.file)
202216
for child in parent.children:

test_files/FakeAppData2/outofsyc.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)