Sunday, September 29, 2013

HOWTO easy wrapper for php code beautifier / formatter

I wanted a tool that formatted php from the command line. phpstylist does what I want, but it is tedious to supply the options each time you run it. Below is a wrapper so you can set default options.

To install:

  1. Save phpstylist as /usr/local/bin/phpStylist.php
  2. Save the script below as /usr/local/bin/pstyle
  3. Edit /usr/local/bin/pstyle
    1. set MAKE_BACKUP='yes' , if you want to keep the original unformulated files
    2. set MAKE_HTML to a lower case 'yes or 'no' make an html copy of the formatted file
    3. un-comment the # STYLIST_OPTIONS+= lines that contain the php formatting options you want
  4. Be sure /usr/local/bin/ is in your $PATH

To run:

       pstyle phpFile.php
       pstyle phpFile1.php phpFile2.php phpFile3.php 

# wrapper for
# by

  set -u # halt on undefined BASH variables
  set -e # halt if command doesn't return 'no error' aka 0


MAKE_BACKUP='yes'  # lower case 'yes' or 'no', backup the php file before formatting it
MAKE_HTML='yes'    # lower case 'yes or 'no'   make an html copy of the formatted file


# uncomment/edit options you want
# see php -f phpStylist.php for more options
# --------------------

# Indentation and General Formatting:

#   STYLIST_OPTIONS+=" --indent_size 4  "      # 4 characters per indentation level
#   STYLIST_OPTIONS+="--indent_with_tabs          "      # Indent with tabs instead of spaces
#   STYLIST_OPTIONS+="--keep_redundant_lines      "      # Keep redundant lines
#   STYLIST_OPTIONS+="--space_inside_parentheses  "      # Space inside parentheses
#   STYLIST_OPTIONS+="--space_outside_parentheses "      # Space outside parentheses
#   STYLIST_OPTIONS+="--space_after_comma         "      # Space after comma

# Operators:
#   STYLIST_OPTIONS+="--space_around_assignment     "    # Space around = .= += -= *= /= <<<
#   STYLIST_OPTIONS+="--align_var_assignment        "    # Align block +3 assigned variables
#   STYLIST_OPTIONS+="--space_around_comparison     "    # Space around == === != !== > >= < <=
#   STYLIST_OPTIONS+="--space_around_arithmetic     "    # Space around - + * / %
#   STYLIST_OPTIONS+="--space_around_logical        "    # Space around && || AND OR XOR << >>
#   STYLIST_OPTIONS+="--space_around_colon_question "    # Space around ? :
#Functions, Classes and Objects:                        
#   STYLIST_OPTIONS+="--line_before_function         "   # Blank line before keyword
#   STYLIST_OPTIONS+="--line_before_curly_function   "   # Opening bracket on next line
#   STYLIST_OPTIONS+="--line_after_curly_function    "   # Blank line below opening bracket
#   STYLIST_OPTIONS+="--space_around_obj_operator    "   # Space around ->
#   STYLIST_OPTIONS+="--space_around_double_colon    "   # Space around ::

# Control Structures:
#   STYLIST_OPTIONS+="--space_after_if               "    # Space between keyword and opening parentheses
#   STYLIST_OPTIONS+="--else_along_curly             "    # Keep else/elseif along with bracket
#   STYLIST_OPTIONS+="--line_before_curly            "    # Opening bracket on next line
#   STYLIST_OPTIONS+="--add_missing_braces           "    # Add missing brackets to single line structs
#   STYLIST_OPTIONS+="--line_after_break             "    # Blank line after case "break"
#   STYLIST_OPTIONS+="--space_inside_for             "    # Space between "for" elements
#   STYLIST_OPTIONS+="--indent_case                  "    # Extra indent for "Case" and "Default"

#Arrays and Concatenation:
#   STYLIST_OPTIONS+="--line_before_array            "    # Opening array parentheses on next line
#   STYLIST_OPTIONS+="--vertical_array               "    # Non-empty arrays as vertical block
#   STYLIST_OPTIONS+="--align_array_assignment       "    # Align block +3 assigned array elements
#   STYLIST_OPTIONS+="--space_around_double_arrow    "    # Space around double arrow
#   STYLIST_OPTIONS+="--vertical_concat              "    # Concatenation as vertical block
#   STYLIST_OPTIONS+="--space_around_concat          "    # Space around concat elements

# Comments:
#   STYLIST_OPTIONS+="--line_before_comment_multi    "    # Blank line before multi-line comment (/*)
#   STYLIST_OPTIONS+="--line_after_comment_multi     "    # Blank line after multi-line comment (/*)
#   STYLIST_OPTIONS+="--line_before_comment          "    # Blank line before single line comments (//)
#   STYLIST_OPTIONS+="--line_after_comment           "    # Blank line after single line comments (//)

# provide help for bad args 
if [[ $# -eq 0  ]]; then
  echo '  Syntax is: pstyle <php file to style> [php file to style ] [..] '
  exit 1 

# did we copy phpStylist.php to right place
if [[ ! -r $STYLIST_CMD  ]]; then
  echo "$STYLIST_CMD is missing or unreadable"
  exit 1


for file_to_style in "$@"
    MAKE_BACKUP=${MAKE_BACKUP,,}       # make it lower case
    if [[ $MAKE_BACKUP=='yes' ]]; then
        cp "$1" "$1.bak" 

    CMD="php  -f $STYLIST_CMD $file_to_style $STYLIST_OPTIONS"
    $CMD > "$OUT_FILE"

    mv "$OUT_FILE" "$file_to_style"
    if [[ $MAKE_HTML=='yes' ]]; then
      php -s "$file_to_style" > "$file_to_style.html"

Saturday, May 25, 2013

What: Logical fallacies, Dan Phelps & Lowell backyard chickens

Regardless of your thoughts on back-yard chickens in Lowell, it is instructive (perhaps it could be part of a UML 1st year logic & rhetoric curriculum) to enumerate the principles of logic that Dan Phelps violates in his recent Lowell Sun Newspaper anti-chicken column

The bulk of Dan Phelp's assertions are Red Herrings:

  • He recently enjoyed eating a whole chicken
  • He doesn't like the city of Buffalo
  • He has a British friend named Joel who says "Gorgeous"
  • He likes burgers better than chickens.
  • Los Angeles and Seattle have smog and rain
  • Lowell could have smog and rain if we wanted.

A red herring is a fallacy that attempts to divert attention from the original issue. Changing the subject doesn't prove any point. The Britishness of Dan's friend does not add or remove evidence of the health and safety of backyard chickens in Lowell.

Red Herring is a tempting classification for this next assertion:

  • Dan Phelps has nothing against [eating] chickens

However, it is more probably a Non-Sequitur since he implies that his lack of prejudice against eating chickens supports the conclusion that he is qualified to have an opinion on backyard chickens in Lowell.

Non-Sequitur is Latin for "doesn't follow". The conclusion does not necessarily follow from the premises. In other words, a logical connection is implied where none exists. Another example

  • All men are humans.
  • Mary is human.
  • Therefore, Mary is a man.

Or fleshing out Dan's argument a bit:

  • Eating chickens is reasonable.
  • Dan Phelps eats chickens
  • Dan Phelps has a reasonable opinion about backyard chickens.

Confusing Cause and Effect assumes cause and effect for two variables simply because they occur together.

  • Lowell shouldn't be like Somerville.

Let us table the low probability that Somerville is a bad place.

The presence or absence of chickens doesn't make one place like another. Lowell won't become like Somerville MA, Los Angeles CA, Westford MA or Madison WI, if we allow backyard chickens. We currently prohibit back yard chickens, but we aren't like Lawrence MA or New Milford NJ.

Ad hominem is Latin for "To the Man", it is a logical fallacy that attacks the person making the argument instead of the argument. Dan Phelps asserts:
  • [Stupid] people moved to Lowell to raise chickens even though Lowell is a city and live chickens aren't legal in Lowell now.
  • Lowell's legal department will copy another city's ordinance because they are too lazy to draft a fresh one

Assuming both of these unsupported assertions are true, neither speaks to the merit (or lack of merit) of backyard chickens.

Damning with faint praise is a logical fallacy that offers weak or unenthusiastic praise and insinuates that no stronger praise is warranted. Dan Phelps asserts:

  • The pro-chicken people aren't cowards or reckless, suicidal drivers.

This statement doesn't address the merits (or not) of backyard chickens. It implies that the best that can be said of the pro-chicken people is pretty weak. The fallacy is not claiming the lack of merit directly so the assertion can be challenged.

Reductio ad absurdum, (reduction to absurdity) is often logically legitimate If the negation of a statement (rocks have weight) is obviously absurd (rocks float weightless in calm air) than an argument is prove true. Dan Phelps asserts (sarcastically) that:

  • The proposed chicken ordinance should allow people to have a couple hundred cows and run backyard slaughter houses.

The difficulty is that nobody is proposing any cows or slaughterhouses, the ordinance is about 6 chickens. Dan Phelps is actually making a Straw man argument. He is arguing against something that has not been proposed.

The fallacy of division If a part has a quality, (e.g. small) than every part has that same quality. Dan Phelps asserts:

  • Some yards in Lowell are too small for a single chicken.

Because some yards are too small, it does not follow that all yards are too small.

There are a two assertions that Dan Phelps makes that are not logical fallacies(though they are suspect for other reasons).

  • Houses on Walker Street are close together.
  • Bob Rafferty, a proponent of backyard chickens lives on Walker street.

While it isn't clear if all houses on Walker Street are close together. Logically, it is possible to assume Bob is proposing to have chickens in his backyard, close to other houses.

The point here is neither pro nor anti backyard chicken. I'm a vegetarian, I don't eat chickens or eggs. I'm not 100% convinced either faction is right. However, I am suspicious of sloppy thinking. You (Dan Phelps) don't make any kind of logical argument by (pardon my Latin) yelling out random bullshit. chicken-shit

Thursday, May 23, 2013

Why: opposing Lowell backyard chickens is good.

I don't eat chickens or eggs. I didn't come into this fight on a particular side. The position against backyard chickens in Lowell Massachusetts is roughly

  • Noise
  • Waste.
  • Property values
  • Stray chickens

Please comment, if I've missed something.

The pro-chicken people are less specific. They want immigrants to stay in touch with their rural roots, to give everyone a closer connection to what food is and to take a small step away from crappy industrial food injecting toxins into our bodies and our planet.

The biggest pro-chicken argument is that they don't create problems, or that chickens create fewer problems than animals that are now legal in Lowell. I haven't heard them specifically address the stray chicken issue. Their other counter-points are:

  • Roosters, the noise makers are specifically prohibited under the proposed ordinance
  • A 40 pound dog makes 4 times the waste of 10 chickens. The limit is 6 chickens per back yard.
  • The 10 places with the most expensive real estate in the country allow backyard chickens.
  • Brooklyn, Los Angeles, Somerville, Lexington and many, many other places have no problems with chickens.

In theory arguments come down to definitions and facts. Very quickly and very shallowly fact-checking I can see that Chickens in Beverly Hills haven't hurt property values there.

At yesterday's Neighborhood Subcommittee meeting John Leahy offered three unanimously approved resolutions directing the objective professionals in the Departments of Animal Control, Health and Tax Assessment to prepare reports predicting the likely impacts of chickens on these offices. In theory, the facts will become clear and the council will make a rational decision based on the facts. (I'm supporting this theory)

Unfortunately, chickens might not be the issue. At the meeting, the anti-chicken people used phrases like "those people" "lifestyle choice" and "decency". One speaker complained

    "We have enough with medical marijuana, now this ?" 

For the anti-chicken people, conforming to a particular notion of "decent" appears essential. What consenting adults do behind closed doors could be a matter of public concern.. Being "different" is a potential problem. Backyard chicken success in Somerville is a minus because Somerville is freaky place and we don't want Lowell to get freaky.

Actually, different is essential for growth. It is not a coincidence that Massachusetts has much more tech and tolerance than Mississippi. The sorts of places that tolerate harmless quirks like backyard chickens or allow people to smoke dope instead of drinking beer are the places that are open to different and better tech and different and better living.

However, by definition, a strength through diversity muscle system requires respect and exercise for all the muscles in the system. The anti-chicken folks are as important as the pro-chicken folks because they give a damn and they are taking a stand. If everyone reads Michael Pollan and Mark Bittman watches Food Inc, while sitting in the brewd coffee shop, sipping soy latte and eating quiche, we won't have as strong an ordinance as we will from being different.

For example, the haranguing of Bud Caufield, the apparent leader of the anti-chicken folks is often more helpful than it appears. He and I are both members of the Lowell YMCA, one of the places where old Lowell meets new Lowell. If I run easy on the treadmill to warm up, don't sweat and touch only the start and stop buttons with the tip of my finger, I don't bother to wipe down the treadmill console. Mr Caufield is not happy about my habits. He once stood, yelling at me red faced and arms waving for 5 minutes. He resumed his harangue after we hit the showers. His voice followed me out into the parking lot as I walked away. Apparently, "I didn't sweat on the machine, thanks for your input" isn't a discussion ender for everyone.

I still don't wipe down the treadmill every time. However, thanks to Mr Caufield, I am extra scrupulous about wiping down machines with even the possibility of a faint hint of my sweat taint. I'm not afraid of another rant. I am (now) aware of my obligation to keep to my committed position. (sweat means wipe, & no sweat means no wipe)

If the facts as established by the Lowell's professional engineers and planners do support backyard chickens, opposition to them is still a good thing. If chickens aren't smelly and noisy, patiently and specifically addressing people's concerns will help keep chickens from being a nuisance.

If the real issue is fear of the "other", listening patiently will help but not resolve the issue.

In Massachusetts, most people opposed gay marriage when the courts made it legal. After a few years, of living with it, most people realised that nobody had been harmed and many people had been helped. Most people changed their minds.

It will probably be the same for Chickens.

Tuesday, May 21, 2013

HOWTO override DHCP resolv.conf nameserver setting on Debian Wheezy running Network Manager

Why run a DNS server on your laptop?

  • It is easy.
  • You are getting jerked around
    1. DNS built into the wireless access point is slow or broken
    2. Your ISP is evil and hijacks DNS

Evil is the big issue. DNS (Domain Name Service) translates names. for example, "" should be IP# . An evil DNS server might send it to instead and show you a page of ads.


  • You are running Debian Wheezy (not tested with Ubuntu)
  • You use a dynamic (DHCP) address
  • You use Network Manager

If you are using a static IP address on one interface you can edit /etc/network/interfaces and set " dns-nameservers" for the interface in question.

With DHCP (via Network Manager), your computer connects to the network, sends a broadcast asking for an IP address. The DHCP (dynamc host configuration protocal ) server replies with an IP address, default gateway, nameserver(s) and other network information.

Your goal to over-ride nameserver that your ISP sends you via DHCP

These steps should work on Debian Etch/Squeeze/Wheezy , but I've tested them only on Wheezey

     # Install the DNS server
     sudo apt-get install bind9

     # install the package that lets you tell Network Manager 
     # what nameservers to use
     sudo apt-get install resolvconf

Add these lines to /etc/resolvconf/resolv.conf.d/head

  #    edit /etc/resolvconf/resolv.conf.d/* as appropriate

  # google's no cost , non-evil , free DNS server

  # copy of bind running locally the cache 
  # probably isn't as big as google's

Ignore the comment that tells you not to edit the file. The purpose is to tell you not to edit the generated /etc/resolv.conf file

If things don't work

  • Revert to your backed-up copy of /etc/resolvconf/resolv.conf.d/head
  • Consider DNSmasq

Other possibilities

Use the Network Manager GUI to edit each individual connection, one-at-a-time-tediously

  1. Right click the icon
  2. Edit Conections
  3. Wireless
  4. Double click <your connection name>
  5. IPv4 settings
  6. Change "Automatic (DHCP)" to "Automatic (DHCP) address only"
  7. set DNS Servers to: ''
  8. Save


Thanks to Thomas Hood for writing the resolvconf package

Monday, May 20, 2013

HOWTO debian / ubuntu re-install packages in bulk.

To put the same packages on the new computer as the old, you can follow the mostly helpful instructions on the immutable Debian Wiki entry on Listing Debian packages. A mild defect of these instructions is they omit the step of installing dselect and updating it's package database. So to be complete:

   # on old system:
   dpkg --get-selections >old_system_selections.txt

   # on new system
   sudo apt-get install dselect
   sudo dselect update
   sudo dpkg --get-selections < old_system_seletions.txt
   sudo apt-get dselect-upgrade

It is possible that some packages won't exist on the new system. For example, if you are changing distributions, squeeze-->wheezy or architechtures i386-->amd64. In that case you can expect some errors like:

    dpkg: warning: package not in database at line 303: libgudev-1.0-0:i386

Sunday, May 19, 2013

HOWTO: Boot from either member of RAID 1 mirror in Linux

Our staging server (running Debian Wheezy) recently lost one of it's mirrored RAID 1 volumes. (Mounted on /var/backups ).

After the helpful email from both mdadm and smartmond (enable in /etc/default/smartmontool). I replaced the drive, per the fine mdadam manual.

All went well, until for fun, I decided to check if the server would boot with only half of /dev/md0 (mounted on / ) available. The server didn't boot.

After some head scratching, I realized I hadn't installed grub in the MBR of both discs.  The RAID array (mirror) is defined in terms of volumes , or partitions, not discs. To install grub on both MBRs I had to:
     sudo dpkg-reconfigure grub-pc
Though it would have worked as well to:
    sudo grub-setup /dev/sda
    sudo grub-setup /dev/sdb
Once Grub was on both MBR, all was good.

WhyTo: Boston (apache) Barcamp 2013 Enterprise Open Source

So far there is nothing on the Barcamp Boston site apart from ("spring 2013") If +Gregg Bedard hadn't dug around, I might have missed that the related (?) Apache Barcamp event today (2013-05-19).

Apache Barcamp this year had fewer people than past year's plain Barcamps. (30 vs 300) and less diversity (no rubber stamp art, just java, java and java) Still it was worth a 7:00am train to sit around with 30 developers who are not like me.

The big, practical work related, take away for me was that a goal for the Apache 2.5 httpd server is to be as fast and resource friendly as Ngnix . --I've been looking at Nginx as a way to shoehorn my group's MVHub application into the no dollar cost Amazon virtual machine. Maybe if I focus on other projects the resource thing will fix itself.

The big emotional take away was that the Open Office part of the Libre /Open Office fork wasn't as politically compromised as I'd thought. For example, the Libre people can use the Open Office code because the Apache License is not very restrictive. However the Open Office people can't use the Libre office code. Oracle no longer controls Open Office.

I think I am supposed to take away "the apache way", an official, required talk at all apache events. Trivializing the Zen thing, the apache way is to favor community over code, (because without community you have no code) and pragmatism over ideology. Most Apache Foundation projects are powered by Volunteers paid by their corporate day jobs to work on Apache stuff. For example Apache Board Member Ross Gardler works for Microsoft.

The crowd was solidly Enterprise/Corporate ( Mitre, IBM )"open source" (make money with open software) rather than "free software" ( un-break the world) and solidly Java. (me, 2 sys admins, a .Net guy & 26 Java developers) There was a guy from Carbonite, (backup software). They pay Microsoft 1.5 million dollars per year to use MS software on the 1,000 servers they use to support 2 million people's backups. They are thinking of open source to save money.

There was a **tiny** little bit of stupid Java snobbery. For example Hippo talked about their Java based open source product. A few people in the group down-talked PHP / Drupal & Aquia  Acquia has about 300 employees and many more customers than Hippo. Just a little snobbery though, I've seen a lot worse at most Perl meetings.

The not good/bad, just different take away was that there is another world of coders that I had not visited before. These guys work in small groups, for large corporations (IBM, MITRE, SugarCRM , Google ) on projects that effect hundreds of thousands of or millions of users. For example see: Open Social Rave and Shindig

If I understand correctly, OpenSocial was created by Google around 2006 partly to make Orkut work better and partly to go after Facebook. Right now Google isn't publicly involved with OpenSocial.

Shindig lets you make social widgets (Profiles, relationships, shared applications). It is the reference implementation of OpenSocial. Google is apparently using a private fork of Shindig in most of their products.

Rave lets you combine Shindig and other widgets.

The goal is corporate social (IBM 400,000 employees) and mashups. So you can check your email or tweets in the ticket system or handle tickets in your email without switching applications. Or (presumably) you can deal with your calendar in Gmail For flesh or fictional corporate people, answering a specific tag in Facebook inside another Application focused on your paid workflow might be a win.

Assuming I understood it all.