Wednesday, September 25, 2013

Capturing Screen shots on Android via ADB

Let's use screencap tool on Android to capture a screen shot and save it to a file on Android's internal /sdcard.

adb shell screencap -p /sdcard/myScreenShot.png

Ok, lets capture a series of screen shots - a movie.

adb shell mkdir -p /sdcard/moovie1; 
for i in `seq -w 1 1 155`; do \
  adb shell screencap -p /sdcard/moovie1/cap_$i.png && \
  echo -e "cap_$i.png"; \
done;

Now let's download the images:

for i in `seq -w 1 1 155`; do \
  adb pull /sdcard/moovie1/cap_$i.png && \
  adb shell rm /sdcard/moovie1/cap_$i.png && \
  echo -e "cap_$i.png\n"; \
done;

Ok, now let's make a movie with help of well-known imagemagick tool::
Scale images to 50% size and convert them to GIF format.

for i in `ls`; \
  do convert $i -scale 50% $i.gif; \
done;

convert -delay 100 -loop 0 cap*.gif animated_screen.gif

Also one could use following script to save captured images directly to computer

 adb shell screencap -p | sed 's/\r$//' > screen.png 
or 
adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen.png

to cat images directly to a file on a computer (src)
(but for series of screen shots that will be a bit slower).


adb shell screencap -h
usage: screencap [-hp] [-d display-id] [FILENAME]
   -h: this message
   -p: save the file as a png.
   -d: specify the display id to capture, default 0.
If FILENAME ends with .png it will be saved as a png.
If FILENAME is not given, the results will be printed to stdout.


Tuesday, September 24, 2013

A strange way to populate SSH keys from Jenkins to Jenkins Slave node

Using sshpass tool as a workaround for ssh interactive password prompt. :-)


mkdir tmpz
cd tmpz
tar -zxvf sshpass.tgz

cd sshpass-1.05
./configure
make
./sshpass -p "hudson"  ssh-copy-id -i ~/.ssh/id_rsa.pub hudson@10.116.65.165

Tuesday, September 17, 2013

Use perl to edit multiple lines XML

Ok, here is a strange request - to edit some xml file over ssh on Mac OS X.

One needs to change false to true in following

<key>system.privilege.taskport</key> <dict> <key>allow-root</key> <false/>

Luckily, Mac OS X has perl (not sure if it was installed along with XCode). So let's use perl for that:

ssh user@192.168.1.2 "set -x; \
echo 'Password' | sudo -S \
perl -p -0777 -i -e 's@<key>system.privilege.taskport</key>.*?<dict>.*?<key>allow-root</key>.*?<false/>@<key>system.privilege.taskport</key>\n\t\t<dict>\n\t\t\t<key>allow-root</key>\n\t\t\t<true/>@sg' /etc/authorization"

Breaking this down:
  • -p tells it to loop through the input and print
  • -0777 tells it to use the end of file as the input separator, so that it gets the whole thing in in one slurp
  • -e means here comes the stuff I want you to do

And the substitution itself:
  • use @ as a delimiter so you don't have to escape /
  • use *?, the non-greedy version, to match as little as possible, so we don't go all the way to the last occurrence of </xyz> in the file
  • use the s modifier to let . match newlines (to get the multiple-line tag values)
  • use the g modifier to match the pattern multiple times
Perl expression was constructed with help of this answer from stackoverflow.

Friday, September 13, 2013

Insert a text at pattern using regexp in VIM

Say one is editing a text file in vim:


blah-blah234qqqqqqqqqq
blah-blah345234qqqqqqqqqq

and would like to insert a space at position after blah-blah345[digits] in each line.

One could use a following search-n-replace expression with help of regexp:

:%s/\(blah-blah\d\+\)/\1  /g

\( \) - for group selection
\1 - for first group selected
\d - for digit

Sunday, September 8, 2013

Find elements by XPath with JavaScript (example)


Get elements from action menu on mail page of yandex.ru:

r = document.evaluate("//*[@class='b-toolbar__block b-toolbar__block_chevron']/a", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);

el = r.iterateNext();

while(el) { 
  if (window.getComputedStyle(el).display == "none") {
    continue; 
  } 
  console.log(el.getAttribute('data-action')); 
  el=r.iterateNext();
}





Friday, September 6, 2013

Using tar pipe for copying directory excluding files or directories.

A nice use case for tar pipe

Say we have a directory, with tons of subdirectories, each of which is a checked subversion project.
Dir structure looks like this.

RootDir / 
  /dir1/
    .svn
    dirs....
  /dir2/
    .svn
    dirs....

One wants to copy everything but .svn dirs.

Let's use tar pipe for that: 

tar --exclude \.svn -c RootDir | tar -C /path/to/destination/ -xv


Monday, September 2, 2013

Reveal Developer options on OS Android >= 4.2

On devices with OS Android >= 4.2  menu item "Developer options" appears to be hidden.

If you ever want to enable "Developer options" on your device, please do the following:

1. Go to Settings -> About tablet 
2. Perform about 7 taps on item Build number (the last menu item on the list)

Then "{} Developer options" will appear.


Jenkins + Gradle + Proxy

To make Gradle use HTTP Proxy when executing on Jenkins with Gradle plugin installed,
one has to use Environment variable JAVA_OPTS to pass proxy servers to Gradle.

For example:

JAVA_OPTS=-Dhttps.proxyHost=192.168.1.1 -Dhttps.proxyPort=3128 -Dhttp.proxyHost=192.168.1.1 -Dhttp.proxyPort=3128

Also one could use gradle.properties or configure gradle build files to use proxies.
Though JAVA_OPTS is a more universal way as one could have multy node configuration of Jenkins. For example: master does not use proxy, and some slave node is located behind proxy, so setting JAVA_OPTS in node environment will do the job.