Skip to content

Instantly share code, notes, and snippets.

@nileshk
nileshk / base_url.js
Created August 18, 2014 18:35
Get current "base" URL (without the document portion or parameters)
// Get current URL without the document portion or parameters
// For example, if the URL is:
// http://hostname:8080/path/to/application/document.html?param1=1&param2=2
// baseUrl will be:
// http://hostname:8080/path/to/application/
var baseUrl = [location.protocol, '//', location.host, location.pathname.split('/').slice(0, -1).join('/'), '/'].join('');
@nileshk
nileshk / BeanCompareUtil.java
Created September 9, 2014 01:52
Java 8 bean comparison static method using lambdas of getters to specify which properties to compare
package com.nileshk;
import java.util.function.Function;
import java.util.stream.Stream;
public class BeanCompareUtil {
public static <T> boolean compareBeans(T bean1, T bean2, Function<T, Object>... getters) {
return Stream.of(getters).allMatch(getter -> {
Object val1 = getter.apply(bean1);
require 'rubygems'
require 'sequel'
require 'fileutils'
module Jekyll
module Drupal
QUERY = "
select n.nid node_id,
n.title post_title,
@nileshk
nileshk / build.xml
Created March 8, 2013 20:51
Get SVN revision number of last commit in Ant build
<exec executable="svn" spawn="false" dir="." outputproperty="revision.number">
<arg line="info"/>
<redirector>
<outputfilterchain>
<linecontainsregexp><regexp pattern="^Last\sChanged\sRev:\s[0-9]*$" /></linecontainsregexp>
<tokenfilter>
<replaceregex pattern="^Last\sChanged\sRev:\s([0-9]*)$" replace="\1" flags="g" />
</tokenfilter>
</outputfilterchain>
</redirector>
@nileshk
nileshk / top-level.py
Last active December 14, 2015 17:29
List top level folders of current directory
import os
print os.walk('.').next()[1]
@nileshk
nileshk / pid_exists.py
Last active December 15, 2015 09:28
Find out if a process exists based on PID
import os
import errno
def pid_exists(pid):
"""Check whether pid exists in the current process table."""
if pid < 0:
return False
try:
os.kill(pid, 0)
except OSError, e:
@nileshk
nileshk / http_basic.py
Created March 25, 2013 15:59
Fetch contents of a URL with HTTP Basic Authentication
import urllib2
import base64
def fetch(url, username, password):
try:
request = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
return urllib2.urlopen(request).read()
except urllib2.HTTPError as ex:
@nileshk
nileshk / after-primefaces-css.xhtml
Created April 11, 2013 14:16
How to get a CSS file to load after Primefaces CSS (tested with Primefaces 3.5.0)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:pe="http://primefaces.org/ui/extensions">
<h:head>
@nileshk
nileshk / osx_sqlplus_oracle_instant_client.sh
Created December 16, 2015 20:14
Example of dynamically setting DYLD_LIBRARY_PATH to Oracle Instant Client folder for running sqlplus on Mac OS X
#!/bin/bash
if [[ "$OSTYPE" == "darwin"* ]]; then
export DYLD_LIBRARY_PATH=$(dirname $(which sqlplus))
fi
# ... rest of your script that calls Oracle sqlplus
@nileshk
nileshk / jar-list-packages.py
Created September 28, 2013 04:44
Python script to list the packages contained in a folder full of jar files (e.g. for scanning jar files in WEB-INF/lib)
#!/usr/bin/python
from __future__ import print_function
import string
import os
import sys
import re
import argparse
from subprocess import Popen, PIPE