From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by mx.groups.io with SMTP id smtpd.web11.3166.1586206760313412158 for ; Mon, 06 Apr 2020 13:59:20 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 192.55.52.120, mailfrom: nathaniel.l.desimone@intel.com) IronPort-SDR: suZXouFI3E7VqWOqZGMZDnREL/xA/rkwF9yOIHo0Ph/vI7G8PjxUg51/EmTL0OGWsEJmTttXR1 Uj5KdUMXDrAA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Apr 2020 13:59:18 -0700 IronPort-SDR: oEVZ4TwKWli1cvqDk2T+n646IC/DHSakQx0YP/nx2woAIO6uAtNI+JUTiJs6/5gX2531EgmFRV nKUR0eZyc2Dw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,352,1580803200"; d="scan'208";a="241889106" Received: from sravikum-mobl1.amr.corp.intel.com ([10.255.229.189]) by fmsmga007.fm.intel.com with ESMTP; 06 Apr 2020 13:59:18 -0700 From: "Nate DeSimone" To: devel@edk2.groups.io Cc: Ashley DeSimone , Puja Pandya , Erik Bjorge , Prince Agyeman , Bret Barkelew , Philippe Mathieu-Daude Subject: [edk2-staging/EdkRepo] [PATCH] EdkRepo: Add Support for macOS Date: Mon, 6 Apr 2020 13:59:04 -0700 Message-Id: <20200406205904.27226-1-nathaniel.l.desimone@intel.com> X-Mailer: git-send-email 2.25.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Signed-off-by: Nate DeSimone Cc: Ashley DeSimone Cc: Puja Pandya Cc: Erik Bjorge Cc: Prince Agyeman Cc: Bret Barkelew Cc: Philippe Mathieu-Daude --- build-scripts/build_linux_installer.py | 2 + build-scripts/set_version_and_build_wheels.py | 10 +-- edkrepo/config/config_factory.py | 6 +- edkrepo_installer/linux-scripts/install.py | 65 +++++++++++++++++-- 4 files changed, 67 insertions(+), 16 deletions(-) diff --git a/build-scripts/build_linux_installer.py b/build-scripts/build_linux_installer.py index 0130552..11dd8d7 100755 --- a/build-scripts/build_linux_installer.py +++ b/build-scripts/build_linux_installer.py @@ -10,6 +10,7 @@ from argparse import ArgumentParser import fnmatch, os, shutil, subprocess, sys import set_version_and_build_wheels as build_edkrepo +import traceback def main(): parser = ArgumentParser() @@ -31,6 +32,7 @@ def main(): try: build_edkrepo.main() except: + traceback.print_exc() print('Failed to build edkrepo wheel') return 1 diff --git a/build-scripts/set_version_and_build_wheels.py b/build-scripts/set_version_and_build_wheels.py index 42e58cc..7df0c84 100755 --- a/build-scripts/set_version_and_build_wheels.py +++ b/build-scripts/set_version_and_build_wheels.py @@ -3,7 +3,7 @@ ## @file # set_version_and_build_wheels.py # -# Copyright (c) 2017 - 2019, Intel Corporation. All rights reserved.
+# Copyright (c) 2017 - 2020, Intel Corporation. All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause-Patent # @@ -214,7 +214,7 @@ def build_wheels(extension_pkgs): def copy_wheels_and_set_xml(package_version, extension_pkgs): dir_path = os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))), "dist") dest_path = os.path.join(dir_path, "self_extract") - if ostype == LINUX: + if ostype != WIN: dest_path = os.path.join(dest_path, 'wheels') if not os.path.isdir(dest_path): os.makedirs(dest_path) @@ -241,9 +241,9 @@ def create_final_copy_script(version): f.write("pushd ..\\dist\n") f.write("ren \"setup.exe\" \"EdkRepoSetup-{}.exe\"\n".format(version)) f.write("popd\n") - elif ostype == LINUX: + else: with open('final_copy.py', 'w') as f: - f.write('#!/usr/bin/python3\n') + f.write('#!/usr/bin/env python3\n') f.write('import os, shutil, sys\n') f.write('dist_name = "edkrepo-{{}}".format("{}")\n'.format(version)) f.write('installer_dir = "../dist/self_extract"\n') @@ -257,7 +257,7 @@ def _copy_file(source, destination): check_call("cp -f {} {}".format(source, destination), shell=True) def make_version_cfg_file(version): - if ostype == LINUX: + if ostype != WIN: cfg_src = os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))), 'edkrepo_installer', 'linux-scripts') install_cfg = configparser.ConfigParser(allow_no_value=True) install_cfg.read(os.path.join(cfg_src, 'install.cfg')) diff --git a/edkrepo/config/config_factory.py b/edkrepo/config/config_factory.py index e3a437f..b86e0b8 100644 --- a/edkrepo/config/config_factory.py +++ b/edkrepo/config/config_factory.py @@ -3,7 +3,7 @@ ## @file # config_factory.py # -# Copyright (c) 2017- 2019, Intel Corporation. All rights reserved.
+# Copyright (c) 2017 - 2020, Intel Corporation. All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause-Patent # @@ -30,9 +30,7 @@ def get_edkrepo_global_data_directory(): common_appdata = create_unicode_buffer(MAX_PATH) SHGetFolderPath(None, CSIDL_COMMON_APPDATA, None, SHGFP_TYPE_CURRENT, common_appdata) global_data_dir = os.path.join(common_appdata.value, "edkrepo") - elif sys.platform == "darwin": - raise OSError("macOS support is in progress") - elif sys.platform.startswith("linux") or os.name == "posix": + elif sys.platform == "darwin" or sys.platform.startswith("linux") or os.name == "posix": global_data_dir = os.path.expanduser("~/.edkrepo") if not os.path.isdir(global_data_dir): if not os.path.exists(os.path.dirname(global_data_dir)): diff --git a/edkrepo_installer/linux-scripts/install.py b/edkrepo_installer/linux-scripts/install.py index 52f0c52..099954d 100755 --- a/edkrepo_installer/linux-scripts/install.py +++ b/edkrepo_installer/linux-scripts/install.py @@ -23,6 +23,21 @@ import sys import traceback import xml.etree.ElementTree as et +# +# Environment detection +# +MAC = "mac" +LINUX = "linux" +if sys.platform == "darwin": + ostype = MAC +elif sys.platform.startswith("linux"): + ostype = LINUX +elif os.name == "posix": + print("Warning: Unrecognized UNIX OS... treating as Linux") + ostype = LINUX +else: + raise EnvironmentError("Unsupported OS") + tool_sign_on = 'Installer for edkrepo version {}\nCopyright(c) Intel Corporation, 2020' # Data here should be maintained in a configuration file @@ -31,6 +46,7 @@ directories_with_executables = ['git_automation'] cfg_src_dir = os.path.abspath('config') whl_src_dir = os.path.abspath('wheels') def_python = 'python3' +nfs_home_directory_data = re.compile(r"NFSHomeDirectory:\s*(\S+)") # ZSH Configuration options prompt_regex = re.compile(r"#\s+[Aa][Dd][Dd]\s+[Ee][Dd][Kk][Rr][Ee][Pp][Oo]\s+&\s+[Gg][Ii][Tt]\s+[Tt][Oo]\s+[Tt][Hh][Ee]\s+[Pp][Rr][Oo][Mm][Pp][Tt]") @@ -63,7 +79,8 @@ def init_logger(verbose): def get_args(): parser = ArgumentParser() - parser.add_argument('-l', '--local', action='store_true', default=False, help='Install edkrepo to the user directory instead of system wide') + if ostype != MAC: + parser.add_argument('-l', '--local', action='store_true', default=False, help='Install edkrepo to the user directory instead of system wide') parser.add_argument('-p', '--py', action='store', default=None, help='Specify the python command to use when installing') parser.add_argument('-u', '--user', action='store', default=None, help='Specify user account to install edkrepo support on') parser.add_argument('-v', '--verbose', action='store_true', default=False, help='Enables verbose output') @@ -218,6 +235,18 @@ def _check_version(current, expected): return 1 return 0 +def get_user_home_directory(username): + if ostype == MAC: + res = default_run(['dscl', '.', '-read', '/Users/{}'.format(username), 'NFSHomeDirectory']) + data = nfs_home_directory_data.match(res.stdout.strip()) + if data: + return data.group(1) + else: + raise ValueError("home directory not found") + else: + res = default_run(['getent', 'passwd', username]) + return res.stdout.strip().split(':')[5] + def get_site_packages_directory(): res = default_run([def_python, '-c', 'import site; print(site.getsitepackages()[0])']) return res.stdout.strip() @@ -424,6 +453,9 @@ def do_install(): # Initialize information based on command line input username = args.user + install_to_local = False + if ostype != MAC and args.local: + install_to_local = True try: cfg = configparser.ConfigParser(allow_no_value=True) @@ -449,7 +481,7 @@ def do_install(): # Determine the user running sudo log.info('\nCollecting system information:') - if not args.local: + if not install_to_local and ostype != MAC: try: res = default_run(['id', '-u']) except: @@ -466,11 +498,21 @@ def do_install(): log.info('- Unable to determine current user. Run installer using the --user flag and specify the correct user name.') return 1 try: - res = default_run(['getent', 'passwd', username]) - user_home_dir = res.stdout.strip().split(':')[5] + user_home_dir = get_user_home_directory(username) except: log.info('- Unable to determine users home directory') return 1 + if ostype == MAC: + try: + res = default_run(['id', '-u']) + except: + log.info('- Failed to determine user ID') + return 1 + if res.stdout.strip() == '0': + log.info('- Installer must NOT be run as root') + return 1 + if os.path.commonprefix([user_home_dir, sys.executable]) != user_home_dir: + install_to_local = True default_cfg_dir = os.path.join(user_home_dir, cfg_dir) get_add_prompt_customization(args, user_home_dir) log.info('+ System information collected') @@ -625,7 +667,7 @@ def do_install(): install_whl = wheels_to_install[whl_name]['install'] if install_whl: install_cmd = [def_python, '-m', 'pip', 'install'] - if args.local: + if install_to_local: install_cmd.append('--user') install_cmd.append(os.path.join(whl_src_dir, whl)) try: @@ -639,23 +681,32 @@ def do_install(): set_execute_permissions() log.info('+ Marked scripts as executable') + #If pyenv is being used, regenerate the pyenv shims + if shutil.which('pyenv') is not None: + try: + res = default_run(['pyenv', 'rehash']) + log.info('+ Generated pyenv shims') + except: + log.info('- Failed to generate pyenv shim') #Install the command completion script if shutil.which('edkrepo') is not None: - if args.local: + if install_to_local or ostype == MAC: command_completion_script = os.path.join(default_cfg_dir, 'edkrepo_completions.sh') else: command_completion_script = os.path.join('/', 'etc', 'profile.d', 'edkrepo_completions.sh') try: res = default_run(['edkrepo', 'generate-command-completion-script', command_completion_script]) - if args.local: + if install_to_local or ostype == MAC: shutil.chown(command_completion_script, user=username) os.chmod(command_completion_script, 0o644) add_command_completions_to_shell(command_completion_script, args, username, user_home_dir) + log.info('+ Configured edkrepo command completion') except: log.info('- Failed to configure edkrepo command completion') if args.verbose: traceback.print_exc() + log.log(logging.PRINT, '\nInstallation complete\n') return 0 -- 2.25.2