From 4b8814fa9ef56dfe287c661de9f2156ce81fb33c Mon Sep 17 00:00:00 2001 From: Fabien Meghazi Date: Wed, 28 May 2014 19:32:55 +0200 Subject: [PATCH] Prevent scaffold --init to be executed in another module --- openerp/cli/scaffold.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/openerp/cli/scaffold.py b/openerp/cli/scaffold.py index 8d580eba0b4..4ea1fbd0200 100644 --- a/openerp/cli/scaffold.py +++ b/openerp/cli/scaffold.py @@ -33,21 +33,16 @@ class Scaffold(Command): dest = directory(args.dest) if args.init: - module_name = snake(args.init) - module = functools.partial(os.path.join, dest, module_name) + args.module = snake(args.init) + module = functools.partial(os.path.join, dest, args.module) if os.path.exists(module()): die("Can't initialize module in `%s`: Directory already exists." % module()) + if self.get_module_root(dest): + die("Can't init a new module in another module, you probably want to run this " + "command from your project's root") else: - module_name = dest.split(os.path.sep)[-1] - # find the module's root directory - while not os.path.exists(os.path.join(dest, '%s.py' % self.manifest)): - new_dest = os.path.abspath(os.path.join(dest, os.pardir)) - if dest == new_dest: - die("Can't find module directory. Please `cd` to it's path or use --dest") - module_name = dest.split(os.path.sep)[-1] - dest = new_dest + args.module, dest = self.get_module_root(dest) module = functools.partial(os.path.join, dest) - args.module = module_name if args.init: self.dump('%s.jinja2' % self.manifest, module('%s.py' % self.manifest), config=args) @@ -73,6 +68,17 @@ class Scaffold(Command): self.add_init_import(module('controllers', '__init__.py'), controller_module) self.dump('controllers.jinja2', module('controllers', controller_file), config=args) + def get_module_root(self, path): + module_name = path.split(os.path.sep)[-1] + # find the module's root directory + while not os.path.exists(os.path.join(path, '%s.py' % self.manifest)): + new_path = os.path.abspath(os.path.join(path, os.pardir)) + if path == new_path: + return None + module_name = path.split(os.path.sep)[-1] + path = new_path + return (module_name, path) + def has_import(self, initfile, module): with open(initfile, 'r') as f: for imp in ast.parse(f.read()).body: