Translate

Archive for the 'Linux' Category

Check if your PHP’s diff syntax isn’t breaking

Saturday, August 11th, 2007

Say you’re working on emacs, without any kind of syntax checking, and you don’t want to commit your code cause you might have syntax errors.

If you’re smart, the best way to know what files you actually changed, is using

svn diff | grep +++

(that is, if you’re using subversion, which I hope you are)

then you’d have to do,

php -l 

on each one of the files…

well, that looks like a script to me.

Add the following script to your path, name it… checkdiffsyntax

#!/bin/bash

#Put all the files that changed on a temp file and then dump them into a MYBLOOP_DIFF_FILES variable
TMP_FILE=/tmp/changed_${USER}
svn diff | grep +++ | awk {'print $2'} > ${TMP_FILE}; export MYBLOOP_DIFF_FILES=`cat ${TMP_FILE}`

for file in $MYBLOOP_DIFF_FILES
do
  php -l $file
done

rm ${TMP_FILE}

UPDATE
I learn how to use for as a one liner on bash, and also how to do this without dumping the filepaths to a file which gets fed into a string… now it just goes straight from awk into the for loop, sick. I will leave the example above for future reference in case you need how to put the contents of a file on a variable and then reading from them

New script looks like this now, one liner.

svn diff | grep +++ | for file in `awk {'print $2'}`; do echo ${file}; php -l ${file}; done

chmod 777 it, and you’re ready to go.

Should look like this when you run it

angel@foobar:~/public_html$ checkdiffsyntax
No syntax errors detected in members/ajax/mashup.php
No syntax errors detected in foo/mashups/twitter/twitter.php
No syntax errors detected in foo/classes/utils.php
No syntax errors detected in foo/classes/user.php
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Apache2: EnableSendFile

Tuesday, August 7th, 2007

Aunque no recomiendo apache para servir archivos estaticos (demasiado overhead, es mejor que le eches un ojo a nginx o a lighttpd), encontre hoy una directiva que puede eliminar un poco el overhead al hacer una llamada directa al sistema -> sendfile.

De momento lo que hice en uno de los servidores fue muy sencillo:

sudo apt-get install sendfile

Agregamos la directiva en /etc/apache2/apache2.conf

EnableSendFile On

(Puedes poner esta directiva solo en algun directorio, si lo deseas solo en un directorio)

Reseteamos el servidor

sudo kill -HUP <#proceso_principal_apache>

Chequeamos que no haya pasado ningun error con la configuracion

tail -f /var/log/apache2/error.log

[Tue Aug 07 08:11:30 2007] [notice] SIGHUP received.  Attempting to restart
[Tue Aug 07 08:11:33 2007] [notice] Apache/2.2.3 (Ubuntu) configured -- resuming normal operations

Supongo que ahora esta funcionando con sendfile, noto menos tiempo de espera para recibir el archivo.

Si alguien tiene algo que comentar sobre EnableSendFile, bienvenido, primera vez que lo pruebo, aunque brevemente pq montamos nginex en esa maquina a los 10minutos de probar esto. :(

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

2 Linux Commands I always forget

Friday, August 3rd, 2007

Taken on the museum of sex in paris

ldd

ldd is good when you need to know what shared dependencies has an executable file. For example.

angel@mbweb1:/bin$ ldd /bin/bash
        libncurses.so.5 => /lib/libncurses.so.5 (0x00002ac651098000)
        libdl.so.2 => /lib/libdl.so.2 (0x00002ac6512f4000)
        libc.so.6 => /lib/libc.so.6 (0x00002ac6514f8000)
        /lib64/ld-linux-x86-64.so.2 (0x00002ac650e7b000)

ltrace

As seen in the manual:

ltrace is a program that simply runs the specified command until it exits. It intercepts and records the dynamic library calls which are called by the executed process and the signals which are received by that process. It can also intercept and print the system calls executed by the program.

Here’s some sample output of ltrace

ltrace -p 929
--- SIGSTOP (Stopped (signal)) ---
--- SIGSTOP (Stopped (signal)) ---
fcntl(10, 4, 2050, 12, 0)                                                              = 0
accept(10, 0x7fffb0855b30, 0x7fffb08557ac, -1, 0)                                      = 76
fcntl(10, 4, 2, 0, 0)                                                                  = 0
signal(17, NULL)                                                                       = NULL
request_init(0x7fffb08557b0, 2, 0x7fffb0857ac5, 1, 76)                                 = 0x7fffb08557b0
sock_host(0x7fffb08557b0, 0, 0x7fffb08555d8, 40, 0x7fffb085583a)                       = 0
hosts_access(0x7fffb08557b0, 0x2b0bfaa95580, 0x7fffb0853648, -1, 0x7fffb085583a)       = 1
getsockname(76, 0x7fffb0855b20, 0x7fffb08557ac, 0x2aaab0000000, 0x2aaab0017ea0)        = 0
malloc(8496)                                                                           = 0x2aaab01857b0
malloc(256)                                                                            = 0x2aaab000b490
malloc(256)                                                                            = 0x2aaab000b5a0
memset(0x2aaab0186678, '\000', 472)                                                    = 0x2aaab0186678
memset(0x2aaab0186b48, '\000', 72)                                                     = 0x2aaab0186b48
memset(0x2aaab0186558, '\000', 72)                                                     = 0x2aaab0186558
memset(0x2aaab0186608, '\000', 112)                                                    = 0x2aaab0186608
pthread_mutex_init(0x2aaab01863c0, 0xda6668, 0, 0x2aaab0186678, 0)                     = 0
pthread_mutex_lock(0xd94200, 0xda6668, 0, 0x2aaab0186678, 0xda6668)                    = 0
memcpy(0x2aaab0185c68, "", 528)                                                        = 0x2aaab0185c68
malloc(41)                                                                             = 0x2aaab0002f80
memcpy(0x2aaab0002fa0, "%H:%i:%s", 8)                                                  = 0x2aaab0002fa0
malloc(41)                                                                             = 0x2aaab00646e0
memcpy(0x2aaab0064700, "%Y-%m-%d", 8)                                                  = 0x2aaab0064700
malloc(50)                                                                             = 0x2aaab0064720
memcpy(0x2aaab0064740, "%Y-%m-%d %H:%i:%s", 17)                                        = 0x2aaab0064740
pthread_mutex_unlock(0xd94200, 0xdaaab1, 17, 0, 64)                                    = 0
strcmp("latin1", "utf8")                                                               = -1
memset(0x2aaab0185e78, '\000', 1312)                                                   = 0x2aaab0185e78
malloc(1040)                                                                           = 0x2aaab00576e0
malloc(256)                                                                            = 0x2aaab0017ea0
pthread_mutex_lock(0xd93f00, 0x2aaab0185bf8, 0, 704, 128)                              = 0
pthread_mutex_unlock(0xd93f00, 0x2aaab0185bf8, 0x3252d661, 1, 128)                     = 0
pthread_self(0x2aaab0186398, 0x2aab7963b5ce, 0x39988b2e, 0xf9988b2b, 0xd93f00)         = 0x2b0bfbde9e70
pthread_self(0x2aaab0186398, 0x2aab7963b5ce, 0x39988b2e, 0xf9988b2b, 0xd93f00)         = 0x2b0bfbde9e70
malloc(240)                                                                            = 0x2aaab0017d20
memset(0x2aaab0017d20, '\000', 240)                                                    = 0x2aaab0017d20
sprintf("", "")                                                                        = 11
fcntl(76, 4, 0, 0x7fffb0855450, 0)                                                     = 0
fcntl(76, 3, 0, -1, 0)                                                                 = 2
malloc(16391)                                                                          = 0x2aaab0131590
fcntl(76, 4, 2050, 0, 0x200000)                                                        = 0
setsockopt(76, 0, 1, 0x7fffb0855634, 4)                                                = 0
setsockopt(76, 6, 1, 0x7fffb0855634, 4)                                                = 0
pthread_mutex_lock(0xd93f00, 6, 501, -1, 4)                                            = 0
pthread_cond_signal(0xd94920, 6, 0x2aaab01857c0, 0, 4)                                 = 0
pthread_mutex_unlock(0xd93f00, 5, 1, -1, 0xd94920)                                     = 0
select(13, 0x7fffb0855720, 0, 0, 0)                                                    = 1
fcntl(10, 4, 2050, 12, 0)                                                              = 0
accept(10, 0x7fffb0855b30, 0x7fffb08557ac, -1, 0)                                      = 80
fcntl(10, 4, 2, 0, 0)                                                                  = 0
signal(17, NULL)                                                                       = NULL
request_init(0x7fffb08557b0, 2, 0x7fffb0857ac5, 1, 80)                                 = 0x7fffb08557b0
sock_host(0x7fffb08557b0, 0, 0x7fffb08555d8, 40, 0x7fffb085583a)                       = 0
hosts_access(0x7fffb08557b0, 0x2b0bfaa95580, 0x7fffb0853648, -1, 0x7fffb085583a)       = 1
getsockname(80, 0x7fffb0855b20, 0x7fffb08557ac, 0x2aaab0000000, 0x2aaab0017fb0)        = 0
malloc(8496)                                                                           = 0x2aaab01355a0
malloc(256)                                                                            = 0x2aaab0017fb0
malloc(256)                                                                            = 0x2aaab00180c0
memset(0x2aaab0136468, '\000', 472)                                                    = 0x2aaab0136468
...

strace

the same as ltrace, but only for system calls.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Bjarne Stroustrup at Google NY, July 31st 2007

Tuesday, July 31st, 2007

Tonight, July 31st of 2007 I had the privilege of attending a talk by the man himself, the creator of the C++ language, Bjarne Stroustrup.

He was sharing with us how things are going for the next version of C++0x – The 0x stands for a possible year of this decade where it will be released.

One of the interesting things that happened to me at this meeting was to finally have the honor of meeting Sebastian Delmont, and oh, those 3 geeky Red Hat guys, wonder if they were actually part of Red Hat or just hardcore fans, good thing I was wearing my Ubuntu shirt to represent for the Ubunteros.

As for C++0x, I guess my thoughts aren’t that deep, I honestly don’t care much about C++ at this point in my life, and it seems it’s an old language striving hard to keep the pace with newer features found in new languages. It seems its very hard to reach consensus on the C++ board, and its very hard to accept new features given the language is already too big, its also funny to see that everything that gets hughe (millions of people use it), will always have problems in terms of adapting to new conditions or injecting innovation to itself. Bjarne created the language around 25 years ago, and there are features that only now he’s finally been able to get accepted for this version. C++ however, we must admit is a language that’s heavily used, (but I think by end users, not the software engineering community), on software like Photoshop, Google search engine, and the Mars rovers (these were the examples Bjarnes kept bringing again and again, I would also be proud of course).

For me, I’ll stick to Python, Nasa’s been using it too for quite a while, the same with Google and many other applications, hey, YouTube’s backend runs in python.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Nueva franela Ubuntu

Tuesday, July 31st, 2007

Acaba de llegar mi nueva franela con el logo de Ubuntu. Puedes hacer franelas como estas en spreadshirt.com

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Python Reference: Binary Operators

Saturday, July 28th, 2007

Python binary operators are pretty much the same as in any other language, however I notice most programmers tend to waste a lot of memory by creating lots and lots of properties say in DB tables, or Objects and using the wrong datatypes. I think its elegant to use the concept of binary flags, for example, if you have an object that has around 8 or 16 boolean properties, you can store their state in 1 byte or 2 bytes (1 char or 2 char fields), and turn on/off the bits on those fields.

Usually, if you have a binary field, and you want to, turn on bits, toggle bits, and check bits, you do the following.

Suppose “config” is an 8 bit number, and you want to modify this bits individually, say config=4 (0b100) and you want to turn on the rightmost bit to have 0b101 (which is a 5) you could do the following

>>> #Uses the | operator
... def turnBitOn(config, binaryFlag):
...   return config | binaryFlag
...
>>> config = 4
>>> config = turnBitOn(config,1)
>>> print config
5

If you want to check if a Bit is turned on, just use the “&” operator, if the result is the same as the bit you’re comparing, then its turned on.

>>> def checkBit(config,binaryFlag):
...   return binaryFlag == config & binaryFlag
...
>>> print checkBit(config,1)
True
>>> print checkBit(config,2) #checks 0b010 in 0b101
False

What if you just want to toggle a bit, no matter what’s in there?
Just use binary XOR, the “^” operator.

>>> def toggleBit(config,binaryFlag):
...   return config ^ binaryFlag
...
>>> print config
5
>>> config = toggleBit(config,1)
>>> print config
4
>>> config = toggleBit(config,1)
>>> print config
5

And now, the last basic operation would be to turn off a bit. For this, you should do a NAND operation. The Binary Not in python as in most programming languages is the “~” operator. This is how you can use it to turn off bits.

You have to do it in conjunction with the & operator, sort of doing a NAND

>>> 5 & (~1)
4
>>> 5 &~ 1
4

So we could define our turnOffBit function as follows:

>>> def turnOffBit(config,binaryFlag):
...   return config & (~binaryFlag)
...
>>> print config
5
>>> config = turnOffBit(config,1)
>>> print config
4
>>> config = turnOffBit(config,1)
>>> print config
4

Hope this makes a good reference for those trying to make the most out of their bytes.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Python: How to debug HTTP while using urllib2

Friday, July 27th, 2007
...
import urllib
import urllib2

#this is just to prepare a dynamic uri (this is actual code from a system I'm building, sorry)
fileDownloadServiceURL = '%s://%s:%s/%s' % (transport,server,port,pathToController)
postData = {'URI':fileUri} #add more post stuff here
postData = urllib.urlencode(postData) #make sure you encode your post data

#add some custom headers if you need them
headers={"Host": server+':'+port,"Cookie":"JSESSIONID="+sessionId,"User-Agent":"Name of your User-Agent Here"}

#prepare your request, with headers and post data
req = urllib2.Request(fileDownloadServiceURL,postData,headers)

#and this is the magic. Create a HTTPHandler object and put its debug level to 1
httpHandler = urllib2.HTTPHandler()
httpHandler.set_http_debuglevel(1)

#Instead of using urllib2.urlopen, create an opener, and pass the HTTPHandler
#and any other handlers... to it.
opener = urllib2.build_opener(httpHandler)

#User your opener to open the Request.
urlHandle = opener.open(req)

#you'll end up with a file-like object... which I called urlHandle
...

The ouput will have useful debugging info about the HTTP connection.

connect: (localhost, 8080)
send: u'POST /arcturus-web/fileVariableDownload.service HTTP/1.1\r\nAccept-Encoding: identity\r\nContent-Length: 57\r\nConnection: close\r\nUser-Agent: Temboo Twyla/Arcturus HTTP Downloader\r\nHost: localhost:8080\r\nCookie: JSESSIONID=1b8xl8nozb2i\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n'
send: 'URI=temboo%3A%2F%2Fwww.one.com%2Ffiles.f%2Fmapping1.m.var'
reply: 'HTTP/1.1 200 OK\r\n'
header: Content-Language: en-US
header: Content-Type: text/html; charset=ISO-8859-1
header: Connection: close
header: Server: Jetty(6.0.2)
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

How to update the location of your subversion Repo without checking out again everything

Tuesday, July 10th, 2007

So you checked out code from a place, and whoever runs your subversion server decided to change the URL, or decided to switch from http:// to https:// or to svn+ssh:// … If you’re a noob, you’ll probably just checkout everything again.

Next time this happens just use svn switch

Here’s an example of real life when sourceforge updated the urls of their svn repos, this is what I had to do:

svn switch --relocate 

https://svn.sourceforge.net/svnroot/frostwire/trunk

http://frostwire.svn.sourceforge.net/svnroot/frostwire/trunk

* That command is supposed to be all in one line of course, its written like that for formatting purposes of this blog.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Trabaja como desarrollador de software en New York

Wednesday, June 6th, 2007

Todas las semanas me llegan multiples correos de reclutadores ofertando excelentes posiciones en New York y zonas cercanas.
En muchos de los casos me llaman por telefono, y me preguntan si conozco personas que pueden llenar esas posiciones, mi respuesta es “No, but I know a lot of talent in South America, if your company can sponsor H1b then I can give you talent”

Supongo que en Venezuela y otros paises como Colombia, Chile, Mexico, Peru, etc. hay mucho talento mal pagado, esclavizado por menos de $1000 al mes, creo que si estas leyendo esto y quieres aprovechar tu talento al maximo, deberias empezar por unirte a la lista de correos que he creado, donde todas las semanas envio las descripciones de las oportunidades e informacion de contacto.

La lista de correos te servira ademas para ver que competencias debes pulir o aprender si quieres venir a USA, mas especificamente a New York a trabajar como desarrollador. Con 1 o 2 anos de experiencia puedes aspirar sueldos que empiezan por $50mil al ano, el limite del sueldo depende de lo pilas que seas, pero ya con unos 3 anos de experiencia aqui, estaras rondando facilmente los $100mil anuales o mas, todo depende la dificultad de lo que desees hacer. Se que si te metes en el area de finanzas (que es lo que mas se consigue aqui en NY), programando en lenguajes como C++ (multihilos), o en J2EE las pagas llegan facilmente a los $150k anuales. De todos modos hay muchas oportunidades para otras tecnologias mas suaves y practicas como Python, Ruby, PHP, y por supuesto para otras no tan practicas como .NET ;)

Aqui la direccion del grupo
http://groups.google.com/group/nycsoftwarejobs

Solo unete y empezaras a recibir los mensajes, supongo que tambien puedes suscribirte al feed RSS.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

How to update file timestamps in Python

Tuesday, May 29th, 2007

Sometimes you can be real picky like me about timestamps of files, for example, during my wedding we had a few digital cameras, and one of the cameras had its internal clock 4 hours behind. So what better way for a lazy guy like you to change timestamps than writing a short python script to fix the problem.

I wrote this script on the same folder where the pictures with the lagged modification times existed:

import os
import time
from stat import *

#returns a list of all the files on the current directory
files = os.listdir('.')

for f in files:
  #my folder has some jpegs and raw images
  if f.lower().endswith('jpg') or f.lower().endswith('crw'):
    st = os.stat(f)
    atime = st[ST_ATIME] #access time
    mtime = st[ST_MTIME] #modification time

    new_mtime = mtime + (4*3600) #new modification time

    #modify the file timestamp
    os.utime(f,(atime,new_mtime))

Very nice, I like

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)


  • Categories

  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • October 2009
  • September 2009
  • July 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004