Saturday, August 7, 2010

My Maemo development environment

The autum is almost here, so I think it is time to post something to here again. For that purpose, this message is the number one, I started to write this at the 8th of May, I did some edits during the June and July, but finally, I will post this to here, now the 7th of August, so it takes only 4 months to develope a blog post, but I had to say that it had been beautiful summer here at Finland (and still is, you can't say it is autumn when there is still almost 30 degrees of Celsius outside).

When I started to write pyKake, I just used the PyGTK Editor in my N900. And to create the first version of it, that was all I needed. Then, after I release the preview version of it (like I want to call that version 0.3), I find out that it was easier to edit and rewrite code in laptop and then move it to the device to test. That was nice and easy... as long as there weren't need to do multiple test in short time period. Problem was, that for every test you had to use email or mass storage mode (selected when you plug device to the computer) to get needed files into it.

So I did little research and find this nice guide how to to create SSH connection to the device, and again I was satisfied. In this point I just connect the device to computer and create a network connection to it (or basically 2 of them, because I also use the device as a mobile broadband). After this all I needed to do was to take the SFTP connection with FileZilla to it and trasfer the files.

So, in this point of code writing, I edited code with Gedit and send it to the N900 over the SFTP and then tested it (in the device), and that was pretty good way to code... Until I needed to do it multiple times in very short perioids. So I started the research again and ended up to this dynamic trio: Geany as an IDE, N900 as a testing environment and a small script to automatically upload modified source files to the device (so, I don't need to do it manually every time I save the file :P ). And the result? Well, when I edit the source and save the file, the script (if it is running) notice the modify and send the new one with the scp to the device, neat isn't it? And I had to mention, that if you use terminal plug-in of Geany to take ssh connection to the device and run the test, you got all (error) output you need in to the Geany, so it is simple to read during the coding. I like this, I like this a lot, and how you can do it buy yourself? Well, now I tell it to you, right after the jump.

These guidelines are for Ubuntu, but they should work with other Linux distros too, for Windows/Mac users, these may work or not, but probably you need to do something (or lot) in differend way. I have used this method in Ubuntu 9.10 and now in Kubuntu 10.04 (I changed the Desktop, when I upgraded, because the next version of pyKake is in Qt4, more about that in the future), and it works perfectly in those both.

1. Enable SSH in device and install needed applications to computer.
To enable SSH follow this tutorial, remember to use good passphrase (just for the security) and maybe you want to do this too. Now you should have ssh ready in device. Next install inotify-tools to your computer (just do the apt-get magic or use the Synaptic). Btw, if you are paranoid with the device always running as the ssh server (so anyone who had the username, password and ip address of the device can remotely log in to it), install SSH Status and Switcher from Extras, you can use this nice little widget to enable and disable SSH in the device as you needed. 

2. Create the RSA-authenticated login instead of passwords.
Next step is to create a rsa key pair, to get more info about rsa, read here. To create these key pairs, we start to follow this tutorial only exception is, that we don't do this for user, we do this for root (of course, you can do this to user too, if you want and it is probably more secure way than my), and we don't disable password based logins. So remember, for root (or user), you had to had strong passphrase, not just abcd1234, but more something like: 1aKb# @ r4Gh/er CZx I mean that use passphrase, with letters, numbers other symbols, what you remember (more harder, more safer). And again, you can do this as it is in tutorial too, you just had to remember it later, that you use user instead of root.

And now, creating the rsa authentication for root:

Follow step 1 in tutorial.

In step 2 do this instead of that:

      user@computer $ scp ~/.ssh/id_rsa.pub root@192.168.2.15:/root/
      (It will ask the password) 
      user@computer $ ssh root@192.168.2.15 
      root@192.168.2.15's password: 
      root@N900 # mkdir /root/.ssh
      root@N900 # cat /root/id_rsa.pub >> /root/.ssh/authorized_keys 
      root@N900 # chmod 600 authorized_keys 
      root@N900 # ~/.ssh $ rm /root/id_dsa.pub 
      root@N900 # exit

Now you should have (in device) .ssh folder in /root  and file authorized_keys in it, (just ln -la /root and ln -l /root/.ssh/ in terminal to find it out. This also means that you should be able to use ssh without root's password, to test this, just log out and log back in. (Of course, this works only in the computer which you use to create key pair, in other computers you are still asked for password, to get this working in others too, do the steps 1 and 2 again, in that computer.)

3. Copy and edit the script to automate the data transfer.
Now the device should be ready, you can login without password and send files over scp without password. So this is the moment, when we create a script to automatically move files from computer to device, so do this in your computer, not in device.

The script:

#!/bin/sh

# get the current path
CURPATH=`pwd`

inotifywait -mq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' \
-e modify /home/yabba/Työpöytä/pykake_current/ | while read date time dir file; do

       FILECHANGE=${dir}${file}
       scp $FILECHANGE root@192.168.2.15:/home/user/${file} 
       echo "At ${time} on ${date}, file ${file} was moved to N900"
done 

Now, first copy that (without line numbers) to file and save it as a script file (like automate.sh) in the location you want (your home folder or in the project folder). Then in line 7 add the location of the project you are working with (where you save the files you want move to the device when they change). In line 10, if you want to save files in some other location than /home/user/ in the device (like /home/user/myapp/), change the path to that location (change only that /home/user/ part, nothing else). Now, you should have working auto transfer.

To test this script, open terminal (in computer) and go to location where you save the script and run it, just type ./automate.sh (or what ever you name it) and leave the terminal open. Then go to the work location (which you added in line 7) and save new file/modify old one. There should be message like:

      hello.py                                                 100%   28     0.0KB/s   00:00   
      At 18:55 on 07/08/10, file hello.py was moved to N900

So you got the scp output (the first line) and the message from the script. To check do this really work, just go to the location in the device (given in line 10 and which default is /home/user/) and run ln, you should see the moved file in list. Btw, in line 11, you can change the output message of script in the way you want ;)

4. Happy hacking :)
At the moment I write this, this is all about my Development IDE. I'm sure there will be some changes in future (program is going to grow as they used to, and because of that, the needs of shortcuts will also grow). So maybe I will write some updates later, but for now, this is all there is and it's working.

No comments:

Post a Comment