How to use prepend_line method in Slash

Best Python code snippet using slash

update_env_file.py

Source:update_env_file.py Github

copy

Full Screen

1from utils import get_inventory, get_service_fqdn, get_db_fqdn, remove_line_from_file2import sys3def prepend_line(path, line):4 file = open(path, "r+")5 content = file.read()6 file.seek(0,0)7 file.write(line.rstrip('\r\n') + '\n' + content)8def replace_host_ip_line(fqdn, env_file_path):9 # Remove exisiting line10 export_text = 'export HOST_IP'11 # remove_line_from_file(env_file_path, f"{export_text}=")12 remove_line_from_file(env_file_path, "{}=".format(export_text))13 # Add in new line, assigning the fqdn14 # host_ip_export_line = f"{export_text}={fqdn}"15 host_ip_export_line = "{}={}".format(export_text, fqdn)16 prepend_line(env_file_path, host_ip_export_line)17 18def replace_db_host_ip_line(fqdn, env_file_path):19 # Remove existing line20 existing_line = 'DB_HOST_IP=$HOST_IP'21 remove_line_from_file(env_file_path, existing_line)22 # We're also going to remove a line that might previously have been added by this script23 new_line = 'export DB_HOST_IP='24 remove_line_from_file(env_file_path, new_line)25 # Add in new line, assigning the fqdn26 export_text = 'export DB_HOST_IP'27 # host_ip_export_line = f"{export_text}={fqdn}"28 host_ip_export_line = "{}={}".format(export_text, fqdn)29 prepend_line(env_file_path, host_ip_export_line)30def set_db_ports(env_file_path):31 port='3306'32 line="export STROOM_DB_PORT=" 33 remove_line_from_file(env_file_path, line)34 prepend_line(env_file_path, line + port)35 line="export STROOM_STATS_DB_PORT=" 36 remove_line_from_file(env_file_path, line)37 prepend_line(env_file_path, line + port)38 line="export STROOM_AUTH_DB_PORT=" 39 remove_line_from_file(env_file_path, line)40 prepend_line(env_file_path, line + port)41 42def main():43 path_to_stack = sys.argv[1]44 # env_file_path = f"{path_to_stack}/latest/config/stroom_core.env"45 env_file_path = "{}/latest/config/stroom_core.env".format(path_to_stack)46 inventory = get_inventory()47 fqdn = get_service_fqdn(inventory)48 db_fqdn = get_db_fqdn(inventory)49 set_db_ports(env_file_path)50 replace_host_ip_line(fqdn, env_file_path)51 replace_db_host_ip_line(db_fqdn, env_file_path)...

Full Screen

Full Screen

cisco_parser.py

Source:cisco_parser.py Github

copy

Full Screen

...25standardize_intfs(parse)26parse.commit() # commit() **must** be called before searching again27## regular expression usage in has_line_with() to find if the config has a matching line28if not parse.has_line_with(r'^service\stimestamp'):29 ## prepend_line() adds a line at the top of the configuration30 parse.prepend_line('service timestamps debug datetime msec localtime show-timezone')31 parse.prepend_line('service timestamps log datetime msec localtime show-timezone')32parse.prepend_line('! -- Micah Raabe -- !')33## Write the new configuration...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Slash automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful