Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: script_compiler.py

Issue 4995669794226176: Issue 2539 - Move VCS abstraction part of ensure_dependencies into a separate module (Closed)
Patch Set: Created May 18, 2015, 3:23 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # coding: utf-8
2
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 import importlib
8 import inspect
9
10 def multiline_repr(str):
Sebastian Noack 2015/05/19 11:52:41 Nit: Please avoid variable names conflicting with
11 return "'''%s'''" % str.replace('\\', '\\\\').replace("'", "\\'")
Sebastian Noack 2015/05/19 11:52:41 Note that you only need to escape quotes if follow
12
13 def compile_script(main, modules=[]):
14 '''
15 Combines the source code of a main module and a number of additional modules
16 in a single Python script.
17 '''
18
19 yield '#!/usr/bin/env python\n'
Sebastian Noack 2015/05/19 11:52:41 I'd rather pass in the file object and use the pri
20 yield 'modules = []\n'
21 for module in modules + [main]:
22 path = inspect.getsourcefile(reload(importlib.import_module(module)))
23 with open(path, 'rb') as file:
24 data = file.read()
25 yield 'modules.append((%s, %s))\n' % (repr(module), multiline_repr(data))
26
27 yield '''
28 import imp
29 import importlib
Sebastian Noack 2015/05/19 11:52:41 importlib isn't used in the generated file.
30 import sys
31
32 if __name__ == '__main__':
33 for module, source in modules:
Sebastian Noack 2015/05/19 11:52:41 IMO, both, the generated and the generating code w
34 m = imp.new_module(module)
35 if (module, source) == modules[-1]:
36 m.__name__ = __name__
37 m.__file__ = __file__
38 exec source in m.__dict__
39 sys.modules[module] = m
40 '''
OLDNEW

Powered by Google App Engine
This is Rietveld