From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga02.intel.com (mga02.intel.com []) by mx.groups.io with SMTP id smtpd.web12.583.1586219997646584672 for ; Mon, 06 Apr 2020 17:39:58 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=fail (domain: intel.com, ip: , mailfrom: nathaniel.l.desimone@intel.com) IronPort-SDR: gsf0b39bWigaK69DYz8EBFZvbnZioO6es93YsitZabzrtZLVOiDSiaI18znjLVju2QktluaoKa kCf6+zKmIiXg== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Apr 2020 17:39:57 -0700 IronPort-SDR: QmjdnjkFDYWY3ucophOaVaA+/IX4OC5ZgG9w5Qtmy3x+TR90hSDF2lOo0BC0Yte7M1HMx+Wyuu ea9rrLjjFb8Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,352,1580803200"; d="scan'208";a="251059374" Received: from vbuzyanx-mobl.amr.corp.intel.com ([10.254.111.95]) by orsmga003.jf.intel.com with ESMTP; 06 Apr 2020 17:39:56 -0700 From: "Nate DeSimone" To: devel@edk2.groups.io Cc: Puja Pandya , Erik Bjorge , Prince Agyeman , Bret Barkelew , Philippe Mathieu-Daude Subject: [edk2-staging/EdkRepo] [PATCH 1/2] EdkRepo: Add setup_git_pyenv_mac.sh Date: Mon, 6 Apr 2020 17:39:49 -0700 Message-Id: <20200407003950.33249-2-nathaniel.l.desimone@intel.com> X-Mailer: git-send-email 2.25.2 In-Reply-To: <20200407003950.33249-1-nathaniel.l.desimone@intel.com> References: <20200407003950.33249-1-nathaniel.l.desimone@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Script to configure pyenv and git for use on macOS. Signed-off-by: Nate DeSimone Cc: Puja Pandya Cc: Erik Bjorge Cc: Prince Agyeman Cc: Bret Barkelew Cc: Philippe Mathieu-Daude --- .../mac-scripts/setup_git_pyenv_mac.sh | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100755 edkrepo_installer/mac-scripts/setup_git_pyenv_mac.sh diff --git a/edkrepo_installer/mac-scripts/setup_git_pyenv_mac.sh b/edkrepo_installer/mac-scripts/setup_git_pyenv_mac.sh new file mode 100755 index 0000000..34083d1 --- /dev/null +++ b/edkrepo_installer/mac-scripts/setup_git_pyenv_mac.sh @@ -0,0 +1,118 @@ +#!/usr/bin/env bash +# +## @file setup_git_pyenv_mac.sh +# +# Copyright (c) 2020, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# + +IFS='' read -r -d '' python_script <<"EOF" +import os +import re +import subprocess +import sys +import traceback + +profile_source_regex = re.compile(r"source\s+~/\.bashrc") +profile_source_regex2 = re.compile(r".\s+~/\.bashrc") + +ls_alias_regex = re.compile(r"alias\s+ls='ls\s+-G'") +bash_completion_regex = re.compile(r"\[\[\s+-r\s+\"/usr/local/etc/profile.d/bash_completion.sh\"\s+\]\]\s+&&\s+.\s+\"/usr/local/etc/profile.d/bash_completion.sh\"") +zsh_autoload_compinit_regex = re.compile(r"autoload\s+-U\s+compinit") +zsh_autoload_bashcompinit_regex = re.compile(r"autoload\s+-U\s+bashcompinit") +zsh_autoload_colors_regex = re.compile(r"autoload\s+-U\s+colors") +zsh_colors_regex = re.compile(r"\n\s*colors\n") +zsh_compinit_regex = re.compile(r"compinit\s+-u") +zsh_bashcompinit_regex = re.compile(r"\n\s*bashcompinit\n") +pyenv_init_regex = re.compile(r"eval\s+\"\$\(pyenv\s+init\s+-\)\"") + +ls_alias = "alias ls='ls -G'" +bash_completion = '[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"' + +zsh_autoload_compinit = 'autoload -U compinit' +zsh_autoload_bashcompinit = 'autoload -U bashcompinit' +zsh_autoload_colors = 'autoload -U colors' +zsh_colors = 'colors' +zsh_compinit = 'compinit -u' +zsh_bashcompinit = 'bashcompinit' + +pyenv_init = ''' +# Use the pyenv intalled Python interperter +if command -v pyenv 1>/dev/null 2>&1; then + eval "$(pyenv init -)" +fi +''' + +def add_command_to_startup_script(script_file, regex, command): + script = '' + if os.path.isfile(script_file): + with open(script_file, 'r') as f: + script = f.read().strip() + data = regex.search(script) + if not data: + if script == '': + script = command + else: + script = '{}\n{}'.format(script, command) + with open(script_file, 'w') as f: + f.write(script) + +def main(): + home_dir = os.path.expanduser('~') + + # Add "source ~/.bashrc" to ~/.bash_profile if it does not have it already + bash_profile_file = os.path.join(home_dir, '.bash_profile') + bash_profile = '' + if os.path.isfile(bash_profile_file): + with open(bash_profile_file, 'r') as f: + bash_profile = f.read().strip() + data = profile_source_regex.search(bash_profile) + if not data: + data = profile_source_regex2.search(bash_profile) + if not data: + if bash_profile == '': + bash_profile = 'source ~/.bashrc\n' + else: + bash_profile = '{}\nsource ~/.bashrc\n'.format(bash_profile) + with open(bash_profile_file, 'w') as f: + f.write(bash_profile) + + # Add pyenv configuration to ~/.bashrc if it does not have it already + bash_rc_file = os.path.join(home_dir, '.bashrc') + add_command_to_startup_script(bash_rc_file, ls_alias_regex, ls_alias) + add_command_to_startup_script(bash_rc_file, bash_completion_regex, bash_completion) + add_command_to_startup_script(bash_rc_file, pyenv_init_regex, pyenv_init) + zsh_rc_file = os.path.join(home_dir, '.zshrc') + add_command_to_startup_script(zsh_rc_file, ls_alias_regex, ls_alias) + add_command_to_startup_script(zsh_rc_file, zsh_autoload_compinit_regex, zsh_autoload_compinit) + add_command_to_startup_script(zsh_rc_file, zsh_autoload_bashcompinit_regex, zsh_autoload_bashcompinit) + add_command_to_startup_script(zsh_rc_file, zsh_autoload_colors_regex, zsh_autoload_colors) + add_command_to_startup_script(zsh_rc_file, zsh_colors_regex, zsh_colors) + add_command_to_startup_script(zsh_rc_file, zsh_compinit_regex, zsh_compinit) + add_command_to_startup_script(zsh_rc_file, zsh_bashcompinit_regex, zsh_bashcompinit) + add_command_to_startup_script(zsh_rc_file, bash_completion_regex, bash_completion) + add_command_to_startup_script(zsh_rc_file, pyenv_init_regex, pyenv_init) + + print('Pyenv configured successfully') + return 0 + +if __name__ == '__main__': + ret_val = 255 + try: + ret_val = main() + except Exception: + print('Unhandled Exception...') + traceback.print_exc() + + sys.exit(ret_val) +EOF + +# On Catalina and later python3 is preferred, +# however it is not packaged by Apple on earlier OSes +if [ -x "$(command -v python3)" ]; then + python3 -c "$python_script" + exit $? +else + python -c "$python_script" + exit $? +fi -- 2.25.2