Thursday, February 5, 2015

HOWTO Create an SVN Branch from a Tag

Overview

Bugs happen and sometimes you need to create a patch. Best way to do this using SVN is to take an existing tagged version of your code (which you hopefully do before each build) and create a branch off of it.

Approach

For this to happen you need to know which tag to use and what you want to call the branch. In the example below, I am using 20141205_Version_R8–1–0–07 as my tag. Within this location is an SVN copy of what was in trunk at the time the tag was created.

Look at the contents of the tag

To see the contents of the tag, use the following command
> svn list http://svn.homeworld.lan/repos/python/tags/20141205_Version_R8-1-0-07/
You will get a listing similar to what you would see in trunk.

Create the Branch

To create the branch all you will do is tell SVN to copy the contents from the tag into a new (non-existing) target folder. In this case the folder within branches is called 20150205_R8–1–1_Patch. Remember that this folder will be created and filled by SVN automatically so the name of the target folder is all you need to provide.
> svn copy http://svn.homeworld.lan/repos/python/tags/20141205_Version_R8-1-0-07/ \
    http://svn.homeworld.lan/repos/python/branches/20150205_R8-1-1_Patch \
    -m "Creating a branch off of tag 20141205_Version_R8-1-0-07"

Committed revision 630.
Once completed you will see a revision number (630 in this case)

Look at the contents of the new branch

To confirm that the branch worked as you would expect, you can get a listing like you did for the tag.
> svn list http://svn.homeworld.lan/repos/python/branches/20150205_R8-1-1_Patch

Getting to Work

Now that this is done you can checkout the code from the branch into a working directory and get to work solving the worlds problems (certainly not your own).
> mkdir 20150205_R8-1-1_Patch
> cd 20150205_R8-1-1_Patch
> svn checkout http://svn.homeworld.lan/repos/python/branches/20150205_R8-1-1_Patch ./

Conclusion

Using a few commands you can easily create a branch from a tag. If you do not happen to have a tag available, you can branch off a specific revision of your trunk using the revision command line option. But hopefully you will create tags as you go for each build you create or for each release pushed to SQA or any testing group.

HOWTO Create lockfile logic using BATCH language

Overview

The goal of this HOWTO is to show how to manage a lock file in batch language. This is handy for scheduled processes.

Example

@echo off
REM
REM Example of howto use a lockfile in BATCH language
REM

:INIT
CD \BatchJobs
ECHO Current Working Directory
CD

:START
IF EXIST NIGHTLYRUN.LOCK GOTO SKIP_WORK
ECHO Turning on Lock
ECHO ""LOCK"" > NIGHTLYRUN.LOCK
REM Do work here. this is where your program or process goes. 
ECHO Turning off Lock
DEL NIGHTLYRUN.LOCK

GOTO DONE

:SKIP_WORK
ECHO ""This process is already running..""

:DONE

Conclusion

Using this approach you can prevent multiple instances of something running. A possible enhancement is something that can sense a stale lock if there is a crash or something. however this should help for most situations.

HOWTO Python Setup UNICODE Logging

Overview

Python does a very good job handling UNICODE for most modules, however, logging is not one of then. To get around this is easy but a special technique is required.

Common Approach

Normally with logging you pass a file to the logger and then write to it as you need though the logger interface. This approach is fast but does not allow the setting of the file encoding.
import logging
logging.basicConfig(
    level=logging.DEBUG,
    filename=filename,
    filemode="w")

formatter = logging.Formatter('%(asctime)-15s: %(levelname)-7s - %(message)s')
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)

log = logging.getLogger("MAIN")
log.addHandler(ch)
_logger = log

Improved Approach

To get around this and to ensure you control the formatting, a file stream will need to be created first and then that stream can be passed to logger.
import logging
import logging.handlers
log_fh = open("/logs/my_unicode.log", "w", encoding="utf-8")
logging.basicConfig(
    level=logging.DEBUG,
    format=config.FORMAT)

formatter = logging.Formatter('%(asctime)-15s: %(levelname)-7s - %(message)s')
ch = logging.StreamHandler(log_fh)
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)

log = logging.getLogger("MAIN")
log.addHandler(ch)
The log_fh holds the handle to the UTF–8 stream which is passed to the logger StreamHandler. Once set, text will be properly encoded as it is passed to the logger.

Conclusion

Using a slightly altered technique it should be possible to write anything to a python log using the build in logger module. There is no reason to use this technique for all logging even if you do not think UNICODE will be used.

Sunday, February 1, 2015

HOWTO Add/Subtract Days or Time

Objective

Show different database techniques for determining the date in the past (or future). The following will show you HOWTO get three days ago based on CURRENT_TIMESTAMP. All will generate something like this.
CurrentTime ThreeDaysAgo
2015–02–01 11:42:04 2015–01–29 11:42:04
Below you will find information MySQL, SQL Server, SQLITE3, and PostgreSQL.

MySQL

Using MySQL, take a timestamp DB property (like CURRENT_TIMESTAMP for “now” or a property from a table). Then add + or - followed by the keyword INTERVAL. Then a number followed by a unit like day if you (for example) want to add or subtract a certain amount of days. The fllowing code subtracts three days from now.
mysql> SELECT CURRENT_TIMESTAMP as CurrentTime,
         CURRENT_TIMESTAMP - INTERVAL 3 day as ThreeDaysAgo;
For More Information

Microsoft SQL Server

Using SQL Server, the end result is the same but they use a function to do the same thing. Using the DateAdd function, you specify a unit, quantity, and DB property. In the following example I use “now” and subtract three days from it.
SELECT CURRENT_TIMESTAMP as CurrentTime,  
    DateAdd(day,-3,CURRENT_TIMESTAMP) as ThreeDaysAgo;

SQLITE3

For SQLITE, you need two wrapper the date time property in the datetime(…) function with a paramter telling it what manipulations you would like to do. In this case, I want to do subtract three days.
sqlite> SELECT CURRENT_TIMESTAMP as CurrentTime, 
datetime(CURRENT_TIMESTAMP, '-3 days') as ThreeDaysAgo
For More Information

PostgreSQL

I have not had a chace to work this out for postgresql yet.
For Information on this point visit

Conclusions

Using the above syntax, you will be able to manipulate dates to add or subtract time as your code requires.