Tuesday, December 11, 2012

Mac OS X - Terminal Tricks


This article covers some useful tricks in the Mac OS X terminal. Search for hidden files, Switch between the 32 bit and 64 bit kernel, fixing kernel extensions, enabling NTFS support and more.

  1. Finding absolutely anything on your Macintosh
  2. How to tell if you’re running the 32 bit or 64 bit kernel in Mac OS X Snow Leopard
    1. Method 1
    2. Method 2
  3. How to switch between 32-bit and 64-bit kernel mode in Mac OS 10.6
    1. Method 1 - Startup key combination (for current startup only)
    2. Method 2 - On-disk setting (persistent)
  4. Delete all files anywhere on your Mac which match a search term
  5. Working with Kernel Extensions
    1. Load kext
    2. Unload kext
    3. Troubleshooting kext (Mac OS 10.6.2 and above)
    4. Change Ownership of kext
  6. Enable NTFS drive support in OSX Snow Leopard or Lion

Finding absolutely anything on your Macintosh

The Spotlight feature doesn't search the Library or hidden locations.

Aside from Spotlight feature, OSX also has a Find feature. Unlike Spotlight you can configure OSX's Find feature (CMD+F) look for system files by performing the following:

  1. Press CMD+F to open the Find window.
  2. Towards the left of the window, just above where the search results would appear, you'll a pull-down menu which says (by default)Kind. Click this, and choose Other.
  3. In the window that opens, scroll down, find, and check off System files.
  4. Click OK to close the window.
  5. Now, you can perform searches which will include System files in the results. To do this, select the following criteria for your search:
    System files include.

However, even this will not search all files on your Mac (i.e. items in hidden folders). To search all locations for a file, perform the following:

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    sudo find / -name "*Bicycles*"

    ...where in-place of Bicycles you enter your search term.
  3. You will be presented with a list of file-paths to any items whose file name contains your search term. The search can take a long time to complete. When the command prompt is displayed again (the same one which initially appeared at the top of the Terminal window when you first opened it), you'll know the search has completed.

How to tell if you’re running the 32 bit or 64 bit kernel in Mac OS X Snow Leopard

Summary
Snow Leopard 10.6 is the first Mac OS X version to ship with a 64 bit kernel. While our Mac has to have a 64 bit processor to utilize that kernel, some 64 bit Macintoshes do not default to using the 64 bit kernel.

Note: Not all applications support the 64 bit kernel. When using applications which only support 32-bit kernel mode, be sure you are running your computer using the 32-bit kernel.

Method 1

  1. In the upper-left of the screen, select the Apple menu, and choose About this Mac.
  2. In the window that opens, click the More Info button.
  3. In the Contents pane on the left-hand side of the window that opens, select Software.

    On the right, under System Software Overview, if 64-bit Kernel and Extensions is set to No, you are running a 64-bit kernel.

Method 2

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    uname -a
  3. If you’re using a 32 bit Kernel in Mac OS X, you'll see:

    Darwin iMac.local 10.0.0 Darwin Kernel Version 10.0.0: Fri Jul 31 22:47:34 PDT 2009; root:xnu-1456.1.25~1/RELEASE_I386 i386

    If you’re using a 64 bit Kernel in Mac OS X, you'll see:

    Darwin iMac.local 10.0.0 Darwin Kernel Version 10.0.0: Fri Jul 31 22:47:34 PDT 2009; root:xnu-1456.1.25~1/RELEASE_X86_64 x86_64

How to switch between 32-bit and 64-bit kernel mode in Mac OS 10.6

Summary
Mac OS X v10.6 Snow Leopard includes a 64-bit kernel. On hardware that supports the 64-bit kernel, you can choose whether to start up (boot) your Mac using the new 64-bit kernel or the earlier 32-bit kernel.

Products Affected
Mac OS X Server 10.6, Mac OS X 10.6, Mac Pro (Mid 2010)

Method 1 - Startup key combination (for current startup only)

  • If your Mac uses the 32-bit kernel by default, but supports the 64-bit kernel, you can start up using the 64-bit kernel by holding the 6 and 4 keys during startup.
  • If your Mac uses the 64-bit kernel by default, you can start up with the 32-bit kernel by holding the 3 and 2 keys during startup.

    Note:Your Mac will revert to the default kernel the next time you reboot it.

Method 2 - On-disk setting (persistent)

  • To select the 64-bit kernel for the current startup disk, perform the following:
    1. Open Macintosh HD > Applications > Utilities > Terminal
    2. In the window that opens, type the following, then press Enter

      sudo systemsetup -setkernelbootarchitecture x86_64
    3. Restart your computer.
  • To select the 32-bit kernel for the current startup disk, perform the following:
    1. Open Macintosh HD > Applications > Utilities > Terminal
    2. In the window that opens, type the following, then press Enter

      sudo systemsetup -setkernelbootarchitecture i386
    3. Restart your computer.

      Note: This setting is stored in the /Library/Preferences/SystemConfiguration/com.apple.Boot.plist file and will take effect every time you start up from this disk. If you start up from a different disk, the setting on that disk, or the hardware default, will take effect.

Delete all files anywhere on your Mac which match a search term

Be very careful with this one. There's almost no reason you'd need to do this. If you're trying to delete something, you should try any and all of the following first (listed in order):

  1. If you're deleting an application, just find it, drag it to the trash, and empty the trash.
  2. Finding the file using Spotlight, the OSX Find Feature, or the method described above. Then delete it yourself.
  3. AppZapper - Free utility which aids in completely uninstalling programs.
  4. AppCleaner - Free utility which aids in completely uninstalling programs and can catch files that Appzapper may miss.
  5. DesInstaller - Simple Macintosh utility for uninstalling device drivers. Be careful not to delete the wrong file(s).
  6. Leopard Cache Cleaner - Powerful Utility Useful for Installation Problems

If these don't accomplish what you needed, and your problem is serious enough, consider reinstalling your Mac OS, or using Time Machineto revert to a previously saved working state (You are using Time Machine right?).

The main point here is that the procedure below should be used at-your-own-risk, with extreme caution, and as an absolute last resort. Anything deleted using this method is not undo-able. If you delete the wrong file, worst-case-scenario, you could remove a critical system file, preventing your Mac from booting.

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    sudo find / -name "*Bicycles*" –delete

    ...where in-place of Bicycles you enter your search term.
  3. Any items whose file name contains your search term will be permanently deleted from your computer. The operation can take a long time to complete. When the command prompt is displayed again (the same one which initially appeared at the top of the Terminal window when you first opened it), you'll know the operation has completed.

Working with Kernel Extensions

Load kext

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    sudo kextload /System/Library/Extensions/Bicycles.kext/

    ...where in-place of Bicycles you enter the name of the Kernel Extension you'd like to load.

Unload kext

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    sudo kextunload /System/Library/Extensions/Bicycles.kext/

    ...where in-place of Bicycles you enter the name of the Kernel Extension you'd like to unload.

Troubleshooting kext (Mac OS 10.6.2 and above)

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    sudo kextutil /System/Library/Extensions/Bicycles.kext

    ...where in-place of Bicycles you enter the name of the Kernel Extension you'd like to look at.

    You'll be presented with information on the status of the extension.

Change Ownership of kext

Often, using the troubleshooting step above, you'll find that you have ownership issues with a kext.
  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    sudo chown -R root:wheel /System/Library/Extensions/Bicycles.kext

    ...where in-place of Bicycles you enter the name of the Kernel Extension you'd like to change.

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    sudo chmod -R 755 /System/Library/Extensions/Bicycles.kext

    ...where in-place of Bicycles you enter the name of the Kernel Extension you'd like to change.

Enable NTFS drive support in OSX Snow Leopard or Lion

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    diskutil info /Volumes/volname

    ...where volname is the name of the NTFS volume.
  3. From the output, copy the Volume UUID value to the clipboard.
  4. Type the following, then press Enter:

    sudo nano /etc/fstab
  5. Then Type the following:

    UUID=111-111-111 none ntfs rw

    ...where 111-111-111 is the UUID you copied.
  6. Save and quit the nano function by typing Ctrl+XY, and then Enter.
  7. Restart your system.

Installing the INTL extension on MAMP


I just started developing in Symfony2, and the installation told me I should install the Intl extension (intl.so) for internationalization. Although Symfony2 works without the extension, I suspect I will run into problems sooner or later, e.g. when using the translation service. So after trying some different ways to compile and install the extension, I finally found that it is fairly easy using Macports.

First, you need to install Macports (http://www.macports.org/install.php), which allows you to compile and install many Linux packages without any hassle.
Please note, if compiling for 32-bit version of MAMP, please read last section of this blog post (Wrong architecture) before continuing !
After installation, go to your terminal and run following command :
sudo port install php5-intl
This will take a while, and will install a whole lot of packages (even apache, php5, …). When this is done, copy the file /opt/local/lib/php/extensions/no-debug-non-zts-20090626/intl.so to the extensions dir of your MAMP application (for PHP5.3, this is/Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626) :
cp /opt/local/lib/php/extensions/no-debug-non-zts-20090626/intl.so /Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626/
Now, add the extension to your php.ini file :
# /Applications/MAMP/conf/php5.3/php.ini
... some stuff here

extension=intl.so
Then (re)start your server, and the extension should be available (check phpinfo()). Have fun :)
Wrong architecture
If you get an error about wrong architecture, you have probably compiled everything in 64-bit while MAMP is compiled in 32-bit (and thus the extensions need to be in 32-bit as well). If so, edit the/opt/local/etc/macports/macports.conf file, and change (or uncomment) following line :
# /opt/local/etc/macports/macports.conf
build_arch                      i386
From now on, macports will compile everything in 32-bit.


PS: related info

how to find is it 32bit or 64 bit

switch(PHP_INT_SIZE) {
    case 4:
        echo '32-bit version of PHP';
        break;
    case 8:
        echo '64-bit version of PHP';
        break;
    default:
        echo 'PHP_INT_SIZE is ' . PHP_INT_SIZE;
        break;
}

how to uninstall homebrew
https://gist.github.com/mxcl/1173223

how to uninstall macports
http://guide.macports.org/chunked/installing.macports.uninstalling.html

Mac OS X - Terminal Tricks

Mac OS X - Terminal Tricks


Finding absolutely anything on your Macintosh

The Spotlight feature doesn't search the Library or hidden locations.

Aside from Spotlight feature, OSX also has a Find feature. Unlike Spotlight you can configure OSX's Find feature (CMD+F) look for system files by performing the following:

  1. Press CMD+F to open the Find window.
  2. Towards the left of the window, just above where the search results would appear, you'll a pull-down menu which says (by default)Kind. Click this, and choose Other.
  3. In the window that opens, scroll down, find, and check off System files.
  4. Click OK to close the window.
  5. Now, you can perform searches which will include System files in the results. To do this, select the following criteria for your search:
    System files include.

However, even this will not search all files on your Mac (i.e. items in hidden folders). To search all locations for a file, perform the following:

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    sudo find / -name "*Bicycles*"

    ...where in-place of Bicycles you enter your search term.
  3. You will be presented with a list of file-paths to any items whose file name contains your search term. The search can take a long time to complete. When the command prompt is displayed again (the same one which initially appeared at the top of the Terminal window when you first opened it), you'll know the search has completed.

Friday, November 09, 2012

Stupid Git Trick - getting contributor stats

Stupid Git Trick - getting contributor stats


getting contributor stats for your non-public project can be a real pain, that is, unless you're using git!

We use github for all of our source code and documentation on the Ohloh team. We've even got a few public open source projects up there that you should check out, but I'll talk about those another time.
Earlier this week I tweeted my YTD commit stats from my new job:
And I was asked how I came up with those numbers, which made me realize two things:
  1. git is awesome
  2. those numbers are wrong!

git is awesome

Git is awesome, and for a of other reasons that I'll cover in some future posts. Git is awesome right now because it collects stats. Not as a main feature, not really anyway, but as a by product of you and your team pushing code around.
Every commit in git is logged so git knows what the heck is going on at any given moment. Git is like that kid in college or high school that was so awesome at taking notes that he was able to buy a cannondale super v using the money he made selling copies of his notes to other people.
Git could probably afford to buy Amazing Fantasy issue 15 (first appearance of spider man for the non-nerds), git is that good at it.
Getting your commits from a git repo is pretty damned easy:
git log --author="Jared Barboza" --pretty=tformat: --numstat
That line will ask git to go into its log and pull out the commits that you've made and format them using the "numstat" formatting, more on this in a moment. The output will look like the example below, with the first column displays the number of additions (lines you added) and the second number is the number of deletions (lines removed).
5       8       public/javascripts/main.js
2000    0       public/javascripts/jquery/jquery.cycle.all.js
10      9       public/stylesheets/layout.css
Awesome, let's see if we can a nicer looking total out of this. Now, in order to do this we're going to need to do some terminal magic with gawk .
If you've never used gawk before this might be really strange looking to you but it's pretty easy to grok. Gawk allows us to run "code" via the terminal and uses any text piped to it as arguments. So we can do this:
git log --author="Jared Barboza" --pretty=tformat: --numstat | \
gawk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } END \
{ printf "added lines: %s removed lines: %s total lines: %s\n",add,subs,loc }' -
This will pipe (more about piping in the next section) the output from the git log command into gawk which will use each line as a new set of arguments and will keep a running total of the additions (first argument) , deletions (second argument) and the difference between the two (the "true" lines of code I've commited).
After gawk is done we'll see this result on our terminal:
added lines: 2015 removed lines: 17 total lines: 1998
For those of you who are linux termnial n00bs like me, the \ in the terminal command above is a line continuation character. It lets us tell the terminal that the command is continued on the next line.
The | character tells the terminal to take the output from the command on the left and feed it to the command on the right, this is called "piping".
For all my Windows peeps, you can grab grep and gawk for Windows to get this to work.
Now, back to the stats. They're great but they are not really accurate are they? I mean it's counting a jquery library (jQuery Cycle by Mr. Amazing himself, Mike Alsup ) which isn't my code so I don't want that to be part of my count. Which brings me to my next point.

the numbers i posted are completely wrong!

When I posted those numbers on the twitters I totally ignored the fact that I was counting files that I didn't write. So I needed to figure out how to remove certain lines from the result before it gets passed into gawk.
I found grep has a -v option that tells it to ignore lines that match the text supplied. So I can use the following in the "pipeline" (a chain of piped commands) to trim out the unwanted code file.
grep -v public/javascripts/jquery
When that it combined with the script from before I end up with:
git log --author="Jared Barboza" --pretty=tformat: --numstat | \
grep -v public/javascripts/jquery | \
gawk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } END \
{ printf "added lines: %s removed lines : %s total lines: %s\n",add,subs,loc }' -
Which will print out
added lines: 15 removed lines: 17 total lines: -2
Now this is a much better count. We can see that I've actually removed code from our codebase which is always a good thing.

the "official stats" for my first 3 weeks

So now that we have a much better way of determining commit stats, I re-did all my stats for the last three weeks. I used grep commands to break out the commit stats even more e.g. using grep /test and grep /public/javascripts to get the stats for unit tests and javascript.
Unit Tests: added lines: 696 removed lines: 107
Controllers: added lines: 43 removed lines: 50
Models: added lines: 71 removed lines: 77
Helpers: added lines: 15 removed lines: 7
Views: added lines: 221 removed lines: 143
Javascript: added lines: 1112 removed lines: 927
Css: added lines: 416 removed lines: 220
Project Total: added lines: 2574 removed lines: 1531
  • 6.9% of removals and 27% of additions were unit tests
  • 60.5% of removals and 43% of additions were javascript


More than 3 requests, I'll translate this to Chinese.
超过3个请求,我就会把这篇文章翻译成中文。

Tuesday, October 09, 2012

Working with table using jQuery | Narendra


To add a new row at the end of the table, use the following code,
  1. $(‘#table_id >  tbody:last’).append(‘
    ….
    ’);  
Here, table_id is an id of your table.
You can also append an html to the table like,
  1. var tr_html = “
    Hello  World
    ”;  
  2.   
  3. $(‘#table_id > tbody:last’).append(tr_html);  
Remove all rows from table,
  1. $('#myTable').find('tr').remove();  
You can also delete a particular row from the table using the id of row.
  1. $(‘#row_id’).remove();  
To get last row of the table,
  1. $(‘#table_id tr:last’);  
Get total rows in table,
Use a selector that will select all the rows and take the length.
  1. var rowCount = $('#table_id tr').length;  
If you use or in your table, you’ll have to use the following syntax or you’ll get a incorrect value:
  1. var rowCount = $('#myTable >tbody >tr').length;  


More than 3 requests, I'll translate this to Chinese.
超过3个请求,我就会把这篇文章翻译成中文。

Friday, October 05, 2012

Use virtuals forms with Symfony2

Use virtuals forms with Symfony2



We have 2 entities. A Company and a Customer:


namespace ...;

class Company
{
    private $name;
    private $website;

    private $address;
    private $zipcode;
    private $city;
    private $country;

    // Some nice getters / setters here.
}


namespace ...;

class Customer
{
    private $firstName;
    private $lastName;

    private $address;
    private $zipcode;
    private $city;
    private $country;

    // Some nice getters / setters here.
}
Like you can see, both of our entities have these fields: addresszipcodecitycountry.
Now, we want to build 2 forms. One for create/update a Company and the second to create/update a Customer.
Of course, we have only two entities which have to contains some location informations... for now! Maybe later, some entities will have this fields. So, we have to find a solution to not duplicate our code!
First, we create very simple CompanyType and CustomerType:


namespace ...;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class CompanyType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('name', 'text')
            ->add('website', 'text')
        ;
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => '...\Company',
        );
    }

    public function getName()
    {
        return 'company';
    }
}


namespace ...;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class CustomerType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('firstName', 'text')
            ->add('lastName', 'text')
        ;
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => '...\Customer',
        );
    }

    public function getName()
    {
        return 'customer';
    }
}
Definitely nothing complicated here.
Now, we have to deal with our four duplicated fields... Here is a (simple) location FormType:


namespace ...;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class LocationType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('address', 'textarea')
            ->add('zipcode', 'string')
            ->add('city', 'string')
            ->add('country', 'text')
        ;
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'virtual' => true
        );
} public function getName() { return 'location'; } }
We can't specify a data_class option in this FormType because, we don't have a Location Entity.
We don't have a location field in our entity so we can't directly link our LocationType.
Of course, we absolutely want to have a dedicated FormType to deal with location (remember, DRY!)
There is a solution!
We can set the option 'virtual' => true in the getDefaultOptions method of our LocationType and directly start use it in our 2 first types.
Look at the result:

// CompanyType

public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('foo', new LocationType());
}

// CustomerType

public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('bar', new LocationType());
}
With the virtual option set to false (default behavior), the Form Component expect a Foo (or Bar) object or array which contains our four location fields. Of course, we don't have this object/array in our entities and we don't want it!
With the virtual option set to true, the Form Component skip our Foo (or Bar) object or array. So, it directly access to our 4 location fields which are in the parent entity!
(One more time, thank to Alexandre Salomé for the tips)


More than 3 requests, I'll translate this to Chinese.
超过3个请求,我就会把这篇文章翻译成中文。