All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] [yocto-ab-helper] utils.py: Resolved unicode data expansion
@ 2018-07-03  1:44 Aaron Chan
  2018-07-03 13:25 ` Richard Purdie
  0 siblings, 1 reply; 6+ messages in thread
From: Aaron Chan @ 2018-07-03  1:44 UTC (permalink / raw)
  To: richard.purdie, yocto; +Cc: Aaron Chan

Updated patch to trigger handlestr() when unicode string is found
during iteration json.loads(config.json). Unicode and list with data
expansion were not handled hence adding this patch to handle conversion.
Added a debug message to dump pretty json data populated to ourconfig[c].

e.g "REPO_STASH_DIR" read as ${BASE_HOMEDIR}/git/mirror, where it should be
    "REPO_STASH_DIR" as /home/pokybuild/git/mirror

Signed-off-by: Aaron Chan <aaron.chun.yew.chan@intel.com>
---
 scripts/utils.py | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/scripts/utils.py b/scripts/utils.py
index d26cd0c..32caa4f 100644
--- a/scripts/utils.py
+++ b/scripts/utils.py
@@ -152,11 +152,13 @@ def loadconfig():
         except:
             pass
 
-    def handlestr(config, ourconfig, c):
+    def handlestr(config, ourconfig, c, debug=False):
         if not c in ourconfig:
             ourconfig[c] = config[c]
         if isinstance(config[c], str):
             resolvexp(r"(\${(.+)})", config, c)
+        if debug:
+            print(json.dumps(ourconfig[c], indent=4, sort_keys=True))
         
     ourconfig = {}
     for f in files.split():
@@ -168,6 +170,8 @@ def loadconfig():
             for c in config:
                 if isinstance(config[c], dict):
                     handledict(config, ourconfig, c)
+                elif isinstance(config[c], str):
+                    handlestr(config, ourconfig, c)
                 else:
                     ourconfig[c] = config[c]
 
-- 
2.7.4



^ permalink raw reply related	[flat|nested] 6+ messages in thread
* [PATCH] [yocto-ab-helper] utils.py: Resolved unicode data expansion
@ 2018-07-04  6:07 Aaron Chan
  0 siblings, 0 replies; 6+ messages in thread
From: Aaron Chan @ 2018-07-04  6:07 UTC (permalink / raw)
  To: richard.purdie, yocto; +Cc: Aaron Chan

Patch fix on utils:getconfig:expandresult function to handle the expansion
This patch is to add a condition to handle unicode entries as dict & list
have been handled during expandresult.

janitor/clobberdir: [line 46]: changes
from : trashdir = ourconfig["TRASH_DIR"]
to   : trashdir = utils.getconfig("TRASH_DIR", ourconfig)

scripts/utils.py:  [line 41-47]: added
getconfig invokes only unicode entries and handles the data expansions.
This allows ${BUILDDIR} to be expanded, to retain ${BUILDDIR} in ourconfig[c],
we should never invoke utils.getconfig("BUILDDIR", ourconfig) in our scripts
unless we intend to change the BUILDDIR paths.

Signed-off-by: Aaron Chan <aaron.chun.yew.chan@intel.com>
---
 janitor/clobberdir | 5 ++---
 scripts/utils.py   | 8 ++++++++
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/janitor/clobberdir b/janitor/clobberdir
index 5dab5af..5e04ed7 100755
--- a/janitor/clobberdir
+++ b/janitor/clobberdir
@@ -43,11 +43,10 @@ if "TRASH_DIR" not in ourconfig:
     print("Please set TRASH_DIR in the configuration file")
     sys.exit(1)
 
-trashdir = ourconfig["TRASH_DIR"]
+trashdir = utils.getconfig("TRASH_DIR", ourconfig)
 
 for x in [clobberdir]:
     if os.path.exists(x):
         trashdest = trashdir + "/" + str(int(time.time())) + '-'  + str(random.randrange(100, 100000, 2))
         mkdir(trashdest)
-        subprocess.check_call(['mv', x, trashdest])
-
+        subprocess.check_call(['mv', x, trashdest])
\ No newline at end of file
diff --git a/scripts/utils.py b/scripts/utils.py
index db1e3c2..373f8de 100644
--- a/scripts/utils.py
+++ b/scripts/utils.py
@@ -26,6 +26,7 @@ def configtrue(name, config):
 # Handle variable expansion of return values, variables are of the form ${XXX}
 # need to handle expansion in list and dicts
 __expand_re__ = re.compile(r"\${[^{}@\n\t :]+}")
+__expansion__ = re.compile(r"\${(.+)}")
 def expandresult(entry, config):
     if isinstance(entry, list):
         ret = []
@@ -37,6 +38,13 @@ def expandresult(entry, config):
         for k in entry:
             ret[expandresult(k, config)] = expandresult(entry[k], config)
         return ret
+    if isinstance(entry, unicode):
+        entry = str(entry)
+        entryExpand = __expansion__.match(entry).group(1)
+        if entryExpand:
+            return entry.replace('${' + entryExpand + '}', config[entryExpand])
+        else:
+            return entry
     if not isinstance(entry, str):
         return entry
     class expander:
-- 
2.7.4



^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-07-05  5:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-03  1:44 [PATCH] [yocto-ab-helper] utils.py: Resolved unicode data expansion Aaron Chan
2018-07-03 13:25 ` Richard Purdie
2018-07-04  6:42   ` Chan, Aaron Chun Yew
2018-07-04  7:51     ` Richard Purdie
2018-07-05  5:38       ` Chan, Aaron Chun Yew
2018-07-04  6:07 Aaron Chan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.