2
# Copyright 2010 Canonical Ltd. This software is licensed under the
3
# GNU Affero General Public License version 3 (see the file LICENSE).
13
from bzrlib.branch import Branch
14
from bzrlib.export import export
16
from canonical.buildd.pottery import intltool
19
class GenerateTranslationTemplates:
20
"""Script to generate translation templates from a branch."""
22
def __init__(self, branch_spec, result_name, work_dir, log_file=None):
23
"""Prepare to generate templates for a branch.
25
:param branch_spec: Either a branch URL or the path of a local
26
branch. URLs are recognized by the occurrence of ':'. In
27
the case of a URL, this will make up a path for the branch
28
and check out the branch to there.
29
:param result_name: The name of the result tarball. Should end in
31
:param work_dir: The directory to work in. Must exist.
32
:param log_file: File-like object to log to. If None, defaults to
35
self.work_dir = work_dir
36
self.branch_spec = branch_spec
37
self.result_name = result_name
38
self.logger = self._setupLogger(log_file)
40
def _setupLogger(self, log_file):
41
"""Sets up and returns a logger."""
44
logger = logging.getLogger("generate-templates")
45
logger.setLevel(logging.DEBUG)
46
ch = logging.StreamHandler(log_file)
47
ch.setLevel(logging.DEBUG)
52
"""Set `self.branch_dir`, and check out branch if needed."""
53
if ':' in self.branch_spec:
54
# This is a branch URL. Check out the branch.
55
self.branch_dir = os.path.join(self.work_dir, 'source-tree')
56
self.logger.info("Getting remote branch %s..." % self.branch_spec)
57
self._checkout(self.branch_spec)
59
# This is a local filesystem path. Use the branch in-place.
60
self.logger.info("Using local branch %s..." % self.branch_spec)
61
self.branch_dir = self.branch_spec
63
def _checkout(self, branch_url):
64
"""Check out a source branch to generate from.
66
The branch is checked out to the location specified by
69
self.logger.info("Opening branch %s..." % branch_url)
70
branch = Branch.open(branch_url)
71
self.logger.info("Getting branch revision tree...")
72
rev_tree = branch.basis_tree()
73
self.logger.info("Exporting branch to %s..." % self.branch_dir)
74
export(rev_tree, self.branch_dir)
75
self.logger.info("Exporting branch done.")
77
def _makeTarball(self, files):
78
"""Put the given files into a tarball in the working directory."""
79
tarname = os.path.join(self.work_dir, self.result_name)
80
self.logger.info("Making tarball with templates in %s..." % tarname)
81
tarball = tarfile.open(tarname, 'w|gz')
82
files = [name for name in files if not name.endswith('/')]
84
full_path = os.path.join(self.branch_dir, path)
85
self.logger.info("Adding template %s..." % full_path)
86
tarball.add(full_path, path)
88
self.logger.info("Tarball generated.")
91
"""Do It. Generate templates."""
92
self.logger.info("Generating templates for %s." % self.branch_spec)
94
pots = intltool.generate_pots(self.branch_dir)
95
self.logger.info("Generated %d templates." % len(pots))
97
self._makeTarball(pots)
101
if __name__ == '__main__':
102
if len(sys.argv) < 3:
103
print "Usage: %s branch resultname [workdir]" % sys.argv[0]
104
print " 'branch' is a branch URL or directory."
105
print " 'resultname' is the name of the result tarball."
106
print " 'workdir' is a directory, defaults to HOME."
108
if len(sys.argv) == 4:
109
workdir = sys.argv[3]
111
workdir = os.environ['HOME']
112
script = GenerateTranslationTemplates(
113
sys.argv[1], sys.argv[2], workdir)
114
sys.exit(script.generate())