1# Software License Agreement (BSD License)
2#
3# Copyright (c) 2015, Open Source Robotics Foundation, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10#  * Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12#  * Redistributions in binary form must reproduce the above
13#    copyright notice, this list of conditions and the following
14#    disclaimer in the documentation and/or other materials provided
15#    with the distribution.
16#  * Neither the name of Willow Garage, Inc. nor the names of its
17#    contributors may be used to endorse or promote products derived
18#    from this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31# POSSIBILITY OF SUCH DAMAGE.
32
33"""
34Common functions that can be used to mark spaces, e.g. build and devel, to indicate which tools previously built the space.
35
36This allows the tools to detect cross tool talk and avoid it where appropriate
37"""
38
39
40from __future__ import print_function
41
42import os
43
44SPACE_BUILT_BY_MARKER_FILENAME = '.built_by'
45
46
47def get_previous_tool_used_on_the_space(space_path):
48    """
49    Return the tool used to build the space at the given path, or None.
50
51    Returns None if the path does not exist or if there is no built by file.
52
53    :param str space_path: path to the space in question.
54    :returns: str identifying the tool used to build the space or None.
55    """
56    if os.path.isdir(space_path):
57        marker_path = os.path.join(space_path, SPACE_BUILT_BY_MARKER_FILENAME)
58        if os.path.isfile(marker_path):
59            with open(marker_path, 'r') as f:
60                return f.read().strip()
61    return None
62
63
64def mark_space_as_built_by(space_path, tool_name):
65    """
66    Place a marker file in the space at the given path, telling who built it.
67
68    The path to the marker is created if necessary.
69
70    :param str space_path: path to the space which should be marked.
71    :param str tool_name: name of the tool doing the marking.
72    :raises: OSError, others, when trying to create the folder.
73    """
74    if not os.path.isdir(space_path):
75        # Might fail if it's a file already or for permissions.
76        os.makedirs(space_path)
77    marker_path = os.path.join(space_path, SPACE_BUILT_BY_MARKER_FILENAME)
78    with open(marker_path, 'w') as f:
79        f.write(tool_name)
80