diff --git a/superflore/generators/bitbake/run.py b/superflore/generators/bitbake/run.py index 7d377b4f..d5652e25 100644 --- a/superflore/generators/bitbake/run.py +++ b/superflore/generators/bitbake/run.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import argparse import os import sys @@ -21,61 +20,31 @@ from superflore.generate_installers import generate_installers from superflore.generators.bitbake.gen_packages import regenerate_installer from superflore.generators.bitbake.ros_meta import RosMeta +from superflore.parser import get_parser +from superflore.repo_instance import RepoInstance from superflore.TempfileManager import TempfileManager +from superflore.utils import clean_up from superflore.utils import err from superflore.utils import file_pr from superflore.utils import info +from superflore.utils import load_pr from superflore.utils import ok +from superflore.utils import save_pr from superflore.utils import warn # Modify if a new distro is added active_distros = ['indigo', 'kinetic', 'lunar'] -# just update packages, by default. -mode = 'update' -preserve_existing = True -overlay = None def main(): - global overlay - global preserve_existing - - parser = argparse.ArgumentParser('Deploy ROS packages into Yocto Linux') - parser.add_argument( - '--ros-distro', - help='regenerate packages for the specified distro', - type=str - ) - parser.add_argument( - '--all', - help='regenerate all packages in all distros', - action="store_true" - ) - parser.add_argument( - '--output-repository-path', - help='location of the Git repo', - type=str - ) + preserve_existing = True + overlay = None + parser = get_parser('Deploy ROS packages into Yocto Linux') parser.add_argument( '--tar-archive-dir', help='location to store archived packages', type=str ) - parser.add_argument( - '--only', - nargs='+', - help='generate only the specified packages' - ) - parser.add_argument( - '--pr-comment', - help='comment to add to the PR', - type=str - ) - parser.add_argument( - '--upstream-repo', - help='location of the upstream repository', - type=str - ) selected_targets = active_distros args = parser.parse_args(sys.argv[1:]) pr_comment = args.pr_comment @@ -86,6 +55,21 @@ def main(): warn('"{0}" distro detected...'.format(args.ros_distro)) selected_targets = [args.ros_distro] preserve_existing = False + elif args.dry_run and args.pr_only: + parser.error('Invalid args! cannot dry-run and file PR') + elif args.pr_only and not args.output_repository_path: + parser.error('Invalid args! no repository specified') + elif args.pr_only: + try: + prev_overlay = RepoInstance(args.output_repository_path, False) + msg, title = load_pr() + prev_overlay.pull_request(msg, title) + clean_up() + sys.exit(0) + except Exception as e: + err('Failed to file PR!') + err('reason: {0}'.format(e)) + sys.exit(1) repo_org = 'allenh1' repo_name = 'meta-ros' if args.upstream_repo: @@ -159,6 +143,9 @@ def main(): regen_dict = dict() regen_dict[args.ros_distro] = args.only overlay.commit_changes(args.ros_distro) + if args.dry_run: + save_pr(overlay, args.only, pr_comment) + sys.exit(0) delta = "Regenerated: '%s'\n" % args.only file_pr(overlay, delta, '', pr_comment, distro=args.ros_distro) ok('Successfully synchronized repositories!') @@ -230,5 +217,9 @@ def main(): # Commit changes and file pull request overlay.commit_changes(args.ros_distro) + if args.dry_run: + info('Running in dry mode, not filing PR') + save_pr(overlay, delta, missing_deps, pr_comment) + sys.exit(0) file_pr(overlay, delta, missing_deps, pr_comment) ok('Successfully synchronized repositories!') diff --git a/superflore/generators/ebuild/run.py b/superflore/generators/ebuild/run.py index 28463bff..511d0627 100755 --- a/superflore/generators/ebuild/run.py +++ b/superflore/generators/ebuild/run.py @@ -13,82 +13,33 @@ # See the License for the specific language governing permissions and # limitations under the License. -import argparse import os import sys -import time from rosinstall_generator.distro import get_distro from superflore.generate_installers import generate_installers from superflore.generators.ebuild.gen_packages import regenerate_pkg from superflore.generators.ebuild.overlay_instance import RosOverlay +from superflore.parser import get_parser from superflore.repo_instance import RepoInstance from superflore.TempfileManager import TempfileManager +from superflore.utils import clean_up from superflore.utils import err from superflore.utils import file_pr from superflore.utils import info +from superflore.utils import load_pr from superflore.utils import ok +from superflore.utils import save_pr from superflore.utils import warn # Modify if a new distro is added active_distros = ['indigo', 'kinetic', 'lunar'] -# just update packages, by default. -preserve_existing = True -overlay = None - - -def clean_up(): - if os.path.exists('.pr-message.tmp'): - os.remove('.pr-message.tmp') - if os.path.exists('.pr-title.tmp'): - os.remove('.pr-title.tmp') def main(): - global overlay - global preserve_existing - - parser = argparse.ArgumentParser('Deploy ROS packages into Gentoo Linux') - parser.add_argument( - '--ros-distro', - help='regenerate packages for the specified distro', - type=str - ) - parser.add_argument( - '--all', - help='regenerate all packages in all distros', - action="store_true" - ) - parser.add_argument( - '--dry-run', - help='run without filing a PR to remote', - action="store_true" - ) - parser.add_argument( - '--pr-only', - help='ONLY file a PR to remote', - action='store_true' - ) - parser.add_argument( - '--output-repository-path', - help='location of the Git repo', - type=str - ) - parser.add_argument( - '--only', - nargs='+', - help='generate only the specified packages' - ) - parser.add_argument( - '--pr-comment', - help='comment to add to the PR', - type=str - ) - parser.add_argument( - '--upstream-repo', - help='location of the upstream repository', - type=str - ) + overlay = None + preserve_existing = True + parser = get_parser('Deploy ROS packages into Gentoo Linux') args = parser.parse_args(sys.argv[1:]) pr_comment = args.pr_comment selected_targets = None @@ -103,24 +54,9 @@ def main(): elif args.pr_only and not args.output_repository_path: parser.error('Invalid args! no repository specified') elif args.pr_only: - try: - with open('.pr-message.tmp', 'r') as msg_file: - msg = msg_file.read().rstrip('\n') - with open('.pr-title.tmp', 'r') as title_file: - title = title_file.read().rstrip('\n') - except OSError: - err('Failed to open PR title/message file!') - err( - 'Please supply the %s and %s files' % ( - '.pr_message.tmp', - '.pr_title.tmp' - ) - ) - raise try: prev_overlay = RepoInstance(args.output_repository_path, False) - info('PR message:\n"%s"\n' % msg) - info('PR title:\n"%s"\n' % title) + msg, title = load_pr() prev_overlay.pull_request(msg, title) clean_up() sys.exit(0) @@ -194,15 +130,10 @@ def main(): overlay.regenerate_manifests(regen_dict) overlay.commit_changes(args.ros_distro) if args.dry_run: - # TODO(allenh1): update this PR style. - info('Running in dry mode, not filing PR') - title_file = open('.pr-title.tmp', 'w') - title_file.write('rosdistro sync, {0}\n'.format(time.ctime())) - pr_message_file = open('.pr-message.tmp', 'w') - pr_message_file.write('%s\n%s\n' % (pr_comment, '')) + save_pr(overlay, args.only, pr_comment) sys.exit(0) - file_pr(overlay, '', '', pr_comment) - clean_up() + delta = "Regenerated: '%s'\n" % args.only + file_pr(overlay, delta, '', pr_comment) ok('Successfully synchronized repositories!') sys.exit(0) @@ -275,10 +206,7 @@ def main(): if args.dry_run: info('Running in dry mode, not filing PR') - title_file = open('.pr-title.tmp', 'w') - title_file.write('rosdistro sync, {0}\n'.format(time.ctime())) - pr_message_file = open('.pr-message.tmp', 'w') - pr_message_file.write('%s\n%s\n' % (delta, missing_deps)) + save_pr(overlay, delta, missing_deps, pr_comment) sys.exit(0) file_pr(overlay, delta, missing_deps, pr_comment) diff --git a/superflore/parser.py b/superflore/parser.py new file mode 100644 index 00000000..f7fe1dce --- /dev/null +++ b/superflore/parser.py @@ -0,0 +1,62 @@ +# Copyright 2018 Open Source Robotics Foundation, Inc. +# +# 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 argparse + + +# set up a parser and return it +def get_parser(tool_tip, is_generator=True): + parser = argparse.ArgumentParser(tool_tip) + if is_generator: + parser.add_argument( + '--ros-distro', + help='regenerate packages for the specified distro', + type=str + ) + parser.add_argument( + '--all', + help='regenerate all packages in all distros', + action="store_true" + ) + parser.add_argument( + '--dry-run', + help='run without filing a PR to remote', + action="store_true" + ) + parser.add_argument( + '--pr-only', + help='ONLY file a PR to remote', + action='store_true' + ) + parser.add_argument( + '--output-repository-path', + help='location of the Git repo', + type=str + ) + parser.add_argument( + '--only', + nargs='+', + help='generate only the specified packages' + ) + parser.add_argument( + '--pr-comment', + help='comment to add to the PR', + type=str + ) + parser.add_argument( + '--upstream-repo', + help='location of the upstream repository', + type=str + ) + return parser diff --git a/superflore/utils.py b/superflore/utils.py index 674dd451..7b5648ad 100644 --- a/superflore/utils.py +++ b/superflore/utils.py @@ -18,6 +18,7 @@ import re import string import sys +import time from superflore.exceptions import UnknownLicense from superflore.exceptions import UnknownPlatform @@ -41,7 +42,7 @@ def info(string): # pragma: no cover print(colored('>>>> {0}'.format(string), 'cyan')) -def file_pr(overlay, delta, missing_deps, comment, distro=None): +def get_pr_text(comment=None): msg = '' if comment: msg += '%s\n' % comment @@ -49,7 +50,37 @@ def file_pr(overlay, delta, missing_deps, comment, distro=None): args = sys.argv args[0] = args[0].split('/')[-1] msg += '```\n%s\n```' % ' '.join(args) + + +def save_pr(overlay, delta, missing_deps, comment): + with open('.pr-title.tmp', 'w') as title_file: + title_file.write('rosdistro sync, {0}\n'.format(time.ctime())) + with open('.pr-message.tmp', 'w') as pr_msg_file: + pr_msg_file.write('%s\n' % get_pr_text(comment)) + + +def load_pr(): + try: + with open('.pr-message.tmp', 'r') as msg_file: + msg = msg_file.read().rstrip('\n') + with open('.pr-title.tmp', 'r') as title_file: + title = title_file.read().rstrip('\n') + except OSError: + err('Failed to open PR title/message file!') + err( + 'Please supply the %s and %s files' % ( + '.pr_message.tmp', + '.pr_title.tmp' + ) + ) + raise + return msg, title + clean_up() + + +def file_pr(overlay, delta, missing_deps, comment, distro=None): try: + msg = get_pr_text(comment) overlay.pull_request('%s\n%s\n%s' % (msg, delta, missing_deps), distro) except Exception as e: err( @@ -62,6 +93,13 @@ def file_pr(overlay, delta, missing_deps, comment, distro=None): sys.exit(1) +def clean_up(): + if os.path.exists('.pr-message.tmp'): + os.remove('.pr-message.tmp') + if os.path.exists('.pr-title.tmp'): + os.remove('.pr-title.tmp') + + def make_dir(dirname): try: os.makedirs(dirname)