Completed
Push — master ( 545613...55df2f )
by Daniel
14s queued 11s
created

sources.localizations_compile   A

Complexity

Total Complexity 0

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
eloc 66
dl 0
loc 72
rs 10
c 0
b 0
f 0
1
"""
2
localization_setup - facilitates localization file compilation (from .po to .mo)
3
"""
4
import glob
5
import os
6
import pathlib
7
8
file_pattern = os.path.join(os.path.dirname(__file__), '**/*.po')\
9
    .replace('/test', '/sources')\
10
    .replace('\\', '/')
11
list_localisation_source_files = glob.glob(file_pattern, recursive=True)
12
localisation_compilation_is_required = False
13
file_list_complete = False
14
domains_to_compile = []
15
file_counter = 0
16
compiling_files_counter = 0
17
while not file_list_complete:
18
    get_domain_details_for_compilation = False
19
    current_file = list_localisation_source_files[file_counter].replace('/', '\\')
20
    compiled_localisation_file = current_file.replace('.po', '.mo')
21
    folder_parts = os.path.dirname(current_file).split('\\')
22
    current_locale = folder_parts[(len(folder_parts)-2)]
23
    if os.path.lexists(pathlib.Path(compiled_localisation_file)):
24
        source_last_modified = os.path.getmtime(current_file)
25
        compiled_last_modified = os.path.getmtime(compiled_localisation_file)
26
        if source_last_modified > compiled_last_modified:
27
            localisation_compilation_is_required = True
28
            get_domain_details_for_compilation = True
29
            print('#' + str(file_counter) + ', '
30
                  + 'For locale ' + current_locale + ' source file '
31
                  + os.path.basename(current_file)
32
                  + ' is newer than compiled one '
33
                  + os.path.basename(compiled_localisation_file)
34
                  + ' so compilation needs to take place to remedy this')
35
            print('===>' + current_file + ' has ' + str(source_last_modified)
36
                  + ' vs. ' + str(compiled_last_modified))
37
    else:
38
        get_domain_details_for_compilation = True
39
        print('#' + str(file_counter) + ', '
40
              + 'For locale ' + current_locale + ' does not exists, '
41
              + 'therefore will require compiling, so '
42
              + os.path.basename(current_file)
43
              + ' will be used to generate '
44
              + os.path.basename(compiled_localisation_file)
45
              + ' in the same folder: ' + os.path.dirname(current_file))
46
        localisation_compilation_is_required = True
47
    if get_domain_details_for_compilation:
48
        domains_to_compile.append(compiling_files_counter)
49
        domains_to_compile[compiling_files_counter] = {
50
            'input-file': current_file,
51
            'output-file': compiled_localisation_file,
52
            'locale': current_locale,
53
        }
54
        compiling_files_counter += 1
55
    file_counter += 1
56
    if file_counter == len(list_localisation_source_files):
57
        file_list_complete = True
58
project_root = os.path.dirname(__file__).replace('/sources', '')
59
compiler_binary = os.path.join(
60
    os.path.dirname(__file__)\
61
        .replace('sources', 'virtual_environment/Scripts')\
62
        .replace('/', '\\'),
63
    'python.exe')
64
if localisation_compilation_is_required:
65
    for current_domain_to_compile in domains_to_compile:
66
        os.system(compiler_binary + ' ' + os.path.join(project_root, 'localizations_setup.py')
67
                  + ' compile_catalog'
68
                  + ' --input-file=' + current_domain_to_compile['input-file']
69
                  + ' --output-file=' + current_domain_to_compile['output-file']
70
                  + ' --locale ' + current_domain_to_compile['locale']
71
                  + ' --statistics')
72