16 de octubre de 2014

How to undo a git rebase / squash / fixup? (a.k.a. "we all poop it sometimes")



Today I had a bit of a mess in one of my PRs by doing an interactive rebase (git rebase -i HEAD~) and a fixup / squash.

Now: What to do now, after doing the wrong squash?

Good news: virtually everything can be undone in GIT :)

"git reflog" was what I was looking for to put thing where they were before:

$ git reflog

At this point you'll see a long list of commits like this one:

 $ git reflog
e074e05 HEAD@{0}: rebase: aborting
b3885fe HEAD@{1}: rebase -i (start): checkout HEAD~7
e074e05 HEAD@{2}: pull origin master: Merge made by the 'recursive' strategy.
b3885fe HEAD@{3}: rebase -i (finish): returning to refs/heads/ticket
b3885fe HEAD@{4}: rebase -i (pick): something
....

Then you need to pick the action you want to go back to and reset to it with this command:

$ git reset --hard HEAD@{N}

Where "N" is the number between curly braces from the list of commits displayed by git reflog.

Et voilà!

26 de septiembre de 2014

Get file extension from filename with Python


Several times I had to put some code together some Python code to get the extension from a filename.

It was required for it to work OK with complex extensions, like ".tar.gz", and remain functional when you have dots in the filename, like in this case: "some.filename.1.v2.tar.gz".

After trying several options, the one that did the trick was using regular expressions, wrapped in a little function.

Here the case for keeping the ".", and getting the extension in the fashion ".tar.gz" or ".png":



Here the case for not keeping the ".", and getting the extension in the fashion "tar.gz" or "png":




Et voilà! :D

26 de junio de 2014

How to revive Mac App Store when its stuck forever waiting...


How to revive Mac App Store when its stuck forever waiting...

I recently went to the App Store application on my Mac to download the Lion version of Apple's Xcode development tools.  I was prompted for my Apple ID and password (which I provided) and then I left the App Store app on its own to complete the download and install.  After about 30 minutes I went to check in on its progress and saw that it had downloaded exactly 0 bytes.  The status on the Purchased page was Waiting...

Waiting for Godot

...and waiting and waiting.  I quit and restarted App Store and still my downloads were stuck waiting.  I dug around a little and it turns out this is not an uncommon problem but the remedies suggested were all over the place.  Everything from installing updates and deleting caches (both good ideas) to deleting your Library folder (not a good idea).  I like to attack problems like this by starting with the easiest and least invasive measures.

I should state right off the bat that these solutions are meant to incur as little disruption as possible but do require the use of some programs that might not be in your usual repertoire: Activity Monitor and Terminal.  If you are at all uncomfortable using these tools you can always use that old Windows chestnut... reboot.  Yes, rebooting your Mac should have the same affect as solution 1 at least.  But if you don't want the hassle of rebooting then read on.


Solution 1: Kill the Zombie

The App Store uses a process called "storeagent" that runs continuously in the background.  It seems that sometimes this process can go a little wonky and fail to respond to requests to download app purchases.  What we want to do is kill this zombie process (it will start up again automatically).

Start Activity Monitor
First, quit the App Store application (e.g. by selecting "App Store->Quit App Store" or pressing Cmd-Q or click the red dot in the top left corner of the window).  Start upActivity Monitor (you'll find it on Lion's launchpad in the Utilities collection or using Finder in Applications/Utilities/Activity Monitor.  Activity monitor is used to display the processes running on your Mac.  You could go hunt around for the process called "storeagent" but an easier method is to enter "storeagent" in the Activity Monitor filter box as illustrated below (some of the numbers will be different).  Highlight the "storeagent" process by clicking on it.  The "Quit Process" stop sign should become available.  Go ahead and click on Quit Process to kill "storeagent". You'll be asked if you really want to quit the process.  Confirm by clicking "Quit".  Normally this should do the trick and the process will disappear immediately and be replaced a little while later by a new process (with a different number in the PID column).  However, if the process is a deep zombie, you will need to click "Force Quit" in the confirmation dialog.  Do this only as a last resort as it is possible to damage system files by using Force Quit too often. 

Activity Monitor Filtered for "storeagent".  Note the "Quit Process" stop sign is now clickable.
Now go ahead and restart the App Store application.  Click on Purchased, find your purchase and select Resume. After you enter your Apple ID and password your purchase should start downloading.   However, if the App Store is still stuck in Waiting move on to Solution 2.

Solution 2 - Purge the Caches

Solution 1 has always worked for me but I've heard of cases where more intervention is required.  If this fits your situation then go ahead and quit the App Store application again.  Now we do some typing into Terminal. You can find Terminal in the same Utilities folder that you found Activity Monitor.  When you start it, it should look something like below.

At the Terminal prompt
What we're going to do is use some commands to delete App Store caches.  You do not need to worry about deleting these files since, by definition, they are a copy of what is already stored on the Apple servers and will automatically be restored.  Type of the following commands exactly as you see below (in fact, go ahead and copy and paste them into Terminal).

rm -r ~/Library/Caches/com.apple.appstore
rm -r ~/Library/Caches/com.apple.storeagent
rm ~/Library/Preferences/com.apple.appstore.plist
rm ~/Library/Preferences/com.apple.storeagent.plist
rm ~/Library/Cookies/com.apple.appstore.plist

Now go and do the steps in Solution 1 again.  Once you're done that, start up App Store, go to Purchases, and select Resume on your download.  Enter your Apple ID and password and you're off to the races.

Source: http://scheyeniam.blogspot.com.ar/2011/08/how-to-revive-mac-app-store-when-its.html?spref=bl

How to merge two folders in Mac OS X or Linux


Short answer


Using the command line (Terminal):
cp -r -n ~/Desktop/src/* ~/Desktop/destination/
The command above adds the src content and the subdirectories to the destination without overwriting the content already present in the destination.

Long answer

Even if the content overlaps, you can still use cp to do it. Assume that you have two folders on your desktop: the src and the destination folders and you want to merge src into destination:
enter image description here
To merge, just do:
cp -r ~/Desktop/src/* ~/Desktop/destination/
NOTE When you use this, the content in src overwrites the content in the destination folder and adds the extra stuff that are missing in the destination. It shouldn't matter if you just want to add the missing files from src into destination.
ALSO it doesn't matter how many subdirectories are there, it will just go through each folder recursively and it will overwrite the content and will add the stuff that is missing in the destination folder.
BUT
PITFALL If you have huge files (like video files), you don't want to wait until everything is overwritten, it adds a lot of overhead.
PITFALL SOLUTION: Instead, you can use the -n flag to skip the overwriting:
cp -r -n ~/Desktop/src/* ~/Desktop/destination/
This is the description of the -n flag from the man page:
man cp
 -n    Do not overwrite an existing file.  (The -n option overrides any
       previous -f or -i options.)
Further Reading
  1. http://stackoverflow.com/questions/5088332/overhead-of-a-flag-in-cp-command
Source:

7 de abril de 2014

Installing MySQL-Python in Mac OS X Mavericks

If you are in Mac OS X Mavericks and while trying to install MySQL-Python you run into this error:



This is a what makes the trick:



Then, pip works its way towards installing MySQL-Python OK:



Voilà!

26 de marzo de 2014

Create a global GIT hook to check Flake8 before each commit

First, let's install Flake8:




Note It is important to set the right version when installing Flake8, as there there are some versions that are bugged and will prevent the GIT hook to work (like the 2.1.0 version).

Now, let's setup global git commit hooks:

1. Enable git templates:



This tells git to copy everything in ~/.git-templates to your per-project .git/ directory when you run git init

2. Create a directory to hold the global hooks:



3. Write your hooks in ~/.git-templates/hooks.

For example, here's a post-commit hook (located in ~/.git-templates/hooks/post-commit):



4. Make sure the hook is executable.



5. Re-initialize git in each existing repo you'd like to use this in:




NOTE if you already have a hook defined in your local git repo, this will not overwrite it.

Now, let's create a global git pre-commit hook:


Finally, let's take it for a spin!

Go to some of your repos and re-initialize it to take the pre-commit hook:


Now edit some of your Python files, introducing some violation (like a >80 columns line, only one blank line between function/class definitions, etc.) and try to commit it:


Voilà!



8 de enero de 2014

Custom sorting function as parameter to sorted() in Python


So, I am working with Python and I come across a list of image names,  and I'd like to have them sorted. So, I call the sorted function with this list:



HA! It doesn't work as I would have liked it to. So, what can we do to achieve the sorting of the list as we want it?

Python's standard library sorted function, as you can see here, has an optional cmp parameter, through which you can pass a custom comparison function.

Going back to my example, I needed the images to be compared by the number in their name. I made this little function to achieve this goal:



And now let's put altogether:



TA DA!