-
Notifications
You must be signed in to change notification settings - Fork 0
/
m4s.py
70 lines (59 loc) · 1.99 KB
/
m4s.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import json
import requests
from pathlib import Path
from zipfile import ZipFile
def get_json(json_file: Path):
data = json_file.read_text()
return json.loads(data)
CONFIG = get_json(Path('config.json'))
def clear_dir(dir: Path):
for child in dir.iterdir():
if child.is_file():
child.unlink()
elif child.is_dir():
clear_dir(child)
child.rmdir()
def ready_dir(dir: Path):
if dir.exists():
clear_dir(dir)
else:
dir.mkdir()
def copy_file(src: Path, dest_dir: Path):
(dest_dir / src.name).write_bytes(src.read_bytes())
def copy_dir(src_dir: Path, dest_dir: Path):
ready_dir(dest_dir)
for child in src_dir.iterdir():
if child.is_file():
copy_file(child, dest_dir)
elif child.is_dir():
copy_dir(child, dest_dir / child.name)
def install_mod(mod, install_dir: Path):
r = requests.get(mod['url'])
zip_file = Path('temp.zip')
print('Downloading', mod['name'], 'zip...')
zip_file.write_bytes(r.content)
with ZipFile(str(zip_file), 'r') as zip_ref:
print('Extracting', mod['name'], 'zip...')
zip_ref.extractall(str(install_dir))
zip_file.unlink()
def main():
sims_dir = Path(CONFIG['simsdir'])
if not sims_dir.exists():
raise Exception('The Sims directory does not exist.')
if CONFIG['backup']['enabled']:
backup_dir = Path(CONFIG['backup']['dir'])
if not backup_dir.exists():
raise Exception('The backup directory does not exist.')
for folder in CONFIG['backup']['folders']:
print('Backing up', folder, 'folder...')
copy_dir(sims_dir / folder, backup_dir / folder)
install_dir = sims_dir / 'Mods'
if not install_dir.exists():
install_dir.mkdir()
for mod in CONFIG['modlist']:
if mod['enabled']:
mod_dir = install_dir / mod['name']
ready_dir(mod_dir)
install_mod(mod, mod_dir)
if __name__ == '__main__':
main()