Asciinema link: https://asciinema.org/a/131481 **New!** wiki page for coordinating work: https://github.com/ansible/community/wiki/Testing:-boilerplate,-wildcard-imports,-and-get_exception **Important Addendum** * To find what files need modifying, please use this wiki page: https://github.com/ansible/community/wiki/Testing:-boilerplate,-wildcard-imports,-and-get_exception instead of the information in the asciinema video. * There are now three tests for these cleanups rather than one. * boilerplate.sh * no-get-exception.sh * no-wildcard-import.sh Hi, this is to a short asciicast to show how to help out with three cleanups of the ansible code base If you're going to work on this, please coordinate so that we don't step on each others work. Talk to abadger1999 on either irc.freenode.net or on the slack channel for ansible (if you have access) There are three cleanups that we're doing right now: * Remove wild card imports (from ansible.modules_utils.basic import *) * Removing get_exception in favor of python3/python-2.6+ syntax * For this cleanup, I also cleanup module.fail_json() calls since I have to touch the code around them anyways:: try: do_something() except Exception: e = get_exception() module.fail_json(msg=str(e)) Replace with:: try: do_something() except Exception as e: module.fail_json(msg=to_native(e), exception=traceback.format_exc()) * Shorten GPL header and add boilerplate. Replacing something like this:: #!/usr/bin/python # -*- coding: utf-8 -*- # (c) 2014, Anders Ingemann # # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . # ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['preview'], 'supported_by': 'community'} with something like this:: #!/usr/bin/python # -*- coding: utf-8 -*- # (c) 2014, Anders Ingemann # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function __metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['preview'], 'supported_by': 'community'} == Step 1 Finding something to work on == * Look on the wiki https://github.com/ansible/community/wiki/Testing:-boilerplate,-wildcard-imports,-and-get_exception * You can also coordinate with Toshio, abadger1999 on irc.freenode.net ## **EDIT** Removed some steps from the asciinema video that are no longer ## relevant. Use the wiki page instead * For this training I want to find a directory that has both get_exception porting and wildcard imports * We'll choose monitoring * Now we need to enter the monitoring directory and work on the code: * Here are the two greps I use to discover just how much work there is in a directory: * Wildcard imports * grep -r 'import \*' . * get_exception removal * grep -r 'get_exception' . * We'll choose to work on logicmonitor_facts.py since it needs both import * fixes and get_exception fixes == Get helper script for removing wildcard imports == * We can run a script to help us take care of the wildcards. It's not perfect but it cuts down significantly on the amount of work to do: * git clone https://github.com/abadger/dewildcard/ * You'll also need pyflakes installed: * pip3 install --user --upgrade pyflakes * I installed the python3 version but for this script it shouldn't matter if you have py2 or py3 versions * And you'll also need the script somewhere in your PATH * cp dewildcard/dewildcard ~/bin/ * For the script to work, you'll need to have the ansible library available to use: * source hacking/env-setup == Fixing a module == * Okay, now we need to run the script on some of the files with problems. I am just going to specify one file but if you want, you can specify all of the files that you are going to work on. dewildcard can handle multiple files * dewildcard logicmonitor_facts.py * git diff . * As you can see, the script did a good job on the ansible.module_utils.urls import (only one thing left) * ....But it did a mediocre job on the module_utils.basic import... We probably are not using all of those imports in this file. * The imports also need some cleanup: * The script could not detect that there was a duplicate open_url import next. * It has these parenthesis even for something that has just one entry. * It didn't move the imports to the right location in the file * Now for the manual cleanup! === Fixing boilerplate === * First thing when we open the file is to modify the boilerplate. * Copy the header-boilerplate.txt file from this gist somewhere where you can copy and paste it easily into your editor. * https://gist.github.com/abadger/35f6918ac1ce48f97cc173c35a718d76 * In vim, the way to do that is to highlight the text in visual mode (Use Capital V. Then arrows to highlight the block of text and then: ("ly) * The (") selects a named register * The (l) is the register name I've chosen. * The (y) means to yank the highlighted text into the register. * Go to the Copyright line. We want to paste our new text just after that. * To paste from that register in vim, press ("lp) * (") Means select a named register * (l) is the register name * (p) means paste after the line that I'm on. * I then delete the old license header. * In vim, I used (capital V) to enter Visual mode, used the arrows to highlight that whole block of text, and then (d) to delete the highlighted text. * I leave two blank lines here (between __metaclass__ = type and ANSIBLE_METADATA) to keep pep8 happy * That's the boilerplate done. Now for imports! === Fixing wildcard imports === * Find the section that has the imports * In vim I used (/import) to find the import lines. * Notice that this module is a hybrid: some of the imports are just below the documentation where they are supposed to be but others are elsewhere. We're going to be coming back to the "right place for imports" frequently so figure out how to save that location in your editor * In vim press (:ma a) to drop a marker here * (:) Colon starts entering a command that has to be termineated with * (ma) says I'm going to name a marker at this line * (a) is the name I'm assigning to the marker * () submits the command * Then I go to the bottom of the file to see what imports are there * In vim: (:$) * (:) for another multi-character command * ($) means move to the last line of the file * (