Newer
Older
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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""Generate a reST file from configuration files
"""
import argparse
import ConfigParser
import csv
import logging
import os
import sys
_logger = logging.getLogger(__name__)
__version__ = '0.1.0'
__date__ = '2016-07-15'
__updated__ = '2016-07-15'
def main(argv=None): # IGNORE:C0111
"""Parse arguments and launch conversion
"""
if argv is None:
argv = sys.argv
else:
sys.argv.extend(argv)
program_version = __version__
program_build_date = str(__updated__)
program_version_message = '%%(prog)s %s (%s)' % (
program_version, program_build_date)
program_shortdesc = __doc__.split("\n")[1]
program_license = '''%s
Created by Vincent Hatakeyama on %s.
Copyright 2016 XCG Consulting. All rights reserved.
Licensed under the MIT License
Distributed on an "AS IS" basis without warranties
or conditions of any kind, either express or implied.
USAGE
''' % (
program_shortdesc,
str(__date__),
)
# Argument parsing
parser = argparse.ArgumentParser(
description=program_license,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'-V', '--version', action='version',
version=program_version_message)
parser.add_argument(
'-v', '--verbose', dest='verbose', action='count',
help="set verbosity level [default: %(default)s]")
parser.add_argument(
'-d',
'--directory',
help="Source directory [default: %(default)s]",
default='..',
)
parser.add_argument(
'-o',
'--output',
help="Output file name",
)
nmspc = parser.parse_args()
verbose = nmspc.verbose
if not verbose:
logging.basicConfig(level=logging.WARN)
if verbose == 1:
logging.basicConfig(level=logging.INFO)
if verbose and verbose > 1:
logging.basicConfig(level=logging.DEBUG)
basedir = os.path.dirname(sys.argv[0])
conf2rst(basedir, nmspc.directory, nmspc.output)
def conf2rst(basedir, directory, output=None):
"""
:param basedir: directory with the odoo_versions.csv file
:param output: file to write to, else prints in stdout
"""
# read setup.cfg
parser = ConfigParser.ConfigParser()
setup_path = os.path.join(directory, 'setup.cfg')
parser.read(setup_path)
odoo_type = parser.get('odoo_scripts', 'odoo_type', 'odoo8')
addon_dirs = parser.get('odoo_scripts', 'addon_dirs', '').split()
# analyse Dockerfile if any
if 'odoo_type' != 'bzr':
dockerfile_path = os.path.join(directory, 'Dockerfile')
with open(dockerfile_path) as dockerfile_file:
for line in dockerfile_file:
if line.startswith('FROM '):
dockerbase = line[5:].strip()
_logger.info("Docker base: %s", dockerbase)
# load odoo_version.csv
versions = {}
# add path to script
with open(os.path.join(basedir, 'odoo_versions.csv')) as versions_file:
reader = csv.reader(versions_file, delimiter=',')
for line in reader:
versions[line[0]] = line[1]
if odoo_type != 'bzr':
odoo_version = versions.get(dockerbase, "Unknown")
else:
odoo_version = 'bzr'
_logger.info("Odoo version %s", odoo_version)
rst = {
'odoo': {"Odoo": {'version': odoo_version}},
'tools': {}, 'modules': {}, 'other': {},
}
# load the conf file
# test if there is a .hgconf file
hgconf_path = os.path.join(directory, '.hgconf')
if os.path.exists(hgconf_path):
_logger.debug("Found .hgconf at %s", hgconf_path)
parser = ConfigParser.ConfigParser()
parser.read(hgconf_path)
for section in parser.sections():
if parser.get(section, 'layout') == 'odoo_scripts':
rst['tools']['odoo_scripts'] = {
'version': parser.get(section, 'track'),
'repository': parser.get(section, 'pulluri'),
}
elif any(
parser.get(section, 'layout').startswith(dir)
for dir in addon_dirs
):
rst['modules'][section] = {
'version': parser.get(section, 'track'),
'repository': parser.get(section, 'pulluri'),
}
else:
rst['other'][section] = {
'version': parser.get(section, 'track'),
'repository': parser.get(section, 'pulluri'),
}
# TODO handle nest.yaml case
confnest_path = os.path.join(directory, 'nest.yaml')
if os.path.exists(confnest_path):
_logger.debug("Found nest.yaml at %s", confnest_path)
_logger.error("confnest parsing not implemented")
# output the rst
if output:
with open(output, 'wb') as output_file:
write_rst(output_file, rst)
else:
write_rst(sys.stdout, rst)
def write_rst(out, rst, headers=True):
"""Write rst to out (be it an open file or std out).
"""
out.write("Versions\n")
out.write("========\n\n")
for group in ('odoo', 'modules', 'other', 'tools'):
if rst.get(group):
out.write(".. list-table:: %s\n" % group)
out.write(" :widths: 2 3\n")
if headers:
out.write(" :header-rows: 1\n")
out.write("\n")
if headers:
out.write(" * - element\n")
out.write(" - version\n")
for element in rst[group].keys():
out.write(" * - %s\n" % element)
out.write(" - %s\n" % rst[group][element]['version'])
out.write("\n")
if __name__ == '__main__':
main()