Improved custom addons path support

- get_module_path checks for zip files
	- fixed zipped module problem

bzr revid: ame@tinyerp.com-20080716090715-pbuby8xpuwoam7h1
This commit is contained in:
Amit Mendapara 2008-07-16 14:37:15 +05:30
parent 92f942b031
commit 6c1e9c0627
3 changed files with 31 additions and 22 deletions

View File

@ -134,9 +134,18 @@ class Node(Singleton):
def get_module_path(module):
"""Return the path of the given module.
"""
if os.path.exists(opj(ad, module)):
return opj(ad, module)
return opj(_ad, module)
if os.path.exists(opj(ad, '%s.zip' % module)):
return opj(ad, '%s.zip' % module)
if os.path.exists(opj(_ad, module)):
return opj(_ad, module)
if os.path.exists(opj(_ad, '%s.zip' % module)):
return opj(_ad, '%s.zip' % module)
def get_module_resource(module, *args):
"""Return the full path of a resource of the given module.
@ -169,7 +178,7 @@ def create_graph(module_list, force=None):
module = module[:-4]
mod_path = get_module_path(module)
terp_file = get_module_resource(module, '__terp__.py')
if os.path.isfile(terp_file) or zipfile.is_zipfile(mod_path+'.zip'):
if os.path.isfile(terp_file) or zipfile.is_zipfile(mod_path):
try:
info = eval(tools.file_open(terp_file).read())
except:
@ -283,12 +292,11 @@ def register_classes():
sys.stdout.flush()
mod_path = get_module_path(m)
if not os.path.isfile(mod_path + '.zip'):
if not os.path.isfile(mod_path):
# XXX must restrict to only addons paths
imp.load_module(m, *imp.find_module(m))
else:
import zipimport
mod_path = mod_path + '.zip'
try:
zimp = zipimport.zipimporter(mod_path)
zimp.load_module(m)

View File

@ -38,9 +38,7 @@ import release
import zipimport
import wizard
import addons
ver_regexp = re.compile("^(\\d+)((\\.\\d+)*)([a-z]?)((_(pre|p|beta|alpha|rc)\\d*)*)(-r(\\d+))?$")
suffix_regexp = re.compile("^(alpha|beta|rc|pre|p)(\\d*)$")
@ -199,7 +197,7 @@ class module(osv.osv):
def get_module_info(self, name):
try:
f = tools.file_open(os.path.join(tools.config['addons_path'], name, '__terp__.py'))
f = tools.file_open(addons.get_module_resource(name, '__terp__.py'))
data = f.read()
info = eval(data)
if 'version' in info:
@ -343,18 +341,17 @@ class module(osv.osv):
filepath = files[lang]
# if filepath does not contain :// we prepend the path of the module
if filepath.find('://') == -1:
filepath = os.path.join(tools.config['addons_path'], module['name'], filepath)
filepath = addons.get_module_resource(module['name'], filepath)
tools.trans_load(filepath, lang)
return True
# update the list of available packages
def update_list(self, cr, uid, context={}):
robj = self.pool.get('ir.module.repository')
adp = tools.config['addons_path']
res = [0, 0] # [update, add]
# iterate through installed modules and mark them as being so
for name in os.listdir(adp):
for name in addons.get_modules():
mod_name = name
if name[-4:]=='.zip':
mod_name=name[:-4]
@ -384,19 +381,18 @@ class module(osv.osv):
self._update_category(cr, uid, ids[0], terp.get('category',
'Uncategorized'))
continue
terp_file = os.path.join(adp, name, '__terp__.py')
mod_path = os.path.join(adp, name)
terp_file = addons.get_module_resource(name, '__terp__.py')
mod_path = addons.get_module_path(name)
if os.path.isdir(mod_path) or os.path.islink(mod_path) or zipfile.is_zipfile(mod_path):
terp = self.get_module_info(mod_name)
if not terp or not terp.get('installable', True):
continue
if not os.path.isfile(os.path.join(adp, mod_name+'.zip')):
if not os.path.isfile(mod_path):
import imp
# XXX must restrict to only addons paths
imp.load_module(name, *imp.find_module(mod_name))
else:
import zipimport
mod_path = os.path.join(adp, mod_name+'.zip')
zimp = zipimport.zipimporter(mod_path)
zimp.load_module(mod_name)
id = self.create(cr, uid, {
@ -472,7 +468,6 @@ class module(osv.osv):
def download(self, cr, uid, ids, download=True, context=None):
res = []
adp = tools.config['addons_path']
for mod in self.browse(cr, uid, ids, context=context):
if not mod.url:
continue
@ -486,7 +481,7 @@ class module(osv.osv):
if not download:
continue
zipfile = urllib.urlopen(mod.url).read()
fname = os.path.join(adp, mod.name+'.zip')
fname = addons.get_module_path(mod.name)
try:
fp = file(fname, 'wb')
fp.write(zipfile)

View File

@ -61,7 +61,7 @@ def init_db(cr):
terp_file = addons.get_module_resource(i, '__terp__.py')
mod_path = addons.get_module_path(i)
info = False
if os.path.isfile(terp_file) and not os.path.isfile(mod_path+'.zip'):
if os.path.isfile(terp_file) and not os.path.isfile(mod_path):
info = eval(file(terp_file).read())
elif zipfile.is_zipfile(mod_path):
zfile = zipfile.ZipFile(mod_path)
@ -189,15 +189,21 @@ def file_open(name, mode="r", subdir='addons'):
zipname = os.path.join(tail, zipname)
else:
zipname = tail
if zipfile.is_zipfile(head+'.zip'):
zname = head
if zipfile.is_zipfile(head + '.zip'):
zname = head + '.zip'
if zipfile.is_zipfile(zname):
import StringIO
zfile = zipfile.ZipFile(head+'.zip')
zfile = zipfile.ZipFile(zname)
try:
zname = os.path.splitext(zname)[0]
return StringIO.StringIO(zfile.read(os.path.join(
os.path.basename(head), zipname).replace(
os.path.basename(zname), zipname).replace(
os.sep, '/')))
except:
name2 = os.path.normpath(os.path.join(head + '.zip', zipname))
name2 = os.path.normpath(os.path.join(zname + '.zip', zipname))
pass
for i in (name2, name):
if i and os.path.isfile(i):