-
Notifications
You must be signed in to change notification settings - Fork 0
Neu folder handler #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
noschXL
wants to merge
12
commits into
master
Choose a base branch
from
NeuFolderHandler
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6dad2f0
added a nvim config and a python select script
noschXL e66add3
finished the python script to install userconfigs
noschXL ed4a3c9
added flags to the userinstall
noschXL 186c8e8
fixed minor errors
noschXL c938094
fixed an dir bug
noschXL bc9ecbb
now removing old configs
noschXL 0b3ec4f
now using shutil to rm dirs with content
noschXL e74acea
fixed installpackages.py?
noschXL 637fa83
added user folder handling
noschXL c2e3898
finished the Memberfolders and added a quick fix to installuserconfig.py
noschXL beee18d
forgot to set the group in userhandler for chown
noschXL 34a5038
changed the path to user folders
noschXL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import os | ||
| import shutil | ||
| import sys | ||
| import pwd | ||
| import grp | ||
|
|
||
| cwd = os.path.dirname(os.path.abspath(__file__)) | ||
|
|
||
| users = os.listdir("/home") | ||
| configdirectory = cwd + "/../configs" | ||
|
|
||
| args = sys.argv[1:] | ||
|
|
||
| def chown(name = "roboag", group = "roboag", folder = ""): | ||
| if folder == "": | ||
| sys.exit("error: no folder given") | ||
|
|
||
| userid = pwd.getpwnam(name).pw_uid | ||
| groupid = grp.getgrnam(group).gr_gid | ||
|
|
||
| for root, dirs, files in os.walk(folder): | ||
| for name in dirs + files: | ||
| os.chown(os.path.join(root, name), userid, groupid) | ||
|
|
||
| def gethelp(): | ||
| print("this script is made to install configs to /home/<user>/.config") | ||
| print(" -h: shows this help screen") | ||
| print(" -y: autoconfirm all, usefull for automatic updates") | ||
| print(" -d: enables debug mode") | ||
|
|
||
| def InstallConfig(debug = False, NoUser = False): | ||
| print() | ||
| ### GET CONFIG TO INSTALL ### | ||
| config_modules = os.listdir(configdirectory) | ||
| if debug: | ||
| print("DEBUG MODE ENABLED, NO CHANGES ARE MADE") | ||
| print(f"cwd: {cwd}") | ||
| print(f"configdirectory: {configdirectory}") | ||
| print(f"config_modules: {config_modules}") | ||
| echo = ["0: all below"] | ||
| echo += [str(i+ 1) + ": " + module for i, module in enumerate(config_modules)] | ||
| for line in echo: | ||
| print(line) | ||
|
|
||
| configselection = -1 | ||
| if NoUser: | ||
| configselection = 0 | ||
| else: | ||
| try: | ||
| userinput = input("config to install (default = 0): ") | ||
| print() | ||
| if userinput == "": | ||
| configselection = 0 | ||
| else: | ||
| configselection = int(userinput) | ||
| except ValueError as e: | ||
| print("error: " + e.__str__()) | ||
| print("Aborting ...") | ||
| sys.exit() | ||
|
|
||
| if configselection < 0 or configselection > len(config_modules): | ||
| print("selection out of bounds") | ||
| print("Aborting ...") | ||
| sys.exit() | ||
|
|
||
|
|
||
| ### SAME THING FOR USERS ### | ||
| echo = ["0: all below"] | ||
| echo += [str(i+ 1) + ": " + module for i, module in enumerate(users)] | ||
| for line in echo: | ||
| print(line) | ||
|
|
||
| userselection = -1 | ||
|
|
||
| if NoUser: | ||
| userselection = 0 | ||
| else: | ||
| try: | ||
| userinput = input("user to install to (default = 0): ") | ||
| print() | ||
|
|
||
| if userinput == "": | ||
| userselection = 0 | ||
| else: | ||
| userselection = int(userinput) | ||
| except ValueError as e: | ||
| print("error: " + e.__str__()) | ||
| print("Aborting ...") | ||
| sys.exit() | ||
|
|
||
|
|
||
| if userselection < 0 or userselection > len(users): | ||
| print("selection out of bounds") | ||
| print("Aborting ...") | ||
| sys.exit() | ||
|
|
||
|
|
||
| configs_to_install = [] | ||
| print("installing: ") | ||
| if configselection == 0: | ||
| for module in config_modules: | ||
| print(" - " + module) | ||
| configs_to_install.append(module) | ||
| else: | ||
| print(" - " + config_modules[configselection - 1]) | ||
| configs_to_install.append(config_modules[configselection - 1]) | ||
|
|
||
| users_to_install = [] | ||
| print("for: ") | ||
| if userselection == 0: | ||
| for user in users: | ||
| print(" - " + user) | ||
| users_to_install.append(user) | ||
| else: | ||
| print(" - " + users[userselection - 1]) | ||
| users_to_install.append(users[userselection - 1]) | ||
|
|
||
| if not NoUser: | ||
| if not input("are you sure? y/N: ").strip().lower() in ["yes", "y"]: | ||
| print("Aborting ...") | ||
| sys.exit() | ||
|
|
||
| for user in users_to_install: | ||
| userconfigdir = "/home/"+user+"/.config/" | ||
| for config in configs_to_install: | ||
| if os.path.exists(userconfigdir+config): | ||
| print("removing old config") | ||
| if debug == False: | ||
| if os.path.exists(userconfigdir+config): | ||
| shutil.rmtree(userconfigdir+config) | ||
| shutil.copytree(configdirectory + "/" +config, userconfigdir+config) | ||
| chown(name = user,group = user ,folder = userconfigdir+config) | ||
| print("copied "+configdirectory+"/"+config+" into "+userconfigdir+config) | ||
|
|
||
| print("finished") | ||
|
|
||
| if args == []: | ||
| InstallConfig() | ||
| sys.exit() | ||
|
|
||
| if args[0][:2] == "-h": | ||
| gethelp() | ||
| sys.exit() | ||
| else: | ||
| flags = args[0][1:] | ||
| NoUser = False | ||
| Debug = False | ||
| for flag in flags: | ||
| if flag == "y": | ||
| NoUser = True | ||
| if flag == "d": | ||
| Debug = True | ||
|
|
||
| InstallConfig(debug=Debug, NoUser=NoUser) |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also split up functions to create, move, perms seperately |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import os | ||
| import sys | ||
| import shutil | ||
| import time | ||
| import pwd | ||
| import grp | ||
|
|
||
| MEMBER_FOLDER_PATH = "/opt/roboag/data/roboag/User" | ||
|
|
||
| def list_members(): | ||
| return os.listdir(MEMBER_FOLDER_PATH) | ||
|
|
||
| def create_member_folder(name): | ||
| path = os.path.join(MEMBER_FOLDER_PATH, name) | ||
| os.mkdir(path) | ||
| os.mkdir(path + "/neu") | ||
|
|
||
| def chown(name = "roboag", group = "roboag", folder = ""): | ||
| if folder == "": | ||
| sys.exit("error: no folder given") | ||
|
|
||
| userid = pwd.getpwnam(name).pw_uid | ||
| groupid = grp.getgrnam(group).gr_gid | ||
|
|
||
| for root, dirs, files in os.walk(folder): | ||
| for name in dirs + files: | ||
| os.chown(os.path.join(root, name), userid, groupid) | ||
|
|
||
| def create_member_neu_folder(name, debug = False): | ||
| memberfolder = os.path.join(MEMBER_FOLDER_PATH, name) | ||
| date = time.strftime("%Y_%m_%d_") | ||
|
|
||
| if not debug: | ||
| os.makedirs(memberfolder + "/neu", exist_ok = True) | ||
| os.makedirs(memberfolder + f"/{date}", exist_ok = True) | ||
| else: | ||
| print("would create:") | ||
| print(memberfolder + "/neu") | ||
| print(memberfolder + f"/{date}") | ||
|
|
||
| neucontent = os.listdir(memberfolder + "/neu/") | ||
| if neucontent == []: | ||
| print(name, "did not have anything in 'neu'") | ||
| return | ||
|
|
||
| if not debug: | ||
| shutil.move(memberfolder + "/neu/", memberfolder + f"/{date}/.") | ||
| else: | ||
| print("would move:") | ||
| print(memberfolder + "/neu/") | ||
| print(memberfolder + f"/{date}/.") | ||
|
|
||
| ### USER PERMS | ||
| if not debug: | ||
| chown("roboag", "roboag", memberfolder + f"/{date}") | ||
| chown("roboag", "roboag", memberfolder + "/neu") | ||
| else: | ||
| print("would change perms:") | ||
| print(memberfolder + f"/{date}") | ||
| print(memberfolder + "/neu") | ||
| print("to:") | ||
| print("roboag:roboag") | ||
|
|
||
| def NewNeuFolder(debug = False): | ||
| for folder in os.listdir(MEMBER_FOLDER_PATH): | ||
| if folder == "_alte_Daten": | ||
| continue | ||
| create_member_neu_folder(folder, debug = debug) | ||
|
|
||
| print("done :-)") | ||
|
|
||
| def help(): | ||
| print("this script handles the member folders") | ||
| print(" -h: shows this help screen") | ||
| print(" -d: enables debug mode") | ||
| print(" -n: creates a new neu folder for all users") | ||
| print(" -a: creates a new member folder") | ||
| print(" -r: removes a member folder") | ||
|
|
||
| args = sys.argv[1:] | ||
| if args == []: | ||
| help() | ||
| sys.exit() | ||
|
|
||
| Debug = False | ||
| Neu = False | ||
| add = False | ||
| rem = False | ||
|
|
||
| for char in args[0][1:]: | ||
| if char == "h": | ||
| help() | ||
| sys.exit() | ||
| if char == "d": | ||
| Debug = True | ||
| if char == "n": | ||
| Neu = True | ||
| if char == "a": | ||
| add = True | ||
| if char == "r": | ||
| rem = True | ||
|
|
||
| if add and rem: | ||
| print("error: you can't add and remove at the same time") | ||
| help() | ||
| sys.exit() | ||
|
|
||
| if add: | ||
| if not Debug: | ||
| create_member_folder(args[2]) | ||
| else: | ||
| print("would create:") | ||
| print(os.path.join(MEMBER_FOLDER_PATH, args[2])) | ||
|
|
||
| if rem: | ||
| if not Debug: | ||
| shutil.rmtree(os.path.join(MEMBER_FOLDER_PATH, args[2])) | ||
| else: | ||
| print("would remove:") | ||
| print(os.path.join(MEMBER_FOLDER_PATH, args[2])) | ||
|
|
||
| if Neu: | ||
| NewNeuFolder(debug = Debug) | ||
| sys.exit() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move a lot of the installuserconfig stuff into seperate functions, this is hard to read
optimally you would have the install function pretty much only have descriptive calls to functions so you can see
which would make the code a lot easier to navigate for new people/if you haven't worked on it in a long time