summaryrefslogtreecommitdiff
path: root/pygnulib/GLMakefileTable.py
blob: 74e6fe7d7dfda737356b87bd70963c17e9498d31 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python
# encoding: UTF-8

#===============================================================================
# Define global imports
#===============================================================================
import os
import re
import sys
import codecs
import hashlib
import subprocess as sp
from . import constants
from .GLError import GLError
from .GLConfig import GLConfig
from .GLFileSystem import GLFileSystem


#===============================================================================
# Define module information
#===============================================================================
__author__ = constants.__author__
__license__ = constants.__license__
__copyright__ = constants.__copyright__


#===============================================================================
# Define global constants
#===============================================================================
PYTHON3 = constants.PYTHON3
NoneType = type(None)
APP = constants.APP
DIRS = constants.DIRS
ENCS = constants.ENCS
UTILS = constants.UTILS
MODES = constants.MODES
TESTS = constants.TESTS
compiler = constants.compiler
joinpath = constants.joinpath
cleaner = constants.cleaner
string = constants.string
isabs = os.path.isabs
isdir = os.path.isdir
isfile = os.path.isfile
normpath = os.path.normpath
relpath = os.path.relpath
filter_filelist = constants.filter_filelist


#===============================================================================
# Define GLMakefileTable class
#===============================================================================
class GLMakefileTable(object):
    '''This class is used to edit Makefile and store edits as table.
    When user creates  Makefile, he may need to use this class.'''

    def __init__(self, config):
        '''GLMakefileTable.__init__(config) -> GLMakefileTable

        Create GLMakefileTable instance.'''
        if type(config) is not GLConfig:
            raise(TypeError('config must be a GLConfig, not %s' %
                            type(config).__name__))
        self.config = config
        self.table = list()

    def __getitem__(self, y):
        '''x.__getitem__(y) = x[y]'''
        if type(y) is not int:
            raise(TypeError('indices must be integers, not %s' %
                            type(y).__name__))
        result = self.table[y]
        return(dict(result))

    def editor(self, dir, var, val):
        '''GLMakefileTable.editor(dir, var, val)

        This method is used to remember that ${dir}Makefile.am needs to be edited
        to that ${var} mentions ${val}.'''
        if type(dir) is bytes or type(dir) is string:
            if type(dir) is bytes:
                dir = dir.decode(ENCS['default'])
        else:  # if dir has not bytes or string type
            raise(TypeError(
                'dir must be a string, not %s' % (type(dir).__name__)))
        if type(var) is bytes or type(var) is string:
            if type(var) is bytes:
                var = var.decode(ENCS['default'])
        else:  # if var has not bytes or string type
            raise(TypeError(
                'var must be a string, not %s' % (type(var).__name__)))
        if type(val) is bytes or type(val) is string:
            if type(val) is bytes:
                val = val.decode(ENCS['default'])
        else:  # if val has not bytes or string type
            raise(TypeError(
                'val must be a string, not %s' % (type(val).__name__)))
        dictionary = {'dir': dir, 'var': var, 'val': val}
        self.table += [dictionary]

    def parent(self):
        '''GLMakefileTable.parent()

        Add a special row to Makefile.am table with the first parent directory
        which contains or will contain Makefile.am file.
        GLConfig: sourcebase, m4base, testsbase, testflags, makefile.'''
        m4base = self.config['m4base']
        sourcebase = self.config['sourcebase']
        testsbase = self.config['testsbase']
        makefile = self.config['makefile']
        inctests = self.config.checkTestFlag(TESTS['tests'])
        dir1 = string('%s%s' % (m4base, os.path.sep))
        mfd = string('Makefile.am')
        if not makefile:
            mfx = string('Makefile.am')
        else:  # if makefile
            mfx = makefile
        dir2 = string()
        while dir1 and \
            (joinpath(self.config['destdir'], dir1, mfd) or
             joinpath(dir1, mfd) == joinpath(sourcebase, mfx) or
             (inctests and joinpath(dir1, mfd) == joinpath(testsbase, mfx))):
            dir2 = joinpath(os.path.basename(dir1), dir2)
            dir1 = os.path.dirname(dir1)
        self.editor(dir1, 'EXTRA_DIST', joinpath(dir2, 'gnulib-cache.m4'))

    def count(self):
        '''GLMakefileTable.count() -> int

        Count number of edits which were applied.'''
        return(len(self.table))