I'm just trying to write to a file with the following function:
(defun test-save ()
(with-open-file (stream "test.txt" :if-does-not-exist :create)
(format stream "success!")))
However, entering in (test-save) generates the following stuff:
What am I doing wrong here?
I'm using Cusp for Eclipse with SBCL on a Mac, if it matters.
UPDATE: now this new error:
And the repl:
COMMON-LISP-USER>
(with-open-file (stream "test.txt" :direction :output
:if-does-not-exist :create)
(format stream "success!"))
error opening #P"/Applications/eclipse/Eclipse.app/Contents/MacOS/test.txt":
File exists
[Condition of type SB-INT:SIMPLE-FILE-ERROR]
0: [ABORT] Return to SLIME's top level.
1: [TERMINATE-THREAD] Terminate this thread (#<THREAD "repl-thread" {12539CB1}>)
]> 0
UPDATE 2:
Solved! I just had to use :if-exists :supersede
Try adding :direction :output to create an output file stream:
(with-open-file (stream "test.txt" :direction :output
:if-does-not-exist :create)
(format stream "success!"))
Related
To be brief with the requirement:
My folder structure looks as below:
Requirement & Challenge: When I try to read the FixedTestFile.dat
from different features, the read method appends the path wrong followed by the path of feature file and hence fails.
Tried with the following ways to read the file but failed with error.
And multipart file myFile = { read: 'src/test/java/e2e/common/data/FixedTestFile.dat', filename: #(randomFileName), contentType: 'text/plain' }
Logs:
Requirement: I have to read file "FixedTestFile.dat" from different features located in folders individual and testScenarios while using in Mutlipart file myFile.
Note : I tried with using classpath: and file: But no luck
And multipart file myFile = { read: 'classpath:e2e/common/data/FixedTestFile.dat', filename: #(randomFileName), contentType: 'text/plain' }
Tried using a variable for filename with path and tried using this as a parameter in read. Did not work.
* string myDataFileWithPath = "\'"+"../common/data/"+fileName+".dat"+"\'"
And multipart file myFile = { read: myDataFileWithPath, filename: #(randomFileName), contentType: 'text/plain' }
So the question is: "How to read a file from two features in another location?" Precisely for a multipart file upload.
Am I missing something or it is a bug while reading files?
I am just getting started with Clojurescript. I wrote some clojurescript code to use the shared aws credentials file to initialize S3 client and list buckets . However my code does not work.
(defn -main [arg1 arg2]
(println "hello")
(let[ creds (new AWS/SharedIniFileCredentials #js{:profile "superman"})
_ (AWS/config.update creds)
; dump out the accesskey to check if it has the correct profile
_ (AWS/config.getCredentials (fn [err] (if (nil? err) (do
(println "its good")
(println AWS/config.credentials.accessKeyId)))))
s3 (new (.-S3 AWS ))
] (.listBuckets s3 (fn[err buckets] (println "err: " err) (println buckets) )) ))
The AWS/config.getCredentials in the above does pick up the correct profile as seen from (println AWS/config.credentials.accessKeyId). The listbuckets code throws the following error:
#object[NodeError TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be of type string or an instance of Buffer, TypedArray, DataView, or KeyObject. Received undefined]
I have Google AWS SDK S3 clojuresript AND is the only link I found . I used that to configure the S3 client but that does not seem to work
I would appreciate any help.
I checked it and the problem seems to be that SDK expects the credentials to be set before anything else, before instantiating the S3 client.
The following works for me on a minimal project with shadow-cljs:
(ns server.main
(:require ["aws-sdk" :as aws]))
(defn main! []
(println "App loaded...")
(let [creds (aws/SharedIniFileCredentials. #js {:profile "example-profile"})
_ (set! (.-credentials aws/config) creds)
s3 (aws/S3.)]
(.listBuckets s3 (fn [err data]
(if err
(println "ERROR:" err)
(println "OK:" data))))))
when I run it:
$ node target/main.js
App loaded...
OK: #js {:Buckets #js [#js {:Name dummy-test-bucket, :CreationDate #inst "2019-05-05T17:32:17.000-00:00"} #js {:Name mydomain.com, :CreationDate #inst "2018-06-19T04:16:10.000-00:00"}], :Owner #js {:DisplayName username, :ID f63f30bc25ab3609b8d3b5be6b3a872dd2c9f7947b2d509e2338357d93e74f2}}
The key was on this answer: https://stackoverflow.com/a/33175424/483566
Following this SO post, I would like to print the value of the preconditions in my function. However it fails for me in the following case (probably destructuring) :
I have a dir? helper function (feel free to skip this one) :
(defn dir? [s]
"returns true if the string passed is is an existing directory"
(->> (clojure.java.io/file s)
((juxt #(.exists %) #(.isDirectory %)))
(every? true?)))
It works just fine, and using the is macro, I get some nice error messages where I can see both the test and the parameters that were passed :
(is (dir? (io/file "resources/static"))) ;; => true
(is (dir? (io/file "resources/statice"))) ;; typo, error below
FAIL in clojure.lang.PersistentList$EmptyList#1
(boot.user4515592986834245937.clj:86) expected: (dir? (io/file
"resources/statice")) actual: (not (dir? #object[java.io.File
0x6730a420 "resources/statice"]))
However, when trying to use it in the precondition :pre, I get an ugly error :
(defn make-something
[&{:keys [dir]
:or {dir "default-dir"}}]
{:pre [(is (dir? (clojure.java.io/file dir)))]}
;;... do something with these
)
(make-something :dir "resources/statices") ;; doesn't exist
clojure.lang.Compiler$CompilerException: java.lang.AssertionError:
Assert failed: (is (dir? (io/file dir))),
compiling:(boot.user4515592986834245937.clj:80:12)
java.lang.AssertionError: Assert failed: (is (dir? (io/file dir)))
How can I get a nice error message in my function just like the one above ?
In case it matters, I am using Clojure 1.7.
You need to check your code (dir? function). Following snippet works for me:
(require '[clojure.java.io :as io])
(defn dir? [s]
(let [dir (io/file s)]
(and (.exists dir)
(.isDirectory dir))))
(defn make-something
[& {:keys [dir] :or {dir "default-dir"}}]
{:pre [(is (dir? dir))]}
(println dir))
(make-something :dir "/tmp")
out => /tmp
ret => nil
(make-something :dir "/xxxx")
FAIL in clojure.lang.PersistentList$EmptyList#1 (form-init3332271312167175068.clj:1)
expected: (dir? dir)
actual: (not (dir? "/xxxx"))
AssertionError Assert failed: (is (dir? dir)) user/make-sth (form-init3332271312167175068.clj:1)
I have the below code which is extracted by using stings command.
I need to edit the code and compile. But I do not have any idea how to proceed.
ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
%s/CMSlog_%02d%02d.log
%.20s %7d %02d:%02d:%02d
%.2000s
%s/CMSrecvlog_%02d%02d.log
%.20s %7d %02d:%02d:%02d
%.2000s
%0*lu
%c%c%.6s%.8s
%s/MacEB13%.4s
EB13 path =[%s]
Mac EB13 ERR [%s] open error
%s/EB13%.4s
stderr [%s] open error
%s/MacEB11%.4s
stderr [%s] open error
%s/EB11%.4s
stderr [%s] open error
mac path = [%s]
%s/MacEB21%.4s
stderr [%s] open error
%s/EB21%.4s
stderr [%s] open error
MacCalculate
%s/MacEB13%.4s
%s/MacEB21%.4s
%s/MacEB11%.4s
MacCalculate Error idx[%d]
Mac open stderr [%s] open error
%2d%2d
%2d%2d%2d
%4d%2d%2d
%02d%02d%02d
%04d%02d%02d
%2d%2d
%02d%02d
%02d
%04d
yymmdd
%02d%02d%02d
mmddyy
%02d%02d%02d
ddmmyy
%02d%02d%02d
yyyymmdd
%04d%02d%02d
mmddyyyy
%02d%02d%04d
ddmmyyyy
%02d%02d%04d
hhmmss
%02d:%02d:%02d
%s database open error !
%s file open error !
%s table open error !
Parameter input error !
Can't execute %s !
Can't describe %s !
Can't declare %s !
Can't prepare %s !
%s data error !
%s error !
%.2d
p_get_etra_date dchg = %d error
19%.6s
20%.6s
19%.4s
20%.4s
%02d%02d%02d
Usage [%s] kind sendfile cfgfile
/data/applications/PgiWebApp/files/current/send/kftc/log
/data/applications/PgiWebApp/files/current/send/kftc/log
make directory error!!!
log directory is not exist!!
make new directory!!
get_config_file error[%.20s]
SENDER_NAME[%s]
PASS_WD[%s]
MAC1[%s]
MAC2[%s]
IP = [%s]
PORT = [%d]
KFTC
RKFTC
BKFTC
LKFTC
DKFTC
PKFTC
kind type error!!! [%s]
GiganCode [%s]
path [%.200s] NOT EXISTS !!
job_dir = [%s]
00%.4s
mac_day = [%s]
db_filename = [%s]
EB13
EB21
EB11
Mac job_kind error =[%d]
job_kind [%d]
22SENDER_NAME[%s]
PASS_WD[%s]
MAC1[%s]
MAC2[%s]
MAC LASTTotalSum = [%.10s]
600-001 respcode error %.3s
FAIL 600-001 respcode error!! check log file.
write_data=[%s]
new_protocol continue.. [%d]
600-003 respcode error %.3s
FAIL 600-003 respcode error!! see log file.
transfer complete
600-004 respcode error %.3s
FAIL 600-004 respcode error!! see log file.
mv %s/%s %s/%s
SUCCESS[%s]
command[%s]
SUCCESS
All success
write buf= [%.*s] len [%d]
write error [%.150s] ret %d sock %d err %d
socket write error!!
read error len %d
socket read error!! check log file!!
CDATA=[%.4s]
11READ ERROR.. len %d
socket read error!! check log file!!
data = [%.*s] r_length=[%d]
msgtype = [%.4s]
%s/%.8s
fopen error [%s]
File open error!! check [%s] file!!
630 respcode error %.3s
630 respcode error!! check log file.
ex_size[%.12s] rec640[%.12s]
fu_size is [%d]
%04ld
%04ld
%04ld
tran_byte = [%.4s]
gubun_code = [%.3s]
gigan_code = [%.8s]
%04d
tran_code = [%.4s]
%.8s
%.8s
%.8s
respcode = [%.3s]
%04d
%04d
%03d
%03ld
%04d
%03d
%04d
%04d
record_num =[%d], idx=[%d], sum_total=[%d]
signal [%d] encountered !!!!
pp value is [%d] in fseek
FLOW ret =[%d] blok_sz =[%d] block_no =[%d] teq_no =[%d]
file_pointer =[%d]
read error len %d
socket read error!!
temp=[%.4s]
r_length = [%ld]
READ ERROR.. len %d
socket read error!!
0300
Unknown Msgtype [%.4s] So I am Dead...
unknown msgtype!! check log file.
%02d%02d%02d
%02d%02d%02d
%.8s
%.8s
%.8s
%.12ld
%.4ld
error on open socket
oops: --> socket error
600-004 respcode error %.3s
600-004 respcode error!! check log file.
Check config file
File=[%s],
no search env file
no search env file
no search env file =
GIGWAN_CODE%d
no search env file
SEND_PATH%d
no search env file
SEND_ERR_PATH%d
no search env file
HISTORY_SEND_PATH%d
no search env file
MAC_SEND_PATH%d
no search env file
no search env file
no search env file
no search env file
no search env file
no search env file
no search env file
no search env file
no search env file
no search env file
no search env file
no search env file
no search env file
no search env file
no search env file
no search env file
no search env file
no search env file
%.9s_SENDER_NAME_LEN
no search env file
%.9s_SENDER_NAME
no search env file
%.9s_PASS_WD
no search env file
%.9s_MAC_KEY1
no search env file
%.9s_MAC_KEY2
no search env file
no search env file
%04d%02d%02d%02d%02d%02d
/usr/sbin/sendmail -t
fopen err
from: %s
to : %s
subject: [DATA] [%s] [%s] [%.6s] [%.6s]
[%s] [%s] [%.6s] [%.6s]
/data/applications/PgiWebApp/files/current/send/kftc/log
/data/applications/PgiWebApp/files/current/recv/kftc/log
START!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
config file
yymmdd
TARGET_IP_ADDR
TARGET_PORT
GIGWAN_COUNT
SENDER_ID
RECEIVE_ID
BLOCK_SIZE
DATA_LENGTH1
DATA_LENGTH2
KFTC_TEMP_PATH
RKFTC_TEMP_PATH
BKFTC_TEMP_PATH
LKFTC_TEMP_PATH
DKFTC_TEMP_PATH
PKFTC_TEMP_PATH
KFTC_PATH
RKFTC_PATH
BKFTC_PATH
LKFTC_PATH
DKFTC_PATH
PKFTC_PATH
SCRIPT_PATH
Also I do not have any idea what the above code is trying to do.
It would be really helpful if anyone can:
Explain the code.
How to decompile
how to compile after the changes.
Thanks,
Anish
I have the below code which is extracted by using stings command.
I need to edit the code and compile.
Your first step should be: find someone who has a clue. It is unlikely that you'll achieve whatever you are trying to do on your own.
That said, strings does not extract code, it only extracts printable strings from the code. Therefore there is no code to explain. (You should also read man strings if you are to understand anything.)
Assuming you still have the original binary, and not just the strings output, you can disassemble that binary using objdump -d (do read man objdump as well). That output, if it is small enough, you can hope someone would be able to understand and explain to you (you could also try to understand it yourself, armed with SPARC assembly manual, such as this one).
Once you understand what the program does, you could write an equivalent program in C, and finally modify it to do whatever you want. But it may be simpler to write such a program from scratch, rather than going through decompile / modify route.
I'm using the request.el library (available via MELPA) to try and create a basic framework from which to start work in earnest on a Stack Exchange mode for Emacs. All I want to do is to be able to return the object as parsed by json-read to the calling function, but I can't even seem to make a connection.
I understand that to have my function return the object, the call must be made synchronously, so that is what :sync t is there for. I've considered making it an asynchronous call, but I don't think it would be beneficial considering its use-case.
At first, after I looked at the messages, I thought 'Maybe I don't have necessary binaries.' I did test request.el with some example calls that come with its documentation, and they work fine, so that's out.
I'm at a loss as to what is wrong. I don't yet have much experience, successful or otherwise, with anything dealing with a network, and don't fully understand the error message I'm getting. As far as I can tell, port 443 of the API is giving me the silent treatment, but I'm hesitant to think that's the case ;).
;; Works like a charm
(defun alist-to-json (alist)
"Converts the key-value pairs of `ALIST` into a JSON-friendly
string: \"key1=value1&key2=value2&\"."
(apply 'concat
(mapcar (lambda (kv)
(format "%s=%s&" (car kv)
(if (stringp (cdr kv))
(cdr kv)
(number-to-string (cdr kv)))))
alist)))
(defvar stack-api-root "https://api.stackexchange.com/2.1/")
(require 'json)
(require 'request)
(defun stack-api-request (call keys-alist)
"Makes the specified `CALL` to the Stack Exchange API with the
key-value pairs given `KEYS-ALIST`. For example,
(stack-api-request \"sites\" '((page . 2) (page_size . 25)))"
(let* ((base-call (concat stack-api-root call "?"))
(options (alist-to-json keys-alist)))
(request base-call
:params options
:parser 'json-read
:sync t)))
Backtrace
Debugger entered--Lisp error: (error "Could not create connection to api.stackexchange.com:443")
signal(error ("Could not create connection to api.stackexchange.com:443"))
error("Could not create connection to %s:%d" "api.stackexchange.com" 443)
url-http([cl-struct-url "https" nil nil "api.stackexchange.com" nil "/2.1/sites?&" nil nil t nil t] #[128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)"] (nil))
url-https([cl-struct-url "https" nil nil "api.stackexchange.com" nil "/2.1/sites?&" nil nil t nil t] #[128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)"] (nil))
url-retrieve-internal("https://api.stackexchange.com/2.1/sites?&" #[128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)"] (nil) nil nil)
url-retrieve("https://api.stackexchange.com/2.1/sites?&" #[128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)"])
url-retrieve-synchronously("https://api.stackexchange.com/2.1/sites?&")
request--url-retrieve-sync("https://api.stackexchange.com/2.1/sites?&" :params "page=2&page_size=25&" :parser json-read :sync t :error (closure (t) (&rest args) (apply (quote request-default-error-callback) (quote "https://api.stackexchange.com/2.1/sites?") args)) :url "https://api.stackexchange.com/2.1/sites?&" :response [cl-struct-request-response nil nil nil nil nil "https://api.stackexchange.com/2.1/sites?&" nil (:params "page=2&page_size=25&" :parser json-read :sync t :error (closure (t) (&rest args) (apply (quote request-default-error-callback) (quote "https://api.stackexchange.com/2.1/sites?") args)) :url "https://api.stackexchange.com/2.1/sites?&" :response #0) nil nil nil url-retrieve nil])
apply(request--url-retrieve-sync "https://api.stackexchange.com/2.1/sites?&" (:params "page=2&page_size=25&" :parser json-read :sync t :error (closure (t) (&rest args) (apply (quote request-default-error-callback) (quote "https://api.stackexchange.com/2.1/sites?") args)) :url "https://api.stackexchange.com/2.1/sites?&" :response [cl-struct-request-response nil nil nil nil nil "https://api.stackexchange.com/2.1/sites?&" nil #0 nil nil nil url-retrieve nil]))
request("https://api.stackexchange.com/2.1/sites?" :params "page=2&page_size=25&" :parser json-read :sync t)
(let* ((base-call (concat stack-api-root call "?")) (options (alist-to-json keys-alist))) (request base-call :params options :parser (quote json-read) :sync t))
stack-api-request("sites" ((page . 2) (page_size . 25)))
eval((stack-api-request "sites" (quote ((page . 2) (page_size . 25)))) nil)
eval-expression((stack-api-request "sites" (quote ((page . 2) (page_size . 25)))) nil)
call-interactively(eval-expression nil nil)
Messages:
Contacting host: api.stackexchange.com:443
Opening TLS connection to `api.stackexchange.com'...
Opening TLS connection with `gnutls-cli --insecure -p 443 api.stackexchange.com'...failed
Opening TLS connection with `gnutls-cli --insecure -p 443 api.stackexchange.com --protocols ssl3'...failed
Opening TLS connection with `openssl s_client -connect api.stackexchange.com:443 -no_ssl2 -ign_eof'...failed
Opening TLS connection to `api.stackexchange.com'...failed
I checked to make sure that this was not a problem with cURL. The call I use for curl is
curl api.stackexchange.com/2.1/sites --compressed
From the outside looking into the request repository, request is doing this as well. I don't know what could be going wrong.
I boiled down your example to the following snippet to reproduce your problem but it actually worked. Can you try this one?
(request
"http://api.stackexchange.com/2.1/sites"
:parser 'json-read
:params '((page . "2") (page_size . "25"))
:success (lambda (&rest args) (princ (plist-get args :data))))
It should print some data to *Messages* buffer and echo area.
EDIT: It seems that the problem in your example is that you are passing string to PARAMS which takes only alist. I will change code to raise error so that it is easier to debug.