summaryrefslogtreecommitdiff
path: root/data/make_project_file.py
blob: 5519e12859d4cfa1fc15b8e6b8cd7ebf76636e89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# encoding: utf-8

import fnmatch
import os
import os.path
import re


# thanks to http://stackoverflow.com/a/5141829/687989
def list_files(includes, excludes, start='.'):
    """
    Returns a list of files matching the glob expressions of the included
    parameter and excluding the files and directories matching the parameter
    excludes.

    :param includes: the files to match, using glob's format.
    :type includes: list of str
    :param excludes: the files and directories to exclude, using glob's format.
    :type excludes: list of str
    """
    # transform glob patterns to regular expressions
    includes = r'|'.join([fnmatch.translate(x) for x in includes])
    excludes = r'|'.join([fnmatch.translate(x) for x in excludes]) or r'$.'

    files_list = []

    for root, dirs, files in os.walk(start):
        # exclude dirs
        if excludes:
            dirs[:] = [d for d in dirs if not re.match(excludes, d)]

        # exclude/include files
        if excludes:
            files = [f for f in files if not re.match(excludes, f)]
        files = [f for f in files if re.match(includes, f)]
        files = [os.path.join(root, f) for f in files]

        for fname in files:
            files_list.append(fname)

    return files_list


if __name__ == '__main__':
    SOURCE_ROOT = 'src'
    TEMPLATE = 'data/bitmask.pro.template'
    PROJECT = 'data/bitmask.pro'

    HEADER = ("# DO NOT EDIT MANUALLY.\n"
              "# qmake file generated by script, any changes will be lost.")

    # Source files
    includes = ["*.py"]
    excludes = ['__init__.py', '_version.py', 'ui_*.py', '*_rc.py',
                'pinned*.py', 'tests']
    sources = list_files(includes, excludes, SOURCE_ROOT)
    sources = " \\\n".join(["../{0}".format(f) for f in sources])

    # Form files
    includes = ["*.ui"]
    excludes = []
    forms = list_files(includes, excludes, SOURCE_ROOT)
    forms = " \\\n".join(["../{0}".format(f) for f in forms])

    # Load template and write project
    template = file(TEMPLATE).read()
    project = template.format(header=HEADER, sources=sources, forms=forms)

    try:
        with open(PROJECT, 'w') as output:
            output.write(project)
            print "Project file generated successfully"
    except IOError, e:
        print "Error saving project file: {0}".format(e)