Commit fe716ce8 authored by Michal Čihař's avatar Michal Čihař

Improved configurability of import_project command.

User can now pass regexp used to parse matches.

Issue #760
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 2b8114bd
...@@ -106,6 +106,12 @@ You can also specify file format to use (see :ref:`formats`) by the ...@@ -106,6 +106,12 @@ You can also specify file format to use (see :ref:`formats`) by the
In case you need to specify version control system to use, you can do this using In case you need to specify version control system to use, you can do this using
``--vcs`` parameter. The default version control is Git. ``--vcs`` parameter. The default version control is Git.
You can override parsing of component name from matched files by
``--component-regexp``. This is a regular expression which will be matched
against file name (as matched by `<filemask>`) and has to contain named group
`name`. This can be also used for excluding files in case they do not match
this expression. For example: ``.*/(?P<name>[^-]*)\.po``
To give you some examples, let's try importing two projects. To give you some examples, let's try importing two projects.
As first we import The Debian Handbook translations, where each language has As first we import The Debian Handbook translations, where each language has
......
...@@ -8,6 +8,7 @@ Released on ? 2015. ...@@ -8,6 +8,7 @@ Released on ? 2015.
* Improved support for PHP files. * Improved support for PHP files.
* Ability to add ACL to anonymous user. * Ability to add ACL to anonymous user.
* Improved configurability of import_project command.
weblate 2.3 weblate 2.3
----------- -----------
......
...@@ -50,6 +50,13 @@ class Command(BaseCommand): ...@@ -50,6 +50,13 @@ class Command(BaseCommand):
'match to a project name' 'match to a project name'
) )
), ),
make_option(
'--component-regexp',
default=None,
help=(
'Regular expression to match component out of filename'
)
),
make_option( make_option(
'--base-file-template', '--base-file-template',
default='', default='',
...@@ -73,6 +80,7 @@ class Command(BaseCommand): ...@@ -73,6 +80,7 @@ class Command(BaseCommand):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(Command, self).__init__(*args, **kwargs) super(Command, self).__init__(*args, **kwargs)
self.filemask = None self.filemask = None
self.component_re = None
self.file_format = None self.file_format = None
self.name_template = None self.name_template = None
self.base_file_template = None self.base_file_template = None
...@@ -93,6 +101,9 @@ class Command(BaseCommand): ...@@ -93,6 +101,9 @@ class Command(BaseCommand):
Returns file name from patch based on filemask. Returns file name from patch based on filemask.
""" """
matches = self.match_regexp.match(path) matches = self.match_regexp.match(path)
if matches is None:
self.logger.warning('Skipping {0}'.format(path))
return None
return matches.group('name') return matches.group('name')
@property @property
...@@ -100,6 +111,8 @@ class Command(BaseCommand): ...@@ -100,6 +111,8 @@ class Command(BaseCommand):
''' '''
Returns regexp for file matching Returns regexp for file matching
''' '''
if self.component_re is not None:
return self.component_re
if self._mask_regexp is None: if self._mask_regexp is None:
match = fnmatch.translate(self.filemask) match = fnmatch.translate(self.filemask)
match = match.replace('.*.*', '(?P<name>.*)') match = match.replace('.*.*', '(?P<name>.*)')
...@@ -140,7 +153,9 @@ class Command(BaseCommand): ...@@ -140,7 +153,9 @@ class Command(BaseCommand):
# Parse subproject names out of them # Parse subproject names out of them
names = set() names = set()
for match in matches: for match in matches:
names.add(self.get_name(match)) name = self.get_name(match)
if name:
names.add(name)
self.logger.info('Found %d subprojects', len(names)) self.logger.info('Found %d subprojects', len(names))
return names return names
...@@ -166,6 +181,22 @@ class Command(BaseCommand): ...@@ -166,6 +181,22 @@ class Command(BaseCommand):
self.file_format = options['file_format'] self.file_format = options['file_format']
self.name_template = options['name_template'] self.name_template = options['name_template']
self.base_file_template = options['base_file_template'] self.base_file_template = options['base_file_template']
if options['component_regexp']:
try:
self.component_re = re.compile(
options['component_regexp'],
re.MULTILINE | re.DOTALL
)
except re.error as error:
raise CommandError(
'Failed to compile regullar expression "{0}": {1}'.format(
options['component_regexp'], error
)
)
if 'name' not in self.component_re.groupindex:
raise CommandError(
'Component regullar expression lacks named group "name"'
)
# Try to get project # Try to get project
try: try:
......
...@@ -48,6 +48,42 @@ class ImportProjectTest(RepoTestCase): ...@@ -48,6 +48,42 @@ class ImportProjectTest(RepoTestCase):
# We should have loaded four subprojects # We should have loaded four subprojects
self.assertEqual(project.subproject_set.count(), 4) self.assertEqual(project.subproject_set.count(), 4)
def test_import_re(self):
project = self.create_project()
call_command(
'import_project',
'test',
self.git_repo_path,
'master',
'**/*.po',
component_regexp=r'(?P<name>[^/-]*)/.*\.po'
)
self.assertEqual(project.subproject_set.count(), 1)
def test_import_re_missing(self):
self.assertRaises(
CommandError,
call_command,
'import_project',
'test',
self.git_repo_path,
'master',
'**/*.po',
component_regexp=r'(?P<xname>[^/-]*)/.*\.po'
)
def test_import_re_wrong(self):
self.assertRaises(
CommandError,
call_command,
'import_project',
'test',
self.git_repo_path,
'master',
'**/*.po',
component_regexp=r'(?P<xname>[^/-]*'
)
def test_import_po(self): def test_import_po(self):
project = self.create_project() project = self.create_project()
call_command( call_command(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment