Skip to content

Commit

Permalink
Add templates folder
Browse files Browse the repository at this point in the history
  • Loading branch information
luthando5 committed Nov 1, 2023
1 parent 49b00d9 commit 65570a1
Show file tree
Hide file tree
Showing 5 changed files with 474 additions and 6 deletions.
12 changes: 6 additions & 6 deletions buildout.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ parts =
omelette
i18ndude
zopepy
# update_translations
update_translations
update_sources
write_code_headers
write_contributors
Expand Down Expand Up @@ -76,11 +76,11 @@ unzip = true
recipe = zc.recipe.egg
eggs = i18ndude

# [update_translations]
# recipe = collective.recipe.template
# output = ${buildout:directory}/bin/update_translations
# input = ${buildout:directory}/templates/update_translations.in
# mode = 755
[update_translations]
recipe = collective.recipe.template
output = ${buildout:directory}/bin/update_translations
input = ${buildout:directory}/templates/update_translations.in
mode = 755

[update_sources]
recipe = collective.recipe.template
Expand Down
14 changes: 14 additions & 0 deletions templates/update_sources.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

BASE=${buildout:directory}
SRC=$BASE/src

cd $SRC

for d in `find $SRC -mindepth 1 -maxdepth 1 -type d`
do
cd $d
echo -e "Current branch of repository $(basename $d): $(git rev-parse --abbrev-ref HEAD)"
git pull
cd ..
done
108 changes: 108 additions & 0 deletions templates/update_translations.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/bin/bash

BASE=${buildout:directory}
I18N=$BASE/bin/i18ndude
SRC=$BASE/src/senaite/core
SRC_BIKA=$BASE/src/bika/lims
LOCALES=$SRC/locales
EXCLUDE="build *.html"

function die() {
echo "ERROR: $1"
}

# Check if we have i18n in place
if test ! -x $I18N; then
echo "You must install i18ndude in your buildout: https://pypi.python.org/pypi/i18ndude"
echo "See https://github.com/collective/collective.developermanual/blob/master/source/i18n/localization.txt"
die "i18ndude not installed"
fi

# Check Transifex Installed
function has_tx() {
TX=$(which tx)
if test ! -x $TX || test -z $TX; then
return -1
else
return 0
fi
}

# Transifex Pull Function
function tx_pull() {
if has_tx; then
cd $LOCALES
echo "Transifex Pull"
tx pull -a -f
else
die "Please install the Transifex Client first: $ pip install transifex-client"
fi
cd $BASE
}

# Transifex Push Function
function tx_push() {
if has_tx; then
cd $LOCALES
echo "Transifex Push"
tx push -s -t --skip
else
die "Please install the Transifex Client first: $ pip install transifex-client"
fi
cd $BASE
}

# Clean all generated .mo files
find $LOCALES -name "*.mo" -delete

# Pull Transifex
tx_pull

# Generate the senaite.core Domain
echo "Processing domain: senaite.core"
# Flush the english (transifex source language) po files
# If we don't do this, new senaite.core-manual.pot translations won't be synced.
echo -n "" > $LOCALES/en/LC_MESSAGES/senaite.core.po
# Re-scan the source code for new strings and create a temporary senaite.core-bika.pot
$I18N rebuild-pot --pot $LOCALES/senaite.core-bika.pot --exclude "$EXCLUDE" --create senaite.core $SRC_BIKA
# Re-scan the source code for new strings and create a temporary senaite.core-tmp.pot
$I18N rebuild-pot --pot $LOCALES/senaite.core-tmp.pot --exclude "$EXCLUDE" --create senaite.core $SRC
# Merge the manual translations of senaite.core-bika.pot into the temporary senaite.core-tmp.pot
$I18N merge --pot $LOCALES/senaite.core-tmp.pot --merge $LOCALES/senaite.core-bika.pot
rm $LOCALES/senaite.core-bika.pot
# Merge the manual translations of senaite.core-manual.pot into the temporary senaite.core-tmp.pot
$I18N merge --pot $LOCALES/senaite.core-tmp.pot --merge $LOCALES/senaite.core-manual.pot
# Overwrite the existing senaite.core.pot
mv $LOCALES/senaite.core-tmp.pot $LOCALES/senaite.core.pot
# Synchronize all .po files with the new senaite.core.pot
$I18N sync --pot $LOCALES/senaite.core.pot $LOCALES/*/LC_MESSAGES/senaite.core.po
echo "Processing domain: senaite.core [DONE]"
echo ""

# Generate Plone Domain
echo "Processing domain: plone"
# Flush the english (transifex source language) po files
# If we don't do this, new plone-manual.pot translations won't be synced.
echo -n "" > $LOCALES/en/LC_MESSAGES/plone.po
# Re-scan the source code for new strings and create a temporary senaite.core-bika.pot
$I18N rebuild-pot --pot $LOCALES/plone-bika.pot --exclude "$EXCLUDE" --create plone $SRC_BIKA
# Re-scan the source code for new strings and create a temporary senaite.core-tmp.pot
$I18N rebuild-pot --pot $LOCALES/plone-tmp.pot --exclude "$EXCLUDE" --create plone $SRC
# Merge the manual translations of senaite.core-bika.pot into the temporary senaite.core-tmp.pot
$I18N merge --pot $LOCALES/plone-tmp.pot --merge $LOCALES/plone-bika.pot
rm $LOCALES/plone-bika.pot
# Remove those resources that already exists in senaite.core domain
# We need this because in Python files, i18ndude simply looks for text within an
# underscore method: _("...") without known the domain. Therefore, resources
# from senaite.core domain are also grabbed inside plone-tmp.pot
# Store the difference to plone.pot
$I18N filter $LOCALES/plone-tmp.pot $LOCALES/senaite.core.pot > $LOCALES/plone.pot
# Remove plone-tmp.pot
rm $LOCALES/plone-tmp.pot
# Synchronize all .po files with the new plone.pot
$I18N sync --pot $LOCALES/plone.pot $LOCALES/*/LC_MESSAGES/plone.po
echo "Processing domain: plone [DONE]"


# Push Transifex
tx_push
118 changes: 118 additions & 0 deletions templates/write_code_headers.py.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/usr/bin/env python
#
# Scans a source tree for python files and writes the header template on top of
# each file

import os
import sys
import optparse
from datetime import datetime

SRC_DIR = "${buildout:directory}/src/senaite"

YEAR_FROM = "2018"
TEMPLATE = """# -*- coding: utf-8 -*-
#
# This file is part of SENAITE.CORE.
#
# SENAITE.CORE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright {copy_years} by it's authors.
# Some rights reserved, see README and LICENSE.
"""


def get_template():
copy_years = datetime.now().strftime("%Y")
if copy_years != YEAR_FROM:
copy_years = '{}-{}'.format(YEAR_FROM, copy_years)
template_data = {
"copy_years": copy_years,
}
return TEMPLATE.format(**template_data)


if __name__ == "__main__":

parser = optparse.OptionParser()

parser.add_option("-d", "--directory",
dest="directory",
default=SRC_DIR,
help="Source code directory to scan "
"[default: %default]")

parser.add_option("-e", "--extensions",
dest="extensions",
default=".py",
help="Comma separated list of file extensions "
"[default: %default]")

options, args = parser.parse_args(sys.argv)

data = {}
directory = options.directory
extensions = map(lambda ext: ext.strip(), options.extensions.split(","))

def callback(arg, dirname, names):
# only write the header to the files where the file extension match
# (.py per default)
file_names = filter(lambda x: os.path.splitext(x)[-1] in extensions,
names)
# generate a list of full file paths
file_paths = map(lambda x: os.path.abspath(os.path.join(dirname, x)),
file_names)
# make a mapping of path -> file data
for path in file_paths:
lines = open(path, "r").readlines()

# Skip Script (Python) files
if lines and lines[0].startswith("## Script (Python)"):
continue

data[path] = lines

# walk the directory
os.path.walk(directory, callback, None)

for path, lines in data.iteritems():
# the new lines start with our header
new_lines = [get_template()]

skip = True
for num, line in enumerate(lines):
# skip all commented lines, but not those of Script (Python)
if skip and line.startswith("#") and not line.startswith("##"):
continue
# skip app empty lines
if skip and line == "\n":
continue

# if we reach this point, we found the first code line
if skip:
print "Found first code line for file {} at {}".format(
path, num)
skip = False

# append all code lines below the new_lines
new_lines.append(line)

with open(path, "w") as f:
# get the last line
last_line = new_lines[-1]
# remove all trailing empty lines and add a single one
new_lines[-1] = last_line.rstrip("\n") + "\n"
f.writelines(new_lines)
print "Wrote header to {}".format(path)
Loading

0 comments on commit 65570a1

Please sign in to comment.