From 95134d87b0aa4e2e41727e1be2464bf1d4251381 Mon Sep 17 00:00:00 2001 From: Lars Kellogg-Stedman Date: Mon, 14 Nov 2016 13:48:59 -0500 Subject: [PATCH] add support for a user locations.txt file to sim_vehicle it is inconvenient to modify locations.txt in the source, because this will lead to the file being constantly marked as modified by git (and potentially included in pull requests by accident). this commit adds support for a user-maintained list of locations. This file lives by default in `$XDG_CONFIG_DIR/ardupilot/locations.txt` (aka `$HOME/.config/ardupilot/locations.txt`), but may also be specified in the `ARDUPILOT_LOCATIONS` environment variable. --- Tools/autotest/sim_vehicle.py | 38 ++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/Tools/autotest/sim_vehicle.py b/Tools/autotest/sim_vehicle.py index 2eed578500..fc1e134e12 100755 --- a/Tools/autotest/sim_vehicle.py +++ b/Tools/autotest/sim_vehicle.py @@ -486,18 +486,42 @@ def do_build(vehicledir, opts, frame_options): os.chdir(old_dir) +def get_user_locations_path(): + '''The user locations.txt file is located by default in + $XDG_CONFIG_DIR/ardupilot/locations.txt. If $XDG_CONFIG_DIR is + not defined, we look in $HOME/.config/ardupilot/locations.txt. If + $HOME is not defined, we look in ./.config/ardpupilot/locations.txt.''' + + config_dir = os.environ.get( + 'XDG_CONFIG_DIR', + os.path.join(os.environ.get('HOME', '.'), '.config')) + + user_locations_path = os.path.join( + config_dir, 'ardupilot', 'locations.txt') + + return user_locations_path + + def find_location_by_name(autotest, locname): """Search locations.txt for locname, return GPS coords""" + locations_userpath = os.environ.get('ARDUPILOT_LOCATIONS', + get_user_locations_path()) locations_filepath = os.path.join(autotest, "locations.txt") comment_regex = re.compile("\s*#.*") - for line in open(locations_filepath, 'r'): - line = line.rstrip("\n") - line = re.sub(comment_regex, "", line) - if len(line) == 0: + for path in [locations_userpath, locations_filepath]: + if not os.path.isfile(path): continue - (name, loc) = line.split("=") - if name == locname: - return loc + + with open(path, 'r') as fd: + for line in fd: + line = re.sub(comment_regex, "", line) + line = line.rstrip("\n") + if len(line) == 0: + continue + (name, loc) = line.split("=") + if name == locname: + return loc + print("Failed to find location (%s)" % cmd_opts.location) sys.exit(1)