64 lines
1.6 KiB
Python
64 lines
1.6 KiB
Python
|
#!/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)
|
||
|
local_mods = os.listdir('mods')
|
||
|
|
||
|
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}")
|
||
|
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)
|