def create_home_dirs(username_list: list, password: str = None): """Sends the list of users to the file server and runs a script on that server to generate new home directories Arguments: username_list {list} -- list of usernames to generate home directories for Keyword Arguments: password {str} -- file server password (default: {None}) """ username_list_as_string = " ".join(username_list) make_home_dirs_cmd = 'ssh -t hackerspace_admin@tyrell "sudo /nfshome/makehomedirs.sh {}"'.format(username_list_as_string) command_response_list = [ (make_home_dirs_cmd, "hackerspace_admin@tyrell's password: "******"[sudo] password for hackerspace_admin: ", None), (password, "$", None), ] if not password: password = getpass("Enter the admin password: "******"Looks like it worked! Unless you see some nasty errors above...") return success else: utils.print_error("Something went wrong but I'm not smart enough to know what...") return False
def generate_new_movie_file(ssh_connection, username, tv, silent=False): output = '/tmp/slideshow.mp4' # remove existing file ssh_connection.send_cmd( 'rm {}'.format(output), print_stdout=False) # don't care about error if it doesn't exists # create video in defaul location cmd = 'python3 movie_maker.py --images tv{}/{} --output {}'.format( tv, username, output) ssh_connection.send_cmd(cmd) # move the file into the proper location success = ssh_connection.send_cmd('cp {} {}/tv{}/{}.a.mp4'.format( output, TV_ROOT, tv, username)) if success == '': if not silent: # copy so still avail for tv 4 move utils.print_success(f"Video created and placed on tv {tv}.") # also place on tv4 if tv != 4: success = ssh_connection.send_cmd('cp {} {}/tv4/{}.a.mp4'.format( output, TV_ROOT, username)) if success == '': if not silent: utils.print_success("Also placed on tv 4.") else: utils.print_error("Something went wrong sending to tv 4.") else: utils.print_error(f"Something went wrong sending to tv {tv}.")
def archive_users(): ssh_connection = SSH(hostname, SERVER_USERNAME) for p in pwd.getpwall(): username = p[0] user_id = pwd.getpwnam(username).pw_uid account_creation_year = int(str(user_id)[:2]) is_not_system_user = 1000 < user_id < 60000 is_older_than_five_years = current_year - account_creation_year >= 5 if is_not_system_user and is_older_than_five_years: utils.print_error( f"Wiping home drive of user {user_id}: {username}\n") confirmation = utils.input_styled( "Are you sure you wish it proceed? [y/N] (q to quit): \n") if confirmation[0].lower() == "y": ssh_connection.send_cmd(f"rm -rf {home_root}{username}/", sudo=True) utils.print_warning(f'deleted {home_root}{username}/') skeleton = f"bash /nfshome/makehomedirs.sh {username}" ssh_connection.send_cmd(skeleton, sudo=True) utils.print_warning( f'place skeleton to {home_root}{username}/') if ssh_connection.dir_exists(f"{home_root}{username}"): utils.print_success( "User was successfully archived, skeleton placed.") else: utils.print_error( "FATAL: Unable to archive user (no home_dir)") elif confirmation == "q": break else: pass
def refresh_slideshow(username=None): if not username: username = utils.input_styled("Enter username: \n") # connects and checks to see if file with the same name already exisits ssh_connection = SSH(TV_FILE_SERVER, TV_FILE_SERVER_USER, TV_FILE_SERVER_PW) print("Updating movie maker script...") copy_movie_maker_to_host(ssh_connection) # Cycle through TV directories to find student folder for tv in range(1, 5): filepath = "{}/tv{}/".format(TV_ROOT, tv) command = 'ls {}'.format(filepath) dir_contents = ssh_connection.send_cmd(command, print_stdout=False).split() if username in dir_contents: utils.print_success("Found art for {} on TV# {}".format( username, tv)) # refresh the video utils.print_warning( "\nGonna regenerate video slideshow now, THIS MIGHT TAKE A WHILE!" ) generate_new_movie_file(ssh_connection, username, tv) return True utils.print_error( "\nSorry, I could find art for {}. Are you sure they have art uploaded to the server?" .format(username)) return None
def reset_password(): print("Who's password do you want to reset?") fullname, student_number = get_and_confirm_user() if not fullname: return password = getpass("Enter the admin password: "******"{}@{}:~$".format(username, hostname) command_response_list = [ ("sudo passwd {}".format(student_number), "[sudo] password for {}:".format(username), None), (password, "LDAP administrator password: "******"New password: "******"wolf", "Retype new password: "******"wolf", prompt_string, "password updated successfully"), ] success = ssh_connection.send_interactive_commands(command_response_list) if success: utils.print_success("Password for {} successfully reset to {}".format( student_number, default_pw)) else: utils.print_error("Something went wrong...") ssh_connection.close()
def turn_tvs_off_or_on(): which_tvs = None not_quitting = True while not_quitting: user_input = utils.input_styled("o[n] or of[f] or [q]uit? ") if user_input == 'n': cmd = TV_ON_CMD elif user_input == 'f': cmd = TV_OFF_CMD elif user_input == 'q': return else: utils.print_error("Sorry, I didn't understand \"{}\". Hit 'n' for on or 'f' for off.".format(user_input)) continue user_input = utils.input_styled("Which TV #? (default = ALL) ") if user_input == 'q': return elif not user_input: # turn them all off which_tvs = valid_tvs elif user_input in valid_tvs: which_tvs = user_input else: utils.print_error("Sorry, I didn't understand \"{}\". Hit Enter to turn them all off, or enter a number for a specific TV.".format(user_input)) continue for tv_number in which_tvs: hostname = "pi-tv{}".format(tv_number) ssh_connection = SSH(hostname, pi.username, pi.password) ssh_connection.send_cmd(cmd) ssh_connection.close()
def guess_tv(username): # First, see if they already have art on a TV tv = get_tv_containing_student(username) # If they don't already have one, then guess based on their last name if tv is None: name = user_utils.get_users_name(username) if name is None: utils.print_error( "I don't recognize the username '{}'. It could be because they don't have an account in the Hackerspace with this username. You can continue anyway tho." .format(username)) return None # Last name A-M = 1, N-Z = 2 # name variable will be fullname, split on spaces and take last element lastname = name.split()[-1] if lastname[0].lower() <= 'M': utils.print_warning( "Suggesting TV 1 because their last name, {}, is A-M".format( lastname)) tv = 1 else: utils.print_warning( "Suggesting TV 2 because their last name, {}, is N-Z".format( lastname)) tv = 2 return tv
def check_for_disconnected_computers(password=None): # if password is None: # password = getpass("Enter the admin password: "******"{hostname} found.") else: utils.print_error(f"{hostname} missing.")
def reinstall_graphics_drivers(): password = getpass("Enter the admin password: "******"Enter the computer numbers, seperated by spaces (where # is from hostname tbl-h10-#-s e.g: 2 15 30): " ) num_list = numbers.split() for num in num_list: utils.print_warning("Trying computer #{}...".format(num)) good_host = False computer_host = "tbl-h10-{}-s".format(num) good_host = utils.host_exists(computer_host) if not good_host: # this computer # doesn't exist or can't connect utils.print_error( "Host not found. Does that computer exist? Is it on?") continue # now that we know we have a good host, ssh into it and try to run puppet ssh_connection = SSH(computer_host, username, password) nvidia_cmd = "bash /opt/NVIDIA-Linux-x86_64-430.50.run --disable-nouveau --run-nvidia-xconfig --no-x-check --silent" utils.print_warning = "Running command: {}".format(nvidia_cmd) utils.print_warning = "This may take a minute..." output = ssh_connection.send_cmd(nvidia_cmd, sudo=True) if "ERROR" in output: utils.print_error("It looks like something went wrong. Sorry!") print(output) else: utils.print_success( "It looks like it worked. Don't worry about those warnings. I'm going to try to restart computer #{}" .format(num)) output = ssh_connection.send_cmd("/sbin/shutdown -r now", sudo=True) utils.print_success("\nComplete\n\n") ssh_connection.close()
def get_new_users_names(username: str = None) -> tuple: """Prompts for a new user's first and last name Arguments: username {str} -- the user's username. If there is a dot, it will provide default names assuming the username is firstname.lastname Returns: tuple -- (firstname, lastname) """ # firstprompt = "First name: " # lastprompt = "Last name: " # name_guess = None firstname_guess = None lastname_guess = None firstname = None lastname = None if username and '.' in username: name_guess = username.split(sep='.') firstname_guess = name_guess[0].upper().strip() lastname_guess = name_guess[1].upper().strip() while not firstname: firstname = utils.input_plus("First name", firstname_guess) # firstname = utils.input_styled(firstprompt).upper().strip() or (name_guess[0] if name_guess else "") # noqa if not firstname: utils.print_error("Come on, I'm sure they have a name. I need a name or I can't continue") elif firstname == "q": return None, None while not lastname: lastname = utils.input_plus("Last name", lastname_guess) # lastname = utils.input_styled(lastprompt).upper().strip() or (name_guess[1] if name_guess else "") # noqa if not lastname: utils.print_error("They need a last name too buds.") elif lastname == "q": return None, None # print(username, firstname or "(No first name provided)", lastname or "(No last name provided)") return firstname.upper().strip(), lastname.upper().strip()
def get_new_username() -> str: """Asks for a new username and checks if it already exists or not Returns: str -- the username if it's new, or {None} if it already exists or bad username """ username = utils.input_styled( "What is the new username? (must start with a letter, then any combo of letters, numbers, or _-.) ") username = username.lower().strip() # make sure it's valid if not re.match(r'^[a-zA-Z][\w\-._]+$', username): utils.print_error("Invalid username {}".format(username)) return None # does user exist already? if utils.user_exists(username): fullname = get_users_name(username) utils.print_warning("The username {} already exists for {}.".format(username, fullname)) return None else: return username
def modify_user(username, ldif_changes_dict, password=None): """[summary] Args: username: ldap username ldif_changes_dict: a dictionary of ldif keys and their new values, e.g {'sn': 'COUTURE', 'givenName': 'TYLERE'} password: admin password """ if not password: password = getpass("Enter the admin password: "******"sudo ldapmodifyuser {}".format(username) EOF = '\x04' # Ctrl + D command_response_list = [] first = True for key, value in ldif_changes_dict.items(): if first: command_response_list.append((main_command, "[sudo] password for hackerspace_admin: ", None)) command_response_list.append((password, "dc=tbl", None)) first = False else: command_response_list.append((main_command, "dc=tbl", None)) change_tuple = (f"replace: {key}\n{key}: {value}\n{EOF}", '$', None) command_response_list.append(change_tuple) success = ssh_connection.send_interactive_commands(command_response_list) if success: utils.print_success("Looks like it worked to me?") return True else: utils.print_error("Something appears to have gone wrong. Hopefully there's a useful error message somewhere up there.") return False
def add_new_user(): created = False while not created: student_number = utils.input_styled("Enter Student Number: \n") password = getpass("Enter the admin password: "******"An account for {}, {}, already exists. Try resetting their password." .format(student_number, student)) else: first_name = utils.input_styled("First Name: \n").upper() last_name = utils.input_styled("Last Name: \n").upper() create = utils.input_styled( "Create account for {} {} {}? y/[n] \n".format( student_number, first_name, last_name)) if create == 'y': ssh_connection = SSH(hostname, username, password) main_command = 'bash hs-ldapadduser.sh "{}" "{}" "{}"'.format( student_number, first_name, last_name) command_response_list = [ ("cd hs-ldap/", ":~/hs-ldap$", None), (main_command, "[sudo] password for hackerspace_admin: ", None), (password, "Enter LDAP Password: "******"hackerspace_admin@tyrell's password: "******"[sudo] password for hackerspace_admin: ", None), (password, ":~/hs-ldap$", "Set owner on: /nfshome/{}".format(student_number)), ] success = ssh_connection.send_interactive_commands( command_response_list) ssh_connection.close() if success: utils.print_success( 'Successfully created account for {} {} {}'.format( student_number, first_name, last_name)) utils.print_success( 'Their default password will be "wolf"') created = True else: utils.print_error( "Something went wrong there, hopefully useful info is printed above...let's try again\n" ) else: print("Aborted that one. \n") if utils.input_styled("Try again? [y]/n: ") == 'n': return input("\nHit enter to continue...\n")
def add_new_title(): fullname, username = user_utils.get_and_confirm_user() fullname = fullname.title() # gets info of the student who made the art fullname_entered = utils.input_plus("Full name", fullname) if fullname_entered: fullname = fullname_entered grad_year = utils.input_styled("Grad Year: \n") last_name = username.split(".")[-1].title( ) # get the last word if the username tyere.couture will get couture # https://pypi.org/project/inquirer/ subject_list = [ inquirer.List( 'subject', message="What subject is the student in?", choices=[ 'Digital Art', 'Digital Photography', '3D Modelling & Animation', 'Custom subject:' ], ), ] choose_subject = inquirer.prompt(subject_list)["subject"] # gets user to input a custom subject if they so choose if choose_subject == "Custom subject:": custom_subject = utils.input_styled("Well then what are they in? \n") choose_subject = custom_subject default_tv = '1' if last_name.upper()[0] <= 'M' else '2' tv = utils.input_plus( "Which TV # are you sending this to (1 for lastname A-M, 2 for N-Z, 3 for Grads)?", default_tv) if not tv: tv = default_tv filename = username + ".a.title" template = "_template.svg" source_file = "scripts/TVs/{}".format(template) temp_filepath_svg = "{}{}.svg".format(temp_dir, filename) filename_png = filename + ".png" temp_filepath_png = temp_dir + filename_png # creates copy of template with the filename it will use os.system("cp {} {}".format(source_file, temp_filepath_svg)) # writes the student information into the copy of the svg template os.system('sed -i -e "s/FIRSTNAME LASTNAME/{}/g" {}'.format( fullname, temp_filepath_svg)) os.system('sed -i -e "s/YYYY/{}/g" {}'.format(grad_year, temp_filepath_svg)) # need to escape the ampersand character in "3D Modelling & Animation" os.system('sed -i -e "s/SUBJECT/{}/g" {}'.format( choose_subject.replace('&', '\&'), temp_filepath_svg)) # creates a png image from the svg os.system('inkscape -z -e {} -w 1920 -h 1080 {}'.format( temp_filepath_png, temp_filepath_svg)) server_filepath = "tv{}/{}/".format(tv, username) # setup a connect so we can makesure the directory exists ssh_connection = SSH(hostname, SERVER_USERNAME, pi.password) # make sure the directory exists, if not create it: if not ssh_connection.file_exists(server_filepath): ssh_connection.send_cmd('mkdir {}'.format(server_filepath)) # move image onto the server with scp (this will fail if they've never connected to hightower before, hence warning at bottom) command = 'sshpass -p "{}" scp {} {}@{}:{}'.format(pi.password, temp_filepath_png, SERVER_USERNAME, hostname, server_filepath) os.system(command) # removes all temp files we created os.system('rm {}'.format(temp_filepath_png)) os.system('rm {}'.format(temp_filepath_svg)) # Check if file now exists on the server title_exists = ssh_connection.file_exists(server_filepath, filename_png) if title_exists: utils.print_success( f"{filename_png} was successfully sent over to TV {tv}") add_images = utils.confirm( f"Would you like to add images to {fullname}'s new shrine?") if add_images: add_new_media(username, tv) else: gen_video = utils.confirm( "Would you like to regenerate the video file?") if gen_video: refresh_slideshow(username=username) else: utils.print_error( f"The title image '{filename_png}' was not added. Maybe inkscape isn't installed? Or it's possible you've never connected to this server before. \n\n" "Try connecting once first by typing `ssh hightower` into a terminal, then answering yes." )
def add_new_media(username=None, tv=None): media_url = True while media_url: # gets and checks the url of the file media_url, name_without_ext, extension = get_media_url() if media_url is None: return # collects information to name the file, and as to which tv to send it to username_input = utils.input_styled( "Enter username (default = {}): \n".format(username)) if not username_input: pass else: username = username_input tv = guess_tv(username) tv_input = utils.input_styled( "What TV # are you sending this to? (default = {}): ".format(tv)) if not tv_input: pass else: tv = tv_input image_name = None name_good = utils.input_styled( "What is the name of this image? (default = {}): ".format( name_without_ext)) if not name_good: image_name = name_without_ext else: image_name = name_good filename = username + ".z." + image_name + extension # Save videos directly in the tv's rtoot directory. if is_video(extension.lower()): filepath = "{}/tv{}/".format(TV_ROOT, tv) # Save images into a subfolder, which will be used to generate a slideshow video else: filepath = "{}/tv{}/{}/".format(TV_ROOT, tv, username) utils.print_warning( "Sending {} to hightower to see if file exists already with that name." .format(filename)) # connects and checks to see if file with the same name already exisits ssh_connection = SSH(TV_FILE_SERVER, TV_FILE_SERVER_USER, TV_FILE_SERVER_PW) already_exists = ssh_connection.file_exists(filepath, filename) # if it does exist, asks user if they want to overwrite it while already_exists and not utils.confirm( "There is a file that already exists with that name. Do you want to overwrite it?", yes_is_default=False): # don't want to overwrite, so get a new name: image_name = utils.input_styled( "Provide a different name for the image: ") filename = username + ".z." + image_name + extension # check again already_exists = ssh_connection.file_exists(filepath, filename) command = "wget -O {}{} '{}' && exit".format(filepath, filename, media_url) # make sure the directory exists, if not create it: if not ssh_connection.file_exists(filepath): ssh_connection.send_cmd('mkdir {}'.format(filepath)) success = ssh_connection.send_cmd(command) if success: utils.print_success( "{} was succesfully sent over to pi-tv{}".format(filename, tv)) else: utils.print_error( "Something went wrong. Check the filename, is it wonky with weird characters?" ) # asks user if they want to add another image if utils.confirm("Would you like to add another image?"): media_url = True else: break ssh_connection.close() if utils.confirm( "Do you want to generate a new video slideshow of this student's art?" ): refresh_slideshow(username=username)
def refresh_user(): utils.print_warning( "This will refresh a user's account by removing all their customizations and settings. However their files/documents will remain. This can be used if the user is experiences UI issues or having other weird problems with their account." ) password = getpass("Enter the admin password: "******"ENSURE THE USER IS LOGGED OUT BEFORE PERFORMING THIS ACTION!\n") fullname, username = user_utils.get_and_confirm_user() if not fullname: return False home_dir = f"/nfshome/{username}" # First, make sure their home drive exists. Sometimes home drive creation fails when # creating new users in bulk! if ssh_connection.dir_exists(home_dir): move = f"mv {home_dir} {home_dir}.bu" ssh_connection.send_cmd(move, sudo=True) utils.print_success(f"✓ Backing up home drive") skeleton = f"bash /nfshome/makehomedirs.sh {username}" ssh_connection.send_cmd(skeleton, sudo=True) utils.print_success(f"✓ Created skeleton") for dir in dirs: transfer_files(home_dir, dir, ssh_connection) if ssh_connection.dir_exists(home_dir): utils.print_success(f"✓ All files have been recovered") remove_backup = f"rm -rf {home_dir}.bu" ssh_connection.send_cmd(remove_backup, sudo=True) utils.print_success(f"✓ Removing old backup") change_ownership = f"chown -R '{username}:students' '{home_dir}'" ssh_connection.send_cmd(change_ownership, sudo=True) utils.print_success(f"✓ Changed ownership root → {username}") utils.print_success( "Operation complete, please have the user log back in.") else: utils.print_error( f"✗ New home directory does not exist, reverting to backup.") revert_to_backup = f"mv {home_dir}.bu {home_dir}" ssh_connection.send_cmd(revert_to_backup, sudo=True) if ssh_connection.dir_exists(home_dir): utils.print_error( "Reverted to backup successfully (No changes).") else: utils.print_error( "FATAL (PANIC): Error while creating new home directory, could not revert to backup. (No home directory exists)" ) panic = f"bash /nfshome/makehomedirs.sh {username}" ssh_connection.send_cmd(panic, sudo=True) if ssh_connection.dir_exists(home_dir): utils.print_success( "A New home directory was able to be made.") else: utils.print_error( "FATAL (PANIC): COULD NOT CREATE NEW HOME DIRECTORY FOR USER." ) else: # no home drive! Need to make it utils.print_warning( "No home drive detected, creating new home drive...") command = f"bash /nfshome/makehomedirs.sh {username}" ssh_connection.send_cmd(command, sudo=True) utils.print_success( "Operation complete, please have the user log back in.") ssh_connection.close()
def remove_media(username=None, tv=None): ssh_connection = SSH(TV_FILE_SERVER, TV_FILE_SERVER_USER, TV_FILE_SERVER_PW) username = utils.input_plus("Enter username", default=username) if username == "q": return False while True: tv = get_tv_containing_student(username) if tv is None: utils.print_error("No art found for {}".format(username)) return False # get of list of the student's art and display the list filepath = "{}/tv{}/{}/".format(TV_ROOT, tv, username) command = 'ls {}'.format(filepath) dir_contents = ssh_connection.send_cmd(command, print_stdout=False).split() media_list = [ inquirer.List( 'art', message= "Which file do you want to delete? I'll display it first so you can confirm.", choices=dir_contents, ), ] art_file = inquirer.prompt(media_list)["art"] art_file_full_path = filepath + art_file # Show the image with Pillow # Transfer locally local_copy = "/tmp/" + art_file ssh_connection.get_file(art_file_full_path, local_copy) try: img = Image.open(local_copy) except IOError: utils.print_error("File not found") ssh_connection.close() return False w, h = img.size aspect_ratio = w / h thumb = img.resize((400, int(400 / aspect_ratio))) thumb.show() delete_file = utils.confirm( "Are you sure you want to delete {}? Hopefully it popped up for you" .format(art_file), yes_is_default=False) if delete_file: cmd = "rm {}".format(art_file_full_path) ssh_connection.send_cmd(cmd, print_stdout=True) # confirm it's gone: if ssh_connection.file_exists(art_file_full_path): utils.print_error( "\nNot sure what happened there, but the file didn't get deleted. Sorry!" ) else: utils.print_success("\nThe file was successfully deleted.") # Keep deleting files ina loop if they want to keep_going = utils.confirm("Remove another file for this student?") if not keep_going: ssh_connection.close() utils.print_warning( "\nDon't forget to refresh the user's video slideshow!\n") return True
def add_new_users_csv_import(): utils.print_warning(""" Importing new users in bulk requires a csv file with at least these two fields (extra fields are ok): Name, Email1 "Bloggins, Jimmy", "*****@*****.**" ...etc There should be no headings row, just students. Usernames can include @stu.sd72.bc.ca or not. If it is included, it will be removed and only firstname.lastname will be used for their username """) print("""To get this list from MyEd: 1. Go to the Students tab 2. Click the filter and choose Students in My Classes 3. Select the grid icon beside the filter and choose EMAIL STUDENT SD72... 4. Click the print icon and choose CSV 5. Save the file to your Downloads as StudentList.csv """) # Get a CSV file and read it: while True: utils.print_warning( "If the csv file is not in the default location you'll have to tell me where it is, and what it's called, e.g:") utils.print_warning('"/home/myusername/Documents/students.csv"') student_csv_file = utils.input_plus('Where can I find the csv file?', default_csv_location) if student_csv_file == 'q': return if os.path.isfile(student_csv_file): # good, carry on with script break else: utils.print_error("I couldn't find this file: {} ".format(student_csv_file)) password = getpass("Enter the admin password: "******"" name_heading = "Name" email_heading = "Email1" ssh_connection = ssh.SSH(hostname, server_username, password) with open(student_csv_file) as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') name_column = None email_column = None for row in csv_reader: if name_column is None: # then this is the header row try: name_column = row.index(name_heading) email_column = row.index(email_heading) except ValueError: # column doesn't exist ssh_connection.close() return False else: try: username = parse_username_from_email(row[email_column]) except IndexError: # Probably blank lines at the end of the list, or missing data? Even missing data should be a blank string print("Skipping line. Blank line at end of list or missing student email field.") continue # Check if user already exists or not if utils.user_exists(username): utils.print_warning("This user already exists: {}".format(username)) continue # go to next user # Name column is in format: "Lastname, Firstname" first_name = row[name_column].split(",")[1] last_name = row[name_column].split(",")[0] add_new_user( username, first_name=first_name, last_name=last_name, password=password, ssh_connection=ssh_connection, bulk_creation=True, ) # next_uid = get_next_avail_uid() # ldif_entry += generate_ldif_entry(username, first_name, last_name, next_uid) + "\n" # success = create_users_from_ldif(ldif_entry, password=password) # create home dir for new user # if success: # return create_home_dirs([username], password) ssh_connection.close() print("All done!")
def add_new_theme(): #gets and checks the url of the file mp3_url = True while mp3_url == True: have_good_input = False mp3_url = "" while not have_good_input: mp3_url = utils.input_styled( "Paste the url to the mp3 file you want to add. (q to quit): \n" ) if mp3_url == 'q': break #check content to ensure proper mp3 type mime_type_good = utils.verify_mimetype(mp3_url, "audio/mpeg") have_good_input = mime_type_good if have_good_input: #then get file number have_good_input = False while not have_good_input: filename = os.path.basename(urlparse(mp3_url).path) name, ext = os.path.splitext(filename) #check if filename is already a number, and offer to use it try: name = int(name) good_name_already = True except ValueError: good_name_already = False prompt = "What number (integers only) do you want to give it?" + ( " [Enter] = {}".format(name) if good_name_already else "") + "\n" mp3_number = utils.input_styled(prompt) try: if good_name_already and not mp3_number: mp3_number = name else: mp3_number = int(mp3_number) have_good_input = True except ValueError: utils.print_error("Dude, that wasn't an integer! ") have_good_input = False filename = "{}.mp3".format(mp3_number) # print("test: {}".format(filename)) print( "Sending {} to pi-themes to see if file exists already with that name." .format(filename)) command = "wget -O /media/THEMES/{} {} && exit".format( filename, mp3_url) filepath = "/media/THEMES/" ssh_connection = SSH(hostname, pi.username, pi.password) #checks if file exists, and if user wants to overwrite it already_exists = ssh_connection.file_exists(filepath, filename) # asks user if they want to overwrite https://www.quora.com/I%E2%80%99m-new-to-Python-how-can-I-write-a-yes-no-question if already_exists == True: overwrite = utils.input_styled( "There is a file that already exists with that name. Do you want to overwrite it? (y/[n]) \n", utils.ByteStyle.WARNING, ) if not overwrite or overwrite.lower()[0] == "n": mp3_url = True elif overwrite.lower()[0] == "y": already_exists = False pass else: utils.print_styled("(y/n)", color=utils.ByteStyle.Y_N) elif already_exists == False: pass else: utils.print_error( "Something went wrong. Expected true or false but got something else" ) #sends the command if already_exists == False: ssh_connection.send_cmd(command) another_code = utils.input_styled( "Would you like to add another code? ([y]/n) \n", color=utils.ByteStyle.Y_N) if not another_code or another_code.lower()[0] == "y": mp3_url = True pass elif another_code.lower()[0] == "n": mp3_url = False else: utils.print_styled( "(y/n)", utils.ByteStyle.Y_N, ) #closes ssh connection ssh_connection.close()
def add_new_title(): #gets info of the student who made the art first_name = utils.input_styled("First name: \n") last_name = utils.input_styled("Last name: \n") grad_year = utils.input_styled("Grad Year: \n") student_number = utils.input_styled("Student number: \n") #https://pypi.org/project/inquirer/ subject_list = [ inquirer.List( 'subject', message="What subject is the student in?", choices=[ 'Digital Art', 'Digital Photography', '3D Modelling & Animation', 'Custom subject:' ], ), ] choose_subject = inquirer.prompt(subject_list)["subject"] #gets user to input a custom subject if they so choose if choose_subject == "Custom subject:": custom_subject = utils.input_styled("Well then what are they in? \n") choose_subject = custom_subject default_tv = '1' if last_name.upper()[0] <= 'M' else '2' tv = utils.input_styled( "Which TV # are you sending this to (1 for lastname A-M, 2 for N-Z)? [Enter] = {}: \n" .format(default_tv)) if not tv: tv = default_tv filename = student_number + ".a." + first_name + last_name template = "_template.svg" source_file = "scripts/TVs/{}".format(template) temp_filepath_svg = "{}{}.svg".format(temp_dir, filename) filename_png = filename + ".png" temp_filepath_png = temp_dir + filename_png #creates copy of template with the filename it will use os.system("cp {} {}".format(source_file, temp_filepath_svg)) # writes the student information into the copy of the svg template os.system('sed -i -e "s/FIRSTNAME LASTNAME/{} {}/g" {}'.format( first_name, last_name, temp_filepath_svg)) os.system('sed -i -e "s/YYYY/{}/g" {}'.format(grad_year, temp_filepath_svg)) os.system('sed -i -e "s/SUBJECT/{}/g" {}'.format(choose_subject, temp_filepath_svg)) # creates a png image from the svg os.system('inkscape -z -e {} -w 1920 -h 1080 {}'.format( temp_filepath_png, temp_filepath_svg)) filepath_pi = "/home/pi-slideshow/tv{}/{}/".format(tv, student_number) # make a folder for a new student when title card is made os.system('sshpass -p "{}" ssh {}@{} && mkdir {}'.format( pi.password, username, hostname, filepath_pi)) #scps into the tv photo directory command = 'sshpass -p "{}" scp {} {}@{}:{}'.format(pi.password, temp_filepath_png, username, hostname, filepath_pi) os.system(command) #removes all files it created os.system('rm {}'.format(temp_filepath_png)) os.system('rm {}'.format(temp_filepath_svg)) #os.system('rm {}.png'.format(filename)) ssh_connection = SSH(hostname, username, pi.password) title_exists = ssh_connection.file_exists(filepath_pi, filename_png) if title_exists: utils.print_success("{} was successfully sent over to TV {}".format( filename_png, tv)) add_images = utils.input_styled( utils.ByteStyle.Y_N, "Would you like to add images to {}'s new shrine? ([y]/n)\n". format(first_name)) if not add_images or add_images.lower()[0] == "y": add_new_media.add_new_media(student_number, tv) else: utils.print_error( "The title image '{}' was not added. Is sshpass installed?".format( filename_png))
def add_new_theme(): # gets and checks the url of the file mp3_url = True while mp3_url is True: have_good_input = False mp3_url = "" while not have_good_input: mp3_url = utils.input_styled("Paste the url to the mp3 file you want to add. (q to quit): \n") if mp3_url == 'q': break # check content to ensure proper mp3 type mime_type_good = utils.verify_mimetype(mp3_url, "audio/mpeg") have_good_input = mime_type_good if have_good_input: # then get file number have_good_input = False while not have_good_input: filename = os.path.basename(urlparse(mp3_url).path) name, ext = os.path.splitext(filename) # check if filename is already a number, and offer to use it try: name = int(name) good_name_already = True except ValueError: good_name_already = False prompt = "What code do you want to give it?" + (" [Enter] = {}".format(name) if good_name_already else "") + "\n" mp3_code = utils.input_styled(prompt) # try: if good_name_already and not mp3_code: mp3_code = name # else: # mp3_number = int(mp3_number) have_good_input = True # except ValueError: # utils.print_error("Dude, that wasn't an integer! ") # have_good_input = False filename = "{}.mp3".format(mp3_code) # print("test: {}".format(filename)) print("Sending {} to pi-themes to see if file exists already with that name.".format(filename)) command = "wget -O {}{} {} && exit".format(THEMES_PATH, filename, mp3_url) ssh_connection = SSH(hostname, pi.username, pi.password) # checks if file exists, and if user wants to overwrite it already_exists = ssh_connection.file_exists(THEMES_PATH, filename) # asks user if they want to overwrite https://www.quora.com/I%E2%80%99m-new-to-Python-how-can-I-write-a-yes-no-question if already_exists: overwrite = utils.confirm( "There is a file that already exists with that name. Do you want to overwrite it?", yes_is_default=False ) if not overwrite: mp3_url = True else: already_exists = False pass elif not already_exists: pass else: utils.print_error("Something went wrong. Expected true or false but got something else") # sends the command if not already_exists: ssh_connection.send_cmd(command) another_code = utils.confirm("Would you like to add another code?") if another_code: mp3_url = True pass else: mp3_url = False ssh_connection.close()