# HG changeset patch
# User Vincent Hatakeyama <vincent.hatakeyama@xcg-consulting.fr>
# Date 1468578002 -7200
#      Fri Jul 15 12:20:02 2016 +0200
# Node ID 7ada23d3fa76db0a11ea05ee681ac0746e402cd8
# Parent  75a17bfb0852d41580b216cd1da22a3ae42c5d4d
Add conf2reST.py script

diff --git a/HISTORY.rst b/HISTORY.rst
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -2,6 +2,11 @@
 History
 =======
 
+2.7
+---
+
+Add conf2reST.py script.
+
 2.0
 ---
 
diff --git a/README.rst b/README.rst
--- a/README.rst
+++ b/README.rst
@@ -33,3 +33,8 @@
 The name of the database is the same as do_tests or can be provided.
 
 The script call ``start``.
+
+conf2reST.py
+-----------
+
+This script is used to produce a reST file from setup.cfg, the Dockerfile and the .hgconf/nest.yaml file.
diff --git a/VERSION b/VERSION
--- a/VERSION
+++ b/VERSION
@@ -1,1 +1,1 @@
-2.6
+2.7
diff --git a/conf2reST.py b/conf2reST.py
new file mode 100755
--- /dev/null
+++ b/conf2reST.py
@@ -0,0 +1,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()
diff --git a/do_tests b/do_tests
--- a/do_tests
+++ b/do_tests
@@ -13,7 +13,7 @@
 #
 # - access to pg database
 #
-# Version 2.6
+# Version 2.7
 
 here=$(dirname $0)
 project_home=$(cd $here && cd .. && echo $PWD)
diff --git a/docker_build b/docker_build
--- a/docker_build
+++ b/docker_build
@@ -1,5 +1,5 @@
 #!/bin/zsh
-# template version 2.6
+# template version 2.7
 
 # TODO add a way to bypass the value, maybe with a key in the setup.cfg file
 repository=dockerhub.xcg.io
diff --git a/odoo_versions.csv b/odoo_versions.csv
new file mode 100644
--- /dev/null
+++ b/odoo_versions.csv
@@ -0,0 +1,6 @@
+odoo type,hash
+bzr,7.0
+xcgd/odoo:7.0,7.0 d246e36c824931613229a6467e21d65d392a2fa3
+xcgd/odoo:7.0-test,7.0 a0484cbe45abb5dc393d3229ee1c6d24a4dfae23
+xcgd/odoo:8.0,8.0 ba54a668d425c6b2de37428184e788552dc11a1c
+xcgd/odoo:8.0-test,8.0 859fe655d295b83d86d5969e301b8f6bcc61c2d1
\ No newline at end of file
diff --git a/run_tests b/run_tests
--- a/run_tests
+++ b/run_tests
@@ -4,7 +4,7 @@
 # This script is used to run tests on a database with demo data installed.
 #
 # Either provide the name of the database (first argument) or it will default to <project>_test
-# Version 2.6
+# Version 2.7
 here=$(dirname $0)
 project_home=$(cd $here && cd .. && echo $PWD)
 tested_modules=$(python -B -c "import ConfigParser ; c = ConfigParser.ConfigParser() ; c.read('${project_home}/setup.cfg') ; print ','.join(c.get('odoo_scripts', 'module_list_tests', '').split())")
diff --git a/setup.cfg b/setup.cfg
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,5 +1,5 @@
 [bumpversion]
-current_version = 2.6
+current_version = 2.7
 parse = (?P<major>\d+)\.(?P<minor>\d+)
 serialize = 
 	{major}.{minor}
diff --git a/start b/start
--- a/start
+++ b/start
@@ -5,7 +5,7 @@
 #
 # You can set ODOO_DATA_DIR and ODOO_DB_HOST if you want to avoid using the default value ($HOME/var/tmp and localhost)
 #
-# Version 2.6
+# Version 2.7
 here=$(dirname $0)
 project_home=$(cd $here && cd .. && echo $PWD)
 echo "Starting odoo for development"