poikilos
2 years ago
27 changed files with 1532 additions and 1169 deletions
@ -1,144 +1,154 @@ |
|||
#!/usr/bin/env python3 |
|||
import os |
|||
import shutil |
|||
import sys |
|||
actions = {"-- Up-to-date: ": "move", "-- Installing: ": "move"} |
|||
changes = { |
|||
"/usr/local/./": "/usr/local/share/minetest/" |
|||
} |
|||
count = 0 |
|||
command_count = 0 |
|||
in_path = "bad_mt5_make_install_output.txt" |
|||
outs_path = os.path.dirname(os.path.realpath(__file__)) |
|||
out_path = os.path.join(outs_path, "install-fix-minetest5-share.sh") |
|||
file_commands = [] |
|||
rmd_cmds = [] |
|||
mkdir_commands = [] |
|||
made_dirs = [] |
|||
mtg_mod_dirs = ["games//minetest_game/mods", "games/minetest_game/mods"] |
|||
with open(in_path) as ins: |
|||
with open(out_path, 'w') as outs: |
|||
outs.write("#!/bin/sh\n") |
|||
count += 1 |
|||
for line_orig in ins: |
|||
line = line_orig.strip() |
|||
action = None |
|||
old_path = None |
|||
for k, try_action in actions.items(): |
|||
if line.startswith(k): |
|||
action = try_action |
|||
old_path = line[len(k):].strip() |
|||
break |
|||
if action == "move": |
|||
found = None |
|||
for old, new in changes.items(): |
|||
if old_path.startswith(old): |
|||
found = old |
|||
new_path = new + old_path[len(old):] |
|||
if not os.path.exists(old_path): |
|||
if not os.path.exists(new_path): |
|||
# raise ValueError( |
|||
# "The program is not installed" |
|||
# " (missing '{}')".format(old_path) |
|||
# ) |
|||
outs.write( |
|||
'# WARNING: expected "{}" (there is' |
|||
' no destination "{}" either)' |
|||
''.format(old_path, new_path) |
|||
) |
|||
else: |
|||
outs.write( |
|||
'# Already moved (no source "{}"' |
|||
' for destination "{}")' |
|||
''.format(old_path, new_path) |
|||
) |
|||
else: |
|||
if os.path.isfile(old_path): |
|||
parent = os.path.split(new_path)[0] |
|||
if parent not in made_dirs: |
|||
made_dirs.append(parent) |
|||
cmd = 'mkdir -p "{}"'.format( |
|||
parent.replace("//", "/") |
|||
|
|||
|
|||
def main(): |
|||
count = 0 |
|||
command_count = 0 |
|||
in_path = "bad_mt5_make_install_output.txt" |
|||
outs_path = os.path.dirname(os.path.realpath(__file__)) |
|||
out_path = os.path.join(outs_path, "install-fix-minetest5-share.sh") |
|||
file_commands = [] |
|||
rmd_cmds = [] |
|||
mkdir_commands = [] |
|||
made_dirs = [] |
|||
mtg_mod_dirs = ["games//minetest_game/mods", "games/minetest_game/mods"] |
|||
with open(in_path) as ins: |
|||
with open(out_path, 'w') as outs: |
|||
outs.write("#!/bin/sh\n") |
|||
count += 1 |
|||
for line_orig in ins: |
|||
line = line_orig.strip() |
|||
action = None |
|||
old_path = None |
|||
for k, try_action in actions.items(): |
|||
if line.startswith(k): |
|||
action = try_action |
|||
old_path = line[len(k):].strip() |
|||
break |
|||
if action == "move": |
|||
found = None |
|||
for old, new in changes.items(): |
|||
if old_path.startswith(old): |
|||
found = old |
|||
new_path = new + old_path[len(old):] |
|||
if not os.path.exists(old_path): |
|||
if not os.path.exists(new_path): |
|||
# raise ValueError( |
|||
# "The program is not installed" |
|||
# " (missing '{}')".format(old_path) |
|||
# ) |
|||
outs.write( |
|||
'# WARNING: expected "{}" (there is' |
|||
' no destination "{}" either)' |
|||
''.format(old_path, new_path) |
|||
) |
|||
mkdir_commands.append(cmd) |
|||
# AFTER all directories BEFORE all files |
|||
options = "" |
|||
if os.path.isfile(new_path): |
|||
options = "-f" |
|||
if len(options) > 0: |
|||
options = " " + options.strip() |
|||
cmd = ( |
|||
'mv' + options |
|||
+ ' "{}" "{}"'.format( |
|||
old_path.replace("//", "/"), |
|||
new_path.replace("//", "/") |
|||
else: |
|||
outs.write( |
|||
'# Already moved (no source "{}"' |
|||
' for destination "{}")' |
|||
''.format(old_path, new_path) |
|||
) |
|||
) |
|||
# outs.write(cmd + "\n") |
|||
# AFTER all directories |
|||
file_commands.append(cmd) |
|||
else: |
|||
# old_path == old_path.replace("//","/") |
|||
# Manually fix: |
|||
if os.path.isfile(old_path): |
|||
parent = os.path.split(new_path)[0] |
|||
if parent not in made_dirs: |
|||
made_dirs.append(parent) |
|||
cmd = 'mkdir -p "{}"'.format( |
|||
parent.replace("//", "/") |
|||
) |
|||
mkdir_commands.append(cmd) |
|||
# AFTER all directories BEFORE all files |
|||
options = "" |
|||
if os.path.isfile(new_path): |
|||
options = "-f" |
|||
if len(options) > 0: |
|||
options = " " + options.strip() |
|||
cmd = ( |
|||
'mv' + options |
|||
+ ' "{}" "{}"'.format( |
|||
old_path.replace("//", "/"), |
|||
new_path.replace("//", "/") |
|||
) |
|||
) |
|||
# outs.write(cmd + "\n") |
|||
# AFTER all directories |
|||
file_commands.append(cmd) |
|||
else: |
|||
# old_path == old_path.replace("//","/") |
|||
# Manually fix: |
|||
|
|||
# rmdir: failed to remove '/usr/local/ |
|||
# ./games//minetest_game/mods': |
|||
# Directory not empty |
|||
# rmdir: failed to remove '/usr/local/ |
|||
# ./games//minetest_game/mods': |
|||
# Directory not empty |
|||
|
|||
# rmdir: failed to remove '/usr/local/ |
|||
# ./games//minetest_game': |
|||
# Directory not empty |
|||
# rmdir: failed to remove '/usr/local/ |
|||
# ./games//minetest_game': |
|||
# Directory not empty |
|||
|
|||
# due to /usr/local/./games// |
|||
# minetest_game/mods/game_commands: |
|||
orphan_mods = ["game_commands"] |
|||
removed_orphan_mods = [] |
|||
# due to /usr/local/./games// |
|||
# minetest_game/mods/game_commands: |
|||
orphan_mods = ["game_commands"] |
|||
removed_orphan_mods = [] |
|||
|
|||
for mod_rel in orphan_mods: |
|||
for mtg_rel in mtg_mod_dirs: |
|||
f_rel = found + mtg_rel |
|||
# such as ("/usr/local/./" |
|||
# + "games//minetest_game/mods") |
|||
if old_path.startswith(f_rel): |
|||
# if mod_rel not in |
|||
# removed_orphan_mods: |
|||
try_path = (found + mtg_rel |
|||
+ "/" + mod_rel) |
|||
if os.path.isdir(try_path): |
|||
cmd = ( |
|||
'rmdir "{}"'.format( |
|||
try_path |
|||
for mod_rel in orphan_mods: |
|||
for mtg_rel in mtg_mod_dirs: |
|||
f_rel = found + mtg_rel |
|||
# such as ("/usr/local/./" |
|||
# + "games//minetest_game/mods") |
|||
if old_path.startswith(f_rel): |
|||
# if mod_rel not in |
|||
# removed_orphan_mods: |
|||
try_path = (found + mtg_rel |
|||
+ "/" + mod_rel) |
|||
if os.path.isdir(try_path): |
|||
cmd = ( |
|||
'rmdir "{}"'.format( |
|||
try_path |
|||
) |
|||
) |
|||
) |
|||
# queue for last stage: |
|||
if cmd not in rmd_cmds: |
|||
rmd_cmds.append(cmd) |
|||
# removed_orphan_mods. |
|||
# append(mod_rel) |
|||
break |
|||
# queue for last stage: |
|||
if cmd not in rmd_cmds: |
|||
rmd_cmds.append(cmd) |
|||
# removed_orphan_mods. |
|||
# append(mod_rel) |
|||
break |
|||
|
|||
cmd = 'rmdir "{}"'.format(old_path) |
|||
rmd_cmds.append(cmd) # AFTER everything |
|||
break |
|||
if found is None: |
|||
outs.write("# WARNING: The destination path is" |
|||
" unknown: ") |
|||
outs.write('# mv "{}" "{}"'.format(old_path, |
|||
old_path)) |
|||
else: |
|||
outs.write("# " + line + "\n") |
|||
cmd = 'rmdir "{}"'.format(old_path) |
|||
rmd_cmds.append(cmd) # AFTER everything |
|||
break |
|||
if found is None: |
|||
outs.write("# WARNING: The destination path is" |
|||
" unknown: ") |
|||
outs.write('# mv "{}" "{}"'.format(old_path, |
|||
old_path)) |
|||
else: |
|||
outs.write("# " + line + "\n") |
|||
count += 1 |
|||
for cmd in sorted(mkdir_commands, key=len): |
|||
outs.write(cmd + "\n") |
|||
count += 1 |
|||
for cmd in sorted(mkdir_commands, key=len): |
|||
outs.write(cmd + "\n") |
|||
count += 1 |
|||
command_count += 1 |
|||
for cmd in file_commands: |
|||
outs.write(cmd + "\n") |
|||
count += 1 |
|||
command_count += 1 |
|||
for cmd in sorted(rmd_cmds, key=len, reverse=True): |
|||
outs.write(cmd + "\n") |
|||
count += 1 |
|||
command_count += 1 |
|||
command_count += 1 |
|||
for cmd in file_commands: |
|||
outs.write(cmd + "\n") |
|||
count += 1 |
|||
command_count += 1 |
|||
for cmd in sorted(rmd_cmds, key=len, reverse=True): |
|||
outs.write(cmd + "\n") |
|||
count += 1 |
|||
command_count += 1 |
|||
|
|||
print('Added {} line(s) to "{}" (including {} command(s))' |
|||
''.format(count, out_path, command_count)) |
|||
|
|||
return 0 |
|||
|
|||
|
|||
print('Added {} line(s) to "{}" (including {} command(s))' |
|||
''.format(count, out_path, command_count)) |
|||
if __name__ == "__main__": |
|||
sys.exit(main()) |
|||
|
@ -0,0 +1,103 @@ |
|||
#!/usr/bin/env python |
|||
''' |
|||
This module assists with building games from other games, mods, and |
|||
patches. |
|||
''' |
|||
from __future__ import print_function |
|||
|
|||
import sys |
|||
import platform |
|||
import os |
|||
|
|||
profile = None |
|||
if platform.system() == "Windows": |
|||
profile = os.environ.get('USERPROFILE') |
|||
else: |
|||
profile = os.environ.get('HOME') |
|||
|
|||
verbosity = 0 |
|||
max_verbosity = 2 |
|||
|
|||
|
|||
def echo0(*args, **kwargs): |
|||
print(*args, file=sys.stderr, **kwargs) |
|||
|
|||
|
|||
def echo1(*args, **kwargs): |
|||
if verbosity < 1: |
|||
return False |
|||
print(*args, file=sys.stderr, **kwargs) |
|||
return True |
|||
|
|||
|
|||
def echo2(*args, **kwargs): |
|||
if verbosity < 2: |
|||
return False |
|||
print(*args, file=sys.stderr, **kwargs) |
|||
return True |
|||
|
|||
|
|||
def get_verbosity(): |
|||
return verbosity |
|||
|
|||
|
|||
def set_verbosity(level): |
|||
if level is True: |
|||
verbosity = 1 |
|||
elif level is False: |
|||
verbosity = 0 |
|||
elif level in range(max_verbosity+1): |
|||
verbosity = level |
|||
raise ValueError( |
|||
"verbosity must be {} at maximum.".format(max_verbosity) |
|||
) |
|||
|
|||
|
|||
try: |
|||
import mtanalyze |
|||
except ModuleNotFoundError as ex: |
|||
# tryMTA = os.path.join(profile, "git", "mtanalyze") |
|||
moduleDir = os.path.dirname(os.path.realpath(__file__)) |
|||
REPO_DIR = os.path.dirname(moduleDir) |
|||
modulesDir = os.path.dirname(REPO_DIR) |
|||
echo0("* looking for mtanalyze in modulesDir \"{}\"" |
|||
"".format(modulesDir)) |
|||
tryMTA = os.path.abspath(os.path.join(modulesDir, "mtanalyze")) |
|||
if os.path.isdir(tryMTA): |
|||
sys.path.append(tryMTA) |
|||
import mtanalyze |
|||
# ^ import mtanalyze/mtanalyze purposely since the main |
|||
# mtanalyze/ directory is a setuptools package not a module. |
|||
else: |
|||
echo0("") |
|||
echo0("You must install mtanalyze alongside") |
|||
echo0("EnlivenMinetest such that ../mtanalize/mtanalize exists") |
|||
echo0("such as via:") |
|||
echo0(" git clone https://github.com/poikilos/mtanalyze {}" |
|||
"".format(tryMTA)) |
|||
echo0("") |
|||
# raise tryMTA |
|||
exit(1) |
|||
|
|||
# from mtanalyze import profile_path |
|||
MY_MODULE_DIR = os.path.dirname(os.path.realpath(__file__)) |
|||
# ^ realpath follows symlinks |
|||
REPO_DIR = os.path.dirname(MY_MODULE_DIR) |
|||
MODS_STOPGAP_DIR = os.path.join(REPO_DIR, "patches", "mods-stopgap") |
|||
if not os.path.isdir(MODS_STOPGAP_DIR): |
|||
echo0("Error: \"{}\" is missing.".format(MODS_STOPGAP_DIR)) |
|||
exit(1) |
|||
BASE_DIR = os.path.join(REPO_DIR, "Bucket_Game-base") |
|||
if not os.path.isdir(BASE_DIR): |
|||
echo0("Error: \"{}\" is missing.".format(BASE_DIR)) |
|||
exit(1) |
|||
BRANCHES_DIR = os.path.join(REPO_DIR, "Bucket_Game-branches") |
|||
if not os.path.isdir(BRANCHES_DIR): |
|||
echo0("Error: \"{}\" is missing.".format(BRANCHES_DIR)) |
|||
exit(1) |
|||
|
|||
# NOTE: get a git repo's origin via: git remote show origin |
|||
|
|||
|
|||
def getSGPath(stopgap_mod_name): |
|||
return os.path.join(MODS_STOPGAP_DIR, stopgap_mod_name) |
@ -0,0 +1,77 @@ |
|||
#!/usr/bin/env python |
|||
import setuptools |
|||
import sys |
|||
import os |
|||
# - For the example on which this was based, see |
|||
# https://github.com/poikilos/linux-preinstall/blob/main/setup.py |
|||
# which is based on |
|||
# https://github.com/poikilos/world_clock/blob/main/setup.py |
|||
# which is based on |
|||
# https://github.com/poikilos/nopackage/blob/main/setup.py |
|||
# which is based on |
|||
# https://github.com/poikilos/pypicolcd/blob/master/setup.py |
|||
# - For nose, see https://github.com/poikilos/mgep/blob/master/setup.py |
|||
|
|||
# python_mr = sys.version_info.major |
|||
# versionedModule = {} |
|||
# versionedModule['urllib'] = 'urllib' |
|||
# if python_mr == 2: |
|||
# versionedModule['urllib'] = 'urllib2' |
|||
|
|||
install_requires = [] |
|||
|
|||
if os.path.isfile("requirements.txt"): |
|||
with open("requirements.txt", "r") as ins: |
|||
for rawL in ins: |
|||
line = rawL.strip() |
|||
if len(line) < 1: |
|||
continue |
|||
install_requires.append(line) |
|||
|
|||
description = '''Manage Minetest using Python.''' |
|||
long_description = description |
|||
if os.path.isfile("readme.md"): |
|||
with open("readme.md", "r") as fh: |
|||
long_description = fh.read() |
|||
|
|||
setuptools.setup( |
|||
name='pyenliven', |
|||
version='0.3.0', |
|||
description=description, |
|||
long_description=long_description, |
|||
long_description_content_type="text/markdown", |
|||
classifiers=[ |
|||
'Development Status :: 3 - Alpha', |
|||
'Programming Language :: Python :: 3', |
|||
('License :: OSI Approved ::' |
|||
' GNU General Public License v2 or later (GPLv2+)'), |
|||
'Operating System :: POSIX :: Linux', |
|||
'Topic :: Software Development :: Version Control', |
|||
], |
|||
keywords=('minetest repo management commit data analyzer' |
|||
' meld merge compare files diff'), |
|||
url="https://github.com/poikilos/EnlivenMinetest", |
|||
author="Jake Gustafson", |
|||
author_email='7557867+poikilos@users.noreply.github.com', |
|||
license='GPLv2.1', |
|||
# packages=setuptools.find_packages(), |
|||
packages=['pyenliven'], |
|||
# include_package_data=True, # look for MANIFEST.in |
|||
# scripts=['example'] , |
|||
# See <https://stackoverflow.com/questions/27784271/ |
|||
# how-can-i-use-setuptools-to-generate-a-console-scripts-entry- |
|||
# point-which-calls> |
|||
entry_points={ |
|||
'console_scripts': [ |
|||
'compatiblizemod=pyenliven.compatiblizemod:main', |
|||
], |
|||
}, |
|||
install_requires=install_requires, |
|||
# versionedModule['urllib'], |
|||
# ^ "ERROR: Could not find a version that satisfies the requirement |
|||
# urllib (from nopackage) (from versions: none) |
|||
# ERROR: No matching distribution found for urllib" |
|||
test_suite='nose.collector', |
|||
tests_require=['nose', 'nose-cover3'], |
|||
zip_safe=False, # It can't run zipped due to needing data files. |
|||
) |
@ -1,183 +1,194 @@ |
|||
#!/usr/bin/env python3 |
|||
import os |
|||
import sys |
|||
import platform |
|||
CMD_REM = "#" |
|||
CMD_RM = "rm " |
|||
CMD_RMDIR = "rmdir " |
|||
if platform.system() == "Windows": |
|||
CMD_REM = "REM " |
|||
CMD_RM = "del " |
|||
CMD_RMDIR = "rd " |
|||
|
|||
# profile_path = None |
|||
# if 'HOME' in os.environ: |
|||
# profile_path = os.environ['HOME'] |
|||
# elif 'USERPROFILE' in os.environ: |
|||
# profile_path = os.environ['USERPROFILE'] |
|||
# downloads_path = os.path.join(profile_path, "Downloads") |
|||
# repo_path = os.path.join(downloads_path, "minetest") |
|||
# if not os.path.isdir(repo_path): |
|||
# repo_path = os.path.join(profile_path, "minetest") |
|||
# if not os.path.isdir(repo_path): |
|||
# print("ERROR: Nothing done since there is no minetest sourcecode" |
|||
# " folder in " + downloads_path |
|||
# + " (nor " + profile_path + ")") |
|||
# exit(1) |
|||
install_manifest_name = "install_manifest.txt" |
|||
# install_manifest_path = os.path.join(repo_path, install_manifest_name) |
|||
# if not os.path.isfile(install_manifest_path): |
|||
# print("ERROR: nothing done since there is no " + |
|||
# install_manifest_name + " in '" + repo_path + |
|||
# "'. The file would only be present if you " + |
|||
# "installed minetest from sourcecode" + |
|||
# "(otherwise this uninstaller is not for you).") |
|||
# exit(2) |
|||
if not os.path.isfile(install_manifest_name): |
|||
print("ERROR: nothing done since there is no " + |
|||
install_manifest_name + " in the current " + |
|||
"directory. You must run: ") |
|||
print(" sudo python3 "+os.path.abspath(__file__)) |
|||
print("from the minetest sourcecode (repo) directory.") |
|||
exit(2) |
|||
directories = [] |
|||
print("Removing files...") |
|||
f_removed_count = 0 |
|||
f_skipped_count = 0 |
|||
f_failed_count = 0 |
|||
retry_lines = [] |
|||
with open(install_manifest_name, 'r') as ins: |
|||
original_line = True |
|||
while original_line: |
|||
original_line = ins.readline() |
|||
if original_line: |
|||
line = original_line.rstrip() # remove trailing newline |
|||
if len(line) > 0: |
|||
d_path = os.path.dirname(line) |
|||
if d_path not in directories: |
|||
if "minetest" in d_path: |
|||
directories.append(d_path) |
|||
# else must be a system directory like |
|||
# /usr/local/share/applications |
|||
if os.path.isfile(line): |
|||
os.remove(line) |
|||
def main(): |
|||
CMD_REM = "#" |
|||
CMD_RM = "rm " |
|||
CMD_RMDIR = "rmdir " |
|||
if platform.system() == "Windows": |
|||
CMD_REM = "REM " |
|||
CMD_RM = "del " |
|||
CMD_RMDIR = "rd " |
|||
|
|||
# profile_path = None |
|||
# if 'HOME' in os.environ: |
|||
# profile_path = os.environ['HOME'] |
|||
# elif 'USERPROFILE' in os.environ: |
|||
# profile_path = os.environ['USERPROFILE'] |
|||
# downloads_path = os.path.join(profile_path, "Downloads") |
|||
# repo_path = os.path.join(downloads_path, "minetest") |
|||
# if not os.path.isdir(repo_path): |
|||
# repo_path = os.path.join(profile_path, "minetest") |
|||
# if not os.path.isdir(repo_path): |
|||
# print("ERROR: Nothing done since there is no minetest sourcecode" |
|||
# " folder in " + downloads_path |
|||
# + " (nor " + profile_path + ")") |
|||
# return 1 |
|||
install_manifest_name = "install_manifest.txt" |
|||
# install_manifest_path = os.path.join(repo_path, install_manifest_name) |
|||
# if not os.path.isfile(install_manifest_path): |
|||
# print("ERROR: nothing done since there is no " + |
|||
# install_manifest_name + " in '" + repo_path + |
|||
# "'. The file would only be present if you " + |
|||
# "installed minetest from sourcecode" + |
|||
# "(otherwise this uninstaller is not for you).") |
|||
# return 2 |
|||
if not os.path.isfile(install_manifest_name): |
|||
print("ERROR: nothing done since there is no " + |
|||
install_manifest_name + " in the current " + |
|||
"directory. You must run: ") |
|||
print(" sudo python3 "+os.path.abspath(__file__)) |
|||
print("from the minetest sourcecode (repo) directory.") |
|||
return 2 |
|||
directories = [] |
|||
print("Removing files...") |
|||
f_removed_count = 0 |
|||
f_skipped_count = 0 |
|||
f_failed_count = 0 |
|||
retry_lines = [] |
|||
with open(install_manifest_name, 'r') as ins: |
|||
original_line = True |
|||
while original_line: |
|||
original_line = ins.readline() |
|||
if original_line: |
|||
line = original_line.rstrip() # remove trailing newline |
|||
if len(line) > 0: |
|||
d_path = os.path.dirname(line) |
|||
if d_path not in directories: |
|||
if "minetest" in d_path: |
|||
directories.append(d_path) |
|||
# else must be a system directory like |
|||
# /usr/local/share/applications |
|||
if os.path.isfile(line): |
|||
f_failed_count += 1 |
|||
retry_lines.append(CMD_RM+'"'+line+'"') |
|||
os.remove(line) |
|||
if os.path.isfile(line): |
|||
f_failed_count += 1 |
|||
retry_lines.append(CMD_RM+'"'+line+'"') |
|||
else: |
|||
f_removed_count += 1 |
|||
else: |
|||
f_removed_count += 1 |
|||
else: |
|||
f_skipped_count += 1 |
|||
f_skipped_count += 1 |
|||
|
|||
print("Removed " + str(f_removed_count) + " file(s) (skipped not" |
|||
" present:" + str(f_skipped_count) + "; failed:" |
|||
+ str(f_failed_count) + ")") |
|||
print("Removed " + str(f_removed_count) + " file(s) (skipped not" |
|||
" present:" + str(f_skipped_count) + "; failed:" |
|||
+ str(f_failed_count) + ")") |
|||
|
|||
# NOTE: the next line makes ASCENDING (by len) list of TUPLES (name,len) |
|||
sorted_directories = [ |
|||
(x, len(x)) for x in sorted(directories, key=len) |
|||
] |
|||
# NOTE: the next line makes ASCENDING (by len) list of TUPLES (name,len) |
|||
sorted_directories = [ |
|||
(x, len(x)) for x in sorted(directories, key=len) |
|||
] |
|||
|
|||
print("Removing folders...") |
|||
# NOTE: they are sorted ASCENDING so start at end: |
|||
d_removed_count = 0 |
|||
d_skipped_count = 0 |
|||
d_failed_count = 0 |
|||
print("Removing folders...") |
|||
# NOTE: they are sorted ASCENDING so start at end: |
|||
d_removed_count = 0 |
|||
d_skipped_count = 0 |
|||
d_failed_count = 0 |
|||
|
|||
# still leaves: |
|||
# /usr/local/share/minetest/games/minetest_game/mods |
|||
# /usr/local/share/minetest/textures/base/pack/: |
|||
# down_arrow.png left_arrow.png right_arrow.png up_arrow.png |
|||
# /usr/local/share/minetest/games/minimal/mods |
|||
# so: |
|||
try_files = ["depends.txt", "down_arrow.png", "left_arrow.png", |
|||
"right_arrow.png", "up_arrow.png"] |
|||
try_dirs = ["mods"] |
|||
# still leaves: |
|||
# /usr/local/share/minetest/games/minetest_game/mods |
|||
# /usr/local/share/minetest/textures/base/pack/: |
|||
# down_arrow.png left_arrow.png right_arrow.png up_arrow.png |
|||
# /usr/local/share/minetest/games/minimal/mods |
|||
# so: |
|||
try_files = ["depends.txt", "down_arrow.png", "left_arrow.png", |
|||
"right_arrow.png", "up_arrow.png"] |
|||
try_dirs = ["mods"] |
|||
|
|||
extra_dirs = [] |
|||
ed_failed_count = 0 |
|||
ed_removed_count = 0 |
|||
extra_files = [] |
|||
e_failed_count = 0 |
|||
e_removed_count = 0 |
|||
for i in reversed(range(len(sorted_directories))): |
|||
d_path = sorted_directories[i][0] |
|||
# for d in reversed(sorted_directories): |
|||
# d_path = d[0] |
|||
# print("checking "+str(d_path)) |
|||
if os.path.isdir(d_path): |
|||
try: |
|||
for try_name in try_files: |
|||
try_path = os.path.join(d_path, try_name) |
|||
if os.path.isfile(try_path): |
|||
extra_files.append(try_path) |
|||
print('Removing known extra file: "' + try_path |
|||
+ '"') |
|||
try: |
|||
os.remove(try_path) |
|||
e_removed_count += 1 |
|||
except Exception as e: |
|||
e_failed_count += 1 |
|||
retry_lines.append(CMD_RM + '"' + try_path |
|||
+ '"') |
|||
print(str(e)) |
|||
for try_name in try_dirs: |
|||
try_path = os.path.join(d_path, try_name) |
|||
if os.path.isdir(try_path): |
|||
extra_dirs.append(try_path) |
|||
print('Removing known extra folder: "' + try_path |
|||
+ '"') |
|||
try: |
|||
os.rmdir(try_path) |
|||
ed_removed_count += 1 |
|||
except Exception as e: |
|||
ed_failed_count += 1 |
|||
retry_lines.append(CMD_RMDIR+'"'+try_path+'"') |
|||
print(str(e)) |
|||
os.rmdir(d_path) |
|||
except Exception as e: |
|||
print(str(e)) |
|||
extra_dirs = [] |
|||
ed_failed_count = 0 |
|||
ed_removed_count = 0 |
|||
extra_files = [] |
|||
e_failed_count = 0 |
|||
e_removed_count = 0 |
|||
for i in reversed(range(len(sorted_directories))): |
|||
d_path = sorted_directories[i][0] |
|||
# for d in reversed(sorted_directories): |
|||
# d_path = d[0] |
|||
# print("checking "+str(d_path)) |
|||
if os.path.isdir(d_path): |
|||
d_failed_count += 1 |
|||
retry_lines.append(CMD_RMDIR+'"'+d_path+'"') |
|||
try: |
|||
for try_name in try_files: |
|||
try_path = os.path.join(d_path, try_name) |
|||
if os.path.isfile(try_path): |
|||
extra_files.append(try_path) |
|||
print('Removing known extra file: "' + try_path |
|||
+ '"') |
|||
try: |
|||
os.remove(try_path) |
|||
e_removed_count += 1 |
|||
except Exception as e: |
|||
e_failed_count += 1 |
|||
retry_lines.append(CMD_RM + '"' + try_path |
|||
+ '"') |
|||
print(str(e)) |
|||
for try_name in try_dirs: |
|||
try_path = os.path.join(d_path, try_name) |
|||
if os.path.isdir(try_path): |
|||
extra_dirs.append(try_path) |
|||
print('Removing known extra folder: "' + try_path |
|||
+ '"') |
|||
try: |
|||
os.rmdir(try_path) |
|||
ed_removed_count += 1 |
|||
except Exception as e: |
|||
ed_failed_count += 1 |
|||
retry_lines.append(CMD_RMDIR+'"'+try_path+'"') |
|||
print(str(e)) |
|||
os.rmdir(d_path) |
|||
except Exception as e: |
|||
print(str(e)) |
|||
if os.path.isdir(d_path): |
|||
d_failed_count += 1 |
|||
retry_lines.append(CMD_RMDIR+'"'+d_path+'"') |
|||
else: |
|||
d_removed_count += 1 |
|||
else: |
|||
d_removed_count += 1 |
|||
else: |
|||
d_skipped_count += 1 |
|||
print("Removed " + str(d_removed_count) + " folder(s) (skipped not" |
|||
" present:" + str(d_skipped_count) + "; failed:" |
|||
+ str(d_failed_count) + ")") |
|||
if e_failed_count > 0: |
|||
print("(failed to remove " + e_failed_count + " known extra file(s)" |
|||
" (will be shown under FAILURES below)") |
|||
if ed_failed_count > 0: |
|||
print("(failed to remove " + ed_failed_count + " known extra" |
|||
" folder(s) (will be shown under FAILURES below)") |
|||
print("Removed " + str(d_removed_count) + " folder(s) (skipped not" |
|||
" present:" + |
|||
str(d_skipped_count) + "; failed:" + str(d_failed_count) + ")") |
|||
d_skipped_count += 1 |
|||
print("Removed " + str(d_removed_count) + " folder(s) (skipped not" |
|||
" present:" + str(d_skipped_count) + "; failed:" |
|||
+ str(d_failed_count) + ")") |
|||
if e_failed_count > 0: |
|||
print("(failed to remove " + e_failed_count + " known extra file(s)" |
|||
" (will be shown under FAILURES below)") |
|||
if ed_failed_count > 0: |
|||
print("(failed to remove " + ed_failed_count + " known extra" |
|||
" folder(s) (will be shown under FAILURES below)") |
|||
print("Removed " + str(d_removed_count) + " folder(s) (skipped not" |
|||
" present:" + |
|||
str(d_skipped_count) + "; failed:" + str(d_failed_count) + ")") |
|||
|
|||
if f_failed_count+d_failed_count+ed_failed_count <= 0: |
|||
print("") |
|||
if f_removed_count+d_removed_count <= 0: |
|||
print("Nothing to do (minetest+minetestserver has 0 known files" |
|||
" on system--you apparently already uninstalled the local" |
|||
" version that was installed using 'sudo make install')") |
|||
code = 0 |
|||
if f_failed_count+d_failed_count+ed_failed_count <= 0: |
|||
print("") |
|||
if f_removed_count+d_removed_count <= 0: |
|||
print("Nothing to do (minetest+minetestserver has 0 known files" |
|||
" on system--you apparently already uninstalled the local" |
|||
" version that was installed using 'sudo make install')") |
|||
else: |
|||
print("OK [finished uninstalling all installed files]") |
|||
print("") |
|||
else: |
|||
print("OK [finished uninstalling all installed files]") |
|||
print("") |
|||
else: |
|||
print("") |
|||
print("") |
|||
print(CMD_REM+"FAILURES:") |
|||
for rl in retry_lines: |
|||
print(rl) |
|||
print("") |
|||
print("In case of any failures are counted above, " |
|||
"try running this script with administrative privileges." |
|||
"If any more remain, you may have to remove them manually.") |
|||
print("") |
|||
print("") |
|||
if not ins.closed: |
|||
print("ERROR: ins was not closed (this should never happen)--" |
|||
"closing manually...") |
|||
ins.close() |
|||
print("") |
|||
print("") |
|||
print(CMD_REM+"FAILURES:") |
|||
for rl in retry_lines: |
|||
print(rl) |
|||
print("") |
|||
print("In case of any failures are counted above, " |
|||
"try running this script with administrative privileges." |
|||
"If any more remain, you may have to remove them manually.") |
|||
print("") |
|||
print("") |
|||
code = 1 |
|||
if not ins.closed: |
|||
print("ERROR: ins was not closed (this should never happen)--" |
|||
"closing manually...") |
|||
ins.close() |
|||
code = 1 |
|||
return code |
|||
|
|||
|
|||
if __name__ == "__main__": |
|||
sys.exit(main()) |
|||
|
@ -1,117 +1,126 @@ |
|||
#!/usr/bin/env python |
|||
import os |
|||
import sys |
|||
import platform |
|||
|
|||
|
|||
def doDie(msg, error_code=1): |
|||
def show_error(msg, error_code=1): |
|||
print() |
|||
print(msg) |
|||
print() |
|||
print() |
|||
exit(error_code) |
|||
|
|||
|
|||
rem_cmd = "#" |
|||
rm_cmd = "rm " |
|||
rmdir_cmd = "rmdir " |
|||
if platform.system() == "Windows": |
|||
rm_cmd = "DEL " |
|||
rmdir_cmd = "RD " |
|||
rem_cmd = "REM " |
|||
profile_path1 = os.environ.get('HOME') |
|||
profile_path = profile_path1 |
|||
profile_path2 = os.environ.get('USERPROFILE') |
|||
if profile_path2 is not None: |
|||
profile_path = profile_path2 |
|||
if profile_path1 is not None: |
|||
print(rem_cmd + "WARNING: HOME is present, but USERPROFILE '" |
|||
+ profile_path + "' is being used.") |
|||
else: |
|||
if profile_path1 is None: |
|||
doDie(rem_cmd + "ERROR: There is nothing to do since neither" |
|||
+ " HOME nor USERPROFILE is present.") |
|||
|
|||
mnf_name = "install_manifest.txt" |
|||
mnf_path = os.path.join(profile_path, mnf_name) |
|||
|
|||
unsorted_list = [] |
|||
|
|||
if not os.path.isfile(mnf_path): |
|||
doDie(rem_cmd + "Uninstall cannot continue since '" + mnf_path |
|||
+ "' is missing.") |
|||
|
|||
with open(mnf_path) as fp: |
|||
for cnt, line_original in enumerate(fp): |
|||
# print("Line {}: {}".format(cnt, line)) |
|||
line = line_original.strip() |
|||
if len(line) > 0: |
|||
unsorted_list.append(line) |
|||
|
|||
if len(unsorted_list) < 1: |
|||
doDie(rem_cmd + "ERROR: There are no files in the manifest '" |
|||
+ mnf_path + "'") |
|||
|
|||
# See https://stackoverflow.com/questions/4659524/\ |
|||
# how-to-sort-by-length-of-string-followed-by-alphabetical-order |
|||
sorted_list = sorted(unsorted_list, key=len, reverse=True) |
|||
# reverse: descending |
|||
# or (also reverse): |
|||
# the_list.sort(key=lambda item: (-len(item), item)) |
|||
|
|||
print(rem_cmd + "Uninstalling...") |
|||
not_removed_files = [] |
|||
not_removed_dirs = [] |
|||
does_not_exist = [] |
|||
file_count = 0 |
|||
dir_count = 0 |
|||
for path in sorted_list: |
|||
if os.path.isfile(path): |
|||
if path[0:1] == ".": |
|||
return error_code |
|||
|
|||
|
|||
def main(): |
|||
rem_cmd = "#" |
|||
rm_cmd = "rm " |
|||
rmdir_cmd = "rmdir " |
|||
if platform.system() == "Windows": |
|||
rm_cmd = "DEL " |
|||
rmdir_cmd = "RD " |
|||
rem_cmd = "REM " |
|||
profile_path1 = os.environ.get('HOME') |
|||
profile_path = profile_path1 |
|||
profile_path2 = os.environ.get('USERPROFILE') |
|||
if profile_path2 is not None: |
|||
profile_path = profile_path2 |
|||
if profile_path1 is not None: |
|||
print(rem_cmd + "WARNING: HOME is present, but USERPROFILE '" |
|||
+ profile_path + "' is being used.") |
|||
else: |
|||
if profile_path1 is None: |
|||
return show_error(rem_cmd + "ERROR: There is nothing to do" |
|||
" since neither" |
|||
" HOME nor USERPROFILE is present.") |
|||
|
|||
mnf_name = "install_manifest.txt" |
|||
mnf_path = os.path.join(profile_path, mnf_name) |
|||
|
|||
unsorted_list = [] |
|||
|
|||
if not os.path.isfile(mnf_path): |
|||
return show_error(rem_cmd + "Uninstall cannot continue since '" |
|||
+ mnf_path + "' is missing.") |
|||
|
|||
with open(mnf_path) as fp: |
|||
for cnt, line_original in enumerate(fp): |
|||
# print("Line {}: {}".format(cnt, line)) |
|||
line = line_original.strip() |
|||
if len(line) > 0: |
|||
unsorted_list.append(line) |
|||
|
|||
if len(unsorted_list) < 1: |
|||
return show_error(rem_cmd + "ERROR: There are no files in the manifest" |
|||
" '"+ mnf_path + "'") |
|||
|
|||
# See https://stackoverflow.com/questions/4659524/\ |
|||
# how-to-sort-by-length-of-string-followed-by-alphabetical-order |
|||
sorted_list = sorted(unsorted_list, key=len, reverse=True) |
|||
# reverse: descending |
|||
# or (also reverse): |
|||
# the_list.sort(key=lambda item: (-len(item), item)) |
|||
|
|||
print(rem_cmd + "Uninstalling...") |
|||
not_removed_files = [] |
|||
not_removed_dirs = [] |
|||
does_not_exist = [] |
|||
file_count = 0 |
|||
dir_count = 0 |
|||
for path in sorted_list: |
|||
if os.path.isfile(path): |
|||
if path[0:1] == ".": |
|||
print(rm_cmd + "\"" + path + "\"") |
|||
not_removed_files.append(path) |
|||
continue |
|||
try: |
|||
os.remove(path) |
|||
file_count += 1 |
|||
except PermissionError: |
|||
not_removed_files.append(path) |
|||
elif os.path.isdir(path): |
|||
if path[0:1] == ".": |
|||
print(rmdir_cmd + "\"" + path + "\"") |
|||
not_removed_dirs.append(path) |
|||
continue |
|||
try: |
|||
os.rmdir(path) |
|||
dir_count += 1 |
|||
except PermissionError: |
|||
not_removed_dirs.append(path) |
|||
else: |
|||
does_not_exist.append(path) |
|||
|
|||
if len(does_not_exist) > 0: |
|||
if len(does_not_exist) == len(sorted_list): |
|||
return show_error(rem_cmd + 'The program is not installed such as' |
|||
' at "{}".' |
|||
''.format(sorted_list[-1])) |
|||
|
|||
show_dot_warning = True |
|||
print(rem_cmd + "Uninstall is complete.") |
|||
print(rem_cmd + "- files: " + str(file_count)) |
|||
print(rem_cmd + "- directories: " + str(dir_count)) |
|||
print(rem_cmd + "- missing: " + len(does_not_exist)) |
|||
if (len(not_removed_files) + len(not_removed_dirs)) > 0: |
|||
for path in not_removed_files: |
|||
if path[0:1] == ".": |
|||
if show_dot_warning: |
|||
print(rem_cmd + "Paths starting with '.' are not yet" |
|||
" implemented.") |
|||
show_dot_warning = False |
|||
print(rm_cmd + "\"" + path + "\"") |
|||
not_removed_files.append(path) |
|||
continue |
|||
try: |
|||
os.remove(path) |
|||
file_count += 1 |
|||
except PermissionError: |
|||
not_removed_files.append(path) |
|||
elif os.path.isdir(path): |
|||
if path[0:1] == ".": |
|||
for path in not_removed_dirs: |
|||
print(rmdir_cmd + "\"" + path + "\"") |
|||
not_removed_dirs.append(path) |
|||
continue |
|||
try: |
|||
os.rmdir(path) |
|||
dir_count += 1 |
|||
except PermissionError: |
|||
not_removed_dirs.append(path) |
|||
else: |
|||
does_not_exist.append(path) |
|||
|
|||
if len(does_not_exist) > 0: |
|||
if len(does_not_exist) == len(sorted_list): |
|||
doDie(" " + rem_cmd + " The program is not installed such as" |
|||
+ " at '" + sorted_list[-1] + "'.") |
|||
|
|||
show_dot_warning = True |
|||
print(rem_cmd + "Uninstall is complete.") |
|||
print(rem_cmd + "- files: " + str(file_count)) |
|||
print(rem_cmd + "- directories: " + str(dir_count)) |
|||
print(rem_cmd + "- missing: " + len(does_not_exist)) |
|||
if (len(not_removed_files) + len(not_removed_dirs)) > 0: |
|||
for path in not_removed_files: |
|||
if path[0:1] == ".": |
|||
if show_dot_warning: |
|||
print(rem_cmd + "Paths starting with '.' are not yet" |
|||
" implemented.") |
|||
show_dot_warning = False |
|||
print(rm_cmd + "\"" + path + "\"") |
|||
for path in not_removed_dirs: |
|||
print(rmdir_cmd + "\"" + path + "\"") |
|||
print(rem_cmd + "Deleting items above FAILED:") |
|||
print(" " + rem_cmd + "- files: " + str(not_removed_file_count)) |
|||
print(" " + rem_cmd + "- directories: " |
|||
+ str(not_removed_dir_count)) |
|||
|
|||
print("") |
|||
print("") |
|||
print(rem_cmd + "Deleting items above FAILED:") |
|||
print(" " + rem_cmd + "- files: " + str(not_removed_file_count)) |
|||
print(" " + rem_cmd + "- directories: " |
|||
+ str(not_removed_dir_count)) |
|||
|
|||
print("") |
|||
print("") |
|||
return 0 |
|||
|
|||
|
|||
if __name__ == "__main__": |
|||
sys.exit(main()) |
|||
|
Loading…
Reference in new issue