Last active
July 28, 2023 15:38
-
-
Save tetoNidan/27223269c35cb922988b6b05858d0f37 to your computer and use it in GitHub Desktop.
A class for troubleshooting steamtinkerlaunch -> [ STL ]. Easily find all locations STL has installed files to. This class can easily remove those files, this makes starting from a clean state a little easier. Usage in the docstring.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""A simple file, class to find or remove steamtinkerlaunch files and | |
directories""" | |
from os import walk, remove | |
from os.path import (expanduser, splitext, isfile, isdir, exists, | |
join as os_join) | |
from shutil import rmtree | |
from pathlib import Path | |
from sys import argv, exit as sexit | |
class STLfind: | |
""" A simple class to find or remove steamtinkerlaunch left over files from | |
uninstall. Or if you just need to find all file locations it's installed to. | |
Arguments flags: | |
-f, --find Will search all directories starting with / looking for | |
steamtinkerlaunch directories and files. | |
By default will exclude /mnt as is common for external | |
drives and backups. Will reveal files that are in /home. | |
Useful if you just need to check if user's stl data is | |
there. | |
The default can be overridden by passing your own **exclude | |
directories. See exclude under keyword arguments. | |
-rm, --remove Will search all directories starting with / looking for | |
steamtinkerlaunch directories and files. If found they will | |
be removed. | |
By default all files in /mnt and /home will be preserved. | |
The files in /mnt are likely from backups like timeshift. | |
The files in /home will be configuration files. Will be | |
handy to have on reinstall to keep | |
~/.config/steamtinkerlaunch settings including MO2. | |
--wipe --wipe must be used with one of the (--remove, -rm) flags. | |
In addition to removing the main directories, it will remove | |
all configuration files in /home. This will be done for all | |
users in /home! --wipe will override the exclude keyword | |
argument. | |
! WARNING ! Beware! This will defiantly remove all steamtinkerlaunch. | |
You have been warned! To exclude directory from being | |
searched see below: Keyword arguments -> exclude | |
-h, --help Show help options. | |
Keyword arguments: | |
exclude Will exclude directories that are searched for | |
steamtinkerlaunch. It takes a list of comma separated values. | |
Example: exclude=mnt,home | |
The above will exclude the directories mnt and home from | |
being searched and thusly, not removed either. | |
Important: You must use the equals sign [E.G. =] to separate the | |
keyword 'exclude' from its values. In the examples case | |
'mnt,home'. | |
You must use the comma [E.G. ,] to separate the values. There | |
must be no spaces before or after the comma. | |
To find everything: | |
exclude= | |
Python module: You can import this class to your project through normal | |
import statement. | |
Important: You must pass a list, tuple or set as the args. | |
You must pass a dictionary as the kargs. The dictionary's | |
value must be a list set or tuple. | |
Example: from STLfind import STLfind as stl_find | |
stl_find(['-f'], {'exclude': ['mnt', 'home'])) | |
The above will find steamtinkerlaunch locations but exclude | |
the directories mnt and home.""" | |
def __init__(self, args, kargs): | |
""" Initiate all variables for use with this class. Use logic to | |
execute the proper methods when properly requested.""" | |
## Set up flags to look for | |
find_ = ('-f', '--find') | |
remove_ = ('-rm', '--remove') | |
wipe_ = '--wipe' | |
help_ = ('-h', '--help') | |
## Set string that will be matched to directories and file names. | |
self.stl = ('steamtinkerlaunch', 'SteamTinkerLaunch') | |
## Set up the found list. Will hold all directories and files found by | |
## the self.find_stl method. | |
self.found = {'files': set(), 'dirs': set()} | |
## Set the remove attribute to False. Will be set to True if user | |
## sets -r or --remove as the args flag | |
self.remove = False | |
## Set up the keyword argument. | |
if 'exclude' in kargs.keys(): | |
if isinstance(kargs['exclude'], str): | |
## kargs['exclude']'s value was a string. Split into a list. | |
kargs['exclude'] = kargs['exclude'].split(',') | |
## Add True variable to indicate the user has specified paths | |
kargs['user_exclude'] = True | |
else: | |
## Users instructions might be unclear. Send help. | |
sexit(self.__doc__) | |
else: | |
## Set up default locations to be excluded. Defaults to /mnt and | |
## /home, /home is included to save stl's config files. Add False | |
## as the user had not passed exclude directories. | |
kargs = {'exclude': ['mnt', 'home']} | |
## Add False variable to indicate the user has not specified paths | |
kargs['user_exclude'] = False | |
## Set the flags to their methods, self.find_stl and self.remove_stl. | |
## Execute those methods. | |
if len([x for x in args for y in find_ if y == x]) != 0: | |
## User wants us to find steamtinkerlaunch : ('-f', '--find') | |
## Find out if the user requested to exclude directories? | |
if kargs['user_exclude'] is False: | |
## Default find behavior is to reveal stl files in /home. | |
kargs = {'exclude': ['mnt']} | |
print('Looking for Steamtinkerlaunch,\n please be patient...') | |
self.find_stl(kargs) | |
elif len([x for x in args for y in remove_ if y == x]) != 0: | |
## User wants us to remove steamtinkerlaunch. : ('-rm', '--remove') | |
## Set self.remove = True | |
self.remove = True | |
## Check to see if user has chosen to override default file removal | |
## with --wipe flag removing /home config from excluded locations. | |
if wipe_ in args: | |
kargs = {'exclude': ['mnt']} | |
## Run the remove method. | |
msg = 'Looking for and removing Steamtinkerlaunch.\n' | |
msg += ' Please be patient...' | |
print(msg) | |
self.remove_stl(kargs) | |
elif len([x for x in args for y in help_ if y == x]) != 0: | |
## User wants help : ('-h', '--help') | |
sexit(self.__doc__) | |
else: | |
## Error, No valid flags passed. Show help! | |
sexit(self.__doc__) | |
def find_stl(self, kargs): | |
""" Find all directories and files that contain steamtinkerlaunch """ | |
## Loop through the root directory. | |
for path_, dirs, files in walk(expanduser('/')): | |
## Split the path_ into a set to use in comparing directories to | |
## exclude from search. | |
parts = Path(path_).parts | |
## Handle exclusion logic for directories not to be searched | |
if len(parts) > 0 and [x for x in kargs['exclude'] if | |
x in parts] == []: | |
for dir_ in dirs: | |
## Loop through directories, Make sure if not a file and | |
## name matches steamtinkerlaunch. | |
if not isfile(dir_) and dir_ in self.stl: | |
## If self.remove is True add to self.found['dirs'] | |
if self.remove is True: | |
## this is to suppress the print statement. Will be | |
## printed to user on removal. | |
self.found['dirs'].add(os_join(path_, dir_)) | |
else: | |
## Add directory location self.found['dirs'] | |
self.found['dirs'].add(os_join(path_, dir_)) | |
## Print files and method action to be taken. | |
print(f'Found: {os_join(path_, dir_)}') | |
for file_ in files: | |
## Loop through files. Make sure is not directory and | |
## filename matches steamtinkerlaunch. | |
if not isdir(file_) and splitext(file_)[0] in self.stl: | |
## If self.remove is True add to self.found['files'] | |
if self.remove is True: | |
## this is to suppress the print statement. Will be | |
## printed to user on removal. | |
self.found['files'].add(os_join(path_, file_)) | |
else: | |
## Add file location self.found['files'] | |
self.found['files'].add(os_join(path_, file_)) | |
## Print found files and method action to be taken. | |
print(f'Found: {os_join(path_, file_)}') | |
def remove_stl(self, kargs): | |
""" Remove all found directories and files that contain | |
steamtinkerlaunch""" | |
## Find all steamtinkerlaunch directories and files | |
self.find_stl(kargs) | |
## Loop through found directories | |
for loc in self.found['dirs']: | |
## Remove directory | |
if exists(loc): | |
rmtree(loc) | |
## Report to user directory has been removed. | |
print(f'Removing: {loc}') | |
## Loop through found files | |
for loc in self.found['files']: | |
## Remove file | |
if exists(loc): | |
remove(loc) | |
## Report to user file has been removed. | |
print(f'Removing: {loc}') | |
if __name__ == '__main__': | |
## Remove the first argument from sys.argv. It's just the file name. | |
argv = argv[1:] | |
## Set up the separator to test if arguments are keywords arguments or not. | |
sep = (':', '=') | |
## Set up the flag arguments. Excluding the above set ('=', ':') | |
arg = [x for x in argv for y in sep if y not in x and x[0] == '-'] | |
## Set up the keyword arguments using the separator ('=', ':') to find them. | |
karg = {x.split(y, maxsplit=1)[0]: x.split(y, maxsplit=1)[1] for | |
x in argv for y in sep if y in x} | |
## Try to run program! | |
try: | |
## Run the program. | |
STLfind(set(arg), karg) | |
except KeyboardInterrupt as kb_error: | |
## User has chosen to exit the program. | |
sexit("\nGoodbye.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Crash course:
!! important: These instructions are a work in progress and incomplete. My goal is to write a description of usage for reference useful for those that have limited experience in the terminal or those who would like to see some better instructions of usage with examples.
Objectives to include in these instructions (reference to the author, me):
Download the file from the top right of this page to whatever directory you want. Unzip the archive. From here I will assume the script is in the
~/Downloads
folder.Open a terminal and change directories to where the script is located.
First we will run the script to find all steamtinkerlaunch locations. I think it's useful to search everything. That way we can set up our directories to be excluded if need be. Directories you might want to keep are any backup locations you have and the files in
~/.config/steamtinkerlaunch
which are the config files and user settings. If you have a backup program like Timeshift. If you have your backups on an external drive and your external backup drive is mounted in /mnt those directories will be excluded by default. For this example though we want to find every location STL is found in.