~svaksha/+junk/product-release-systers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/python -- tested on Python 2.5+ only
# coding: utf-8 
'''
"createpatch.py" is the python program used used to create a mailman patch. 
LICENSE: Copyright(C)2010, Vidya [svaksha] Ayer <vid@svaksha.com>, GPLv3. 
https://code.launchpad.net/~systers-dev/systers/product-release-systers
http://systers.org/systers-dev/doku.php/how_to_create_a_patch

USAGE: python createpatch.py (While the execution feedback will be displayed 
on the terminal, its output is also logged in the "log-createpatch.txt" file. 
'''

#python modules
import sys
import os
import os.path
import popen2
import shlex
import string 
import time
import commands
import datetime
import subprocess
from glob import glob

#python path.
filepath, filename = os.path.split(os.path.abspath(os.path.dirname(__file__)))
sys.path.append(filepath)
sys.path.append("/usr/lib/python2.5/")
sys.path.append("/usr/lib/python2.5/sitepackages/bzrlib/")
sys.path.append("/usr/share/pyshared/bzrlib/")

#print $user 
user = os.environ['USER']
print '\n User:', user
startTime =  datetime.datetime.now()
print '\nThe starting date, time is:', str(startTime)

#logging to file and scr.
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
tee = subprocess.Popen(["tee", "log-createpatch.txt"], stdin=subprocess.PIPE)
os.dup2(tee.stdin.fileno(), sys.stdout.fileno())
os.dup2(tee.stdin.fileno(), sys.stderr.fileno())

#Terminal user input
print "\nThis script automates the patch creation process for these steps: \
    \n001. Make a patch with 2 files \
    \n002. Patch with a directory of files \
    \n003. Dry run before applying  the patch \
    \n004. Apply the patch only when there are no errors. "
print "\nEnter a step number [Ex. 001, to start from step one]:"

#******************************************************************************
#CREATE A PATCH steps are based on this write-up:
#http://systers.org/systers-dev/doku.php/how_to_create_a_patch
#******************************************************************************
#ask user to  input starting step number.
InpStpNo = int(input())   
print "\nStarting step number is: %d" % InpStpNo 
print "\nBatch job begins for: Creating a Patch."

#1.Make a patch with 2 files (this works on most Linux distros):
#*****************************************************************************
#if start from step1
if  int('001') >= InpStpNo:
    try:
        #ask user input for file path.
        print "\nEnter 'oldfile' path (ex. '/home/path/olddir/oldfile' : "
        oldfile = str(input())
        print "\nEnter 'newfile' path (ex. '/home/path/newdir/newfile' : "
        newfile = str(input())
        retcode = subprocess.call(["diff -u %s %s > file.patch" % (oldfile, newfile)],  shell=True)
        print "\nExecuting, diff  -u %s %s  > file.patch"           
        if retcode < 0:
            #if diff fails
            print >>sys.stderr, "\nFAIL: step 001 (Cannot create a patch with two files)", -retcode
            sys.exit()
        else:
            #create patch for 2 files.
            print >>sys.stderr, "\nPASS: step001-Created a patch with 2 files. END 001, OK", retcode
    except OSError, e:
            print >>sys.stderr, "\nERROR: step 001", e
else:
 pass 


#2.Creating a patch with a directory of files
#*****************************************************************************
#if start from step2.
if  int('002') >= InpStpNo and int('001') != InpStpNo:
    try:
        #user input for folder/directory path.
        print "\nEnter 'oldDirectory' path name (Ex. '/home/path/olddir/' :"
        olddir = str(input()) 
        print "\nEnter 'newDirectory' path name (Ex. '/home/path/newdir/' :"
        newdir = str(input()) 
        retcode = subprocess.call(["diff -Naur %s %s > file.patch" % (olddir, newdir)],  shell=True)
        print "\nExecuting, diff  -Naur %s %s  > file.patch"       
        if retcode < 0:
            #if diff of 'old-new' files fail
            print >>sys.stderr, "FAIL: step002-Cannot create a patch with a directory of files:", -retcode
            sys.exit()
        else:
            #create patch for 2 directory's.
            print >>sys.stderr, "PASS: step002-Created a patch with a directory of files. END 002, OK.", retcode
    except OSError, e:
        print >>sys.stderr, "ERROR: step 002", e
else:
 pass 


#3.Dry run before applying the patch: 
#*****************************************************************************
#if start from step3.
if  int('003') >= InpStpNo:
    try:
        #execute dry run.
        retcode = subprocess.call(["patch -p0 --dry-run < file.patch"],  shell=True)
        print "\nExecuting, patch -p0 --dry-run < file.patch"   
        if retcode < 0:
            #dry run fails
            print >>sys.stderr, "FAIL: step003-Dry run failed.", -retcode
            sys.exit()   
        else:
            #dry run OK.
            print >>sys.stderr, "PASS: step003-Dry run to check for errors successful. END 003, OK", retcode
    except OSError, e:
        print >>sys.stderr, "ERROR: step 003, Dry run.", e
else:
 pass

#4.apply the patch only when there are no error
#*****************************************************************************
#if start from step4
if  int('004') >= InpStpNo:
    try:
        #applying the patch
        retcode = subprocess.call(["patch -p0 < file.patch"],  shell=True)
        print "\nExecuting, patch -p0 --dry-run < file.patch"  
        if retcode < 0:
            #Fail to apply patch
            print >>sys.stderr, "FAIL: step004, Cannot apply patch.", -retcode
            sys.exit()
        else:
            #Patch applied successfully.
            print >>sys.stderr, "PASS: step004, Application of patch successful. END 004, OK.", retcode
            print  '\nStatus: Patches applied, OK.'
    except OSError, e:
        print >>sys.stderr, "ERROR: step 004, Dry run.", e
else:
 pass

#Printing Completion and difference TIME.
#*****************************************************************************
endTime = datetime.datetime.now()
print "\nEnding Time for the program 'createpatch.py' is: ", str(endTime)
timedelta = endTime - startTime

#Printing total system installation time taken by 'createpatch.py'.
#*****************************************************************************
installationTime = timedelta
print "\nTotal time taken by 'createpatch.py': ", installationTime