Created
November 27, 2015 17:14
-
-
Save ruiztulio/51ee1ab5e4a99f013858 to your computer and use it in GitHub Desktop.
Simple script for migrating Odoo attachements from database to FS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
import time | |
import base64 | |
import xmlrpclib | |
import codecs | |
import os | |
import sys | |
import hashlib | |
######## Inicio de los parametros configurables ############ | |
# host del que se extraeran los adjuntos | |
HOST = 'localhost' | |
# puerto xmlrpc | |
PORT = 12100 | |
# nombre de la base de datos | |
DB = 'nombre_bd' | |
# usuario | |
USER = 'admin' | |
#clave | |
PASS = 'clave_usuario' | |
# ruta absoluta en la que se almacenaran los adjuntos | |
PATH = '/home/testing/test_next_deploy/server/openerp/filestore' | |
######## Fin de los parametros configurables ############ | |
# Construccion de la url | |
URL = 'http://%s:%d/xmlrpc' % (HOST, PORT) | |
sock = xmlrpclib.ServerProxy('%s/common' % URL) | |
uid = sock.login(DB, USER, PASS) | |
sock = xmlrpclib.ServerProxy('%s/object' % URL) | |
printsock = xmlrpclib.ServerProxy('%s/report' % URL) | |
ids_attach = sock.execute( | |
DB, | |
uid, | |
PASS, | |
'ir.attachment', | |
'search', | |
[('db_datas', '!=', False)] | |
) | |
print 'leido_att', ids_attach | |
os.makedirs(os.path.join(PATH, DB)) | |
for id in ids_attach: | |
db_datas = sock.execute( | |
DB, | |
uid, | |
PASS, | |
'ir.attachment', | |
'read', | |
[id], | |
['res_id', 'db_datas', 'datas_fname'] | |
) | |
print '-- fname', db_datas[0]['datas_fname'] | |
data = base64.b64decode(db_datas[0]['db_datas']) | |
hash_gen = hashlib.sha1() | |
hash_gen.update(data) | |
fname = hash_gen.hexdigest() | |
dname = os.path.join(PATH, DB, fname[:3]) | |
store_fname = os.path.join(fname[:3], fname) | |
if not os.path.isfile(os.path.join(dname, fname)): | |
try: | |
os.makedirs(dname) | |
except Exception, e: | |
pass | |
try: | |
file_xml = open( | |
os.path.join(dname, fname), | |
'wb' | |
) | |
file_xml.write(data) | |
except Exception, e: | |
print "No se pudo gererar el archivo %s " % (db_datas[0]['datas_fname']) | |
raise | |
finally: | |
if file_xml: | |
file_xml.close() | |
sock.execute( | |
DB, | |
uid, | |
PASS, | |
'ir.attachment', | |
'write', | |
[id], | |
{'db_datas': False, | |
'store_fname': store_fname} | |
) | |
print 'Archivos Generados = ', len(ids_attach) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment