Automate Your License Stubs and Get More Done

Like most developers, I have several software development roles. Though I take great joy in my open-source projects, commercial software-development keeps the lights on. More so than with my open-source software, I am often required to place license-stubs at the top of source-code files that I create. Unfortunately, I’m so concerned with getting things done, that I often completely forget to do it. That leaves me with the unsettling choice to either keep going, or to stop working on things just to insert commented text in a bunch of files that are largely internal.

Naturally I would prefer to use a tool that can work over all files in the tree, and ignore any files in which there already exists a stub. It would also need to account for things like shebangs or opening PHP tags, and insert the license content after those.

The plicense (stands for “prepend license”) tool can be used for this. It is available as “plicense” under pip. As an added bonus, it can also treat the license-stub like a template, and automatically substitute things like the year and the author so that you need not update your template every year.

Here is an example of how to use it, right out of the documentation. This is being done in a project that has scripts that need to be processed differently than the rest of the files due to the difference in naming.

  1. Process my scripts/ directory. The files in this directory don’t have extensions. However, I can’t run plicense over the whole tree, or I’ll be including virtualenv and git files, which should be ignored.
    beantool$ plicense -p scripts -r author "Dustin Oprea" LICENSE_STUB 
    scripts/bt
    (1)/(1) directories scanned.
    (1)/(1) files scanned.
    (1)/(1) of the found files needed the stub.
    

    The top portion of the script now looks like:

    #!/usr/bin/env python2.7
    
    # beantool: Beanstalk console client.
    # Copyright (C) 2014  Dustin Oprea
    # 
    # This program is free software; you can redistribute it and/or
    # modify it under the terms of the GNU General Public License
    # as published by the Free Software Foundation; either version 2
    # of the License, or (at your option) any later version.
    
    
    import sys
    sys.path.insert(0, '..')
    
    import argparse
    
  2. Process the Python files in the rest of the tree.

    beantool$ plicense -p beantool -e py -r author "Dustin Oprea" -f __init__.py LICENSE_STUB 
    beantool/job_terminal.py
    beantool/handlers/handler_base.py
    beantool/handlers/job.py
    beantool/handlers/server.py
    beantool/handlers/tube.py
    (2)/(2) directories scanned.
    (5)/(7) files scanned.
    (5)/(5) of the found files needed the stub.
    

    If you run it again, you’ll see that nothing is done:

    beantool$ plicense -p beantool -e py -r author "Dustin Oprea" -f __init__.py LICENSE_STUB 
    (2)/(2) directories scanned.
    (5)/(7) files scanned.
    (0)/(5) of the found files needed the stub.