Common Lisp:使用与lisp进程不同的工作目录启动子进程

时间:2012-04-06 21:14:57

标签: python bash common-lisp dos ccl

假设我有一个目录A和子目录B.我进入A并启动lisp。在那个lisp进程中,我想启动一个Python子进程,其中Python将B视为其当前工作目录。 lisp进程需要在A中有cwd,而python进程在B中应该有cwd。我如何以跨平台,简单的方式执行此操作?

我正在寻找适用于CCL和SBCL的解决方案(可能使用'运行程序功能),适用于Windows,Linux和OS X.

我查看了CCL运行程序文档,但我没有看到更改已启动进程的cwd的方法。

我查看了Python命令行参数,但我没有看到会改变python进程的cwd。

我想到了'cd B'的运行程序调用; python ...',但我不确定它是如何工作的,因为它真的运行了两个程序; cd,然后是python。

Python代码作为输入(作为文件)提供,因此我无法更改任何代码(通过添加os.chdir()调用或类似代码)。

作为子进程启动python输入文件的python包装器文件并不理想,因为我正在发送stdin并监听由lisp启动的python进程的stdout。在lisp和篡改输入文件的python进程之间添加另一个子进程意味着我需要做很多stout / stdin中继,并且我觉得这会很脆弱。

krzysz00的方法非常有效。由于目录更改是在lisp中处理的,因此在启动python进程之前,此方法将用于在不同的子目录中启动其他进程(而不仅仅是python)。

对于文档,这是我的代码使用krzsz00的方法,适用于SBCL& CCL。请注意,它使用Hoyte的defmacro!来自Let Over Lambda的宏,可以轻松避免不必要的变量捕获:

#+:SBCL
(defun cwd (dir)
  (sb-posix:chdir dir))

(defun getcwd ()
  #+SBCL (sb-unix:posix-getcwd)
  #+CCL (current-directory))

(defmacro! with-cwd (dir &body body)
  `(let ((,g!cwd (getcwd)))
     (unwind-protect (progn
                       (cwd ,dir)
                       ,@body)
     (cwd ,g!cwd))))

用法:

(with-cwd "./B"
  (run-program ...))

3 个答案:

答案 0 :(得分:6)

要运行外部程序(如可移植的python进程),请参阅external-program。要更改当前工作目录,请使用文件http://files.b9.com/lboot/utils.lisp中稍微修改过的(公共域)函数cwd,该函数将在下面复制。

(defun cwd (&optional dir)
  "Change directory and set default pathname"
  (cond
   ((not (null dir))
    (when (and (typep dir 'logical-pathname)
           (translate-logical-pathname dir))
      (setq dir (translate-logical-pathname dir)))
    (when (stringp dir)
      (setq dir (parse-namestring dir)))
    #+allegro (excl:chdir dir)
    #+clisp (#+lisp=cl ext:cd #-lisp=cl lisp:cd dir)
    #+(or cmu scl) (setf (ext:default-directory) dir)
    #+cormanlisp (ccl:set-current-directory dir)
    #+(and mcl (not openmcl)) (ccl:set-mac-default-directory dir)
    #+openmcl (ccl:cwd dir)
    #+gcl (si:chdir dir)
    #+lispworks (hcl:change-directory dir)
    #+sbcl (sb-posix:chdir dir)
    (setq cl:*default-pathname-defaults* dir))
   (t
    (let ((dir
       #+allegro (excl:current-directory)
       #+clisp (#+lisp=cl ext:default-directory #-lisp=cl lisp:default-directory)
       #+(or cmu scl) (ext:default-directory)
       #+sbcl (sb-unix:posix-getcwd/)
       #+cormanlisp (ccl:get-current-directory)
       #+lispworks (hcl:get-working-directory)
       #+mcl (ccl:mac-default-directory)
       #-(or allegro clisp cmu scl cormanlisp mcl sbcl lispworks) (truename ".")))
      (when (stringp dir)
    (setq dir (parse-namestring dir)))
      dir))))

结合这两个函数,您需要的代码是:

(cwd #p"../b/")
(external-program:start "python" '("file.py") :output *pythins-stdout-stream* :input *pythons-stdin-stream*)
(cwd #p"../a/")

这将cd到B,运行python进程就好像通过python file.py &一样,将python进程的stdin / stdout发送到指定的流(查看external-program文档以获取更多详细信息),最后执行另一个{l}进程返回到cwd。如果lisp进程应该等到python进程完成,请使用external-program:run而不是external-program:start

答案 1 :(得分:1)

我最终将krzysz00的建议写入了一个可以找到的包here

然后有人指出UIOP附带getcwdchdir。如果你有一个相当新的lisp,那么你的asdf版本将包含UIOP。

答案 2 :(得分:0)

我不知道lisp是什么,但这可行吗?

  import subprocess
  subprocess.Popen('python myscript.py', cwd='B')

http://docs.python.org/library/subprocess.html