Issue about loading modules from different directory

Started by csfreebird, February 25, 2013, 07:11:50 PM

Previous topic - Next topic

csfreebird

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

(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.

;; 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:

;; 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:

./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

#1
;; env.lsp
(println (real-path "env_ubuntu.lsp"))


Must define a root path then "changer-dir" to it .......
Welcome to a newlisper home:)

http://www.cngrayhat.org\">//http://www.cngrayhat.org

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:

;; 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?