Passed
Push — development/test ( 4cbfb6...1114e0 )
by Daniel
01:17
created

sources.localizations_compile   A

Complexity

Total Complexity 0

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
eloc 66
dl 0
loc 69
rs 10
c 0
b 0
f 0
1
import glob
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

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