[svnbook] r3985 committed - * www/bin/build-and-deploy.py...
svnbook at googlecode.com
svnbook at googlecode.com
Thu Aug 11 11:48:07 CDT 2011
Revision: 3985
Author: cmpilato at gmail.com
Date: Thu Aug 11 09:47:50 2011
Log: * www/bin/build-and-deploy.py
Merge the guts of the make-ad-sense.py script into this script,
enabling adsense-ification of builds where requested.
http://code.google.com/p/svnbook/source/detail?r=3985
Modified:
/www/bin/build-and-deploy.py
=======================================
--- /www/bin/build-and-deploy.py Thu Aug 11 08:56:42 2011
+++ /www/bin/build-and-deploy.py Thu Aug 11 09:47:50 2011
@@ -134,6 +134,160 @@
sys.stderr.write(body)
+adsense_left_data = """
+<div id="adsense_left">
+<script type="text/javascript"><!--
+// Text Ad: Skyscraper (120x600)
+google_ad_client = "pub-0505104349866057";
+google_ad_width = 120;
+google_ad_height = 600;
+google_ad_format = "120x600_as";
+google_ad_channel = "";
+google_color_border = "ffffff";
+google_color_bg = "ffffff";
+google_color_link = "0000ff";
+google_color_url = "008000";
+google_color_text = "000000";
+//--></script>
+<script type="text/javascript"
+src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
+</script>
+</div>
+
+"""
+
+adsense_bottom_data = """
+<div id="adsense_bottom">
+<script type="text/javascript"><!--
+// Text Ad: Medium Rectangle (300x250)
+google_ad_client = "pub-0505104349866057";
+google_ad_width = 300;
+google_ad_height = 250;
+google_ad_format = "300x250_as";
+google_ad_channel = "";
+google_color_border = "ffffff";
+google_color_bg = "ffffff";
+google_color_link = "0000ff";
+google_color_url = "000000";
+google_color_text = "000000";
+//--></script>
+<script type="text/javascript"
+src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
+</script>
+</div>
+
+"""
+
+analytics_data = """
+<script src="http://www.google-analytics.com/urchin.js"
type="text/javascript"></script>
+<script type="text/javascript">
+_uacct = "UA-557726-1";
+urchinTracker();
+</script>
+"""
+
+adsense_css = """
+/* Added for AdSense Support */
+body
+{
+ margin-left: 130px;
+ margin-right: 130px;
+}
+#adsense_left
+{
+ position: absolute;
+ left: 0px;
+ top: 0.5in;
+ width: 120px;
+ z-index: 2;
+}
+#adsense_bottom
+{
+}
+"""
+
+def add_adsense_left_html(file):
+ lines = open(file, 'r').readlines()
+ for i in range(len(lines)):
+ start_offset = lines[i].find('<body')
+ if start_offset == -1:
+ continue
+ for j in range(i, len(lines)):
+ end_offset = lines[j][start_offset:].find('>')
+ if end_offset == -1:
+ start_offset = 0
+ else:
+ end_offset = start_offset + end_offset
+ lines[j] = '%s%s%s' \
+ % (lines[j][:end_offset + 1],
+ adsense_left_data,
+ lines[j][end_offset + 1:])
+ open(file, 'w').writelines(lines)
+ return
+ raise Exception, "Never found <body> tag in file '%s'" % (file)
+
+
+def add_adsense_bottom_html(file):
+ lines = open(file, 'r').readlines()
+ for i in range(len(lines)):
+ start_offset = lines[i].find('<div class="navfooter"')
+ if start_offset == -1:
+ continue
+ lines[i] = '%s%s%s' \
+ % (lines[i][0:start_offset],
+ adsense_bottom_data,
+ lines[i][start_offset:])
+ open(file, 'w').writelines(lines)
+ return
+ raise Exception, "Never found <div class=\"nav_footer\"> tag in
file '%s'" % (file)
+
+
+def add_analytics_bug(file):
+ lines = open(file, 'r').readlines()
+ for i in range(len(lines)):
+ start_offset = lines[i].find('</body>')
+ if start_offset == -1:
+ continue
+ lines[i] = '%s%s%s' \
+ % (lines[i][0:start_offset],
+ analytics_data,
+ lines[i][start_offset:])
+ open(file, 'w').writelines(lines)
+ return
+ raise Exception, "Never found </body> tag in file '%s'" % (file)
+
+
+def add_adsense_css(file):
+ open(file, 'a').write(adsense_css)
+
+
+def adsensify(dst_path, dry_run, verbose):
+ if verbose:
+ print "Adding AdSense bits in '%s'" % (dst_path)
+ if dry_run:
+ return
+ stylesheet = os.path.join(dst_path, 'styles.css')
+ if not os.path.exists(stylesheet):
+ return
+ if not dry_run:
+ for child in os.listdir(dst_path):
+ if child[-5:] != '.html':
+ continue
+ try:
+ add_adsense_left_html(os.path.join(dst_path, child))
+ except:
+ pass
+ try:
+ add_adsense_bottom_html(os.path.join(dst_path, child))
+ except:
+ pass
+ try:
+ add_analytics_bug(os.path.join(dst_path, child))
+ except:
+ pass
+ add_adsense_css(stylesheet)
+
+
def do_build(locale, version, src_path, dst_path, formats,
dry_run=False, verbose=False):
@@ -261,6 +415,11 @@
for section in build_sections:
locale = cfg.get(section, 'locale')
version = cfg.get(section, 'version')
+ adsense = get_option(cfg, section, 'adsense')
+ try:
+ adsense = bool(int(adsense))
+ except:
+ adsense = False
src_path = get_option(cfg, section, 'src_path',
'branches/%s/%s' % (version, locale))
dst_path = get_option(cfg, section, 'dst_path',
@@ -273,6 +432,8 @@
build_begin_time = time.time()
do_build(locale, version, src_path, dst_path, formats,
dry_run, verbose)
+ if adsense:
+ adsensify(dst_path, dry_run, verbose)
build_end_time = time.time()
except Exception, e:
if dry_run:
More information about the svnbook-dev
mailing list