Bug 1978 - trouver un moyen de rationaliser l'achat du r930 cper 2017

- préparation de graphique sur les investisseurs du cluster
This commit is contained in:
Guillaume Raffy 2018-02-02 10:32:54 +00:00
parent b7c60939a7
commit defc6936b5
2 changed files with 48 additions and 47 deletions

View File

@ -13,7 +13,7 @@ import os
import re
import sys
import pygraphviz
from mysql2sqlite import mysql_to_sqlite
def add_capacity_constraints(capacity1, capacity2):
"""
@ -31,52 +31,6 @@ def add_capacity_constraints(capacity1, capacity2):
else:
return min(capacity1, capacity2)
def mysql_to_sqlite( mysql_sql_code ):
"""
converts a mysql-compatible sql code into a sqlite-ompatible sql code
note: the original code was found on internet, then tweaked
"""
content = mysql_sql_code
# unused commands
COMMAND_RE = re.compile(r'^(SET).*?;\n$', re.I | re.M | re.S)
content = COMMAND_RE.sub('', content)
# table constraints
TCONS_RE = re.compile(r'\)(\s*(CHARSET|DEFAULT|ENGINE)(=.*?)?\s*)+;', re.I | re.M | re.S)
content = TCONS_RE.sub(');', content)
# remove comments
# content = re.sub(r'^-- Tab[.]', 'toto', content, flags=re.I | re.M | re.S)
# sqlite doesn't like 'unsigned' as in `ip_address_3` tinyint(3) unsigned NOT NULL default '27',
content = re.sub(r' unsigned ', ' ', content)
# sqlite doesn't like 'enum' as in `type` enum('normal','light_out_management') NOT NULL default 'normal',,
content = re.sub(r' enum\([^\)]*\) ', ' varchar(255) ', content)
# insert multiple values
# INSERTVALS_RE = re.compile(r'^(INSERT INTO.*?VALUES)\s*\((.*)\*;', re.I | re.M | re.S)
INSERTVALS_RE = re.compile(r'^(INSERT INTO.*?VALUES)\s*([^;]*);', re.I | re.M | re.S)
#INSERTVALS_RE = re.compile(r'^(INSERT INTO.*?VALUES)\s*((\[^\)](\)));$', re.I | re.M | re.S)
INSERTVALS_SPLIT_RE = re.compile(r'\)\s*,\s*\(', re.I | re.M | re.S)
def insertvals_replacer(match):
insert, values = match.groups()
# print("insert=%s"%insert)
# print("values=%s"%values)
values = re.sub('^\s*\(' ,'', values)
values = re.sub('\)\s*$' ,'', values)
replacement = ''
for vals in INSERTVALS_SPLIT_RE.split(values):
#print("vals=%s"%vals)
replacement = '%s\n%s (%s);' % (replacement, insert, vals)
return replacement
content = INSERTVALS_RE.sub(insertvals_replacer, content)
return content
class Machine(object):
"""
represents a device with input and output power plugs. It could represent a power consumer such as a server (in which case, it has no output plug), or a power distrubtion unit (in which case, it has one input plug and more than one output plugs), or even something else...

47
mysql2sqlite.py Normal file
View File

@ -0,0 +1,47 @@
import re
def mysql_to_sqlite( mysql_sql_code ):
"""
converts a mysql-compatible sql code into a sqlite-ompatible sql code
note: the original code was found on internet, then tweaked
"""
content = mysql_sql_code
# unused commands
COMMAND_RE = re.compile(r'^(SET).*?;\n$', re.I | re.M | re.S)
content = COMMAND_RE.sub('', content)
# table constraints
TCONS_RE = re.compile(r'\)(\s*(CHARSET|DEFAULT|ENGINE)(=.*?)?\s*)+;', re.I | re.M | re.S)
content = TCONS_RE.sub(');', content)
# remove comments
# content = re.sub(r'^-- Tab[.]', 'toto', content, flags=re.I | re.M | re.S)
# sqlite doesn't like 'unsigned' as in `ip_address_3` tinyint(3) unsigned NOT NULL default '27',
content = re.sub(r' unsigned ', ' ', content)
# sqlite doesn't like 'enum' as in `type` enum('normal','light_out_management') NOT NULL default 'normal',,
content = re.sub(r' enum\([^\)]*\) ', ' varchar(255) ', content)
# insert multiple values
# INSERTVALS_RE = re.compile(r'^(INSERT INTO.*?VALUES)\s*\((.*)\*;', re.I | re.M | re.S)
INSERTVALS_RE = re.compile(r'^(INSERT INTO.*?VALUES)\s*([^;]*);', re.I | re.M | re.S)
#INSERTVALS_RE = re.compile(r'^(INSERT INTO.*?VALUES)\s*((\[^\)](\)));$', re.I | re.M | re.S)
INSERTVALS_SPLIT_RE = re.compile(r'\)\s*,\s*\(', re.I | re.M | re.S)
def insertvals_replacer(match):
insert, values = match.groups()
# print("insert=%s"%insert)
# print("values=%s"%values)
values = re.sub('^\s*\(' ,'', values)
values = re.sub('\)\s*$' ,'', values)
replacement = ''
for vals in INSERTVALS_SPLIT_RE.split(values):
#print("vals=%s"%vals)
replacement = '%s\n%s (%s);' % (replacement, insert, vals)
return replacement
content = INSERTVALS_RE.sub(insertvals_replacer, content)
return content