def backup_description(): heap_file = open("./heap.yaml") hosts_file = open("./hosts.yaml") heap_cfg = list(yaml.load_all(heap_file)) hosts_cfg = list(yaml.load_all(hosts_file)) backup_file = open("./backup_description.yaml", 'w') yml = YAML() yml.explicit_start = True yml.Loader = ruamel.yaml.RoundTripLoader yml.Dumper = ruamel.yaml.RoundTripDumper for j in heap_cfg: for i in hosts_cfg: if j['HOST'] == i['HOST']: yaml_str = "VM: " + j['VM'] + "\nHOST: " + j['HOST']\ + "\nDESC: " + api_get_vm_desc( j['HOST'], i['USER'], i['PASSWORD'], j['VM'] ) + "\n" data = yml.load(yaml_str) yml.dump(data, backup_file) break heap_file.close() hosts_file.close() backup_file.close()
from coalib.bears.BEAR_KIND import BEAR_KIND from coalib.collecting.Collectors import collect_bears from dependency_management.requirements.AnyOneOfRequirements import ( AnyOneOfRequirements, ) from dependency_management.requirements.DistributionRequirement import ( DistributionRequirement, ) from dependency_management.requirements.ExecutableRequirement import ( ExecutableRequirement, ) DISABLED_BEARS = [] yaml = YAML(typ='rt') yaml.default_flow_style = False yaml.Dumper = RoundTripDumper BEAR_METADATA_YAML = 'bear-metadata.yaml' BEAR_REQUIREMENTS_YAML = 'bear-requirements.yaml' BEAR_LANGUAGES_YAML = 'bear-languages.yaml' _VERSION_OPERATORS = ('<', '>', '~', '=', '-', '!') THIS_DIR = os.path.dirname(os.path.abspath(__file__)) PROJECT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) PROJECT_BEAR_DIR = os.path.abspath(os.path.join(PROJECT_DIR, 'bears')) def get_args():
def parse_xls(XLS_FILE, VM_NAME_SHEET, VM_NAME_COLUMN, VM_DESCRIPTION_SHEET, VM_DESCRIPTION_COLUMN, ENCODE, HOST_REPRESENT): # get VM and description from XLS tbl = [] book = xlrd.open_workbook(XLS_FILE) vm_sheet = book.sheet_by_index(VM_NAME_SHEET) # Switching to sheet for rownum in range(vm_sheet.nrows)[1:]: # [1:] - skipping 1-st title row row = vm_sheet.row_values(rownum) tbl.append(row[VM_NAME_COLUMN]) for j in range(len(tbl)): vm_description_sheet = book.sheet_by_index(VM_DESCRIPTION_SHEET) # (j+1) - skipping 1-st title row row = vm_description_sheet.row_values(j+1) tbl[j] = tbl[j], row[VM_DESCRIPTION_COLUMN] # ------------------------------------------------------------------------ # get info from MariaDB mariadb_connection = mysql.connector.connect(user='******', database='DBNAME') cursor = mariadb_connection .cursor() if HOST_REPRESENT == "hostname": query = ("select name, parent_title from vEnvironments where parent_title != 'None'") if HOST_REPRESENT == "ip": query = ("select E.name, H.ip_address from vEnvironments E inner join vHosts H on E.parent_title=H.hostname") cursor.execute(query) dbresult = cursor.fetchall() cursor.close() mariadb_connection.close() # ------------------------------------------------------------------------ # convert tuple of tuples to list of lists dbresult = list(list(x) for x in dbresult) # decode every element to "utf-8" for i in range(len(dbresult)): dbresult[i][0] = dbresult[i][0].decode(ENCODE) dbresult[i][1] = dbresult[i][1].decode(ENCODE) # ------------------------------------------------------------------------ # heap - is array VM, host, description heap = [] for j in range(len(tbl)): for i in range(len(dbresult)): if tbl[j][0] == dbresult[i][0]: heap.append((tbl[j][0], dbresult[i][1], tbl[j][1])) break if i == (len(dbresult)-1): heap.append((tbl[j][0], "NULL", tbl[j][1])) # ------------------------------------------------------------------------ # get longest length of VM column (need for formatting output) longest_first = 0 for j in range(len(heap)): if len(heap[j][0]) > longest_first: longest_first = len(heap[j][0]) # get longest lengtn of host column (need for formatting output) longest_second = 0 for j in range(len(heap)): if len(heap[j][1]) > longest_second: longest_second = len(heap[j][1]) # print VM (from XLS), host (from MariaDB; if hos not found - # then print 'NULL'), description (from XLS) print "This table shows VMs, hosts and description. If the host is shown\ as NULL, then for this VM we can not change the description." for j in range(len(heap)): if heap[j][1] == "NULL": print "{0:{1}} {2} {3}".format(heap[j][0], longest_first, ( "\x1b[0;32;41m" + "NULL" + (" "*(longest_second-4)) + "\x1b[0;32;40m"), heap[j][2].encode(ENCODE)) else: print "{0:{1}} {2:{3}} {4}".format( heap[j][0], longest_first, heap[j][1], longest_second, heap[j][2].encode(ENCODE)) # ------------------------------------------------------------------------ # get unique hosts from heap (for which we need passwords) hosts = [] for j in range(len(heap)): if heap[j][1] == "NULL": continue if heap[j][1] not in hosts: hosts.append(heap[j][1]) hosts = sorted(hosts) # sort hosts alpabetically # print hosts print "For these hosts you need to specify the credentials in the\ configuration file hosts.yaml" for j in hosts: print j # ------------------------------------------------------------------------ # write config for hosts: hosts.yaml config_file = open("./hosts.yaml", 'w') yaml = YAML() yaml.explicit_start = True yaml.Loader = ruamel.yaml.RoundTripLoader yaml.Dumper = ruamel.yaml.RoundTripDumper for j in hosts: yaml_str = "HOST: "+j+"\nUSER: USERNAME\nPASSWORD: \n" data = yaml.load(yaml_str) yaml.dump(data, config_file) config_file.close() # ------------------------------------------------------------------------ # write heap to heap.yaml (for using in key -set) config_file = open("./heap.yaml", 'w') yaml = YAML() yaml.explicit_start = True yaml.Loader = ruamel.yaml.RoundTripLoader yaml.Dumper = ruamel.yaml.RoundTripDumper for j in range(len(heap)): if heap[j][1] == "NULL": continue yaml_str = "VM: " + heap[j][0] + "\nHOST: " + heap[j][1] + "\nDESC: "\ + heap[j][2] + "\n" data = yaml.load(yaml_str) yaml.dump(data, config_file) config_file.close() # ------------------------------------------------------------------------ return heap
import itertools import os import sys from ruamel.yaml import YAML, RoundTripDumper from ruamel.yaml.comments import CommentedMap from coalib.bears.BEAR_KIND import BEAR_KIND from coalib.collecting.Collectors import collect_bears from dependency_management.requirements.AnyOneOfRequirements import ( AnyOneOfRequirements) yaml = YAML(typ='rt') yaml.default_flow_style = False yaml.Dumper = RoundTripDumper BEAR_REQUIREMENTS_YAML = 'bear-requirements.yaml' BEAR_LANGUAGES_YAML = 'bear-languages.yaml' _VERSION_OPERATORS = ('<', '>', '~', '=', '-', '!') THIS_DIR = os.path.dirname(os.path.abspath(__file__)) PROJECT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) PROJECT_BEAR_DIR = os.path.abspath(os.path.join(PROJECT_DIR, 'bears')) SUPPORTED_INSTANCES = ( 'PipRequirement', 'NpmRequirement',