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