public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Heng Luo" <heng.luo@intel.com>
To: devel@edk2.groups.io
Cc: Ray Ni <ray.ni@intel.com>, Eric Dong <eric.dong@intel.com>,
	Liming Gao <liming.gao@intel.com>,
	Bob Feng <bob.c.feng@intel.com>, Amy Chan <amy.chan@intel.com>
Subject: [PATCH] BaseTools/Scripts: Add scripts to set PACKAGES_PATH environment
Date: Thu,  2 Apr 2020 16:08:09 +0800	[thread overview]
Message-ID: <20200402080809.2147-1-heng.luo@intel.com> (raw)

Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=2656

1. Add GetPackagesPath.py, it will be used to get package pathes from
  special directories. A sub directory is a qualified package path
  when an EDKII Package can be found under it.
2. Add SetPackagesPath.bat and SetPackagesPath.sh, these scripts call
  GetPackagesPath.py to collect all package paths under specified
  directories and append them to PACKAGES_PATH environment variable.

Cc: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Amy Chan <amy.chan@intel.com>
Signed-off-by: Heng Luo <heng.luo@intel.com>
---
 BaseTools/Scripts/GetPackagesPath.py  | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 BaseTools/Scripts/SetPackagesPath.bat | 33 +++++++++++++++++++++++++++++++++
 BaseTools/Scripts/SetPackagesPath.sh  | 40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 167 insertions(+)

diff --git a/BaseTools/Scripts/GetPackagesPath.py b/BaseTools/Scripts/GetPackagesPath.py
new file mode 100644
index 0000000000..82d66a6106
--- /dev/null
+++ b/BaseTools/Scripts/GetPackagesPath.py
@@ -0,0 +1,94 @@
+## @file
+# Get all recursive package paths from special directories.
+#
+# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+
+import os
+import glob
+import argparse
+
+#
+# Globals for help information
+#
+__prog__ = 'GetPackagesPath.py'
+__copyright__ = 'Copyright (c) 2020, Intel Corporation. All rights reserved.'
+__description__ = 'Gets all recursive package paths in specified directory.\n'
+
+def list_packages_path(root):
+    """ Gets all recursive package paths in specified directory.
+        A directory is a package path if it satisfies conditions below:
+        1. it is a directory
+        2. it is not an EDK II Package. An EDK II Package (directory) is
+           a directory that contains an EDK II package declaration (DEC) file.
+        3. it contains at least one first level EDK II Package.
+        Note: A directory is not package path but its subdirectory could be.
+        Example: edk2-platforms/Features is not package path
+        but edk2-platforms/Features/Intel is.
+
+        :param root: The specified directory to find package paths in it
+        :type root: String
+        :returns: Return all recursive package paths
+        :rtype: String list
+    """
+
+    if (not os.path.exists(root)) or (not os.path.isdir(root)):
+         return []
+
+    if glob.glob(os.path.join(root, '*.dec')):
+        # it is an EDK II Package
+        return []
+
+    paths = []
+    contain_package = False
+    for filename in os.listdir(root):
+        filepath = os.path.join(root, filename)
+        if os.path.isdir(filepath):
+            if glob.glob(os.path.join(filepath, '*.dec')):
+                # it is an EDK II Package
+                contain_package = True
+            else:
+                # gets package paths for subdirectory if it is not package
+                paths = paths + list_packages_path(filepath)
+
+    if contain_package:
+        # root is a package path because it contains EDK II Package
+        # in first level folder, inset it to head of list
+        paths.insert(0, root)
+
+    # return package paths
+    return paths
+
+def get_packages_path(directories):
+    """ For each direcory in directories, gets all recursive package paths
+        in this directory and joins them into one string.
+
+        :param directories: the list of directory
+        :type directories: String list
+        :returns: Return string of package paths
+        :rtype: String
+    """
+
+    packages_path = ''
+    for directory in directories:
+        directory = os.path.abspath(directory)
+        paths = list_packages_path(directory)
+        for path in paths:
+            if packages_path == '':
+                packages_path = path
+            else:
+                packages_path += os.pathsep + path
+    return packages_path
+
+if __name__ == '__main__':
+    # Create command line argument parser object
+    parser = argparse.ArgumentParser(
+            prog=__prog__,
+            description=__description__ + __copyright__,
+            conflict_handler='resolve'
+    )
+    parser.add_argument('directory', nargs='+',
+            help='Specified directory where package packages are got from')
+    args = parser.parse_args()
+    print(get_packages_path(args.directory))
diff --git a/BaseTools/Scripts/SetPackagesPath.bat b/BaseTools/Scripts/SetPackagesPath.bat
new file mode 100644
index 0000000000..a8c45612c9
--- /dev/null
+++ b/BaseTools/Scripts/SetPackagesPath.bat
@@ -0,0 +1,33 @@
+@REM @file
+@REM Windows batch file to set PACKAGES_PATH environment
+@REM
+@REM Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
+@REM SPDX-License-Identifier: BSD-2-Clause-Patent
+@REM
+@REM This script calls GetPackagesPath.py to collect all package paths under
+@REM specified directories and append them to PACKAGES_PATH environment
+@REM variable. A sub directory is a qualified package path when an EDKII
+@REM Package can be found under it.
+
+@echo off
+@if /I "%1"=="" @goto Usage
+@if /I "%1"=="-h" @goto Usage
+@if /I "%1"=="--help" @goto Usage
+@if /I "%1"=="/?" @goto Usage
+
+for /f %%i in ('python %EDK_TOOLS_PATH%\Scripts\GetPackagesPath.py %*') do (
+    if defined PACKAGES_PATH (
+        set "PACKAGES_PATH=%PACKAGES_PATH%;%%i"
+    ) else (
+        set "PACKAGES_PATH=%%i"
+    )
+)
+@goto End
+
+:Usage
+@echo Usage: SetPackagesPath.bat directory [directory ...]
+@echo Copyright(c) 2020, Intel Corporation. All rights reserved.
+@echo Options:
+@echo   --help, -h     Print this help screen and exit
+
+:End
diff --git a/BaseTools/Scripts/SetPackagesPath.sh b/BaseTools/Scripts/SetPackagesPath.sh
new file mode 100644
index 0000000000..e3e337254e
--- /dev/null
+++ b/BaseTools/Scripts/SetPackagesPath.sh
@@ -0,0 +1,40 @@
+
+#
+# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+# This script calls GetPackagesPath.py to collect all package paths under
+# specified directories and append them to PACKAGES_PATH environment
+# variable. A sub directory is a qualified package path when an EDKII
+# Package can be found under it.
+#
+# Note: This script must be \'sourced\' so the environment can be changed:
+# source SetPackagesPath.sh
+# . SetPackagesPath.sh
+
+function Usage()
+{
+    echo "Usage: source SetPackagesPath.sh directory [directory ...]"
+    echo "Copyright(c) 2020, Intel Corporation. All rights reserved."
+    echo "Options:"
+    echo "  --help, -h     Print this help screen and exit"
+    echo "Please note: This script must be \'sourced\' so the environment can be changed."
+    echo ". SetPackagesPath.sh"
+    echo "source SetPackagesPath.sh"
+}
+
+function SetEnv()
+{
+    local paths=$(python $EDK_TOOLS_PATH/Scripts/GetPackagesPath.py $@)
+    if [ "$PACKAGES_PATH" ]; then
+        PACKAGES_PATH=$PACKAGES_PATH:$paths
+    else
+        PACKAGES_PATH=$paths
+    fi
+}
+
+if [ $# -eq 0 -o "$1" == "-h" -o "$1" == "--help" -o "$1" == "/?" ]; then
+    Usage
+else
+    SetEnv $@
+fi
-- 
2.24.0.windows.2


             reply	other threads:[~2020-04-02  8:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-02  8:08 Heng Luo [this message]
2020-04-02 15:59 ` [edk2-devel] [PATCH] BaseTools/Scripts: Add scripts to set PACKAGES_PATH environment Sean
2020-04-07  2:15   ` Ni, Ray
2020-04-07  3:37     ` Bob Feng
2020-04-07  5:56       ` Sean

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=20200402080809.2147-1-heng.luo@intel.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