soledad penadés
repeat 4[fd 100 rt 90]

Archive for the ‘trick’ Category

20080530 Mental note on Safari, cookies and underscores

In short: do not use underscores for your local domains.

Apparently, Safari decided to be kind of very strict, which means it will show a website whose domain name contains underscores, but will dutifully ignore every request to set cookies on it.

It's very funny because if they really wanted to be strict they should not show the website at all, instead of making me peek into odd pages trying to find out why oh why it worked on opera and firefox but not in Safari.

And it seems it also can lead to the same problem in IE, which makes me kind of agree with Mr.doob's lemma: Safari is the new IE!

Luckily underscores are not allowed when registering a domain (I just checked that). Otherwise we would have heard a ton of complaints from customers who ordered a website and then couldn't login at their own website because the domain contained an underscore.

Extra bonus: you can use this snippet of code for checking that cookies are working. Save it as 'cookie-test.php' or something that you fancy and reload reload reload. It should show "X is 0", "X is 1", "X is 2", etc.

If it doesn't work, it'll always show "X is 0";

<?php

if(isset($_COOKIE['x']))
{
        $x = $_COOKIE['x'];
}
else
{
        $x = 0;
}

setcookie('x', $x+1);

echo "X is $x";

?>

20080519 Lua universal binary

Add this post, this one and this comment and you get a Lua universal build for your mac in Leopard - which works for Tiger too! Assuming you uncompressed the source code onto a folder called lua

In lua/Makefile, add a new target, macosx_ub, to the PLATS definition. It should look like this:

# Convenience platforms targets.
PLATS= aix ansi bsd freebsd generic linux macosx macosx_ub mingw posix solaris

and in lua/src/Makefile add this

macosx_ub:
$(MAKE) all MYCFLAGS="-DLUA_USE_MACOSX -isysroot /Developer/SDKs/MacOSX10.4u.sdk -mmacosx-version-min=10.4 -arch ppc -arch i386" "MYLDFLAGS=-Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk -arch ppc -arch i386 -mmacosx-version-min=10.4"

Then instead of typing make macosx, type make macosx_ub et voilà! You have a universal Lua! :-)

20080513 Split files into folders by letter

I had a lot of files in one folder. It is not very practical to browse the folder that way, so I decided to create a little script which would split the files into different folders, using the first letter of the file for naming the folder, as in a, b, c, d… but using Python this time!

import os
import shutil
import sys

if len(sys.argv) > 1:
        folder = sys.argv[1]
else:
        folder = '.'

for item in os.listdir(folder):

        full_path = os.path.join(folder, item)
       
        if os.path.isdir(full_path):
                continue
       
        dst_folder = os.path.join(folder, item[0].lower())
       
        if not os.path.exists(dst_folder):
                os.mkdir(dst_folder)
               
        shutil.move(full_path, os.path.join(dst_folder, item))

Download from svn (this should always be the most up to date version)

To use simply go to your favourite terminal window and type

python split_files_into_folders_by_letter.py /path/to/messy/folder

It will pick the first letter of each file, create a folder with the lowercased letter (if it doesn't exist yet) and move the file to the folder. It won't move folders! (because of the os.path.isdir check).

If there's any pythonista in the audience with ideas for improving this, feel free to leave your suggestions in the comments, thanks! :-)

Note

There wasn't any special reason for not doing it with Ruby; I simply wanted to know what the differences would be. And while the semantics aren't very different (e.g. where it says os.path.join we would use File.join in Ruby), there is a huge difference between them and it's called documentation. Python documentation is hosted in Python's website, it is reviewed (and mostly written too) by Guido van Rossum (Python's author) and it's up to date and very easy to browse and read. Whereas Ruby docs were hosted elsewhere until very recently, are a pain to browse and pretty much a dump of automatically generated docs from source, which I find very uncomfortable to use. It still feels a little bit weird to use python's indenting style but I'm really liking it. A pity there isn't a Hpricot in python :-)

20080421 Building a universal library in Mac OS X

If you need to use a library in a universal binary you'll need to use a universal library too.

Let's use FMOD as an example; libfmod.a is the PPC compiled version, libfmodx86.a is the intel version.

For combining both into a universal library:

lipo -create libfmodx86.a libfmod.a -output libfmod-universal.a
ranlib libfmod-universal.a

In the case of FMOD they provide you the with precompiled static libraries (the .a files). But if you build libraries from source code, there's a magic compiler option called -arch which will help you in this. Unfortunately I don't have an example handy right now. I'll put it whenever I find it again, but it roughly goes on the lines of -arch i386 -arch ppc :-)

I'm not sure that naming a library with something as *-universal.a is a good idea, specially if you have code which uses -Llibraryname for linking libraries — I guess you might need to change that parameter. I am explicitly adding the libraries to the project in XCode so it's working fine for me.

Suggestions and better practices are welcome!

20070221 Deploying websites with Subversion

I think I'm going to move to Subversion all the websites which still aren't versioned. It's so annoying to change things locally, test locally, then you want to upload them to the server and there's always some change which it's not updated and booooom!

With SVN (Subversion) it's much easier. You continue building your stuff locally and when you're happy with it, check it into your Subversion server. Then you log into your hosting server with ssh, and in your /web directory do something like svn co http://mysubversionserver/project/trunk . - that will associate the /web directory with the subversion repository path /project/trunk. Meaning that each time you do changes and commit them to SVN, you can get all the changes rolled to your server by simply connecting with ssh and running svn up in the /web directory.

Obviously for doing this you need a hosting account with ssh access. Most of them do not have such thing, and I really can't understand how could I survive without ssh access before. Even for simple things such as deleting a directory, ftp's and sftp's are terribly slow :-)

There's more sophisticated people which are using build makers like ant or the famous capistrano to deploy stuff but for the moment I still don't need that degree of complexity; it's not worth the effort yet.