【问题标题】:How do I open a file relative to the source files directory in Guile Scheme?如何在 Guile Scheme 中打开相对于源文件目录的文件?
【发布时间】:2021-05-29 11:24:41
【问题描述】:

我正在为我正在处理的 Python 项目编写 Guix 开发人员环境定义,它看起来像这样:

(use-modules (gnu packages)
             (gnu packages python)
             (gnu packages python-crypto)
             (gnu packages python-web)
             (gnu packages python-xyz)
             (ice-9 rdelim)
             ((guix licenses) #:prefix license:)
             (guix build-system python)
             (guix download)
             (guix packages))

(define (requirements-file->guix-packages filename)
  (let* ((requirements-text (with-input-from-file filename read-string))
         (requirements-list (string-split requirements-text #\newline)))
    (map (lambda (requirement)
           (specification->package (string-append "python-" requirement)))
         requirements-list)))

(define internsidor-deps      (requirements-file->guix-packages "requirements.in"))
(define development-tools     (list python-pip python-wrapper python-virtualenv python-jedi))

(append internsidor-deps development-tools)

现在,如果站在项目文件夹的根目录(requirements.inrequirements-prod.in 所在的位置)进行评估,这段代码可以正常工作。但是如果用户移动到一个子文件夹并像这样运行代码

guix environment --ad-hoc --load='../development-environment.scm'

他们会发现自己出现以下错误

guix environment: error: while evaluating '../development-environment.scm', it failed opening 'requirements.in' with the error: No such file or directory

(或者类似的错误,我已经修改了我的 Guix 以便为这个失败提供更好的错误消息。)

那么我的问题是:如何修改对with-input-from-file 的调用,以使路径相对于Scheme 文件而不是shell 的$PWD

【问题讨论】:

    标签: scheme guile


    【解决方案1】:

    您可以使用(dirname (current-filename)) 获取当前文件的目录。另见https://www.gnu.org/software/guile/manual/guile.html#Source-Properties

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-13
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 2010-09-19
      • 2017-03-03
      • 1970-01-01
      相关资源
      最近更新 更多