Blog

Some ramblings and write-ups about tech, music, travelling and other topics.

Bash: reverse i-search and normal i-search

Ctrl+R gives you reverse i-search of bash history, however, once you have selected a command to edit (by hitting ESC) the next search would start from the same line, because it's incremental by definition. This is useful but sometimes you wanna start over. You can rewind/fast-forward your search position in history by:

M->    # move to the latest entry
M-<    # move to the earliest entry
You can also do forward incremental search by Ctrl+S. But unfortunately in bash on most distributions Ctrl+S is assigned to "Stop". Check what the meta commands are assigned to by
stty -a
If you see ixon, you need to turn it off by
stty -ixon
http://stackoverflow.com/questions/549810/control-r-reverse-i-search-in-cygwin-bash-how-do-you-reset-the-search

SSL Certificate for Apache2 in Mac OS X

Generating the private key:

openssl genrsa -des3 -out server.key 1024

Generating the CSR (certificate signing request):

openssl req -new -key server.key -out server.csr

Generating the self signed certificate:

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

Configuring SSL in httpd.conf for Apache:

SSLEngine on
SSLCertificateFile /path/to/generated/server.crt
SSLCertificateKeyFile /path/to/generated/server.key
(replace path appropriately with the path to your certificate and key)

Restart Apache:

apachectl restartApache will ask you for the passphrase to your key. If you think you will be shutting the server down a lot, you may want to remove the passphrase from the key so you can avoid entering it each time. If not, don't worry about it. If so, complete this step after step 2 (Generating the CSR):
cp server.key server.key.copyopenssl rsa -in server.key.copy -out server.key
http://superuser.com/questions/73979/how-to-easily-create-a-ssl-certificate-and-configure-it-in-apache2-in-mac-os-x

[OS X] Removing Custom Items from Finder Sidebar

  • As of 10.6: click drag the folder away then push the cmd key before letting go of the mouse
  • Manually editing ~/Libary/Preferences/com.apple.sidebarlists.plist.

Printing C-style Byte Array Using hexdump

some dummy contents

Mirroring Posts to WordPress.com

I'm an indecisive person; I like all the perks of running my own installation of WordPress, but still want to host my posts on WordPress.com. I've been manually exporting from my blog on my private server (tak.atso-net.jp/blog) to my WordPress.com blog manually using the WordPress eXtended RSS (WXR) "Export" and "Import" tools provided in WordPress dashboard. This is great, but not ideal, especially if you'd rather spend more time scripting to automate something than the time it takes to do the thing manually, like me... .... so, I've written a little Python script that uses the XML-RPC library (xmlrpclib) to fetch all posts on my private server, and posts/update them on my WordPress.com blog (it was raining and I was stuck indoors anyway). Currently the script only mirrors from source to target, overwriting any existing posts with the same title. I'm sure it can be improved to allow updating instead of mirroring by comparing the last modified date, and decide which way to copy, but that, as with everything else on this blog, is left as an exercise for the diligent reader...

#!/usr/bin/env python

import logging
import time
import re
import xmlrpclib
from optparse import OptionParser

MAX_POSTS = 10000   # big enough for ya?

def decode_iso8601(date):
    # Translate an ISO8601 date to the tuple format used in Python's time
    # module.
    regex = r'^(d{4})(d{2})(d{2})T(d{2}):(d{2}):(d{2})'
    match = re.search(regex, str(date))
    if not match:
        raise Exception, '"%s" is not a correct ISO8601 date format' % date
    else:
        result = match.group(1, 2, 3, 4, 5, 6)
        result = map(int, result)
        result += [0, 1, -1]
        return tuple(result)

if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        datefmt='%H:%M:%S')

    parser = OptionParser()

    parser.set_description("Description:
     Mirror posts from one wordpress blog to another")
    parser.set_usage("%prog ")

    parser.add_option("-s", "--source-url", dest="sourceUrl",
                      help="XML-RPC URL of the source blog",
                      metavar="url")

    parser.add_option("-u", "--source-username", dest="sourceUsername",
                      help="Username for source blog",
                      metavar="username")

    parser.add_option("-p", "--source-password", dest="sourcePassword",
                      help="Password for source blog",
                      metavar="password")

    parser.add_option("-t", "--target-url", dest="targetUrl",
                      help="XML-RPC URL of the source blog",
                      metavar="url")

    parser.add_option("-U", "--target-username", dest="targetUsername",
                      help="Username for target blog",
                      metavar="username")

    parser.add_option("-P", "--target-password", dest="targetPassword",
                      help="Password for target blog",
                      metavar="password")

    (options, args) = parser.parse_args()

    logging.info("Source server: %s" % options.sourceUrl)
    source = xmlrpclib.ServerProxy(options.sourceUrl)
    sourcePosts = source.metaWeblog.getRecentPosts(
        1, options.sourceUsername, options.sourcePassword, MAX_POSTS)   # assumes it's the first blog you have
    logging.info("Fetched %d posts" % len(sourcePosts))

    logging.info("Target server: %s" % options.targetUrl)
    target = xmlrpclib.ServerProxy(options.targetUrl)
    targetPosts = target.metaWeblog.getRecentPosts(
        1, options.targetUsername, options.targetPassword, MAX_POSTS)    # assumes it's the first blog you have
    logging.info("Fetched %d posts" % len(targetPosts))

    for sp in sourcePosts:
        logging.info("TITLE: %s" % sp['title'])
        logging.debug("DATE:  %s" % time.strftime('%m/%d/%Y %H:%M:%S',
            decode_iso8601(sp['dateCreated'].value)))
        postId = -1
        for tp in targetPosts:
            if sp['title'] == tp['title']:
                # Edit and existing post - might be better to check the date and decide which way to sync...
                postId = tp['postid']
                logging.debug("    Already exists. Overwriting")
                break
        isPublished = (sp['post_status'] == "publish")
        if postId == -1:
            target.metaWeblog.newPost(
                1,
                options.targetUsername,
                options.targetPassword,
                sp,
                isPublished)
        else:
            target.metaWeblog.editPost(
                postId,
                options.targetUsername,
                options.targetPassword,
                sp,
                isPublished)

Problems Suspending Machines with USB3 in Ubuntu

I've noticed that my new Asus U30FT was having problems suspending - it just got stuck on a blank screen. Long story short, it turns out because newer versions of Ubuntu come with ehci_hcd builtin kernel, and there's a couple of things you need to add to disable all devices that uses ehci_hcd, and unload the xhci module. #!/bin/sh # File: "/etc/pm/sleep.d/20_custom-ehci_hcd". case "${1}" in hibernate|suspend) echo -n "0000:00:1a.0" | tee /sys/bus/pci/drivers/ehci_hcd/unbind echo -n "0000:00:1d.0" | tee /sys/bus/pci/drivers/ehci_hcd/unbind ;; resume|thaw) echo -n "0000:00:1a.0" | tee /sys/bus/pci/drivers/ehci_hcd/bind echo -n "0000:00:1d.0" | tee /sys/bus/pci/drivers/ehci_hcd/bind ;; esac #File: "/etc/pm/config.d/usb3-suspend-workaround". SUSPEND_MODULES="xhci" Source: http://ubuntuforums.org/showthread.php?t=1444822