Newer
Older
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""Generate a reST file from configuration files
"""
import argparse
import logging
import os
import subprocess

Vincent Hatakeyama
committed
__version__ = '0.2.1'

Vincent Hatakeyama
committed
__updated__ = '2019-06-19'
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, 2019 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
''' % (
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
)
# 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 directory: destination directory
: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)
addon_dirs = set(
os.path.dirname(path)
for path in parser.get('odoo_scripts', 'modules').split())
# pip freeze
reqs = dict()
try:
pip_freeze = subprocess.check_output(['pip', 'freeze'])
except OSError as e:
if e.errno == os.errno.ENOENT:
pip_freeze = subprocess.check_output(['pip3', 'freeze'])
else:
# Something else went wrong
raise
for element in pip_freeze.split():
lib_and_version = element.decode('utf-8').split('==')
if len(lib_and_version) == 1: # No explicit version (no ==).
library, version = lib_and_version[0], "N/A"
else:
library, version = lib_and_version
reqs[library] = {'version': version}
'python': reqs,
'tools': {}, 'modules': {}, 'other': {}, 'group of modules': {}
}
# 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():
layout = parser.get(section, 'layout')
if layout == 'odoo_scripts':
group = 'tools'
elif any(layout == addon_dir for addon_dir in addon_dirs):
group = 'group of modules'
layout.startswith(addon_dir) for addon_dir in addon_dirs
group = 'modules'
group = 'other'
rst[group][section] = {
'version': parser.get(section, 'track'),
'repository': parser.get(section, 'pulluri'),
}
# 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)
data = yaml.safe_load(stream)
repos = data['repos']
for module in repos:
if layout == 'odoo_scripts':
group = 'tools'
elif any(layout == addon_dir for addon_dir in addon_dirs):
group = 'group of modules'
elif any(
layout.startswith(addon_dir) for addon_dir in addon_dirs
):
group = 'modules'
else:
group = 'other'
'version': repos[module]['track'],
'repository': repos[module]['pulluri'],
}
with open(output, 'w') 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).
We use anonymous links here (2 underscores) to avoid conflicts when the
same version is shared by 2 repos (as the "link target" system would then
be in use).
out.write("Versions\n")
out.write("========\n\n")
for group in ('modules', 'group of modules', 'python', '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)
version = rst[group][element]['version']
repository = rst[group][element].get('repository', '')
if repository.startswith('ssh://hg@bitbucket.org/'):
text = '`%s <%s/commits/%s>`__' % (
repository.replace(
'ssh://hg@bitbucket.org/',
'https://bitbucket.org/'),
version)
elif repository.startswith('git@bitbucket.org:'):
text = '`%s <%s/commits/%s>`__' % (
repository.replace(
'git@bitbucket.org:',
'https://bitbucket.org/').replace('.git', ''),
version)
else:
text = version
out.write(" - %s\n" % text)