From 223596d7bf8ab9b839e0031022c132a3ccd4fd1d Mon Sep 17 00:00:00 2001 From: Axel Waggershauser Date: Wed, 11 Feb 2015 03:27:40 +0100 Subject: [PATCH] added support for optional custom command and replace_string parameter of vcs_tag --- interpreter.py | 105 +++++++++--------------- meson_vcs_info.py | 60 -------------- test cases/common/73 vcstag/meson.build | 6 ++ vcstagger.py | 53 +++--------- 4 files changed, 59 insertions(+), 165 deletions(-) delete mode 100755 meson_vcs_info.py diff --git a/interpreter.py b/interpreter.py index 75a413541..d406e9f47 100644 --- a/interpreter.py +++ b/interpreter.py @@ -830,13 +830,6 @@ class Interpreter(): if not isinstance(actual, wanted): raise InvalidArguments('Incorrect argument type.') - def validate_target_name(self, name): - if name in coredata.forbidden_target_names: - raise InvalidArguments('Target name "%s" is reserved for Meson\'s internal use. Please rename.'\ - % name) - if name in self.build.targets: - raise InvalidCode('Tried to create target "%s", but a target of that name already exists.' % name) - def func_run_command(self, node, args, kwargs): if len(args) < 1: raise InterpreterException('Not enough arguments') @@ -1113,15 +1106,50 @@ class Interpreter(): def func_jar(self, node, args, kwargs): return self.build_target(node, args, kwargs, JarHolder) + def detect_vcs(self, source_dir): + vcs_systems = [ + dict(name = 'git', cmd = 'git', repo_dir = '.git', get_rev = 'git describe --dirty=+', rev_regex = '(.*)', dep = '.git/logs/HEAD'), + dict(name = 'mercurial', cmd = 'hg', repo_dir = '.hg', get_rev = 'hg id -n', rev_regex = '(.*)', dep = '.hg/dirstate'), + dict(name = 'subversion', cmd = 'svn', repo_dir = '.svn', get_rev = 'svn info', rev_regex = 'Revision: (.*)', dep = '.svn/wc.db'), + dict(name = 'bazaar', cmd = 'bzr', repo_dir = '.bzr', get_rev = 'bzr revno', rev_regex = '(.*)', dep = '.bzr'), + ] + + segs = source_dir.replace('\\', '/').split('/') + for i in range(len(segs), -1, -1): + curdir = '/'.join(segs[:i]) + for vcs in vcs_systems: + if os.path.isdir(os.path.join(curdir, vcs['repo_dir'])) and shutil.which(vcs['cmd']): + vcs['wc_dir'] = curdir + return vcs + return None + def func_vcs_tag(self, node, args, kwargs): - fallback = kwargs.get('fallback', None) + fallback = kwargs.pop('fallback', None) if not isinstance(fallback, str): raise InterpreterException('Keyword argument must exist and be a string.') - del kwargs['fallback'] - scriptfile = os.path.join(os.path.split(__file__)[0], 'vcstagger.py') - kwargs['command'] = [sys.executable, scriptfile, '@INPUT@', '@OUTPUT@', fallback] - kwargs['build_always'] = True - return self.func_custom_target(node, ['vcstag'], kwargs) + replace_string = kwargs.pop('replace_string', '@VCS_TAG@') + regex_selector = '(.*)' # default regex selector for custom command: use complete output + vcs_cmd = kwargs.get('command', None) + if vcs_cmd and not isinstance(vcs_cmd, list): + vcs_cmd = [vcs_cmd] + # source_dir = os.path.split(os.path.abspath(kwargs.get('infile')))[0] + source_dir = os.path.join(self.environment.get_source_dir(), self.subdir) + if vcs_cmd: + # Is the command an executable in path or maybe a script in the source tree? + vcs_cmd[0] = shutil.which(vcs_cmd[0]) or os.path.join(source_dir, vcs_cmd[0]) + else: + vcs = self.detect_vcs(source_dir) + if vcs: + mlog.log('Found %s repository at %s' % (vcs['name'], vcs['wc_dir'])) + vcs_cmd = vcs['get_rev'].split() + regex_selector = vcs['rev_regex'] + else: + vcs_cmd = [' '] # executing this cmd will fail in vcstagger.py and force to use the fallback string + scriptfile = os.path.join(self.environment.get_script_dir(), 'vcstagger.py') + # vcstagger.py parameters: infile, outfile, fallback, source_dir, replace_string, regex_selector, command... + kwargs['command'] = [sys.executable, scriptfile, '@INPUT0@', '@OUTPUT0@', fallback, source_dir, replace_string, regex_selector] + vcs_cmd + kwargs.setdefault('build_always', True) + return self.func_custom_target(node, [kwargs['output']], kwargs) def func_custom_target(self, node, args, kwargs): if len(args) != 1: @@ -1138,57 +1166,6 @@ class Interpreter(): self.build.targets[name] = tg.held_object return tg - def func_vcs_info(self, nodes, args, kwargs): - # required: none; optional: name, vcs_cmd, vcs_dir, output, build_always - name = args[0] if len(args) > 0 else 'vcs_info' - self.validate_target_name(name) - vcs_dir = kwargs.pop('vcs_dir', self.environment.get_source_dir()) - vcs_cmd = None - - vcs_systems = [ - dict(name = 'git', cmd = 'git', get_rev = 'git describe', dep = '.git/logs/HEAD', get_root = 'git rev-parse --show-toplevel'), - dict(name = 'mercurial', cmd = 'hg', get_rev = 'hg id -n', dep = '.hg/dirstate', get_root = 'hg root'), -# dict(name = 'subversion', cmd = 'svn', get_rev = 'svn info', dep = '.svn/wc.db', get_root = 'svn info'), - ] - for vcs in vcs_systems: - if vcs['cmd'] and shutil.which(vcs['cmd']): - vcs_root = subprocess.Popen(vcs['get_root'], shell=True, cwd=vcs_dir, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).communicate()[0].decode().strip() - vcs_dep = os.path.join(vcs_root, vcs['dep']) - if os.path.exists(vcs_dep): - vcs_cmd = vcs['get_rev'] - kwargs['input'] = vcs_dep - mlog.log('Found %s repository at %s' % (vcs['name'], vcs_root)) - break - - vcs_cmd = kwargs.pop('vcs_cmd', vcs_cmd ) - if not vcs_cmd: - raise InvalidArguments('Could not autodetect vcs system and no custom vcs_cmd was specified.') - kwargs.setdefault('output', name) - script = os.path.join(self.environment.get_script_dir(), 'meson_vcs_info.py') - kwargs['command'] = [script, 'update', vcs_cmd, vcs_dir, '@OUTPUT0@'] - tg = CustomTargetHolder(name, self.subdir, kwargs) - self.build.targets[name] = tg.held_object - return tg - - def func_vcs_configure(self, nodes, args, kwargs): - # required: name, input, vcs_info; optional: output, config_var_name - name = args[0] - self.validate_target_name(name) - kwargs.setdefault('input', args[1] if len(args)>1 else None) - vcs_info = kwargs.pop('vcs_info', None) - if not vcs_info: - raise InvalidArguments('Missing argument "vcs_info".') - if not isinstance(vcs_info, CustomTargetHolder): - raise InterpreterException('Argument "vcs_info" is not of type vcs_info') - vcs_info = vcs_info.held_object - config_var_name = kwargs.pop('config_var_name', 'VCS_INFO') - kwargs.setdefault('output', name) - script = os.path.join(self.environment.get_script_dir(), 'meson_vcs_info.py') - kwargs['command'] = [script, 'configure', vcs_info, config_var_name, '@INPUT0@', '@OUTPUT0@'] - tg = CustomTargetHolder(name, self.subdir, kwargs) - self.build.targets[name] = tg.held_object - return tg - def func_run_target(self, node, args, kwargs): if len(args) < 2: raise InterpreterException('Incorrect number of arguments') diff --git a/meson_vcs_info.py b/meson_vcs_info.py deleted file mode 100755 index 9f5e46245..000000000 --- a/meson_vcs_info.py +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/env python3 - -# Copyright 2013-2015 The Meson development team - -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at - -# http://www.apache.org/licenses/LICENSE-2.0 - -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import sys, os, shutil, shlex, subprocess -import backends - -def update_vcs_info(vcs_cmd, vcs_dir, vcs_info_file): - """Update the string in the vcs_info_file with the stdout of vcs_cmd executed in vcs_dir.""" - - cmd = shlex.split(vcs_cmd) - # Is the command an executable in path or maybe a script in the source tree? - cmd[0] = shutil.which(cmd[0]) or os.path.join(vcs_dir, cmd[0]) - - try: - info = subprocess.check_output(cmd, cwd=vcs_dir) - except FileNotFoundError: - print('Could not execute command "%s".' % ' '.join(cmd)) - sys.exit(1) - except subprocess.CalledProcessError as e: - print('Failed to get repository information from %s.\n' % vcs_dir) - sys.exit(e.returncode) - - info = info.strip() - - if (not os.path.exists(vcs_info_file)) or (open(vcs_info_file, 'rb').read() != info): - open(vcs_info_file, 'wb').write(info) - - -def configure_vcs_info(vcs_info_file, config_var_name, input_file, output_file): - """Configure the input_file by replacing the variable config_var_name with the contents of the vcs_info_file and save it as output_file.""" - - info = open(vcs_info_file).read() - backends.do_conf_file(input_file, output_file, {config_var_name : info}) - - -if __name__ == '__main__': - if not len(sys.argv) in [5,6]: - print('Version Control Systems helper script for Meson. Do not run on your own, mmm\'kay?') - print('%s update ' % sys.argv[0]) - print('%s configure ' % sys.argv[0]) - sys.exit(1) - - if sys.argv[1] == 'update': - update_vcs_info( *sys.argv[2:5] ) - elif sys.argv[1] == 'configure': - configure_vcs_info( *sys.argv[2:6] ) - diff --git a/test cases/common/73 vcstag/meson.build b/test cases/common/73 vcstag/meson.build index 23ff487f2..b37ca6ad9 100644 --- a/test cases/common/73 vcstag/meson.build +++ b/test cases/common/73 vcstag/meson.build @@ -4,5 +4,11 @@ version_src = vcs_tag(input : 'vcstag.c.in', output : 'vcstag.c', fallback : '1.0.0') +version_src_custom = vcs_tag(input : 'vcstag.c.in', +output : 'vcstag-cusom.c', +command : ['git', 'show-ref', '-s', 'refs/heads/master'], +fallback : '1.0.0') + executable('tagprog', 'tagprog.c', version_src) +executable('tagprog-custom', 'tagprog.c', version_src_custom) diff --git a/vcstagger.py b/vcstagger.py index f754358f5..ccc584e92 100755 --- a/vcstagger.py +++ b/vcstagger.py @@ -14,49 +14,20 @@ # See the License for the specific language governing permissions and # limitations under the License. -import sys, os, subprocess +import sys, os, subprocess, re -def tag(infile, outfile, fallback): - tagid = get_string(infile, fallback) - newdata = open(infile).read().replace('@VCS_TAG@', tagid) +def config_vcs_tag(infile, outfile, fallback, source_dir, replace_string, regex_selector, cmd): try: - olddata = open(outfile).read() - if olddata == newdata: - return + output = subprocess.check_output(cmd, cwd=source_dir) + new_string = re.search(regex_selector, output.decode()).group(1).strip() except Exception: - pass - open(outfile, 'w').write(newdata) - -def get_string(infile, fallback): - absfile = os.path.join(os.getcwd(), infile) - directory = os.path.split(absfile)[0] - segs = directory.replace('\\', '/').split('/') - for i in range(len(segs), -1, -1): - curdir = '/'.join(segs[:i]) - if os.path.isdir(os.path.join(curdir, '.git')): - output = subprocess.check_output(['git', 'describe'], - cwd = directory) - return output.decode().strip() - elif os.path.isdir(os.path.join(curdir, '.hg')): - output = subprocess.check_output(['hg', 'identify'], - cwd=directory) - return output.decode().strip() - elif os.path.isdir(os.path.join(curdir, '.bzr')): - output = subprocess.check_output(['bzr', 'revno'], - cwd=directory) - return output.decode().strip() - elif os.path.isdir(os.path.join(curdir, '.svn')): - output = subprocess.check_output(['svn', 'info'], - cwd=directory) - for line in output.decode().split('\n'): - (k, v) = line.split(':', 1) - if k.strip() == 'Revision': - return v.strip() - raise RuntimeError('Svn output malformed.') - return fallback + new_string = fallback + + new_data = open(infile).read().replace(replace_string, new_string) + if (not os.path.exists(outfile)) or (open(outfile).read() != new_data): + open(outfile, 'w').write(new_data) if __name__ == '__main__': - infile = sys.argv[1] - outfile = sys.argv[2] - fallback = sys.argv[3] - tag(infile, outfile, fallback) + infile, outfile, fallback, source_dir, replace_string, regex_selector = sys.argv[1:7] + command = sys.argv[7:] + config_vcs_tag(infile, outfile, fallback, source_dir, replace_string, regex_selector, command)