SCP on paramiko

import paramiko

class SCPClient(paramiko.SSHClient):
  def read_response(self, stdout):
    c = ord(stdout.read(1))
    if c == 0:
      return
    if c == -1:
      raise paramiko.SSHException("Remote scp terminated unexpectedly")
    if c != 1 and c != 2:
      raise paramiko.SSHException("Remote scp sent illegal error code")
    if c == 2:
      raise paramiko.SSHException("Remote scp terminated with error(%s)")
    
    err = stdout.readline()
    raise paramiko.SSHException("Remote scp terminated with error(%s)" % err)
    
  def send(self, localpath=None, remotepath=None, preserve=False, mode="0644"):
    if localpath is None:
      raise Exception("localpath must be specified")
    if remotepath is None:
      raise Exception("remotepath must be specified")
    if not os.path.exists(localpath):
      raise Exception("%s doesn't exist" % localpath)
    rdname = os.path.dirname(remotepath)
    rbname = os.path.basename(remotepath)
    if not rbname:
      rbname = os.path.basename(localpath)
    fsize = os.path.getsize(localpath)
    st = os.stat(localpath)
      
    cmd = "scp -t %s" % (remotepath)
    stdin, stdout, stderr = self.exec_command(cmd)
    
    if preserve:
      mtime = st.st_mtime
      atime = st.st_atime
      tline = "T%d %ld %d %ld\n" % (
        long(mtime), 
        long(mtime*1000000), # not sure
        long(atime), 
        long(atime*1000000)) # not sure
      stdin.write(tline)
    cline = "C%s %d %s\n" % (mode, fsize, rbname)
    stdin.write(cline)
    stdin.flush()
    
    self.read_response(stdout)
    
    fh = open(localpath, "rb")
    while True:
      s = fh.read(4096)
      if not s:
        break
      stdin.write(s)
      stdin.flush()
    fh.close()
    
    stdin.write("\0")
    stdin.flush()
    
    self.read_response(stdout)
    
    stdin.write("E\n")
    stdin.close()

# use it like:
client = SCPClient()
client.connect(host, username="user1", password="password1")
client.send(localpath="/home/foo/foo.txt", remotepath="/home/bar/bar.txt")

I've just ported it from SSH2 implementation of Java, Ganymed. And this experimental code can only send file, not receive -:)

Read OpenSSH/scp.c for more details!

Wondering how you can get remote stderr on error occuring...

"string" and "String" and java.lang.String

js> var s = "abc";
abc
js> s = new String(s);
abc
js> s == new String("abc");
false
js> typeof s
object
js> s instanceof String
true
js> s = s.toString();
abc
js> typeof s
string
js> s == "abc";
true
js> s = new java.lang.String("abc");
abc
js> s instanceof String
true

So it means that "string" (lower case) indicates JS string and "String" (capitalized) does java.lang.String. Note that "String" == "String" compares itself with other in identity, while "string" does with other in value. "String" is a shortcut for "java.lang.String" and java.lang.String#toString returns JS string in Rhino.
Confusing.

Multiplatform easy-embeddable VM

I read http://lucille.atso-net.jp/blog/?p=496 and found the LLVM for the first time. These days I'm thinking about runtime optimization of PL language in the RDBMS. Although the SQL and 2-dimensional table structure is highly sophisticated and strict enough to make it persistent storage, sometimes you would like to put more complicated data like arrays and json. You can put them easily, for example PostgreSQL has even ARRAY[] syntax, in your database but it is a bit harder to operate them. You must write pl/pgSQL or user c function to do it. The pl/pgSQL has the functionality to solve this problem, with very high operation cost because of its capability to execute any syntax of SQL. e.g. SPI provides greate flexibility instead of c function performance.
I want more opration-oriented procedure language. There we don't have to execute SQL nor do something on tables but complicated data types. Customizable operaters would do in high percentage cases.

SELECT oper('{1, 2, 3}'::int4[], 'a * a');

=> '{1, 4, 9}' (int4[])

With LLVM will it be possible??

how to enforce draggable() object to fixed position

I'm using the jquery library these days. It is quite good designed, I believe. But sometimes you would feel it lacks what you'd like to do, especially it is in the very localized problem.
I recommend you to read the original code in such case. The jquery code feeds you very good code reading experience.

So, I was stucked in jquery.ui.draggable. The problem was draggable options "grid" doesn't behave as I expected. My element object was located (x, y) = (52, 110) for example, and told $("#obj").draggable({grid: [2, 2]}) but it moves around like (53, 108), (51, 109)... and so on. Doesn't it mean snap to the grid I told?? I expected the element will be (x, y) = (even, even). The ui.draggable.js 5194 says

drag: function(e) {
	//Compute the helpers position
	this.position = { top: e.pageY - this.offset.top, left: e.pageX - this.offset.left };
	this.positionAbs = { left: e.pageX - this.clickOffset.left, top: e.pageY - this.clickOffset.top };

	//Call plugins and callbacks
	this.checkConstrains();			
	this.position = this.propagate("drag", e) || this.position;
	this.checkConstrains();
	
	$(this.helper).css({ left: this.position.left+'px', top: this.position.top+'px' }); // Stick the helper to the cursor
	if($.ui.ddmanager) $.ui.ddmanager.drag(this, e);
	return false;

around lines 224-238. The essence is

	this.position = this.propagate("drag", e) || this.position;
	this.checkConstrains();
			
	$(this.helper).css({ left: this.position.left+'px', top: this.position.top+'px' }); // Stick the helper to the cursor

which tells you that jquery calls your option-drag function and sets the returned value to its position property if any, then check constraints like bounding box and updates actual css position of the element.

I wrote like:

var scale = 2;
$("#obj").draggable({
//	grid: [scale, scale], 
	drag: function(e, ui){
		self.config.x = parseInt(ui.position.left / scale);
		self.config.y = parseInt(ui.position.top / scale);
		/* this is actually used for the element positions */
		return {
			top: self.config.y * scale, 
			left: self.config.x * scale};
		}
	});

so that the updated position (x, y) must be (even, even).

Hack jquery!

Improved way to use transitional values in user function

Once before, I said

Use of global variable in user function - umitanuki::quartet

Be careful to use global variable and avoid it as long as possible!

I found a way to keep transitional value during user function calls. The hint was in fmgr.h

/*
 * This struct holds the system-catalog information that must be looked up
 * before a function can be called through fmgr.  If the same function is
 * to be called multiple times, the lookup need be done only once and the
 * info struct saved for re-use.
 */
typedef struct FmgrInfo
{
  /* pointer to function or handler to be called */
  PGFunction  fn_addr;    
  /* OID of function (NOT of handler, if any) */
  Oid      fn_oid;      
  /* 0..FUNC_MAX_ARGS, or -1 if variable arg * count */
  short    fn_nargs;    
  /* function is "strict" (NULL in => NULL out) */
  bool    fn_strict;    
  /* function returns a set */
  bool    fn_retset;    
  /* extra space for use by handler */
  void     *fn_extra;    
  /* memory context to store fn_extra in */
  MemoryContext fn_mcxt; 
  /* expression parse tree for call, or NULL */   
  fmNodePtr  fn_expr;    
} FmgrInfo;

/*
 * This struct is the data actually passed to an fmgr-called function.
 */
typedef struct FunctionCallInfoData
{
  /* ptr to lookup info used for this call */
  FmgrInfo   *flinfo;    
  /* pass info about context of call */  
  fmNodePtr  context;    
  /* pass or return extra info about result */
  fmNodePtr  resultinfo;    
  /* function must set true if result is NULL */
  bool    isnull;      
  /* # arguments actually passed */
  short    nargs;      
  /* Arguments passed to function */
  Datum    arg[FUNC_MAX_ARGS];    
  /* T if arg[i] is actually NULL */
  bool    argnull[FUNC_MAX_ARGS]; 
} FunctionCallInfoData;

God'em. PG_FUNCTION_ARGS macro means exactly FunctionCallInfo *fcinfo, which has a member pointer to FmgrInfo flinfo, which holds fn_extra with comments /* extra space for use by handler */.

What does this mean?

You use this pointer area as you like. While the fcinfo is made newly call by call because it has arguments list evaluated per call, fcinfo->flinfo is fixed for a sequence of function calls. So the pointer fn_extra is alive during your function calls. But be careful again, if you do something with stored value in this pointer, you should call palloc / palloc0 to allocate memory. The trap is waiting for you. As I referred in the entry before, palloc'ed memory area is in the MemoryContext and default MemoryContext area for the function is only for per call. To store some value during whole the function calls, MemoryContextSwitchTo(). To which? fcinfo->flinfo->fn_mcxt gets there.

So, since fcinof->flinfo->fn_extra is always NULL unless you set something explicitly, you check it and if it is NULL it's a first call. Prepare for the lunch.

if(fcinfo->flinfo->fn_extra == NULL){
  /* this code will be executed only once on the first */
  // do something
  // then, switch MemoryContext
  MemoryContext oldcontext = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
  void *pointer = palloc(bytes);
  fcinfo->flinfo->fn_extra = pointer;
  MemoryContextSwitchTo(oldcontext);
}else{
  void* pointer = fcinfo->flinfo->fn_extra;
}

Don't forget to restore MemoryContext to old one!

Creating DLL written by C including C++ header

It's my own memorandom. Sometimes you must build dll or so called by c binary. Everything is OK as long as you are playing with c. But once you face to use external c++ library, be bit careful.

I struggled for hours and found that a fact:

#include <cpp-headers>

#ifdef __cplusplus
extern "C"{
#endif

#include <c-headers>

void some-function-prototypes(void *args);

#ifdef __cplusplus
}
#endif

Because c++ compilers rename c++ functions for overriding, normal c functions will be missing without extern "C" declaration. But you call c++ libraries and must hit g++ or so. Include cpp, then include c inside the extern "C"{} as well as declaring prototypes.

OreScript published

OreScript時代の幕開け - yukobaのブログ
In the article above, a proposal of new paradigm in client side scripting. Yesterday an application was published as an experimental one.

http://jtb.co.jp/kihin/

see html source. Hope this will be a good sample and reference.