My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
165
#!/usr/bin/env python
#=======================================================================
#
# FILE: create_header.py
# USAGE: ./create_header.py <file>
# DESCRIPTION: Create file header
# REQUIREMENTS: N/A
# BUGS: N/A
# NOTES: N/A
# AUTHOR: Ali Ayoub
# EMAIL: ali@mellanox.com
# COMPANY: N/A
# CREATED: 09.17.2010-16:52:22
# REVISION: 1.0
#=======================================================================

import sys
import os
import popen2
import time

SNAME = os.path.basename(sys.argv[0])
USAGE = "%s <file>" % (SNAME)
EXAMPLE = "%s %s" % (SNAME, "fofo.sh")

# You may change the following two lines, nobody's perfect
AUTHOR = "Ali Ayoub"
AUTHOR_EMAIL = "ali@mellanox.com"

debug = "--debug" in sys.argv
if debug:
sys.argv.remove("--debug")

log = "--log" in sys.argv
if log:
sys.argv.remove("--log")

def vprint(msg):
if debug > 0:
sys.stdout.write(str(msg) + '\n')
if log > 0:
fd = open(LOG, 'a')
now = time.strftime("%Y.%m.%d-%H.%M.%S", time.localtime())
fd.write("%s:%s: %s\n" % (now, str(sys.argv), msg))
fd.close()


def run_cmd(cmd, retry = 0, nap = 1):
p = popen2.Popen4(cmd)
rc = p.wait()
if rc:
out = "-E- FAILED"
else:
out = p.fromchild.read()
vprint("-V- cmd [%s], rc [%s], out_len [%d], retry [%d]" % \
(str(cmd), str(rc), len(out), retry))
if (rc or len(out) == 0) and retry > 0:
retry = retry -1
try:
time.sleep(nap)
except:
return (rc, out)
return run_cmd(cmd, retry, nap)

return (rc, out)


template=" \n\
#!_LANG_BIN_ \n\
#=======================================================================\n\
# \n\
# FILE: _FILE_ \n\
# USAGE: _USAGE_ \n\
# DESCRIPTION: _DESCRIPTION_ \n\
# REQUIREMENTS: _REQUIREMENTS_ \n\
# BUGS: _BUGS_ \n\
# NOTES: _NOTES_ \n\
# AUTHOR: _AUTHOR_ \n\
# EMAIL: _EMAIL_ \n\
# COMPANY: _COMPANY_ \n\
# CREATED: _DATE_ \n\
# REVISION: _REVISION_ \n\
#=======================================================================\n\
"

# main
def main():
rc = 0
date = time.strftime("%m.%d.%Y-%H:%M:%S", time.localtime())
na = "N/A"
tbd = "TBD"
if len(sys.argv[1:]) != 1:
print "USAGE: %s" % USAGE
print "EXAMPLE: %s" % EXAMPLE
return 1

fn = sys.argv[1]

if not os.path.exists(fn):
print "-E- file not found %s" % fn
return 2


header = template
_LANG_BIN_= "/bin/bash -"
if fn.endswith(".py") or not os.system("head -n1 %s | grep -q -i python" % fn):
_LANG_BIN_ = "/usr/bin/env python"
elif fn.endswith(".pl") or not os.system("head -n1 %s | grep -q -i perl" % fn):
_LANG_BIN_ = "!/usr/bin/perl -w"
else:
vprint("default _LANG_BIN_ to bash")

header = header.replace("_LANG_BIN_", _LANG_BIN_)

_FILE_ = os.path.basename(fn)
header = header.replace("_FILE_", _FILE_)

_USAGE_ = "./" + _FILE_
usage = os.popen(fn + " --help 2>&1 | grep -i usage | cut -d' ' -f3- | sed -e 's,^\ ,'',g'").read().strip()
if len(usage):
_USAGE_ = _USAGE_ + " " + usage
header = header.replace("_USAGE_", _USAGE_)

_DESCRIPTION_ = tbd
header = header.replace("_DESCRIPTION_", _DESCRIPTION_)

_REQUIREMENTS_ = tbd
header = header.replace("_REQUIREMENTS_", _REQUIREMENTS_)

_BUGS_ = na
header = header.replace("_BUGS_", _BUGS_)

_NOTES_ = na
header = header.replace("_NOTES_", _NOTES_)

_AUTHOR_ = AUTHOR
header = header.replace("_AUTHOR_", _AUTHOR_)

_EMAIL_ = AUTHOR_EMAIL
header = header.replace("_EMAIL_", _EMAIL_)

_COMPANY_ = na
header = header.replace("_COMPANY_", _COMPANY_)

_DATE_ = date
header = header.replace("_DATE_", _DATE_)

_REVISION_ = "1.0"
header = header.replace("_REVISION_", _REVISION_)

print header

vprint("-V- done [rc %d]" % rc)
return rc

if __name__ == '__main__':
try:
rc = main()
except Exception, e:
rc = 2
print ""
print "-E- Interrupted! %s" % str(e)
print "-E- Abort."
sys.exit(rc)

Change log

r13 by Ali.Ayoub on Sep 19, 2010   Diff
enhanced auto language and usage detection
Go to: 
Project members, sign in to write a code review

Older revisions

r10 by Ali.Ayoub on Sep 17, 2010   Diff
fix typos
r6 by Ali.Ayoub on Sep 17, 2010   Diff
added create_header.py
All revisions of this file

File info

Size: 4034 bytes, 165 lines

File properties

svn:executable
*
Powered by Google Project Hosting