#!/usr/bin/env python3 import fiximports from sys import platform from DeclarScript.DeclarScript import Command from DeclarScript.DeclarScript import run_commands # test for detection of backups return_code_user_input_backups = [ #(return_code, backup_command_list) (2, [Command('ls -l', 'Beginning backup command list if return code is 2.'), Command('sleep 2', 'End this back up list')]), (3, [Command('ps -a', 'Beginning backup command list if return code is 3.'), Command('echo "Howdy Partner"'), Command('sleep 2', 'End this back up list')]), ] default_backup_list = [ Command('echo "Yup, this one\'s the default backup list. try entering 2 or 3 for the return code."', 'Beginning of the default') ] test_commands_linux = [ Command('ls', "List files."), Command('sleep 1', 'Sleep for 1 second.'), Command('./examples/returncode.py', 'Run the program with user input.', critical=False, return_code_backup_command_lists=return_code_user_input_backups, backup_command_list=default_backup_list), Command('ps', "List processes.") ] test_commands_windows = [ Command('ls', "List files."), Command('sleep 1', 'Sleep for 1 second.'), Command('examples\\returncode.py', 'Run the program with user input.', critical=False, return_code_backup_command_lists=return_code_user_input_backups, backup_command_list=default_backup_list),
#!/usr/bin/env python3 import fiximports from DeclarScript.DeclarScript import Command from DeclarScript.DeclarScript import run_commands # test for a single command test_commands_1 = [Command('ls', "listing all the files in the current directory:")] # test for multiple commands test_commands_2 = [ Command('ls -l', '~listing all the files in the current directory:'), Command('sleep 5', visible=False), Command('touch dummyfile.txt', '~creating a new file'), Command('ls -l', '~listing all the files in the current directory:'), Command('sleep 5', visible=False), Command('rm dummyfile.txt', '~removing the created file'), Command('ls -l', '~listing all the files in the current directory:') ] # test to make sure aaaaa command fails and stops test_commands_3 = [ Command('ls -l', '~listing all the files in the current directory:'), Command('sleep 5', visible=False), Command('aaaaa', '~bad input'), Command('ps -al', '~listing all the processes:') ] # test to make sure jibberish command fails and continues test_commands_4 = [ Command('ls -l', '~listing all the files in the current directory:'), Command('sleep 5', visible=False),
### Tutorial 3 ### ### Backup Command Lists ### # When a command fails, you can supply a backup command list to execute afterwards. # These backup command lists will execute in a recursively, resembling a tree-like # logical structure with different branches. # The main branch is given the name 'root', and the other branches will be named # automatically based on their failed command(s) that caused their execution. # Here's an example of a tree: # *Note that higher level backups must be declared first since other command lists # reference them. DDD_backups = [ Command('echo "We\'re in DDD\'s backup command list"', 'Begin the backup command list:'), Command('ls', 'Listing all the files in the current directory:'), Command('rm SUPER_COOL_FILE.txt', 'Removing the created file...'), Command('ls', 'Listing all the files in the current directory:'), ] CCC_backups = [ Command('echo "We\'re in CCC\'s backup command list"', '~Begin the backup command list:') ] BBB_backups = [ Command('echo "We\'re in BBB\'s backup command list"', 'Begin the backup command list:'), Command('CCC', 'CCC has backups',
#!/usr/bin/env python3 import fiximports from DeclarScript.DeclarScript import Command, run_commands hello_commands = [ Command('echo "Hello World! :)"'), Command('ls -l'), Command('sleep 2', visible=False), Command('ps -al'), Command('cool') ] run_commands(hello_commands)
#!/usr/bin/env python3 import fiximports from DeclarScript.DeclarScript import Command from DeclarScript.DeclarScript import run_commands # test for detection of backups return_code_user_input_backups = [ #(return_code, backup_command_list) (1, [Command('ls -l', 'Beginning backup command list if return code is 1.'), Command('sleep 2', 'End this back up list')]), (3, [Command('ps -a', 'Beginning backup command list if return code is 3.'), Command('sleep 2', 'End this back up list')]) ] test_commands_1 = [ Command('ls', "List files."), Command('sleep 2', 'Sleep for two seconds.'), Command('./examples/user-input.py', 'Run the program with user input.', prompt_responses=['5', '3'], critical=False, return_code_backup_command_lists=return_code_user_input_backups), Command('ls', "List files."), Command('ps', "List processes."), Command('sleep 2', 'Sleep for two seconds.'), Command('sudo ./examples/user-input2.py', 'Run the program with user input.', prompt_responses=['4']), Command('sleep 2', 'Sleep for two seconds.') ] print("\n\n\nRunning Test Commands 1:") run_commands(test_commands_1)
# Any other value for a return code means that something went wrong. There are # countless internet resources that describe what each of these return codes signify. # However, the specific return code values are not the focus of this exercise. # For DeclarScript, all you need to know is this: given a return code, you can # create a backup command list that will be triggered in the event of a specific # return code. This "return_code_backup_command_lists" parameter is completely # separate from the "backup_command_list" parameter for each Command, but they # function nearly identially. Basically, just think of the backup_command_lists as # a catch-all for most errors and return_code_backup_command_lists as situationally # pertaining to specific errors. # Let's look at an example: # The following bunch command lists are referenced by the main return_code_backup_command_list return_code_1_backup = [ Command('sleep 1', visible=False), Command('echo "A partridge in a pear tree"') ] return_code_2_backup = [ Command('sleep 1', visible=False), Command('echo "Two turtle doves and"') ] return_code_3_backup = [ Command('sleep 1', visible=False), Command('echo "Three French hens"') ] return_code_4_backup = [ Command('sleep 1', visible=False),
#!/usr/bin/env python3 import fiximports from DeclarScript.DeclarScript import Command from DeclarScript.DeclarScript import run_commands dddd_backups = [ Command('ls -l', '~listing all the files in the current directory:'), Command('touch dummyfile.txt', '~creating a new file'), Command('ls -l', '~listing all the files in the current directory:'), Command('rm dummyfile.txt', '~removing the created file'), Command('ls -l', '~listing all the files in the current directory:'), Command('sleep 2', '~sleep for 2 seconds:') ] cccc_backups = [ Command('ls -l', '~listing all the files in the current directory:'), Command('sleep 2', '~sleep for 2 seconds:') ] bbbb_backups = [ Command('sleep 2', '~sleep for 2 seconds:'), Command('cccc', '~cccc has backups', retries=2, sleep_between_retries=1, backup_command_list=cccc_backups, critical=True), Command('should not execute anything past this since cccc is critical.', ";)"), Command('ls -l', '~listing all the files in the current directory:'), Command('ls -l', '~listing all the files in the current directory:'),
# linux & osx: './tutorial_5.py --auto-respond' # windows: 'tutorial_5.py --auto-respond' # This checks to see if "--auto-respond" second command line argument if len(sys.argv) > 1 and sys.argv[1] == '--auto-respond': userinput0_responses = [17, 16, 18, 288, 289, 17] # numbers can be strings, ints, or floats userinput1_responses = [3] userinput2_responses = [ 'Y', 'Y', '100', 'N', 'N', '6', 'Y', 'N', 'purple', '17' ] # linux and osx version the_root_command_list_linux_osx = [ Command('echo "DeclarScript is running, everything is going dandy."'), Command('sleep 2', visible=False), Command('echo "Oh, gee whiz. User Input. Just my luck. :("'), Command('./tutorial_resources/userinput0.py', prompt_responses=userinput0_responses), Command('./tutorial_resources/userinput1.py', prompt_responses=userinput1_responses), Command('./tutorial_resources/userinput2.py', prompt_responses=userinput2_responses), # you can also insert the user input in directly: Command('./tutorial_resources/returncode.py', prompt_responses=['0']) ] # windows version the_root_command_list_windows = [
#!/usr/bin/env python3 import fiximports ### Tutorial 1 ### ### Command Lists ### # In all your python DeclarScript files, you must include the following import from DeclarScript.DeclarScript import Command, run_commands # DeclarScript makes it easy to run multiple commands # This is how you declare a list: hello_commands = [ Command('echo "Hello World! :)"'), Command('ls -l'), Command('sleep 2'), Command('ps -al'), Command('qwertyuiop'), ] # This is how you run a list: run_commands(hello_commands) # Now try executing tut_1. # Open up a terminal and navigate to the : # >>> cd <some_file_path>/<your_DeclarScript_root_directory>/DeclarScript/getting_started" # Run each of these files like so: # Windows: $ tutorial_1.py # Linux & OSX: $ ./tutorial_1.py # After Running the tut_1 python script: # You witnessed each of the commands executed in succession, followed
print("sys.path set to: " + str(root)) sys.path.append(str(root)) # Additionally remove the current file's directory from sys.path try: sys.path.remove(str(parent)) except ValueError: # Already removed pass # run the program normally from DeclarScript.DeclarScript import Command, run_commands from sys import platform print('\n\n\n ##### Test 1 #####') tut_1 = [ Command('echo "Hello World! :)"'), Command('ls -l'), Command('sleep 2'), Command('ps -al'), Command('qwertyuiop'), ] run_commands(tut_1) print('\n\n\n ##### Test 2 #####') tut_2 = [ Command('echo "Hello World! :)"'), Command('ls', message='List all the files in the current directory'), Command('ps', 'List the current processes', critical=False), Command('qwertyuiop', 'Bad command, it should fail...',
#!/usr/bin/env python3 import fiximports from DeclarScript.DeclarScript import Command, run_commands ### Tutorial 2 ### ### Options & Defaults ### # You have several options when declaring a single command. # Here's some examples: tutorial_2_commands = [ # Just a single command with no options Command('echo "Hello World! :)"'), # providing a descriptive message for your command: Command('ls', message='List all the files in the current directory'), # since 'message' is the 2nd parameter you can omit 'message=' if you prefer. # ex: Command('ls', 'List all the files in the current directory'), # labeling a command critical. By default, all commands are critical. # 'critical' commands will stop the current command list if they fail Command('ps', 'List the current processes', critical=False), # retrying a command if it fails Command('qwertyuiop', 'Bad command, it should fail...', retries=1, critical=False), # making the setup around a command invisible Command('echo "I\'m invisible! \\(\'o\')/"', visible=False),