public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Ashley E Desimone" <ashley.e.desimone@intel.com>
To: "devel@edk2.groups.io" <devel@edk2.groups.io>,
	"Desimone, Nathaniel L" <nathaniel.l.desimone@intel.com>
Cc: "Pandya, Puja" <puja.pandya@intel.com>,
	"Bjorge, Erik C" <erik.c.bjorge@intel.com>,
	"Agyeman, Prince" <prince.agyeman@intel.com>,
	"Bret Barkelew" <Bret.Barkelew@microsoft.com>,
	Philippe Mathieu-Daude <philmd@redhat.com>
Subject: Re: [edk2-devel] [edk2-staging/EdkRepo] [PATCH 1/2] EdkRepo: Add setup_git_pyenv_mac.sh
Date: Wed, 8 Apr 2020 20:29:57 +0000	[thread overview]
Message-ID: <DM6PR11MB36283DCCBB513F3B31CDC6B5B2C00@DM6PR11MB3628.namprd11.prod.outlook.com> (raw)
In-Reply-To: <20200407003950.33249-2-nathaniel.l.desimone@intel.com>

Reviewed-by: Ashley DeSimone <ashley.e.desimone@intel.com>

-----Original Message-----
From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Nate DeSimone
Sent: Monday, April 6, 2020 5:40 PM
To: devel@edk2.groups.io
Cc: Pandya, Puja <puja.pandya@intel.com>; Bjorge, Erik C <erik.c.bjorge@intel.com>; Agyeman, Prince <prince.agyeman@intel.com>; Bret Barkelew <Bret.Barkelew@microsoft.com>; Philippe Mathieu-Daude <philmd@redhat.com>
Subject: [edk2-devel] [edk2-staging/EdkRepo] [PATCH 1/2] EdkRepo: Add setup_git_pyenv_mac.sh

Script to configure pyenv and git for use on macOS.

Signed-off-by: Nate DeSimone <nathaniel.l.desimone@intel.com>
Cc: Puja Pandya <puja.pandya@intel.com>
Cc: Erik Bjorge <erik.c.bjorge@intel.com>
Cc: Prince Agyeman <prince.agyeman@intel.com>
Cc: Bret Barkelew <Bret.Barkelew@microsoft.com>
Cc: Philippe Mathieu-Daude <philmd@redhat.com>
---
 .../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.<BR> # 
+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





  reply	other threads:[~2020-04-08 20:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-07  0:39 [edk2-staging/EdkRepo] [PATCH 0/2] Add documentation for EdkRepo on macOS Nate DeSimone
2020-04-07  0:39 ` [edk2-staging/EdkRepo] [PATCH 1/2] EdkRepo: Add setup_git_pyenv_mac.sh Nate DeSimone
2020-04-08 20:29   ` Ashley E Desimone [this message]
2020-04-07  0:39 ` [edk2-staging/EdkRepo] [PATCH 2/2] EdkRepo: Update README.md Nate DeSimone
2020-04-08 20:30   ` [edk2-devel] " Ashley E Desimone
2020-04-08 20:28 ` [edk2-devel] [edk2-staging/EdkRepo] [PATCH 0/2] Add documentation for EdkRepo on macOS Ashley E Desimone

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DM6PR11MB36283DCCBB513F3B31CDC6B5B2C00@DM6PR11MB3628.namprd11.prod.outlook.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox