Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Coopdevs OCB mirror
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Coopdevs
Odoo
Coopdevs OCB mirror
Commits
64ec5f36
Commit
64ec5f36
authored
13 years ago
by
Vo Minh Thu
Browse files
Options
Downloads
Patches
Plain Diff
[IMP] modules: began to replace sys.path appending with an import hook.
bzr revid: vmt@openerp.com-20120109081206-p10yack8lhip11gt
parent
bdbc0686
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
openerp-server
+53
-0
53 additions, 0 deletions
openerp-server
openerp/modules/module.py
+1
-22
1 addition, 22 deletions
openerp/modules/module.py
with
54 additions
and
22 deletions
openerp-server
+
53
−
0
View file @
64ec5f36
...
...
@@ -42,6 +42,9 @@ import openerp
__author__
=
openerp
.
release
.
author
__version__
=
openerp
.
release
.
version
import
sys
import
imp
def
check_root_user
():
"""
Exit if the process
'
s user is
'
root
'
(on POSIX system).
"""
if
os
.
name
==
'
posix
'
:
...
...
@@ -213,6 +216,56 @@ if __name__ == "__main__":
check_root_user
()
openerp
.
tools
.
config
.
parse_config
(
sys
.
argv
[
1
:])
class
ImportHook
(
object
):
def
find_module
(
self
,
module_name
,
package_path
):
module_parts
=
module_name
.
split
(
'
.
'
)
if
len
(
module_parts
)
==
3
and
module_name
.
startswith
(
'
openerp.addons.
'
):
return
self
# We act as a loader too.
# TODO list of loadable modules can be cached instead of always
# calling get_module_path().
if
len
(
module_parts
)
==
1
and
\
openerp
.
modules
.
module
.
get_module_path
(
module_parts
[
0
]):
return
self
# We act as a loader too.
def
load_module
(
self
,
module_name
):
module_parts
=
module_name
.
split
(
'
.
'
)
if
len
(
module_parts
)
==
3
and
module_name
.
startswith
(
'
openerp.addons.
'
):
module_part
=
module_parts
[
2
]
if
module_name
in
sys
.
modules
:
return
sys
.
modules
[
module_name
]
if
len
(
module_parts
)
==
1
:
module_part
=
module_parts
[
0
]
if
module_part
in
sys
.
modules
:
return
sys
.
modules
[
module_part
]
try
:
# Check if the bare module name clashes with another module.
f
,
path
,
descr
=
imp
.
find_module
(
module_part
)
print
"
Warning: ambiguous import:
"
,
module_name
,
f
,
path
,
descr
except
ImportError
,
e
:
# Using `import openerp.addons.<module_name>` instead of
# `import <module_name>` is ugly but not harmful.
pass
f
,
path
,
descr
=
imp
.
find_module
(
module_part
,
openerp
.
modules
.
module
.
ad_paths
)
mod
=
imp
.
load_module
(
module_name
,
f
,
path
,
descr
)
sys
.
modules
[
module_part
]
=
mod
sys
.
modules
[
'
openerp.addons.
'
+
module_part
]
=
mod
return
mod
openerp
.
modules
.
module
.
initialize_sys_path
()
sys
.
meta_path
.
append
(
ImportHook
())
import
openerp.addons.hr
print
sys
.
modules
.
get
(
'
openerp.addons.hr
'
)
print
sys
.
modules
.
get
(
'
hr
'
)
check_postgres_user
()
openerp
.
netsvc
.
init_logger
()
report_configuration
()
...
...
This diff is collapsed.
Click to expand it.
openerp/modules/module.py
+
1
−
22
View file @
64ec5f36
...
...
@@ -63,21 +63,11 @@ def initialize_sys_path():
not in the PYTHONPATH.
"""
global
ad_paths
if
ad_paths
:
return
ad_paths
=
map
(
lambda
m
:
os
.
path
.
abspath
(
tools
.
ustr
(
m
.
strip
())),
tools
.
config
[
'
addons_path
'
].
split
(
'
,
'
))
sys
.
path
.
insert
(
1
,
_ad
)
ad_cnt
=
1
for
adp
in
ad_paths
:
if
adp
!=
_ad
:
sys
.
path
.
insert
(
ad_cnt
,
adp
)
ad_cnt
+=
1
ad_paths
.
append
(
_ad
)
# for get_module_path
ad_paths
.
append
(
_ad
)
# for get_module_path
def
get_module_path
(
module
,
downloaded
=
False
):
...
...
@@ -298,17 +288,6 @@ def init_module_models(cr, module_name, obj_list):
t
[
1
](
cr
,
*
t
[
2
])
cr
.
commit
()
# Import hook to write a addon m in both sys.modules['m'] and
# sys.modules['openerp.addons.m']. Otherwise it could be loaded twice
# if imported twice using different names.
#class MyImportHook(object):
# def find_module(self, module_name, package_path):
# print ">>>", module_name, package_path
# def load_module(self, module_name):
# raise ImportError("Restricted")
#sys.meta_path.append(MyImportHook())
def
register_module_classes
(
m
):
"""
Register module named m, if not already registered.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment