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

Make parameters attributes

This easies further processing.
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent f2f5eb47
......@@ -62,6 +62,13 @@ class Command(BaseCommand):
),
)
def __init__(self, *args, **kwargs):
super(Command, self).__init__(*args, **kwargs)
self.filemask = None
self.file_format = None
self.name_template = None
self.base_file_template = None
def format_string(self, template, match):
'''
Formats template string with match.
......@@ -74,11 +81,11 @@ class Command(BaseCommand):
matches = maskre.match(path)
return matches.group(1)
def get_match_regexp(self, filemask):
def get_match_regexp(self):
'''
Prepare regexp for file matching
'''
match = fnmatch.translate(filemask)
match = fnmatch.translate(self.filemask)
match = match.replace('.*.*', '(.*.*)')
return re.compile(match)
......@@ -104,24 +111,24 @@ class Command(BaseCommand):
return workdir
def get_matching_files(self, repo, filemask):
def get_matching_files(self, repo):
'''
Returns relative path of matched files.
'''
matches = glob(os.path.join(repo, filemask))
matches = glob(os.path.join(repo, self.filemask))
return [f.replace(repo, '').strip('/') for f in matches]
def get_matching_subprojects(self, repo, filemask):
def get_matching_subprojects(self, repo):
'''
Scan the master repository for names matching our mask
'''
# Find matching files
matches = self.get_matching_files(repo, filemask)
matches = self.get_matching_files(repo)
weblate.logger.info('Found %d matching files', len(matches))
# Parse subproject names out of them
names = set()
maskre = self.get_match_regexp(filemask)
maskre = self.get_match_regexp()
for match in matches:
names.add(self.get_name(maskre, match))
weblate.logger.info('Found %d subprojects', len(names))
......@@ -131,6 +138,8 @@ class Command(BaseCommand):
'''
Automatic import of project.
'''
# Check params
if len(args) != 4:
raise CommandError('Invalid number of parameters!')
......@@ -139,20 +148,25 @@ class Command(BaseCommand):
'Invalid file format: %s' % options['file_format']
)
# Read params, pylint: disable=W0632
prjname, repo, branch, filemask = args
# Read params
repo = args[1]
branch = args[2]
self.filemask = args[3]
self.file_format = options['file_format']
self.name_template = options['name_template']
self.base_file_template = options['base_file_template']
# Try to get project
try:
project = Project.objects.get(slug=prjname)
project = Project.objects.get(slug=args[0])
except Project.DoesNotExist:
raise CommandError(
'Project %s does not exist, you need to create it first!' %
prjname
args[0]
)
# Do we have correct mask?
if '**' not in filemask:
if '**' not in self.filemask:
raise CommandError(
'You need to specify double wildcard '
'for subproject part of the match!'
......@@ -172,18 +186,14 @@ class Command(BaseCommand):
)
matches = self.get_matching_subprojects(
sub_project.get_path(),
filemask
)
else:
matches, sharedrepo = self.import_initial(
project, repo, branch, filemask, options['name_template'],
options['file_format'], options['base_file_template']
)
matches, sharedrepo = self.import_initial(project, repo, branch)
# Create remaining subprojects sharing git repository
for match in matches:
name = self.format_string(options['name_template'], match)
template = self.format_string(options['base_file_template'], match)
name = self.format_string(self.name_template, match)
template = self.format_string(self.base_file_template, match)
slug = slugify(name)
subprojects = SubProject.objects.filter(
Q(name=name) | Q(slug=slug),
......@@ -204,23 +214,22 @@ class Command(BaseCommand):
repo=sharedrepo,
branch=branch,
template=template,
file_format=options['file_format'],
filemask=filemask.replace('**', match)
file_format=self.file_format,
filemask=self.filemask.replace('**', match)
)
def import_initial(self, project, repo, branch, filemask, name_template,
file_format, base_file_template):
def import_initial(self, project, repo, branch):
'''
Import the first repository of a project
'''
# Checkout git to temporary dir
workdir = self.checkout_tmp(project, repo, branch)
matches = self.get_matching_subprojects(workdir, filemask)
matches = self.get_matching_subprojects(workdir)
# Create first subproject (this one will get full git repo)
match = matches.pop()
name = self.format_string(name_template, match)
template = self.format_string(base_file_template, match)
name = self.format_string(self.name_template, match)
template = self.format_string(self.base_file_template, match)
slug = slugify(name)
if SubProject.objects.filter(project=project, slug=slug).exists():
......@@ -246,9 +255,9 @@ class Command(BaseCommand):
project=project,
repo=repo,
branch=branch,
file_format=file_format,
file_format=self.file_format,
template=template,
filemask=filemask.replace('**', match)
filemask=self.filemask.replace('**', match)
)
sharedrepo = 'weblate://%s/%s' % (project.slug, slug)
......
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