9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov#
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# Various functions
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov#
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# Copyright (c) 2015 Red Hat, Inc.
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# Author: Nikolai Kondrashov <Nikolai.Kondrashov@redhat.com>
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov#
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# This is free software; you can redistribute it and/or modify it
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# under the terms of the GNU General Public License as published by
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# the Free Software Foundation; version 2 only
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov#
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# This program is distributed in the hope that it will be useful, but
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# WITHOUT ANY WARRANTY; without even the implied warranty of
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# General Public License for more details.
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov#
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# You should have received a copy of the GNU General Public License
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# along with this program. If not, see <http://www.gnu.org/licenses/>.
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov#
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashovimport re
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashovimport os
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashovimport subprocess
447d32b6f572e8ba3100668cb8677c19c0085be6Lukas Slebodnikimport config
4e17c050dac8f2c6e2d278c4c4a27001c8d7d164Jakub Hrozekimport shutil
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai KondrashovUNINDENT_RE = re.compile("^ +", re.MULTILINE)
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov
60713f738cedb6e4239604baf6619a0ca986fa49Michal Židek
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashovdef unindent(text):
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov Unindent text by removing at most the number of spaces present in
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov the first non-empty line from the beginning of every line.
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov indent_ref = [0]
efdcf49660505e13607b99ba82eb504cf37b8794Nikolai Kondrashov
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov def replace(match):
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov if indent_ref[0] == 0:
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov indent_ref[0] = len(match.group())
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov return match.group()[indent_ref[0]:]
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov return UNINDENT_RE.sub(replace, text)
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov
60713f738cedb6e4239604baf6619a0ca986fa49Michal Židek
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashovdef run_shell():
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov Execute an interactive shell under "screen", preserving environment.
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov For use as a breakpoint for debugging.
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """
447d32b6f572e8ba3100668cb8677c19c0085be6Lukas Slebodnik my_env = os.environ.copy()
447d32b6f572e8ba3100668cb8677c19c0085be6Lukas Slebodnik my_env["ROOT_DIR"] = config.PREFIX
447d32b6f572e8ba3100668cb8677c19c0085be6Lukas Slebodnik
447d32b6f572e8ba3100668cb8677c19c0085be6Lukas Slebodnik # screen filter out LD_* evniroment varibles.
447d32b6f572e8ba3100668cb8677c19c0085be6Lukas Slebodnik # Back-up them and set them later in screenrc
447d32b6f572e8ba3100668cb8677c19c0085be6Lukas Slebodnik my_env["_LD_LIBRARY_PATH"] = os.getenv("LD_LIBRARY_PATH", "")
447d32b6f572e8ba3100668cb8677c19c0085be6Lukas Slebodnik my_env["_LD_PRELOAD"] = os.getenv("LD_PRELOAD", "")
447d32b6f572e8ba3100668cb8677c19c0085be6Lukas Slebodnik
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov subprocess.call([
447d32b6f572e8ba3100668cb8677c19c0085be6Lukas Slebodnik "screen", "-DAm", "-S", "sssd_cwrap_session", "-c",
447d32b6f572e8ba3100668cb8677c19c0085be6Lukas Slebodnik ".config/screenrc"],
447d32b6f572e8ba3100668cb8677c19c0085be6Lukas Slebodnik env=my_env
447d32b6f572e8ba3100668cb8677c19c0085be6Lukas Slebodnik )
447d32b6f572e8ba3100668cb8677c19c0085be6Lukas Slebodnik
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashovdef first_dir(*args):
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """Return first argument that points to an existing directory."""
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov for arg in args:
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov if os.path.isdir(arg):
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov return arg
4e17c050dac8f2c6e2d278c4c4a27001c8d7d164Jakub Hrozek
4e17c050dac8f2c6e2d278c4c4a27001c8d7d164Jakub Hrozek
4e17c050dac8f2c6e2d278c4c4a27001c8d7d164Jakub Hrozekdef backup_envvar_file(name):
4e17c050dac8f2c6e2d278c4c4a27001c8d7d164Jakub Hrozek path = os.environ[name]
4e17c050dac8f2c6e2d278c4c4a27001c8d7d164Jakub Hrozek backup_path = path + ".bak"
4e17c050dac8f2c6e2d278c4c4a27001c8d7d164Jakub Hrozek shutil.copyfile(path, backup_path)
4e17c050dac8f2c6e2d278c4c4a27001c8d7d164Jakub Hrozek return path
4e17c050dac8f2c6e2d278c4c4a27001c8d7d164Jakub Hrozek
4e17c050dac8f2c6e2d278c4c4a27001c8d7d164Jakub Hrozek
4e17c050dac8f2c6e2d278c4c4a27001c8d7d164Jakub Hrozekdef restore_envvar_file(name):
4e17c050dac8f2c6e2d278c4c4a27001c8d7d164Jakub Hrozek path = os.environ[name]
4e17c050dac8f2c6e2d278c4c4a27001c8d7d164Jakub Hrozek backup_path = path + ".bak"
4e17c050dac8f2c6e2d278c4c4a27001c8d7d164Jakub Hrozek os.rename(backup_path, path)
a73d70f7ebe194337d7732fd644384c94c2ce0d5Fabiano Fidêncio
a73d70f7ebe194337d7732fd644384c94c2ce0d5Fabiano Fidêncio
885da2c88a214eddd8c5ba16118debdf7e8f2666Fabiano Fidênciodef get_call_output(cmd, stderr_output=subprocess.PIPE):
a73d70f7ebe194337d7732fd644384c94c2ce0d5Fabiano Fidêncio process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
885da2c88a214eddd8c5ba16118debdf7e8f2666Fabiano Fidêncio stderr=stderr_output)
a73d70f7ebe194337d7732fd644384c94c2ce0d5Fabiano Fidêncio output, ret = process.communicate()
a73d70f7ebe194337d7732fd644384c94c2ce0d5Fabiano Fidêncio return output.decode('utf-8')