Overwrought/overwrought_slave.py

70 lines
1.8 KiB
Python
Raw Permalink Normal View History

2019-06-03 08:29:03 -04:00
#!/usr/bin/env python3
"""
The end user script for the modpack.
"""
import os
import json
import urllib.parse
import urllib.request
import config_slave
def sync(delete=False):
"""
Synchronize the local `mods/` folder with the remote server.
"""
remote_mods =urllib.request.urlopen(config_slave.remote_addr+'/get').read()
remote_mods = json.loads(remote_mods)
2019-06-15 14:39:21 -04:00
if os.path.isfile('modlist.json'):
with open('modlist.json', 'r') as file:
local_mods = json.loads(file.read())
else:
local_mods = os.listdir('mods')
2019-06-03 08:29:03 -04:00
local_mods_del = [mod for mod in local_mods if mod not in remote_mods]
remote_mods_get = [mod for mod in remote_mods if mod not in local_mods]
for mod in local_mods_del:
if delete:
os.remove(os.path.join('mods', mod))
else:
os.makedirs('mods_old', exist_ok=True)
os.rename(os.path.join('mods', mod), os.path.join('mods_old', mod))
print(f"Deleted {mod}")
for mod in remote_mods_get:
url = config_slave.remote_addr + '/mods/' + urllib.parse.quote(mod)
res = urllib.request.urlopen(url)
data = res.read()
with open(os.path.join('mods', mod), 'wb') as file:
file.write(data)
print(f"Downloaded {mod}")
2019-06-15 14:39:21 -04:00
with open('modlist.json', 'w') as file:
file.write(json.dumps(remote_mods))
2019-06-03 08:29:03 -04:00
print("Success!")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description="Script for synchronizing the local mods/ folder "
"with the remote server."
)
parser.add_argument(
'action',
choices=['sync'],
default='sync',
help="What action to perform."
)
parser.add_argument(
'--delete',
action='store_true',
help="By default, old mods are moved to a mods_old/ folder "
"instead of being deleted. Checking this flag will delete "
"them instead."
)
args = parser.parse_args()
if args.action == 'sync':
sync(args.delete)