Issue about loading modules from different directory

Q&A's, tips, howto's
Locked
csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

Issue about loading modules from different directory

Post by csfreebird »

my setup.lsp loads env.lsp from different folder using relative path, codes in setup.lsp look like so:

Code: Select all

(load "../newlisp_armory/codes/env/env.lsp")
(envex:load-env-tool)
(envex:set-sys-env "CLOUD_ENGINE_HOME" cloud-engine-home)
there are two files under newlisp_armory/codes/env/ folder, env.lsp and env_ubuntu.lsp
the load-env-tool function defined in env.lsp will get the os type and then load env_ubuntu.lsp because I am in Ubuntu. Below is the codes of env.lsp.

Code: Select all

;; env.lsp
(context 'envex)

;; an exception will be thrown out if the OS is neither Linux nor Windows
;; note: only Ubuntu and Windows 7 are supported for now
(define (load-env-tool)
  (if
   (= ostype "Linux") (load "env_ubuntu.lsp")
   (= ostype "Win32") (load "env_win.lsp")
   (throw-error (append "env tool doesn't support this OS for now, ostype:" ostype))
   ))
Now the function set-sys-env is defined in env_ubuntu.lsp file now:

Code: Select all

;; env_ubuntu.lsp
(context 'envex)

;; save the environment variable into /etc/environment
;; need root permission
;; also export the environment for subsequently executed commands
(define (set-sys-env str-name str-value)
  (env str-name str-value)
  (unless (regex str-name (read-file "/etc/environment"))
 	  (unless (append-file "/etc/environment" (append "\n" str-name "=\"" str-value "\"\n"))
 		  (throw-error (append "write /etc/environment failed, error:" (string (sys-error)))))
 	  )
  )
But when executing setup.lsp, I got an error message as follows:

Code: Select all

./setup.lsp 

ERR: problem accessing file in function load : "env_ubuntu.lsp"
called from user defined function envex:load-env-tool
I guess this problem is about the file location, how to solve this?

winger
Posts: 46
Joined: Wed Mar 14, 2012 7:31 am

Re: Issue about loading modules from different directory

Post by winger »

Code: Select all

;; env.lsp 
(println (real-path "env_ubuntu.lsp"))
Must define a root path then "changer-dir" to it .......
Last edited by winger on Thu Feb 28, 2013 7:57 pm, edited 1 time in total.
Welcome to a newlisper home:)
http://www.cngrayhat.org

csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

Re: Issue about loading modules from different directory

Post by csfreebird »

because the real-path is for the folder which contains setup.lsp, env.lsp can't find the env_ubunt.lsp file correctly.
So println will show nil:

Code: Select all

;; env.lsp
(context 'envex)

;; an exception will be thrown out if the OS is neither Linux nor Windows
;; note: only Ubuntu and Windows 7 are supported for now
(define (load-env-tool)
  (println (append "path:" (string (real-path "env_ubuntu.lsp"))))
The output is below:
path:nil

We can't put all module files into one folder in real project. My question is how to find the moudle file's location instead of the current folder of process. My module might need to read configuration file in its folder.

change-dir will change the current folder, but it might cause other codes out of my module to not work.

How do you think?

Locked