/
__init__.py
187 lines (158 loc) · 5.9 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from collective.transmogrifier.transmogrifier import configuration_registry
from collective.transmogrifier.transmogrifier import Transmogrifier
from optparse import OptionParser, OptionGroup
from pkg_resources import resource_filename
import Products.GenericSetup
import logging
import mr.migrator
import sys
logging.basicConfig(level=logging.INFO)
try:
import configparser
except ImportError:
import ConfigParser as configparser
try:
from Zope2.App.zcml import load_config
except:
try:
from Products.Five.zcml import load_config
except:
from zope.configuration.xmlconfig import XMLConfig as load_config
load_config = lambda config, context: load_config(config, context)()
class Context:
pass
class NoErrorParser(OptionParser):
def error(self):
pass
def runner(args={}, pipeline=None):
# Make sure GS ZCML is loaded before we load ours
# XXX This delays loading a bit too long. Getting:
# ConfigurationError: ('Unknown directive',
# u'http://namespaces.zope.org/genericsetup', u'importStep')
# again
#
# load_config('autoinclude.zcml', mr.migrator)
load_config("meta.zcml", Products.GenericSetup)
load_config("configure.zcml", Products.GenericSetup)
load_config('configure.zcml', mr.migrator)
parser = OptionParser()
parser.add_option("--pipeline", dest="pipeline",
help="Transmogrifier pipeline.cfg to use",
metavar="FILE")
parser.add_option("--show-pipeline", dest="showpipeline",
action="store_true",
help="Show contents of the pipeline")
parser.add_option("--zcml", dest="zcml",
action="store",
help="Load zcml")
# Parse just the pipeline args
ispipeline = lambda arg: [
a for a in ['--pipeline', '--show-pipeline'] if arg.startswith(a)]
pargs = [arg for arg in sys.argv[1:] if ispipeline(arg)]
(options, cargs) = parser.parse_args(pargs)
if options.pipeline is not None:
config = options.pipeline
elif pipeline is not None:
config = pipeline
else:
# XXX How about if we look for pipeline.cfg in the cwd?
# config = resource_filename(__name__, 'pipeline.cfg')
config = 'pipeline.cfg'
pipelineid = load_pipeline(config, parser)
pargs = [arg for arg in sys.argv[1:] if not arg.startswith('--template')]
(options, cargs) = parser.parse_args(pargs)
cargs = {}
for k, _, v in [a.partition('=') for a in sys.argv[1:]]:
k = k.lstrip('--')
if ':' in k:
part, _, key = k.partition(':')
if key.lower() == 'debug':
logger = logging.getLogger(part)
logger.setLevel(logging.DEBUG)
else:
section = cargs.setdefault(part, {})
if key in section:
section[key] = '%s\n%s' % (section[key], v)
else:
section[key] = v
else:
pass
#cargs[k] = v
for k, v in cargs.items():
args.setdefault(k, {}).update(v)
overrides = {}
if type(args) == type(''):
for arg in args:
section, keyvalue = arg.split(':', 1)
key, value = keyvalue.split('=', 1)
overrides.setdefault('section', {})[key] = value
else:
overrides = args
if options.showpipeline:
for section, values in overrides.items():
for key,value in values.items():
cparser.set(section, key, value)
cparser.write(sys.stdout)
return
# delay this so arg processing not so slow
load_config("meta.zcml", Products.GenericSetup)
load_config("configure.zcml", Products.GenericSetup)
load_config('configure.zcml', mr.migrator)
if options.zcml:
for zcml in options.zcml.split(','):
if not zcml.strip():
continue
load_config('configure.zcml', __import__(zcml, fromlist=zcml.split('.')))
context = Context()
transmogrifier = Transmogrifier(context)
transmogrifier(pipelineid, **overrides)
def load_pipeline(config, parser):
cparser = configparser.ConfigParser()
try:
config_info = configuration_registry.getConfiguration(config)
fp = open(config_info['configuration'])
pipelineid = config
except:
fp = open(config)
configuration_registry.registerConfiguration(
u'transmogrify.config.mr.migrator',
u"",
u'', config)
pipelineid = 'transmogrify.config.mr.migrator'
try:
# configparser
cparser.read_file(fp)
except:
# ConfigParser
cparser.read(config)
fp.close()
if cparser.has_option('transmogrifier', 'include'):
load_pipeline(cparser.get('transmogrifier', 'include'), parser)
if cparser.has_option('transmogrifier', 'pipeline'):
pipeline = [
p.strip() for p in cparser.get('transmogrifier', 'pipeline').split()]
else:
pipeline = []
for section in pipeline:
if section == 'transmogrifier':
continue
if cparser.has_option(section, '@doc'):
doc = cparser.get(section, '@doc')
else:
doc = ''
group = OptionGroup(parser, section, doc)
for key, value in cparser.items(section):
if key.startswith('@'):
if key == '@doc':
continue
metavar, _, help = value.partition(': ')
if metavar.upper() == metavar:
action = "store"
else:
action = "store_true"
help = value
group.add_option("--%s:%s" % (section, key[1:]), action=action,
help=help,
metavar=metavar)
parser.add_option_group(group)
return pipelineid