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
#! /usr/bin/env python
################################################################################
# jsprocess.py, merge javascript files into main Python file #
# Copyright (C) 2009 Konstantin Ignatiev, ignatiev@users.sourceforge.net #
# #
# This program is distributed under the terms of GNU General Public License, #
# version 2, as published by the Free Software Foundation. #
# See <URL:http://www.fsf.org/licenses/gpl.txt> #
# #
# ########## Informal subversion log ###########################################
# 147 2010-04-30 Fixed generated string
# 131 2009-07-22 Initial version
################################################################################

import os, re, sys, getopt

hshort = """%(exe)s, version 0.1
Usage: %(exe)s [options] <input files>""" % {'exe': os.path.basename(sys.argv[0])}
hlong = """Options:
--inplace -i Overwrite input files
--output -O Output file
-I <dir> add dir to search for JS files"""
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], "hiI:O:",
["help","--inplace","-output"])
except getopt.GetoptError, (msg):
raise Exception(msg)
if not args : opts.append ( ("-h",None) )
inplace = False
jspath = ['.']
out_file = None
for opt, val in opts :
if opt in ["-h","--help"] :
if opt == "-h" : print hshort, "\nUse '--help' for more info"
else : print hshort, "\n", hlong
sys.exit(0)
elif opt in ["-i", "--inplace"] : inplace = True
elif opt in ["-I"] : jspath.append ( val )
elif opt in ["-O","--output"] : out_file = val
else : raise Exception("Option %r not handled" % ((opt,val),))

if len(args) > 1 and out_file is None :
raise Exception ( "Cannot use --output with > 1 input files" )

for file in args :
fh = open(file)
c = fh.read()
fh.close ()
jsfiles = [m.group(1) for m in re.compile(r'inline_js\s*\(\s*[\'"](.+?)[\'"]\s*\)').finditer(c)]
jscon = {}
for jf in jsfiles :
a = [os.path.join(p,jf) for p in jspath if os.path.exists(os.path.join(p,jf))]
if a :
cmd = "java -classpath %(home)s/classpath/js.jar -jar %(home)s/classpath/shrinksafe.jar %(js)s | %(home)s/bin/jsmin.py" % \
{'js' : a[0], 'home' : os.environ['HOME']}
ph = os.popen ( cmd )
jscon[jf] = ph.read ()
ph.close ()
else :
print "could not find file '%s'" % jf
if not jscon :
print "File '%s' : nothing to substitute, skipping...." % file
continue
inline_func = """def inline_js ( fname ) :
if False :
return "<SCRIPT src=\\\\"%s\\\\" type=\\\\"text/javascript\\\\">\\\\n</SCRIPT>\\\\n" % fname
""" + "\n".join([""" elif fname == "%s" :
return r'''<SCRIPT type="text/javascript">%s\\n</SCRIPT>''' """ %
(jf,jscon[jf]) for jf in jsfiles if jf in jscon]) + """
else :
raise Exception("Invalid input file '%s'" % fname )

"""
exfun = re.compile(r"^def inline_js.+\n([ \t]+.*?('''(.|\n)+?''').*\n|[ \t]+.*\n|\n)+", re.MULTILINE)
if exfun.search(c) :
c = exfun.sub ( inline_func, c )
else :
# This does look like weird way to write c += inline_func,
# but it's not the same thing: Perl substitution treats '\' differently,
# thus re-interpreting the string
# Thus we do it to keept his processing consistent with the other case.
c = re.compile("$").subn ( inline_func, c + "\n\n", 1 )[0]

if inplace :
fh = open ( file, "w" )
fh.write ( c )
fh.close ()
print "File %s modified" % file
elif out_file is not None :
fh = open ( out_file, "w" )
fh.write ( c )
fh.close ()
print "File %s created" % out_file
else :
print "%d JS files substituted OK, use '--output' to get rusuls" % len(jscon.keys())

Change log

r147 by kostya on Apr 30, 2010   Diff
Merged in a few fixes in svnlist.py and
jsprocess.py, long in use
Go to: 
Project members, sign in to write a code review

Older revisions

r131 by kostya on Jul 22, 2009   Diff
jsprocess.py added, fixed a problem in
svnlist.py
All revisions of this file

File info

Size: 4147 bytes, 96 lines
Powered by Google Project Hosting