How to use replace_in_file method in localstack

Best Python code snippet using localstack_python

replace_versions.py

Source:replace_versions.py Github

copy

Full Screen

...72 response.close()73 74 return json.loads(data.decode("utf-8"))7576def replace_in_file(fn, to_replace, new_str):77 newlines = []78 with open(fn,'r') as f:79 for line in f.readlines():80 newlines.append(line.replace(to_replace, new_str))81 82 with open(fn, 'w') as f:83 for line in newlines:84 f.write(line)85 86def get_content_hash(fn):87 with open(fn, "rb") as f:88 return hashlib.md5(f.read()).hexdigest() 89 90 91content_hash_replacements = {}9293def replace_with_content_hashes_line(line, fn_prefix):94 global content_hash_replacements95 96 regex = ""97 if line.lower().find("<link rel=\"stylesheet\" type=\"text/css\"")!=-1:98 regex = r"href=\"(.*?)\""99 100 if line.lower().find("<script language=\"javascript\" src=\"")!=-1:101 regex = r"src=\"(.*?)\""102 103 if len(regex)>0:104 m = re.search(regex, line)105 if m:106 fn = m.group(1)107 108 if not os.path.exists(fn_prefix + fn) and fn in content_hash_replacements:109 new_fn = content_hash_replacements[fn]110 return line.replace(fn, new_fn)111 112 h = get_content_hash(fn_prefix + fn)113 if line.find(h)==-1:114 l = fn.split(".")115 l.insert(1, "chash-" + h)116 new_fn = ".".join(l)117 content_hash_replacements[fn] = new_fn118 try:119 os.unlink(fn_prefix + new_fn)120 except:121 pass122 os.rename(fn_prefix + fn, fn_prefix + new_fn)123 return line.replace(fn, new_fn)124 125 return line126 127128def replace_with_content_hashes(fn, fn_prefix):129 newlines = []130 with open(fn, 'r', encoding='utf-8', errors='ignore') as f:131 for line in f.readlines():132 newlines.append(replace_with_content_hashes_line(line, fn_prefix))133 134 with open(fn, 'wb') as f:135 for line in newlines:136 f.write(line.encode("utf-8"))137 138def replace_version_info():139 with open("client/version.txt", "rt") as f:140 version = f.read().strip()141 142 replace_in_file("client/data/version_info.txt", "$version$", version)143 replace_in_file("install_client_linux.sh", "$version$", version)144 replace_in_file("osx_installer/update_install.sh", "$version$", version)145 146147148replace_with_content_hashes("urbackupserver/www/index.htm", "urbackupserver/www/")149replace_with_content_hashes("urbackupserver/www/help.htm", "urbackupserver/www/")150replace_with_content_hashes("urbackupserver/www/license.htm", "urbackupserver/www/")151152version = get_version(get_branch())153154if version == None:155 exit(1)156157version["server"]["full_rev"] = version["server"]["full"] + " Rev. " + get_head_rev();158159server_short_files = ["urbackupserver/www/index.htm",160 "urbackupserver_installer_win/urbackup_server.nsi",161 "urbackupserver_installer_win/generate_msi.bat"]162163for short_file in server_short_files:164 replace_in_file(short_file, "$version_short$", version["server"]["short"])165 166 167replace_in_file("urbackupserver/www/index.htm", "$version_full$", version["server"]["full_rev"])168replace_in_file("urbackupserver_installer_win/urbackup_server.wxs", "$version_full_numeric$", version["server"]["full_numeric"])169replace_in_file("urbackupserver_installer_win/urbackup_server.wxi", "$product_id$", str(uuid.uuid1()))170if os.path.exists("client"):171 client_short_files = ["client_version.h",172 "client/urbackup.nsi",173 "client/urbackup_update.nsi",174 "client/urbackup_notray.nsi",175 "client/build_msi.bat",176 "client/build_client.bat",177 "osx_installer/resources/welcome.html",178 "create_osx_installer.sh",179 "install_client_linux.sh"]180181 for short_file in client_short_files:182 replace_in_file(short_file, "$version_short$", version["client"]["short"])183184 version_maj = version["client"]["full_numeric"].split(".")[0]185 version_min = int(version["client"]["full_numeric"].split(".")[1])*1000+int(version["client"]["full_numeric"].split(".")[2])186 187 version_num_short = version["client"]["full_numeric"].split(".")[0] + "." + version["client"]["full_numeric"].split(".")[1] + "." + version["client"]["full_numeric"].split(".")[2]188 replace_in_file("osx_installer/info.plist", "$version_num_short$", version_num_short)189 replace_in_file("create_osx_installer.sh", "$version_num_short$", version_num_short)190 191 replace_in_file("osx_installer/info.plist", "$version_maj$", version_maj)192 replace_in_file("osx_installer/info.plist", "$version_min$", str(version_min))193194 replace_in_file("client/urbackup.wxs", "$version_full_numeric$", version["client"]["full_numeric"])195 replace_in_file("client/urbackup.wxi", "$product_id$", str(uuid.uuid1()))196 replace_in_file("clientctl/main.cpp", "$version_full_numeric$", version["client"]["full_numeric"])197 198 replace_version_info()199 ...

Full Screen

Full Screen

cba_a3.py

Source:cba_a3.py Github

copy

Full Screen

1import os2import subprocess3cba_git_hash = 'ba5c8ea6228a44615172fadb5d8fcdfb5bc0f03e'4def replace_in_file(file, old, new):5 fin = open(file, "rt")6 data = fin.read()7 fin.close()8 data = data.replace(old, new)9 fout = open(file, "wt")10 fout.write(data)11 fout.close()12current_dir = os.path.dirname(os.path.realpath(__file__))13cba_dir = os.path.join(current_dir, 'cba_a3')14a3_dir = os.path.join(cba_dir, 'include', 'a3')15addons_dir = os.path.join(cba_dir, 'addons')16if os.path.exists(cba_dir):17 subprocess.call(['git', '-C', cba_dir, 'fetch'])18else:19 subprocess.call(['git', 'clone', 'https://github.com/CBATeam/CBA_A3.git', cba_dir])20subprocess.call(['git', '-C', cba_dir, 'reset', '--hard', cba_git_hash])21# Enable diag_log instead of CBA_fnc_log22replace_in_file(os.path.join(addons_dir, 'main', 'script_mod.hpp'), '#define PREFIX cba', '#define PREFIX cba\n#define DEBUG_SYNCHRONOUS')23# Change hashes tests to call instead of spawn24replace_in_file(os.path.join(addons_dir, 'hashes', 'test.sqf'), '0 spawn', 'call')25# Disable tests currently not working26replace_in_file(os.path.join(addons_dir, 'arrays', 'test.sqf'), '"findTypeName", ', '')27replace_in_file(os.path.join(addons_dir, 'common', 'test.sqf'), '"config", ', '')28replace_in_file(os.path.join(addons_dir, 'common', 'test.sqf'), '"inventory", ', '')29replace_in_file(os.path.join(addons_dir, 'common', 'test.sqf'), '"position", ', '')30replace_in_file(os.path.join(addons_dir, 'common', 'test.sqf'), '"weaponComponents", ', '')31replace_in_file(os.path.join(addons_dir, 'diagnostic', 'test.sqf'), ', "parameters"', '')32replace_in_file(os.path.join(addons_dir, 'events', 'test.sqf'), '"globalEventJIP"', '')33replace_in_file(os.path.join(addons_dir, 'hashes', 'test.sqf'), '"parseJSON", ', '')34replace_in_file(os.path.join(addons_dir, 'network', 'test.sqf'), '"network"', '')35# Dummy workarounds for commands not yet implemented36replace_in_file(os.path.join(addons_dir, 'arrays', 'fnc_findNull.sqf'), 'controlNull, ', '')37replace_in_file(os.path.join(addons_dir, 'arrays', 'fnc_findNull.sqf'), 'displayNull, ', '')38replace_in_file(os.path.join(addons_dir, 'arrays', 'fnc_findNull.sqf'), 'locationNull, ', '')39replace_in_file(os.path.join(addons_dir, 'arrays', 'fnc_findNull.sqf'), 'taskNull, ', '')40replace_in_file(os.path.join(addons_dir, 'arrays', 'fnc_findNull.sqf'), 'teamMemberNull, ', '')41replace_in_file(os.path.join(addons_dir, 'arrays', 'test_findNil.sqf'), 'displayNull', 'missionNamespace')42replace_in_file(os.path.join(addons_dir, 'arrays', 'test_findNull.sqf'), 'displayNull', 'missionNamespace')43replace_in_file(os.path.join(addons_dir, 'arrays', 'test_findTypeName.sqf'), 'controlNull, ', '')44replace_in_file(os.path.join(addons_dir, 'arrays', 'test_findTypeName.sqf'), 'displayNull, ', '')45replace_in_file(os.path.join(addons_dir, 'arrays', 'test_findTypeName.sqf'), 'locationNull, ', '')46replace_in_file(os.path.join(addons_dir, 'arrays', 'test_findTypeName.sqf'), 'taskNull, ', '')47replace_in_file(os.path.join(addons_dir, 'common', 'fnc_createNamespace.sqf'), 'createLocation ["CBA_NamespaceDummy", DUMMY_POSITION, 0, 0]', '"CBA_NamespaceDummy" createVehicle DUMMY_POSITION')48replace_in_file(os.path.join(addons_dir, 'common', 'fnc_createNamespace.sqf'), 'createSimpleObject ["CBA_NamespaceDummy", DUMMY_POSITION]', '"CBA_NamespaceDummy" createVehicle DUMMY_POSITION')49replace_in_file(os.path.join(addons_dir, 'common', 'test_macro_is_x.sqf'), 'controlNull', '""')50replace_in_file(os.path.join(addons_dir, 'common', 'test_macro_is_x.sqf'), 'displayNull', '""')51replace_in_file(os.path.join(addons_dir, 'common', 'test_macro_is_x.sqf'), 'locationNull', '""')52replace_in_file(os.path.join(addons_dir, 'common', 'test_macro_is_x.sqf'), 'missionconfigfile', 'configFile')53replace_in_file(os.path.join(addons_dir, 'diagnostic', 'fnc_error.sqf'), 'QGVAR(Error) cutRsc [QGVAR(Error), "PLAIN"];', '')54replace_in_file(os.path.join(addons_dir, 'diagnostic', 'fnc_error.sqf'), '_control ctrlSetStructuredText composeText _compose;', '')55replace_in_file(os.path.join(addons_dir, 'main', 'script_macros_common.hpp'), 'controlNull', '""')56replace_in_file(os.path.join(addons_dir, 'main', 'script_macros_common.hpp'), 'displayNull', '""')57replace_in_file(os.path.join(addons_dir, 'main', 'script_macros_common.hpp'), 'locationNull', '""')58replace_in_file(os.path.join(addons_dir, 'strings', 'fnc_formatNumber.sqf'), 'localize', '')59replace_in_file(os.path.join(addons_dir, 'vectors', 'fnc_vectAdd.sqf'), 'BIS_fnc_vectorAdd', '{}')60replace_in_file(os.path.join(addons_dir, 'vectors', 'fnc_vectDot.sqf'), 'BIS_fnc_dotProduct', '{}')61sqfvm_binary = 'sqfvm'62sqfvm_test_script = os.path.join(current_dir, 'cba_a3.sqf')63sqfvm = [sqfvm_binary, '--automated', '--no-execute-print']64# Map a3 and cba folders into Arma prefixes65sqfvm.extend(['--virtual', '{}|\\a3'.format(a3_dir)])66sqfvm.extend(['--virtual', '{}|\\x\\cba\\addons'.format(addons_dir)])67# Load addon configs68for addon in os.listdir(addons_dir):69 sqfvm.extend(['--input-config', '{}/{}/config.cpp'.format(addons_dir, addon)])70sqfvm.extend(['--input-sqf', sqfvm_test_script])71def run(cmd):72 proc = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, universal_newlines = True)73 for stdout_line in iter(proc.stdout.readline, ""):74 yield stdout_line.strip()...

Full Screen

Full Screen

conanfile.py

Source:conanfile.py Github

copy

Full Screen

...19 self.run("git clone https://gitee.com/xia-chu/ZLToolKit.git")20 # This small hack might be useful to guarantee proper /MT /MD linkage21 # in MSVC if the packaged project doesn't have variables to set it22 # properly23 tools.replace_in_file("ZLToolKit/CMakeLists.txt", "project(ZLToolKit)",24 '''project(ZLToolKit)25include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)26conan_basic_setup()27include(${CMAKE_BINARY_DIR}/conan_paths.cmake)28set(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR} ${CMAKE_MODULE_PATH})''')29 # 屏蔽测试程序30 tools.replace_in_file("ZLToolKit/CMakeLists.txt", "add_subdirectory(tests)", '''#add_subdirectory(tests)''')31 if self.options.shared:32 tools.replace_in_file("ZLToolKit/CMakeLists.txt", "add_library(${CMAKE_PROJECT_NAME}_shared SHARED ${SRC_LIST})", '''add_library(${CMAKE_PROJECT_NAME} SHARED ${SRC_LIST})''')33 tools.replace_in_file("ZLToolKit/CMakeLists.txt", "target_link_libraries(${CMAKE_PROJECT_NAME}_shared ${LINK_LIB_LIST})", '''target_link_libraries(${CMAKE_PROJECT_NAME} ${LINK_LIB_LIST})''')34 tools.replace_in_file("ZLToolKit/CMakeLists.txt", "set_target_properties(${CMAKE_PROJECT_NAME}_shared PROPERTIES OUTPUT_NAME \"${CMAKE_PROJECT_NAME}\")", ''' set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES OUTPUT_NAME "${CMAKE_PROJECT_NAME}")''')35 tools.replace_in_file("ZLToolKit/CMakeLists.txt", "install(TARGETS ${CMAKE_PROJECT_NAME}_shared ARCHIVE DESTINATION ${INSTALL_PATH_LIB} LIBRARY DESTINATION ${INSTALL_PATH_LIB})", '''install(TARGETS ${CMAKE_PROJECT_NAME} ARCHIVE DESTINATION ${INSTALL_PATH_LIB} LIBRARY DESTINATION ${INSTALL_PATH_LIB})''')36 else:37 tools.replace_in_file("ZLToolKit/CMakeLists.txt", "add_library(${CMAKE_PROJECT_NAME}_static STATIC ${SRC_LIST})", '''add_library(${CMAKE_PROJECT_NAME} STATIC ${SRC_LIST})''')38 tools.replace_in_file("ZLToolKit/CMakeLists.txt", "set_target_properties(${CMAKE_PROJECT_NAME}_static PROPERTIES OUTPUT_NAME \"${CMAKE_PROJECT_NAME}\")", '''set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES OUTPUT_NAME "${CMAKE_PROJECT_NAME}")''')39 tools.replace_in_file("ZLToolKit/CMakeLists.txt", "install(TARGETS ${CMAKE_PROJECT_NAME}_static ARCHIVE DESTINATION ${INSTALL_PATH_LIB})", '''install(TARGETS ${CMAKE_PROJECT_NAME} ARCHIVE DESTINATION ${INSTALL_PATH_LIB})''')40 pass41 # 区分静态和动态链接库42 if not self.options.shared:43 tools.replace_in_file("ZLToolKit/CMakeLists.txt", "add_library(${CMAKE_PROJECT_NAME}_shared SHARED ${SRC_LIST})", ''' #[[add_library(${CMAKE_PROJECT_NAME}_shared SHARED ${SRC_LIST}) ''')44 tools.replace_in_file("ZLToolKit/CMakeLists.txt", "install(TARGETS ${CMAKE_PROJECT_NAME}_shared ARCHIVE DESTINATION ${INSTALL_PATH_LIB} LIBRARY DESTINATION ${INSTALL_PATH_LIB})", ''' install(TARGETS ${CMAKE_PROJECT_NAME}_shared ARCHIVE DESTINATION ${INSTALL_PATH_LIB} LIBRARY DESTINATION ${INSTALL_PATH_LIB})]] ''')45 else:46 tools.replace_in_file("ZLToolKit/CMakeLists.txt", "add_library(${CMAKE_PROJECT_NAME}_static STATIC ${SRC_LIST})", ''' #[[add_library(${CMAKE_PROJECT_NAME}_static STATIC ${SRC_LIST}) ''')47 tools.replace_in_file("ZLToolKit/CMakeLists.txt", "install(TARGETS ${CMAKE_PROJECT_NAME}_static ARCHIVE DESTINATION ${INSTALL_PATH_LIB})", ''' install(TARGETS ${CMAKE_PROJECT_NAME}_static ARCHIVE DESTINATION ${INSTALL_PATH_LIB})]] ''')48 pass49 def build(self):50 cmake = CMake(self)51 cmake.configure(source_folder="ZLToolKit")52 cmake.build()53 # Explicit way:54 # self.run('cmake %s/hello %s'55 # % (self.source_folder, cmake.command_line))56 # self.run("cmake --build . %s" % cmake.build_config)57 def package(self):58 self.copy("*.h", dst="include", src="ZLToolKit/src")59 self.copy("*.lib", dst="lib", keep_path=False)60 self.copy("*.dll", dst="bin", keep_path=False)61 self.copy("*.so", dst="bin", keep_path=False)...

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 localstack 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