From a0475b87a97cf0335fa15af31e4eec57e3fb0256 Mon Sep 17 00:00:00 2001 From: jonnybravo Date: Wed, 26 Apr 2023 20:00:01 +0200 Subject: [PATCH] commit message from python script --- create_yml/main.py | 62 ++++++++++++++++++++++++++++++ create_yml/test.yml | 12 ++++++ jenkins/modify_all_jenkins_conf.py | 28 ++++++++++++++ start_vms/main.py | 3 ++ xml_test/main.py | 39 +++++++++++++++++++ xml_test/xmldocument.xml | 11 ++++++ 6 files changed, 155 insertions(+) create mode 100644 create_yml/main.py create mode 100644 create_yml/test.yml create mode 100644 jenkins/modify_all_jenkins_conf.py create mode 100644 xml_test/main.py create mode 100644 xml_test/xmldocument.xml diff --git a/create_yml/main.py b/create_yml/main.py new file mode 100644 index 0000000..7daf2b6 --- /dev/null +++ b/create_yml/main.py @@ -0,0 +1,62 @@ +#! /usr/bin/env python3 + +import yaml +import ruamel.yaml + +def read_and_modify_one_block_of_yaml_data(filename = str,key = str,value = str): + with open(filename, 'r') as f: + data = yaml.safe_load(f) + data[f'{key}'] = f'{value}' + with open(filename, 'w') as file: + yaml.dump(data,file,sort_keys=False) + print('done!') + + +def read_modify_save_yaml_data(filename,index,key,value,write_file): + with open(filename,'r') as f: + data = yaml.safe_load_all(f) + loaded_data = list(data) + print(loaded_data) + loaded_data[index][f'{key}'].append(f'{value}') + with open(filename, 'w') as file: + yaml.dump_all(loaded_data,file, sort_keys=False) + print(loaded_data) + + + + +if __name__ == "__main__": + #read yaml + with open('/home/jonnybravo/Projekte/docker-compose/adguard-home/docker-compose.yml', 'r') as file: + prime_service = yaml.safe_load(file) + #print(prime_service) + print(prime_service["services"]["adguardhome"]["image"]) + #write yaml + my_yaml = yaml.safe_load(''' +--- +age: 30 +automobiles: + - brand: Honda + type: Odyssey + year: 2018 + - brand: Toyota + type: Sienna + year: 2015 +name: John +test: + - test1 + - test2 +''') + #parsing yaml + yaml = ruamel.yaml.YAML() + yaml.indent(sequence=4, offset=2) +#yaml.dump(foo, sys.stdout) + + #print(pars_test) + #writing test_yaml + with open('test.yml', 'w') as file: + yaml.dump(my_yaml, file) + + #example modify yaml +#read_and_modify_one_block_of_yaml_data(filename="test.yml", key="year", value="2020") +#read_modify_save_yaml_data('test.yml', 0, 'test', 'test3', "test2") \ No newline at end of file diff --git a/create_yml/test.yml b/create_yml/test.yml new file mode 100644 index 0000000..7838722 --- /dev/null +++ b/create_yml/test.yml @@ -0,0 +1,12 @@ +age: 30 +automobiles: + - brand: Honda + type: Odyssey + year: 2018 + - brand: Toyota + type: Sienna + year: 2015 +name: John +test: + - test1 + - test2 diff --git a/jenkins/modify_all_jenkins_conf.py b/jenkins/modify_all_jenkins_conf.py new file mode 100644 index 0000000..abc67ac --- /dev/null +++ b/jenkins/modify_all_jenkins_conf.py @@ -0,0 +1,28 @@ +#! /usr/bin/env python3 + +import os + +def read_sshkeys(target_folder = str, search_string = str): + key_liste = [] + for root, dirs, files in os.walk(target_folder, topdown=False, ): + for name in files: + if name == search_string: + FullPATH = str(os.path.join(root, name)) + #if FullPATH.endswith(".pub") is False :#and FullPATH.endswith("known_hosts") is False and FullPATH.endswith("authorized_keys") is False: + #if FullPATH. + key_liste.append({"FullPath": FullPATH, + "Name": name, + "Root": root, + "Folder": dirs}) + return key_liste + + +if __name__ == "__main__": + search_jenkins_folder = "/home/jonnybravo/.jenkins/data/jobs" + for i in read_sshkeys(target_folder=search_jenkins_folder, search_string="config.xml"): + print(i["FullPath"]) + with open(i["FullPath"], 'r') as file: + data = file.read() + data = data.replace('-1', '') + with open(i["FullPath"], 'w') as file: + file.write(data) diff --git a/start_vms/main.py b/start_vms/main.py index e69de29..61da18e 100644 --- a/start_vms/main.py +++ b/start_vms/main.py @@ -0,0 +1,3 @@ + + +vboxmanage startvm ansible_umgebung_ubuntu_1682357579668_76595 --type headless \ No newline at end of file diff --git a/xml_test/main.py b/xml_test/main.py new file mode 100644 index 0000000..2060ba4 --- /dev/null +++ b/xml_test/main.py @@ -0,0 +1,39 @@ +import xml.etree.ElementTree as ET +XMLexample_stored_in_a_string =''' + + + + + + + + +''' +mytree = ET.parse('xmldocument.xml.txt') +myroot = mytree.getroot() + +# iterating through the price values. +for prices in myroot.iter('price'): + # updates the price value + prices.text = str(float(prices.text)+10) + # creates a new attribute + prices.set('newprices', 'yes') + +# creating a new tag under the parent. +# myroot[0] here is the first food tag. +ET.SubElement(myroot[0], 'tasty') +for temp in myroot.iter('tasty'): + # giving the value as Yes. + temp.text = str('YES') + +# deleting attributes in the xml. +# by using pop as attrib returns dictionary. +# removes the itemid attribute in the name tag of +# the second food tag. +myroot[1][0].attrib.pop('itemid') + +# Removing the tag completely we use remove function. +# completely removes the third food tag. +myroot.remove(myroot[2]) + +mytree.write('output.xml') \ No newline at end of file diff --git a/xml_test/xmldocument.xml b/xml_test/xmldocument.xml new file mode 100644 index 0000000..8632ce2 --- /dev/null +++ b/xml_test/xmldocument.xml @@ -0,0 +1,11 @@ + + + + + + + + + + +