Skip to content

Commit

Permalink
Reimplement get-mods.sh as a Python script
Browse files Browse the repository at this point in the history
Users no longer require sh, curl, unzip, or a *nix environment in order
to automatically download the required mods. This should allow users of
other operating systems such as Windows to also use this script.

Bug: minetest-tools#28
  • Loading branch information
vilhelmgray committed Apr 3, 2020
1 parent 3e3fe2e commit 4406989
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 50 deletions.
53 changes: 53 additions & 0 deletions get_mods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python3

import io
import os
import shutil
import sys
import urllib.request
import zipfile

worldmods = os.path.join(os.getcwd(), "worldmods")
if not os.path.exists(worldmods):
print("The provided path does not exist.")
exit(1)

mods = (
("https://codeload.github.com/minetest-mods/mesecons/zip/master", "mesecons"),
("https://codeload.github.com/LNJ2/carpet/zip/master", "carpet"),
("https://codeload.github.com/minetest-mods/crops/zip/master", "crops"),
("https://codeload.github.com/minetest-mods/flowerpot/zip/master", "flowerpot"),
("https://codeload.github.com/minetest-mods/lapis/zip/master", "lapis"),
("https://codeload.github.com/minetest-mods/quartz/zip/master", "quartz"),
("https://codeload.github.com/minetest-mods/xdecor/zip/master", "xdecor"),
("https://codeload.github.com/oOChainLynxOo/hardenedclay/zip/master", "hardenedclay"),
("https://codeload.github.com/minetest-mods/nether/zip/master", "nether"),
("https://codeload.github.com/ShadowNinja/minetest_bedrock/zip/master", "minetest_bedrock"),
("https://gitlab.com/VanessaE/basic_materials/-/archive/master/basic_materials-master.zip", "basic_materials"),
("https://gitlab.com/VanessaE/biome_lib/-/archive/master/biome_lib-master.zip", "biome_lib"),
("https://gitlab.com/VanessaE/plantlife_modpack/-/archive/master/plantlife_modpack-master.zip", "plantlife_modpack"),
("https://gitlab.com/VanessaE/signs_lib/-/archive/master/signs_lib-master.zip", "signs_lib")
)

# Some servers don't recognize the default Python-urllib user agent
headers = { 'User-Agent':'Mozilla' }

for url, mod in mods:
print("Fetching:", mod);
request = urllib.request.Request(url, None, headers)
with urllib.request.urlopen(request) as response:
with zipfile.ZipFile(io.BytesIO(response.read()), 'r') as mod_zip:
mod_zip.extractall(worldmods)
mod = os.path.normpath(worldmods+"/"+mod)
os.rename(mod+"-master", mod)
minetest_bedrock = os.path.normpath(worldmods+"/"+"minetest_bedrock")
bedrock = os.path.normpath(worldmods+"/"+"bedrock")
os.rename(minetest_bedrock, bedrock)

# Remove unneeded/unwanted submods
prune = ( "dryplants", "along_shore", "molehills", "woodsoils", "bushes", "bushes_classic", "youngtrees", "3dmushrooms", "cavestuff", "poisonivy", "trunks" )
for ex in prune:
ex = "plantlife_modpack/" + ex
print("Pruning:", ex)
ex = os.path.normpath(worldmods+"/"+ex)
shutil.rmtree(ex)
53 changes: 4 additions & 49 deletions mcimport.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

import os
import shutil
import stat
import sys
import logging
Expand Down Expand Up @@ -50,54 +51,8 @@
sn.write(" vm:update_map()\n")
sn.write("end)\n\n")

if not os.path.exists(sys.argv[2]+"/get-mods.sh"):
path = sys.argv[2]+"/get-mods.sh"
with open(path, "w") as md:
md.write('''\
#!/bin/sh
# run this script to automatically get all the required mods
mods=(
https://codeload.github.com/minetest-mods/mesecons/zip/master,mesecons
https://codeload.github.com/LNJ2/carpet/zip/master,carpet
https://codeload.github.com/minetest-mods/crops/zip/master,crops
https://codeload.github.com/minetest-mods/flowerpot/zip/master,flowerpot
https://codeload.github.com/minetest-mods/lapis/zip/master,lapis
https://codeload.github.com/minetest-mods/quartz/zip/master,quartz
https://codeload.github.com/minetest-mods/xdecor/zip/master,xdecor
https://codeload.github.com/oOChainLynxOo/hardenedclay/zip/master,hardenedclay
https://codeload.github.com/minetest-mods/nether/zip/master,nether
https://codeload.github.com/ShadowNinja/minetest_bedrock/zip/master,minetest_bedrock
https://gitlab.com/VanessaE/basic_materials/-/archive/master/basic_materials-master.zip,basic_materials
https://gitlab.com/VanessaE/biome_lib/-/archive/master/biome_lib-master.zip,biome_lib
https://gitlab.com/VanessaE/plantlife_modpack/-/archive/master/plantlife_modpack-master.zip,plantlife_modpack
https://gitlab.com/VanessaE/signs_lib/-/archive/master/signs_lib-master.zip,signs_lib
)
cd worldmods
for item in ${mods[@]} ; do
(
url=$(echo $item | cut -d, -f1)
mod=$(echo $item | cut -d, -f2)
echo "Fetching: $mod"
curl -q -L -o $mod.zip $url
unzip -qq $mod.zip
rm $mod.zip
mv $mod-master $mod
mv minetest_bedrock bedrock
)
done
# remove unneeded/unwanted submods
for ex in plantlife_modpack/dryplants plantlife_modpack/along_shore plantlife_modpack/molehills plantlife_modpack/woodsoils plantlife_modpack/bushes plantlife_modpack/bushes_classic plantlife_modpack/youngtrees plantlife_modpack/3dmushrooms plantlife_modpack/cavestuff plantlife_modpack/poisonivy plantlife_modpack/trunks; do
echo "Pruning: $ex"
rm -rf $ex
done
''')
st = os.stat(path)
os.chmod(path, st.st_mode | stat.S_IXUSR)
get_mods = os.path.join(os.path.dirname(sys.argv[0]), "get_mods.py")
shutil.copy(get_mods, sys.argv[2])

mcmap = MCMap(sys.argv[1])
mtmap = MTMap(sys.argv[2])
Expand All @@ -107,4 +62,4 @@
mtmap.save()

print("Conversion finished!\n")
print("Run \"sh get-mods.sh\" in the new world folder to automatically download all required mods.")
print("Run \"python3 get_mods.py\" in the new world folder to automatically download all required mods.")
2 changes: 1 addition & 1 deletion mcimport.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ if [ $? == 0 ]; then
fi
cd "${HOME}/.minetest/worlds/$OUT"
(
bash get-mods.sh
python3 get_mods.py
echo "====================="
echo "Finished! You can now close this window!"
) | zenity --text-info --width=800 --height=600 --title='Downloading required mods.' --text='Downloading...'
Expand Down

0 comments on commit 4406989

Please sign in to comment.