...looks interesting... !
This are my notes in the fields of computer science and technology. Everything is written with ABSOLUTE NO WARRANTY of fitness for any purpose. Of course, feel free to comment anything.
Thursday, August 12, 2010
Install Flash Player without admin rights on Windows
Follow this blog entry. It worked on Windows XP in a VMware machine under Linux.
Tuesday, July 6, 2010
Space usage profiling: massif
massif is a valgrind tool, that can be used to profile the space usage of a program:
valgrind --tool=massif ./myprogram
The result is a massiv.out. file. The most important information in it can be visualized using e.g. ms_print:
ms_print massif.out.0000 | less
valgrind --tool=massif ./myprogram
The result is a massiv.out.
ms_print massif.out.0000 | less
Friday, January 1, 2010
Install kernel headers in linux using aptitude
sudo aptitude install linux-kernel-headers linux-headers-`uname -r`
Thursday, December 31, 2009
ssh autocompletion in tcsh
I wrote the following for my .tcshrc to get some autocompletion for ssh.
set hostlist=(`cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | cut -f 1 -d ',' | uniq`)
complete ssh 'p/1/$hostlist/' 'p/2/c/'
Saturday, December 12, 2009
TestDisk
Hard disk failure recovery tool; tries to repair partition tables, boot sectors, MBR; allows to read and copy files from lost partitions; an undelete feature is also included.
Homepage: http://www.cgsecurity.org/wiki/TestDisk
Developer: Christophe Grenier
Version: 6.11 (April 19, 2009)
License: open source free software
Platform: Linux/Windows/Mac
Homepage: http://www.cgsecurity.org/wiki/TestDisk
Developer: Christophe Grenier
Version: 6.11 (April 19, 2009)
License: open source free software
Platform: Linux/Windows/Mac
Friday, December 11, 2009
Inkscape
Vector graphics program, similar to commercial packages like Adobe Illustrator, with advanced editing features and importing/exporting in a lot of different formats (e.g. PDF). The native format is the open format SGV.
Homepage: http://www.inkscape.org/
Developer: The Inkscape Team
Version: 0.47 (November 21, 2009)
License: open source free software
Platform: Linux/Windows/Mac
Homepage: http://www.inkscape.org/
Developer: The Inkscape Team
Version: 0.47 (November 21, 2009)
License: open source free software
Platform: Linux/Windows/Mac
Sunday, October 11, 2009
Starting ssh-agent from .tcshrc
While connecting to a server using SSH, you need either to provide a password or to use a public-private key pair to identify yourself. The latter is interesting, as you don't have to write your password each time. A copy of the public key must be saved in the computer acting as ssh server under ".ssh/authorized_keys", the private key stays only on the client computer.
However for security reasons it is strongly reccomended to set a password on the key file. Otherwise anyone who can steal your key somehow (like accessing your computer physically), can steal your identity. But if your key file has a password, then you would have to write the password each time, so it wouldn't be much better than using passwords instead of keys in the first time. Luckily there is a solution: ssh-agent and ssh-add. ssh-agent runs as a deamon and remembers the password to your key. ssh-add allows you to tell ssh-agent the password.
As ssh-agent keep running when you log off your session, you may add some lines to your shell startup script, to test if ssh-agent is running, otherwise to start it and ask you the password (via ssh-add). In the github guides there is a script for bash (). I adapted it to use it in tcsh, which I use as a shell. Here is it (I added these lines at the bottom of my .tcshrc file, only *before* setting the prompt explicitely, as I used it to distinguish interactive sessions from non-interactive ones):
However for security reasons it is strongly reccomended to set a password on the key file. Otherwise anyone who can steal your key somehow (like accessing your computer physically), can steal your identity. But if your key file has a password, then you would have to write the password each time, so it wouldn't be much better than using passwords instead of keys in the first time. Luckily there is a solution: ssh-agent and ssh-add. ssh-agent runs as a deamon and remembers the password to your key. ssh-add allows you to tell ssh-agent the password.
As ssh-agent keep running when you log off your session, you may add some lines to your shell startup script, to test if ssh-agent is running, otherwise to start it and ask you the password (via ssh-add). In the github guides there is a script for bash (). I adapted it to use it in tcsh, which I use as a shell. Here is it (I added these lines at the bottom of my .tcshrc file, only *before* setting the prompt explicitely, as I used it to distinguish interactive sessions from non-interactive ones):
# start ssh agent if not started yet
set SSH_ENV = "$HOME/.ssh/environment"
unset SSH_AGENT_RUNNING
if (-f $SSH_ENV) then
source $SSH_ENV
ps $SSH_AGENT_PID > /dev/null && set SSH_AGENT_RUNNING
endif
if ($?prompt && ! $?SSH_AGENT_RUNNING) then
echo "Initializing SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' >! $SSH_ENV
chmod 600 $SSH_ENV
source $SSH_ENV
echo "done: ssh-agent is now running, PID $SSH_AGENT_PID"
/usr/bin/ssh-add
endif
Sunday, September 13, 2009
My virtual linux server
In the windows installation on my laptop, I also run a linux server in background. Here is how I setup my virtual linux server.
As a virtualization software I installed Sun VirtualBox. I created a virtual machine. I gave it a little less than the half of my memory and turned on support for AMD-V extensions (as I did also in my host machine BIOS). As HD controller I selected SATA (the default is IDE). For networking I use the NAT configuration. I forwarded the SSH port as described in the virtual box manual (backlinux is the name of my virtual machine):
VBoxManage setextradata "backlinux" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/Protocol" TCPThen I followed the suggestion of Sebastian Bauer's blog: I installed the ntwind software's hidden start tool (homepage) and created two one-liner batch files that I use to start and stop the virtual machine. There are also more sofisticated approaches to run the machine as a windows service, however I found this very simple and I chose it for this reason.
VBoxManage setextradata "backlinux" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/GuestPort" 22
VBoxManage setextradata "backlinux" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/HostPort" 2222
Here is the start.bat file: (paths are specific to my machine)
"c:\Program Files\standalone\hstart\hstart" /NOCONSOLE ""c:\Program Files\Sun\VirtualBox\vboxheadless" -s backlinux -vrdp off"
while the stop.bat file contains:
"c:\Program Files\Sun\VirtualBox\VBoxManage.exe" controlvm backlinux savestate
I created two hard disk images for the machine, called backlinux.vdi and backlinux.home.vdi. I set both to dynamical 8 Gb. I later attached backlinux.home to /home. I keep /home on a separate disk, so that I can make use of the snapshots feature of Virtualbox, without loosing the data in /home. To do this I changed the type of backlinux.home.vdi to write-through using:
"c:\Program Files\Sun\VirtualBox\VBoxManage.exe" modifyhd backlinux.home.vdi --type writethrough
Then I installed ubuntu-server (jaunty, i.e. 9.04) with the option "minimal virtual machine" (press F4 by the installation) and I added some software packages I need/like using aptitude (for example: libncurses-dev, libcairo2-dev, ruby1.9, git-core, gitk ... and relative dependences). The machine has no x-server installed, but I have an X server installed under Windows (Xming), so I can start also graphical software using X-forwarding.
To allow the virtual machine to access the host disk (C:), I installed the VBoxLinuxGuestAdditions from the shell script in the virtual CD image (.iso) provided with virtual box (you need to install linux-headers package for your kernel for that). Then I created a shared folder "c" using the virtual box gui. In Linux then I mounted the folder, creating a /C folder, adding this line to /etc/fstab:
c /C vboxsfAs last step, I created two icons starting from NewTux.svg of Wikimedia Commons (author: Larry Ewing). I resized it to a 48px square and overlayed a green triangle (start icon) and a red square (stop icon). I created then links to my batch files to my desktop and assigned the icons to the links.
Here is the result:
VirtualBox
There are several available platform virtual machines available. I generally use the Sun VirtualBox. I usually run a Linux server in my Windows-running laptop, using a virtual hard disk for it and accessing it with PuTTY and WinSCP.
VirtualBox is able to run a virtual machine also without an output terminal/window (headless). I use this for my Linux server.
Another possibility it offers is to run a machine booting from a real partition instead of a virtual hard disk. However this makes things quite difficult and is an experimental feature. The hardware found by the operating system will be different in a virtual box and booting as host, so this will lead to several problems. For example I was unable to run a deployed Vista from Linux and I could only run a deployed Ubuntu in Windows with command line interface.
Homepage: http://www.virtualbox.org/
Developer: Sun
Version: 3.0.6 (September 9, 2009)
Platform: Cross-platform
PuTTY
A terminal emulator using ssh or telnet connections to remote servers. I always use this for ssh connections from a Windows machine to a Linux machine (usually using WinSCP for file exchanges) - including my virtual linux server I run in background.
In Windows the program settings (including the saved sessions) are stored in the registry at the position: Computer\HKEY_CURRENT_USER\Software\SimonTatham\PuTTY .
Developer: Simon Thatam
Version: 0.60 (April 29, 2007)
Platform: cross-platform
License: open source
Saturday, September 12, 2009
DVD43
In order to rip/transcode DVD you own, for example to see the videos on your mobile device, this tool may be useful. It circumvents copy protection and zone settings. It may be illegal under certain circumstances and jurisdictions, however the tool itself is probably legal, if you don't engage in llegal activities with it.
Homepage: http://www.dvd43.com/
Developer: unknown, maybe for legal reasons (?) [whois information]
Version: 4.4.1 (June 29, 2009)
License: closed source freeware
Platform: Windows
HandBrake
This software comes in hand, when you want to upload videos to a mobile phone or media player. It allows to transcode easily to mp4 format with a good control of options, encoder to use, resizing...
Here is my settings file (user_presets.xml), which I use to convert files to display them in my phone:
<?xml version="1.0"?>My settings are: ffmpeg encoding, mp4 container format, aac stereo sound, 50% constant quality, resize to 400px width. Note that to be able to create a preset under Vista you need to start the application with administrative rights.
<ArrayOfPreset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Preset>
<Level>0</Level>
<Name>Mobile phone</Name>
<Query> -f mp4 -w 400 -e ffmpeg -q 0.5 -a 1 -E faac -B 160 -R 0 -6 stereo -D 1 -m -v </Query>
</Preset>
</ArrayOfPreset>
Homepage: http://handbrake.fr/
Platform: cross-platform
Version: 0.9.3 (Nov 23, 2008)
License: open source freeware
VLC media player
This media player allows to reproduce a wide spectrum of media files. It is extremely flexible and has a lot of advanced features, probably more than any other media player. The graphical interface (which is one of the different interfaces it has) is nevertheless very simple and lightweight.
You can select the GUI language using Ctrl+P and changing the first option in the dialog.
Homepage: http://www.videolan.org/vlc/
Developer: VideoLAN project
Version: 1.0.1 (July 27, 2009)
Platform: cross-platform
License: freeware, open source
Homepage: http://www.videolan.org/vlc/
Developer: VideoLAN project
Version: 1.0.1 (July 27, 2009)
Platform: cross-platform
License: freeware, open source
SyncToy
As a simple backup strategy under Windows, I synchronize my important folders with a copy on an external drive. To do that I am using SyncToy, a free-to-download tool from Microsoft.
Download link: http://www.microsoft.com/downloads/[...]
Developer: Microsoft
Version: 2.0 (August 15, 2008)
Platform: Windows
License: freeware, closed source
Download link: http://www.microsoft.com/downloads/[...]
Developer: Microsoft
Version: 2.0 (August 15, 2008)
Platform: Windows
License: freeware, closed source
Windows Alpha Blender
This tool allows to set the tranparency of a windows using a shortcut to increase (default: Ctrl+F11; my setting: Ctrl+<) or decrease (default: Ctrl+F12; my setting: Ctrl+Shift+<) the alpha transparency value of the currently active window. It is not very sofisticated, but it can be useful sometimes.
Personal settings (ie the keyboard shortcuts) are kept in the registry.
Homepage: http://www.vaultmate.com/freewaregifts.php
Developer: VaultMate Software
Version: 1.2 (June 7, 2008)
License: freeware, closed source
Platform: Windows
Developer: VaultMate Software
Version: 1.2 (June 7, 2008)
License: freeware, closed source
Platform: Windows
Colors in grep
To turn on colors in grep, you may add --color=auto to the GREP_OPTIONS environment variable. The way to do it is of course shell-dependant. E.g. add the following to your shell rc file:
export GREP_OPTIONS='--color=auto'
Unlocker
This tool allows to identify the process, which is currently locking a given file or directory and if desidered to force unlocking of the file from one or all processes. It works as a shell extension in explorer. Note that under Vista the program requires a confirmation to be allowed to access a file (if the UAC system is on).
Homepage: http://ccollomb.free.fr/unlocker/
Version: 1.8.7 (May 1st, 2008)
Platform: Windows
License: freeware, closed source
WinSCP
WinSCP is the perfect windows tool to connect to remote servers and exchange (download/upload) files or edit them. It is a SFTP and FTP client.
There is a standalone version and an installer based setup; if using the latter, beware that it will ask you install additional software using the OpenCandy system. However at least the default is not to install any additional software.
I like to store the preferences in ini file instead of the registry, so that I can export them easily to other computers. To turn on the ini preferences storage: in WinSCP login dialog select "preferences", then "Other general options: Preferences", "Storage" and change the "configuration storage" value to "INI file". The default location of the ini file under Windows Vista will be: "C:\Users\\AppData\Local\VirtualStore\Program Files\WinSCP". Under older Windows version it will be directly in the application directory (C:\program files\WinSCP).
Homepage: http://winscp.net/
Documentation: http://winscp.net/eng/docs/guides
Version: 4.2.3 beta (August 18, 2009)
Platform: Windows
License: freeware
Friday, September 11, 2009
Gimp
Gimp is great if you cannot afford on do not want to pay a lot of money for professional software (i.e. Photoshop). According to some it *is* a professional level software, according to other not yet; however it has more than enough features for amateur photo manipulation and other image related tasks (e.g. icon editing, image conversion, resizing etc). The first version was realeased in 1996 by two computer science students (Kimball and Mattis, currently Google engineers).
It is quite complicated to use and probably extreme complicated to master, however it is intuitive enough for simple tasks. Since version 2.6 they invested in an effort to make the interface easier to use (see Wikipedia article). An user manual and documentation page is also available.
set lang=enThen I changed the startmenu entry to point to the bat file instead of the exe file (other settings: execute minimized, use icon of the .exe file).
start gimp-2.6.exe
Name: Gimp
Homepage: http://www.gimp.org/
Version: 2.6.7 (August 14, 2009)
License: free, open-source software
Platform: cross-platform
JDiskReport
There are for sure a lot of disk space analysis tools, but I liked this German Java application a lot: JDiskReport. It is relatively fast and displays a lot of additional information. The space usage or number of files information can be grouped by location on the file system, but also by file size, modification time or file extension. All information can be displayed as pie chart, histogram or detailed list.
Name: JDiskReport
Developer: Karsten Lentzsch (JGoodies, www.jgoodies.com)
Current version: 1.3.1 (July 30, 2008)
License: freeware, partially closed source; open source release planned [see here]
Platform: cross-platform
Tuesday, August 11, 2009
git: write a single commit to file
Write the commit to file:
Apply the commit from the file:
git diff-tree <commit> -p --pretty > <filename>You can also use git format-patch, but that is more specialized for patches to be submitted by email.
Apply the commit from the file:
git apply <filename>Here is an excerpt from the git diff-tree manual page:
When "git-diff-index", "git-diff-tree", or "git-diff-files"
are run with a -p option, "git diff" without the --raw option,
or "git log" with the "-p" option, they do not produce the output
described above; instead they produce a patch file. You can
customize the creation of such patches via the GIT_EXTERNAL_DIFF
and the GIT_DIFF_OPTS environment variables
Monday, August 10, 2009
Scite: add a vertical line at column 80
A tip for the SciTE editor: this is interesting if you want your code to be easy to print and display without horizontal scrolling.
Add the following to the global or user properties file:
Add the following to the global or user properties file:
edge.column=80This is an excerpt of the SciTE documentation for the edge property:
edge.mode=1
edge.colour=#C0DCC0
Indicates long lines. The default edge.mode, 0, does not
indicate long lines. An edge.mode of 1 uses a vertical
line to indicate the specified column and an edge.mode
of 2 changes the background colour of characters beyond
that column. For proportional fonts, an edge.mode of 2
is more useful than 1.
Sunday, August 9, 2009
Compact guide to less commands
Movement
Arrows, page up/down, home/end keys all work as expected. In addition the following are sometimes useful:d/u | Move half page forward / backward |
20g | Move to line 20 |
20% | Move to the 20% point |
ma/'a | set markpoint "a" / move to markpoint "a" (also instead of "a" any other lowcase letter) |
Search
/pattern | search pattern forward |
?pattern | search pattern backward |
n | next match |
N | previous match |
/!pattern | search non-matching line |
Esc-u | turn match highlighting on/off |
Misc
= | show info about the file and current position |
! command | execute command and show its output |
s filename | save to file (if the input was a pipe) |
GIT_PAGER and LESS colors
I was wondering why, working on two different machines, I was able to display git-log and git-diff colors on one machine and not on the other, despite the same git configuration file ~/.gitconfig:
and no particular settings regarding colors in <repository>/.git/config
[color]
diff = auto
status = auto
branch = auto
log = auto
Although I was not using a Mac, the solution was in this blog post: that is, setting the GIT_PAGER variable to cat was already solving the problem; GIT_PAGER sets which program should be used by git to display for example the diff and log output (default less).
The actual problem was in less and how it outputs ANSI color ESC codes. A solution, similarly to what suggested in that post, was to set the LESS variable to -R and leave GIT_PAGER unset, as it originally was. Regarding the other options suggested in the blog post for LESS, -X and -e have no influence on color display, and -R is probably better than -r; in particular, this in an extract of the less manual, at the -r/-R section, explaining why:
The actual problem was in less and how it outputs ANSI color ESC codes. A solution, similarly to what suggested in that post, was to set the LESS variable to -R and leave GIT_PAGER unset, as it originally was. Regarding the other options suggested in the blog post for LESS, -X and -e have no influence on color display, and -R is probably better than -r; in particular, this in an extract of the less manual, at the -r/-R section, explaining why:
-r or --raw-control-chars
Causes "raw" control characters to be displayed.
[...] Warning: when the -r option is used, less cannot
keep track of the actual appearance of the screen
(since this depends on how the screen responds to
each type of control character). Thus, various display
problems may result, such as long lines being split
in the wrong place.
-R or --RAW-CONTROL-CHARS
Like -r, but only ANSI "color" escape sequences are
output in "raw" form. Unlike -r, the screen appearance
is maintained correctly in most cases. [...]
Thursday, July 23, 2009
Conversion of CR+LF to LF
The following will convert all files also in subdirectories
exept for those starting with a dot (skips also such directories).
This works if dos2unix is available, of course; otherwise use sed.
find ^.* -exec dos2unix '{}' \;
I needed it to convert the content of a git repository (that's
why I skipped the . files: I didn't want .git/* files to be included).
Of course you can write better matching chriteria, but this worked
fine for my case.
exept for those starting with a dot (skips also such directories).
This works if dos2unix is available, of course; otherwise use sed.
find ^.* -exec dos2unix '{}' \;
I needed it to convert the content of a git repository (that's
why I skipped the . files: I didn't want .git/* files to be included).
Of course you can write better matching chriteria, but this worked
fine for my case.
Thursday, July 16, 2009
Tiny implementation of a suffix array in Ruby
class SuffixArray
attr_reader :suf, :string
def initialize(string)
@string = string
@suf = (0..string.size-1).sort_by{|i|@string[i..-1]}
end
end
Monday, April 20, 2009
Count distinct values in all columns of a table
The following rake task will show the number of different values of each column of a given table in the database. I wrote it to see statistics on which columns are really used.
namespace :db do
desc "Count distinct values for each non empty column of a table (TABLE=xxx) "+
"optionally using a condition (WHERE=\"yyy\")"
task :count_distincts => :environment do
raise "Specify option TABLE=<table_name>" unless ENV["TABLE"]
puts "Distinct values in table #{ENV['TABLE']}:"
puts "(condition: where #{ENV['WHERE']})" if ENV['WHERE']
c = ActiveRecord::Base.connection
columns = c.select_all("describe #{ENV['TABLE']};").map{|a|a["Field"]}
columns.each do |col|
sql = "select count(distinct #{col}) as c from #{ENV['TABLE']}"
sql << " where #{ENV['WHERE']}" if ENV['WHERE']
n = c.select_one(sql)['c']
puts "#{col}: #{n}" unless n == "0"
STDOUT.flush
end
end
end
Thursday, April 16, 2009
assert.h
Assertions: you express your expectations in a certain position of the code, and this way you avoid bugs that would otherwise very difficult to trace.
In C use the standard library macro assert. You must #include <assert.h>, then you assert(expression); in the code, and if the expression is evaluating to 0 / false, the program aborts with an error msg printed on sterr that tells you which assertion fails and where it is (file/line).
It is also possible to turn assertions off, adding a #define NDEBUG before the #include <assert.h>.
In C use the standard library macro assert. You must #include <assert.h>, then you assert(expression); in the code, and if the expression is evaluating to 0 / false, the program aborts with an error msg printed on sterr that tells you which assertion fails and where it is (file/line).
It is also possible to turn assertions off, adding a #define NDEBUG before the #include <assert.h>.
Tuesday, April 14, 2009
Why main is int and not void.
The standard says so. And it actually makes a difference: here is a detailed discussion about this topic: http://users.aber.ac.uk/auj/voidmain.shtml
Why do you have to pass scanf the address of the variable to write in?
As I was learning C the first time, I remember I was asking this myself... I would have preferred it to return the scanned values, something like:
myVar = scanf("%s") /* don't do this :) */
but of course, in this case (1) you could assign only 1 variable, (2) you would have not had the return value (number of read items)...
Here is some discussion of it: ...
Turn colors on in vi/vim
If the color syntax highlighting is off, you can turn it on by editing (or creating) ~/.vimrc, adding the following line:
:syn on
Monday, April 13, 2009
square root
When I have some time, I will have a look to this algorithm to calculate the square root of a number:
int sqrt(int num) {
int op = num;
int res = 0;
int one = 1 << 14; // The second-to-top bit is set: 1L<<30 for long
// "one" starts at the highest power of four <= the argument.
while (one > op)
one >>= 2;
while (one != 0) {
if (op >= res + one) {
op -= res + one;
res += one << 1;
}
res >>= 1;
one >>= 2;
}
return res;
}
(from Wikipedia)
Saturday, April 11, 2009
XSLT
XSLT: take an XML doc and, through a sort of "stylesheet", make something with the data (another XML doc, or perhaps a XHTML doc). I don't know this language but I guess it's rather useful (although I have no use for it at the moment).
related stuff: libxml, libxml2, libxslt, xsltproc
Links:
- specifications@w3: http://www.w3.org/TR/xslt
related stuff: libxml, libxml2, libxslt, xsltproc
Links:
- specifications@w3: http://www.w3.org/TR/xslt
- tutorial@w3: http://www.w3schools.com/xsl/
- http://en.wikipedia.org/wiki/XSLT
- http://en.wikipedia.org/wiki/LibXML
- http://en.wikipedia.org/wiki/XSLT
- http://en.wikipedia.org/wiki/LibXML
PragProg media: Expression Engine Techniques
I often have a look at the list of media of "PragProg" (http://www.pragprog.com/categories/all?sort=pubdate). The newest title is the screencast serie: "Expression Engine Techniques"
Well I definitely don't need it, anyway I'm always curious, so here is the info I collected (mainly from Wiki) about the topic:
ExpressionEngine http://expressionengine.com/ is a CMS, developer: EllisLab, there is a free version, and two paid ones. A "2.0" is expected in 2009, will be based on CodeIgniter, which is a PHP framework.
capistrano using git
The cap deploy:update task when you are using git runs the following git command:
git checkout -q -b deploy 3fe75somehash.....
meaning :
git checkout -q -b deploy 3fe75somehash.....
meaning :
- creates a new branch named "deploy" (what happens if there is already one called like that?)
- the source of the branch is the commit identified by the given hash
- -q option is "quiet mode"
rsync and symbolic links
some possible behaviours of rsync (from the man page):
(1) SKIP:
default case => symlinks are simply not followed
(2) COPY THE LINK:
"symlinks are recreated with the same target on the destination"
rsync --links
also: rsync --archive implies --links
(3) FOLLOW THE LINK
rsync --copy-links
safe/unsafe:
relevant for case 3 is the "safe" vs. "unsafe" difference which I did not understand good.
This is what is written: "An example where this might be used is a web site mirror that wishes ensure the rsync module they copy does not include symbolic links to /etc/passwd in the public section of the site. Using --copy-unsafe-links will cause any links to be copied as the file they point to on the destination. Using --safe-links will cause unsafe links to be ommitted altogether."
Always back up on a dedicated partition or disk!
My home dir on a certain server was backed up by another company. I set under my home a soft link to another location (several Gb of data). What I didn't know: their backup script was rync-based *with* the option --copy-unsafe-links. Rsync followed the link and backed up tons of stuff clugging the backup hard disk. They used the same hard disk and the same partition also for another function: email server. So that softlink disrupted the email server! At first I though I was to be blamed, but after a second thought, I understood, that it's not 100% true: they should have *never* run the mail server on the same partition where they backed up that stuff!
Ruby and GUIs
Is there a good ruby GUI toolkit?
I want to write a simple app, with a simple DB backend, just to keep some notes. As I want it to run on my laptop, I don't want any webserver running, so no rails app (and also because I want to do some gui programming, it's years I am only working on console or web apps).
I started earlier this morning my Google quest to answer this question, I am not so far yet. I am no expert in GUIs.
Some disorganized thoughts:
- Java of course is a good choice, isn't it? Yes I want to learn Java again (I learned it 10 years ago, and not using it a long time, so I guess my knowledge is totally out-of-date). But now I want just to write a little small app in that simple little lovely ruby language.
- I guess most Win apps are based on "native" widgets. For that maybe you need VS or the like and I am in this moment in no mood to be a MS fan. And I want something cross-platform.
- There a libraries, and like always in IT a lot of names and acronymes, just to confuse stupid newbies like me. QT is one, I think, then there are others (I think some GUI library-flame is also the reason why Gnome and KDE are 2 different desktops, isn't it?). So I guess you use one of that with some good ruby bindings and you are on it. Right?
- I read of Shoes: very simple, maybe good idea, but probably just for beginners, I don't know if I want to waste my time with it. Looks like is just something for kids learning programming?
Friday, November 7, 2008
Thursday, November 6, 2008
app helper in the rails console
The app helper is cool. You can use it to simulate requests to your app:
e.g.
app.get "/"
Friday, August 22, 2008
Hide attributes in Rails / ActiveRecord
Let's assume you have a table "entities" that was created by this Rails migration code:Person and Organisation will be subclasses of Entity (which of course is a subclass of ActiveRecord::Base), using single table inheritance.
When a new object is created (e.g. Person.new), this is a real instance of Person. The list of attributes comes in this case from the public class method columns of ActiveRecord::Base. When records are fetched using a finder method, however, (e.g. Person.first), ActiveRecord goes another way, and calls the private class method instantiate of ActiveRecord::Base, which takes an hash of attributes/values as argument, which conversely derive from the results of the sql query. So to hide attributes you have to hide them both in columns and in instantiate and the trick is done.
create_table :entities do |t|t.string :typeend
t.string :title
t.string :first_name
t.string :last_name
t.binary :logo
If people have no logo and organisations have no title, first name and last name, maybe we would like to hide this columns from their respective classes. Of course, you may say that in this case you shouldn't use STI. But if you really want to... ActiveRecord does not provide hiding functionality. All Person and Organisation instances will see all attributes of Entity. This behaviour can be changed as follows.
When a new object is created (e.g. Person.new), this is a real instance of Person. The list of attributes comes in this case from the public class method columns of ActiveRecord::Base. When records are fetched using a finder method, however, (e.g. Person.first), ActiveRecord goes another way, and calls the private class method instantiate of ActiveRecord::Base, which takes an hash of attributes/values as argument, which conversely derive from the results of the sql query. So to hide attributes you have to hide them both in columns and in instantiate and the trick is done.
Monday, August 18, 2008
Default values in Ruby blocks
The ruby parser does not allow default values to be set in blocks, unlike in method signatures. This is of course valid ruby:
However it is possible to simulate the behaviour, creating a de facto signature with default values. For example lets say I want a proc accepting the same parameters as a method defined as
def method(a, b=0)but this isn't:
#...
end
Proc.new {|a, b=0| } ### syntax error!See e.g. this discussion in Ruby forum.
However it is possible to simulate the behaviour, creating a de facto signature with default values. For example lets say I want a proc accepting the same parameters as a method defined as
def a(b, c=1)
:lambda do |*args| # simulated signature: |b, c=1|This has the disadvantage that is not validating the number of arguments, so let's add some validation code to the block:
b, c = args[0], args[1] || 1
end
lambda do |*args| # simulated signature: |b, c=1|Now the block behaves like if it had the signature |b, c=1|.
b, c = args[0], args[1] || 1
# validate number of arguments:
err = "wrong number of arguments"
if args.size > 2
raise ArgumentError, "#{err} (#{args.size} for 2)"
elsif args.size == 0
raise ArgumentError, "#{err} (0 for 1)"
end
end
Sunday, August 17, 2008
ActiveScaffold and LOB sorting in Oracle
If you are using ActiveScaffold and an Oracle DB, you will notice that you can't natively sort on LOB columns. If you click on the table header for a LOB column, you will get an error message. A way to fix this is to sort using the substr function and pass it to
To make it simpler, I solved the matter including in
method:
config.columns[].sort_by :sql
.To make it simpler, I solved the matter including in
ActiveScaffold::DataStructures::Column
amethod:
def sort_as_lob(lenght = 50, offset = 1)which I use on LOBs column declaring in the active scaffold config block:
sort_by :sql => "dbms_lob.substr(#{name},"+
"#{lenght},"+
"#{offset})"
end
active_scaffold do |config|
config.columns[:my_lob].sort_as_lob
end
Saturday, August 16, 2008
sort_by => :sql in and polymorphic associations
Assume you have a polymorphic association in Rails.
Now let's say I have a table with all "my objects" and I want to sort it according to a specific column in "another object" (for simplicity let us assume any possible "other object" have a column called "label").
There is two ways to do this. The first one is loading all "my objects" instances, then for each one make a query, according to the association type, to find the other objects, than let Ruby sort by label. This is of course not optimized. Through the
'eager loading' ActiveRecord feature, which I think is in the meantime also available for polymorphic associations, it is probably possible to find a better way. However I wanted a single query, so I did it using SQL;
So in my case another_objects can only be of a few types, so I did it this way, in the controller code:
Actually the need for this came because I wanted to sort by a polymorphic association in an ActiveScaffold based controller, and in this case I am not sure I could have specified an eager loading without too much effort (probably overriding the finder method of the list). So I just wrote the sort_by_sql() as a class method and used it in the config block of my active scaffold:
class MyObject < ActiveRecord::BaseWell, "another object" can now be in any other table of the DB, and in the
belongs_to :another_object, :polymorphic => true
end
my_objects
table there are two columns (assuming you followed the conventions) named another_object_id
and another_object_type
, the first one containing the ID of the object, the second the model name. Now let's say I have a table with all "my objects" and I want to sort it according to a specific column in "another object" (for simplicity let us assume any possible "other object" have a column called "label").
There is two ways to do this. The first one is loading all "my objects" instances, then for each one make a query, according to the association type, to find the other objects, than let Ruby sort by label. This is of course not optimized. Through the
'eager loading' ActiveRecord feature, which I think is in the meantime also available for polymorphic associations, it is probably possible to find a better way. However I wanted a single query, so I did it using SQL;
case
was introduced in the standard, if I am not wrong, in the SQL-92 version. I think that most SQL-DBs comply, SQL-Lite probably excluded (I tested only on Oracle). So in my case another_objects can only be of a few types, so I did it this way, in the controller code:
#You can use this sort_by_sql() method in the
# e.g. types = %w[Cat Dog Mouse]
#
def sort_by_sql(types)
sql = '(case (another_object_type)'
sql << types.map do |type|
'when #{type}
then (select label
from #{type.tableize} t
where t.id = another_object_id) '
end.join('')
sql << 'end)'
end
private :sort_by_sql
:order_by => sort_by_sql(...)
key of the find method to sort by the label method of the polymorphic association with only one query. This was sensibly faster in my case. Actually the need for this came because I wanted to sort by a polymorphic association in an ActiveScaffold based controller, and in this case I am not sure I could have specified an eager loading without too much effort (probably overriding the finder method of the list). So I just wrote the sort_by_sql() as a class method and used it in the config block of my active scaffold:
AllowedTypes = %w[Cat Dog Mouse]That worked fine for me.
def self.sort_by_sql(types)
#...the code up here...#
end
active_scaffold :my_objects do |config|
# ...
config.columns[:another_object].sort_by :sql =>
sort_by_sql(*AllowedTypes)
# ...
end
Thursday, August 14, 2008
Execute a Rake task in another
It is easy to make one task dependent on another, for example:
executes task two, three, then one (I have to test that the order is really this). But how to execute task ":two" in the middle of the code of another task?
Here is the solution:
the ENV assignments and execute call have a similar effect to executing in your shell:
Yeah, Rake is a really easy and cool tool for every scripting need...
task :one => [:two, :three]
executes task two, three, then one (I have to test that the order is really this). But how to execute task ":two" in the middle of the code of another task?
Here is the solution:
desc "This task executes task two in its code!"
task :one do
# ... do domething
ENV['PAR1'] = 'xxx'
ENV['PAR2'] = 'yyy'
Rake::Task[ "two" ].execute
# ... do something
end
the ENV assignments and execute call have a similar effect to executing in your shell:
rake two PAR1 = xxx, PAR2 = yyy
Yeah, Rake is a really easy and cool tool for every scripting need...
Fake migrations for rails < 2.0.2
The following rake task can be used to update the migration pointer without actually migrate in rails databases for rails under 2.0.2. This is sometimes useful if you applied something manually or want to skip some migration anyway.
Usage examples:
rake db:pretend:migrate
rake db:pretend:migrate VERSION=20080730181045
rake db:pretend:rollback
namespace(:db) do
namespace(:pretend) do
desc "Pretend the database migrated 1 step or to VERSION=<nn>"
task :migrate => :environment do
c = ActiveRecord::Base.connection
if ENV['VERSION']
version = ENV['VERSION'].to_i
else
version = c.select_one("select version from schema_info")['version'].to_i + 1
end
c.execute("update schema_info set version = #{version}")
puts "Current version: #{version}"
end
desc "Pretend that the last migration did not happen"
task :rollback => :environment do
c = ActiveRecord::Base.connection
version = c.select_one("select version from schema_info")['version'].to_i - 1
c.execute("update schema_info set version = #{version}")
puts "Current version: #{version}"
end
end
end
oracle adapter and rails migrations
The Oracle adapter for active record has a bug that does not allow to create a dump of the schema using rake. The following code is from http://dev.rubyonrails.org/ticket/10415 and corrects this problem.
require 'active_record/connection_adapters/oracle_adapter'
module ActiveRecord
module ConnectionAdapters
class OracleAdapter
# Returns an array of arrays containing the field values.
# Order is the same as that returned by #columns.
def select_rows(sql, name = nil)
result = select(sql, name)
result.map{ |v| v.values}
end
end
end
end
Wednesday, August 13, 2008
A color picker
Looking for a color picker for a website I am preparing, I found many commercial tools and other open source (unobfuscated js) but not free-licensed.
An open source free licensed ('it’s dual licenced under Creative Commons & GPL') color picker is the Colorjack color picker: I like more the version 1.0.4 than 2.0. .
I saw that the code in Colorjack incorporates a function $() which I would have to rename to avoid conflicts with the Prototype framework... (although I didn't actually test it) so I just looked further and found Scripteka, which is a collection of Prototype extensions. Currently there are links to 119 projects.
At least two of them are color picking related, John Dyer's Colorpicker, released in 2007 under a MIT-style license and Jeremy Jongsma's GPL-licensed Control.ColorPicker, released in April 2008.
Not all the projects linked by Scripteka are free licensed: for example Cooltips is not.
Through a comment in John Dyer's blog I came to nogray color picker which is based on another javascript framework (mooTools) and I didn't find the licensing terms.
It's a pity none of them is available as Rails plugin, maybe with some nice helper methods...
An open source free licensed ('it’s dual licenced under Creative Commons & GPL') color picker is the Colorjack color picker: I like more the version 1.0.4 than 2.0. .
I saw that the code in Colorjack incorporates a function $() which I would have to rename to avoid conflicts with the Prototype framework... (although I didn't actually test it) so I just looked further and found Scripteka, which is a collection of Prototype extensions. Currently there are links to 119 projects.
At least two of them are color picking related, John Dyer's Colorpicker, released in 2007 under a MIT-style license and Jeremy Jongsma's GPL-licensed Control.ColorPicker, released in April 2008.
Not all the projects linked by Scripteka are free licensed: for example Cooltips is not.
Through a comment in John Dyer's blog I came to nogray color picker which is based on another javascript framework (mooTools) and I didn't find the licensing terms.
It's a pity none of them is available as Rails plugin, maybe with some nice helper methods...
Sunday, July 13, 2008
Online lectures
An Italian University (Consorzio Nettuno) is based on recorded lectures in several languages, unfortunately not online (or only for their students) but broadcasted by two channels on an european satellite. I have currently no satellite dish... that's a pity...
On the web there are anyway some resources with lectures and other videotaped matherial better than music videos and jokes. Here are some examples I found, grouped by language:
On the web there are anyway some resources with lectures and other videotaped matherial better than music videos and jokes. Here are some examples I found, grouped by language:
- English
- German
- Italian
- SeventLab (Computer Engeneering), heavy DivX files (ca. 400 Mb)
Thursday, October 11, 2007
bash startup
In interactive mode bash executes some startup scripts. First thing to know is if it is a login shell or not a login shell:
* login shell: (executes more things)
(1) general settings for all users are in: /etc/profile
(2) personal settins may be in: (first one readable)
~/.bash_profile
~/.bash_login
~/.profile
(3) before logout: ~/.bash_logout
* non-login:
>bash => ~/.bashrc
>bash => ~/.bashrc
>bash --norc => nothing
>bash --rcfile filename => specify another rc file
>bash --rcfile filename => specify another rc file
Wednesday, April 11, 2007
Learning C: params from the command line
I want to read two parameters from the command line: a string and a number. The number should be a positive integer smaller than 100.
int main (int argc, char *argv[])
{
char * string;
long int number;
/* are there really 2 parameters? */
if (argc != 3) exit(EXIT_FAILURE);
/* catch them */
string = argv[1];
number = atoi(argv[2]);
/* use them */
printf("String parameter: %s\n",string);
printf("Number parameter+1: %d\n",number+1);
}
The number of parameters (as int) and the parameters itself (as char*) are passed to "main".
Everybody writes main as int main (int argc, char *argv), I guess it's possible to use more descriptive names to the arguments of main, like int main (int argument_counter, char *arguments) but it's probably out of fashion - if you write C you should look serious after all.
The classical beginner's note: It must be considered that argv[0] is the program name (what is useful for example for syntax error messages), so the counter is always actually one more than you intuitively expected and you find the first real parameter as argv[1].
Parameter type: as the parameters are actually always strings, you must convert them using some conversion functions like atoi or strtol (I guess).
Subscribe to:
Posts (Atom)
Labels
software installed on my laptop
(13)
windows software
(12)
personal settings
(10)
linux software
(8)
rails
(7)
shell rc file
(6)
linux basics
(5)
C basics
(4)
color the shell
(4)
ruby
(4)
connection to remote servers
(3)
git
(3)
mutimedia software
(3)
rake tasks
(3)
shell
(3)
active scaffold
(2)
backup
(2)
disk analysis
(2)
less
(2)
oracle
(2)
rails migrations
(2)
rails on oracle
(2)
sql
(2)
ssh
(2)
symbolic links
(2)
top open source projects
(2)
virtualbox
(2)
GUI programming
(1)
MIT
(1)
active record
(1)
algorithms
(1)
applications
(1)
apt
(1)
attr_hidden
(1)
bash
(1)
capistrano
(1)
cms tools
(1)
color picker
(1)
common errors
(1)
cooltips
(1)
data recovery
(1)
data structures
(1)
dvd43
(1)
endline chars
(1)
expression engine
(1)
ftp clients
(1)
gimp
(1)
grep
(1)
handbrake
(1)
hard disk failure
(1)
image conversion
(1)
image editing
(1)
inkscape
(1)
java software
(1)
javascript
(1)
jdiskreport
(1)
linux compact guides
(1)
ls
(1)
online lectures
(1)
oracle lob
(1)
photo manipulation
(1)
php
(1)
polymorphic associations
(1)
prototype
(1)
putty
(1)
rails plugins
(1)
rake
(1)
rsync
(1)
ruby blocks
(1)
scanf
(1)
scite
(1)
script
(1)
scripteka
(1)
sftp clients
(1)
sort_by :sql
(1)
suffix array
(1)
synctoy
(1)
tcsh
(1)
timms
(1)
unlocker
(1)
vector graphics
(1)
vi
(1)
video lectures
(1)
vlc media player
(1)
windows alpha blender
(1)
winscp
(1)
xslt xml
(1)
About Me
- Giorgio Gonnella
- Hamburg, Hamburg, Germany
- Former molecular biologist and web developer (Rails) and currently research scientist in bioinformatics.