[racket] define-runtime-path and raco distrib

55 views
Skip to first unread message

Kieron Hardy

unread,
Jul 16, 2012, 5:13:45 PM7/16/12
to users
Hi all,

How do I get a run-time path in the executable created by 'raco distrib', that is the same, relative to the directory containing the source racket code and the executable created by 'raco exe'?

When I execute the following code (modified from the documentation), I observe that the directory for the data file for the executable generated by 'raco distrib':

   read-data: data-file:"...\test-path\test-distrib\.\lib\plt\test-path\exts\ert\...\test-path\data.txt"

is very different from the directory used by both the source module and the executable generated by 'raco exe': 

   read-data: data-file:"...\test-path\data.txt".

How do I make the ".\lib\plt\test-path\exts\ert\...\test-path\" go away and have the data file be packaged in the same directory as the executable?

I need the program to access a data file that I expect an end-user to modify, and the additional subdirs between where the program and data file are a bit of a problem. Perhaps I should be using some other approach?

Thanks in advanced for any advice.

Cheers,

Kieron.

****

#lang racket

; Access a file "data.txt" at run-time that is originally
; located in the same directory as the module source file:
(define-runtime-path data-file "data.txt")
(define (read-data)
  (with-input-from-file data-file
    (lambda ()
      (read-bytes (file-size data-file)))))

Matt Jadud

unread,
Jul 17, 2012, 8:39:28 AM7/17/12
to Kieron Hardy, users
Hi Kieron,

On Mon, Jul 16, 2012 at 5:13 PM, Kieron Hardy <kieron...@gmail.com> wrote:
> How do I get a run-time path in the executable created by 'raco distrib',
> that is the same, relative to the directory containing the source racket
> code and the executable created by 'raco exe'?

I used something along the lines of

(define (UMBRELLA)
(simplify-path
(build-path
(find-system-path 'run-file) 'up 'up)))

which allowed me to run my application both as a double-clickable Mac
app as well as from the command line with

racket foo.rkt

I think some relevant (recent) discussion on this was here:

https://groups.google.com/d/topic/racket-users/CqTiuhY6Aks/discussion

By "I think" I mean "I asked this roughly a week ago, and I think it
answers your question.

Cheers,
Matt
____________________
Racket Users list:
http://lists.racket-lang.org/users

Kieron Hardy

unread,
Jul 17, 2012, 6:09:47 PM7/17/12
to Matt Jadud, users
Thanks for the hint Matt, I noodled around a bit and the code below is what I've come up with, posted here in case it's of use to others.

Basically, to find the data file:
If the (find-system-path 'run-file) or (find-system-path 'exec-file) refer to a (Windows) racket binary (racket.exe, gracket.exe, DrRacket.exe, or the CGC variants) then the source code is being executed, so use the directory containing the source module,
otherwise a generated (i.e. with raco exe) binary is being executed, so use the directory containing the (find-system-path 'run-file).

I've tested running the source and binary in every fashion I could think (from DrRacket, from command line with racket.exe and pals, from windows icon, from windows shortcut icon, from various different directories) and the data file is located reliable.

Unfortunately, I could not come up with a way to use define-runtime-path, and so I have lost the ability for 'raco distrib' to bundle up the data file automatically, and so will have to add a copy of the data file to the directory after 'raco distrib' completes. (It seems ...\collects\compiler\distribute.rkt copies and changes the names (paths) of the runtime files into the distribution directory as part of assemble-distribution procedure and changing the destination location doesn't look trivial). 

Cheers,

Kieron.

****

#lang racket

(require racket/runtime-path)
(require syntax/location)


; Access a file "data.txt" at run-time that is
; located in the same directory as the module source file
; if executing with racket binaries then use the source module location
; otherwise use the location of the compiled exe
(define (get-data-file-name)
  ; check for Windows binaries - change for other OS's
  (define DrRacket? (regexp-match? #rx".*(?i:DrRacket)(?i:cgc)?\\.(?i:exe)" (find-system-path 'run-file)))
  (define racket? (regexp-match? #rx".*(?i:racket)(?i:cgc)?\\.(?i:exe)" (find-system-path 'exec-file)))
  (let-values
    ([(p f b)
      (split-path
        (path->complete-path
          (if (or racket? DrRacket?)
            (quote-module-name)
            (find-system-path 'run-file))))])
;     (printf "get-data-file-name: returning:\"~a\"~n" (build-path p "data.txt"))
     (build-path p "data.txt")
     ))

(define (read-data)
  (let ([data-file (get-data-file-name)])
    (printf "read-data: data-file:\"~a\"~n" data-file)

    (with-input-from-file data-file
      (lambda ()
        (read-bytes (file-size data-file))
        ))))

(read-data)

Matt Jadud

unread,
Jul 17, 2012, 7:39:40 PM7/17/12
to Kieron Hardy, users
Hi Kieron,

On Tue, Jul 17, 2012 at 6:09 PM, Kieron Hardy <kieron...@gmail.com> wrote:
> Unfortunately, I could not come up with a way to use define-runtime-path,
> and so I have lost the ability for 'raco distrib' to bundle up the data file

Glad to help. I ended up with a little script to do the compiling and
copying, because I ran into the same define-runtime-path problems.
Reply all
Reply to author
Forward
0 new messages