Hacktoberfest - The End

The end as come for Hacktoberfest and I have bad news.

I didn’t complete any pull request during the month of October like the challenge demanded. I need not use any excuse, I just didn’t complete the challenge sadly.

I feel sad for not completing the challenge but at the same time, I feel glad that I tried to complete it. Sometimes life gets in the way and the things we plan simply aren’t possible.

I was very much excited to complete it but couldn’t. I won’t give up though - next year I’ll be there again to face the challenge.

Congratulations to the 9839 who succesfuly completed the challenge but above all I’m glad that more than 88000 pull requests were opened in 28366 repositories which I’m sure is very interesting for the OSS community.

I hope that a lot of newcomers have joined the OSS community like I wish to do and that the projects that accepted the challenge and contributions are now better than they were before.

Extracting Data from PostgreSQL

PostgreSQL has an excellent module built specifically for the purpose of extracting data between a file and a table.

This can be used to extract several data in three formats: - Text - CSV - Binary

Sometimes you’d want to extract data to a folder different than the one the database is in. You’d want to extract the data to the machine your logged on instead. This can be done using \copy instead of COPY.

An example of basic usage for extracting data would be, in CSV:

\copy (SELECT * FROM foo WHERE bar IS NOT NULL) TO '/path/to/folder' HEADER CSV

This simple snippet is incredibly useful to generate reports or extract data for analysis, simply and quickly.


Source: - https://www.postgresql.org/docs/9.5/static/sql-copy.html

Let Hacktoberfest Begin!

Earlier this week I read a link post on Hacker News that mentioned Hacktoberfest. Since I’m in love with the German language currently and always like mashup names I thought I’d have a look at it.

Hacktoberfest very similar in its nature to NaNoWriMo but designed and focused on hackers and developers. The event is a partnership between Digital Ocean and Github and aims to “support celebrate open source”. I think it is a brilliant idea.

The goal is simple: make four pull requests on projects hosted on Github between October 1 and October 31 and I’ll win a celebratory T-shirt for completing the challenge.

Since I like open source and I like T-shits, I thought it would be the perfect opportunity to further advance my contributions to open source projects. It doesn’t need to be complicated and out of my current capabilities. Just contribute to an open source currently looking for help. Simple.

I have recently contributed to the translation of KeeWeb by helping in translating it into Portuguese. It was released last week. It still needs some fixing but I was very happy to be able to contribute to such an awesome project.

I hope Hacktoberfest will help in contributing to more open source projects that I find awesome.

To track the challenge, and my results, I’ll be sharing my pull requests whenever I submit them, and follow up if needed.

Let the hacktoberfest begin!

GPU Memory Splitting on Raspbian Jessie

Lately, I’ve running a little side project with the aim of setting up a self-hosted array of software in one my old, beat up laptops.

While I was doing this, I thought that it would be as useful and interesting to try and set up a my personal home theatre - I chose Kodi for the job.

Even though it only supports 15.2 officially on Raspbian, there’s an unofficial repository using 16.1 which means that I could use the full power of Kodi on a low power hardware such as RPi 2.

I started using it and unfortunately it always seem to break when I was trying to stream videos. I couldn’t understand why. I changed Skins, Add-ons, streaming sources and what not. I couldn’t figure out why, some videos broke and some didn’t.

Off to Google I went.

It took me about 10 to 15 minutes to understand what might be the problem. GPU processing was being streched to its maximum capabilities.

On the Raspberry forums I was able to understand that GPU doesn’t have it’s own RAM, and that GPU is responsible for the graphics processing involved in video playback and streaming.

I thought to myself that if I tried to watch something lower than HD…maybe it’d work.

I turned on Kodi and started testing. Lower than HD video…worked like a charm. Higher than HD video…crash.

After the problem origin was found, it was time to understand the solution which turned around GPU RAM I was sure by now.

After googling some more, I found that it was possible to configure Raspbian memory splitting directly from raspi-config:

sudo raspi-config

Select Advanced Options:

raspbian-advanced-configurations

Select A3 Memory Split:

raspbian-memory-split

Configure depending on your needs. I went with 265:

raspbian-memory-values

Select OK. Select Finish and reboot your Raspberry Pi.

This worked for me and helped me achieve better graphics performance on Raspbian.

wifi-password in Perl

tl;dr

I wrote a new little script that shows you the password of the wifi you are currently on using Perl. It can be found here.

$ wifi-password.pl -h

Usage wifi-password [OPTIONS] [SSID]

Options:
 -v, --version    Output version.
 -h, --help    This message.

Some Background

Last week I had some time off, in between jobs, and I chose to use one of those afternoons to implement a new script in of the new languages I’m expected to use in my new job.

Based on that, the options were:

  • PHP
  • Perl
  • ColdFusion Markup Language

ColdFusion would require me to run a server, I checked and the only good open source resource I was able to find was Railo which unfortunately has been down for quite awhile - off it goes.

PHP would have been a great choice but I have some light experience with PHP and an afternoon’s work wouldn’t help much more in the light knowledge I already have - off it goes.

In comes Perl, which I had no knowledge off and actually wanted to try.

Where the Idea Came From

During that same time period, while I was browsing across Hacker News, I came across this great little project written in Shell - wifi-password.

I found the script so simple and elegant that I wished I had thought about it and written it first. So I just decided to write one with Perl.

Even though it’s only supported in Ubuntu environments (at least that’s the only environment I have tested it with) I found it extremely interesting to write and learn the simple basics of Perl syntax which was my main goal.

The Actual Code

At the beginning we start by using strict and warnings along with a shebang:

#!/usr/bin/env perl

use strict;
use warnings;

We use use strict; because basically it prevents a shitload of, as per the documentantion, “expressions that could behave unexpectedly or are difficult to debug, turning them into errors”.

use warnings; is used to enabled the compiler warnings when interpreting the code.

NOTE: warnings should only be used for non-production running scripts. It’ll trigger warnings while the code is running so it will be annoying for end users if it is used.

After that, we set some global variables using the my keyword:

my $version        = "0.0.2";
my $networkManager = "/etc/NetworkManager/";
my $ssid           = "";
my $pwd            = "";

I used my because I didn’t intend to write a Perl module neither did I intended for the variables to be used elsewhere other than this file. According to the documentation the my keyword restricts the scope of a variable to “A my declares the listed variables to be local (lexically) to the enclosing block, file, or eval.”.

Then I went to define a simple subroutine that I could call whenever users needed some help:

# Help message subroutine.
sub help() {
    print("\nUsage wifi-password [OPTIONS] [SSID]\n");
    print("\nOptions:\n");
    print(" -v, --version\tOutput version.\n");
    print(" -h, --help\tThis message.\n");
    print("\n");
}

Another subroutine to check for installed dependencies - because the script has 5 dependencies with few lines of code:

# Check dependencies.
sub checkDependencies() {
    if    ( `which perl` eq "" )  { die "ERROR: perl not installed.\n\n" }
    elsif ( `which nmcli` eq "" ) { die "ERROR: nmcli not installed.\n\n" }
    elsif ( `which cat` eq "" )   { die "ERROR: cat not installed.\n\n" }
    elsif ( `which grep` eq "" )  { die "ERROR: grep not intsalled.\n\n" }
    elsif ( `which sed` eq "" )   { die "ERROR: sed not installed.\n\n" }
}

Then we validate for arguments. Only 2 arguments are evaluated at a time even though there is no limit of arguments to be passed.

Apart from that, to make it faster to develop, it is only possible to pass either a flag or the optional SSID - if both are passed the flag is parsed but the SSID is not:

if ( $#ARGV > 0 ) {
    help();
    exit 1;
}
elsif ( $#ARGV > -1 ) {
    if    ( $ARGV[0] eq "-v" ) { print "$version\n"; exit 1; }
    elsif ( $ARGV[0] eq "-h" ) { help();             exit 1; }
    else                       { $ssid = $ARGV[0] }
}

After that we just call checkDependencies() and we are done with validation.

Next, we check if an SSID was passed. If it wasn’t we just go ahead and check the SSID to which we are connected at the moment. We do this using nmcli, grep along sed, inside a one liner, to retrieve the line from the nmcli which has the “connected” keyword.

We parse the result through a regex that will trim all whitespace that might still be in the string:

# Set SSID or use default.
if ( $ssid eq "" ) {
    $ssid =
`nmcli -t -f type,state,connection d | grep wifi | grep connected: | sed "s/^wifi:connected://"`;

# http://stackoverflow.com/questions/3931569/how-can-i-remove-all-whitespaces-and-linebreaks-in-perl
    $ssid =~ s/\s+//g;

    unless ( $ssid ne "" ) {
        die "ERROR: SSID couldn't be found. Are you connected to wifi?\n\n";
    }
}

I really fancy unless for error handling. It’s a great addition to Perl.

Finally, we just verify that, in fact, there’s a network connection with the defined SSID (this is especially useful if the SSID was given instead of fetched), fetch the password from the NetworkManager files and print the password along with the SSID:

# Verify that a Network connection exists.
my $path = "$networkManager" . "system-connections/$ssid";

unless ( -e $path ) {
    die "ERROR: SSID \"$ssid\" is not defined on this machine.\n\n";
}

# Read from file.
my $command = "sudo cat $path | grep psk= | sed \"s/^psk=//\"";
$pwd = `$command`;

# Print SSID & PWD.
print "SSID: $ssid\n";
print "Password: $pwd\n";

I found that NetworkManager stores its passwords inside files named with the SSID in /etc/NetworkManager/system-connections/[SSID] because of that there’s a need to run this script as a super user. I found this information inside the source code for wifi-password.

Even though it is a very simple script it was a manageable, interesting afternoon spent slowly building the script and learning Perl’s basics.

Hello Internet

This is my first post to test my new Jekyll powered website. I’m developing this website to showcase my experiments and to have a permanent hub for my online persona.

Yours truly,
Hugo Martins