Add script for determining the set of symbols to export from a library.
[gnulib.git] / pygnulib / GLMakefileTable.py
blob74e6fe7d7dfda737356b87bd70963c17e9498d31
1 #!/usr/bin/python
2 # encoding: UTF-8
4 #===============================================================================
5 # Define global imports
6 #===============================================================================
7 import os
8 import re
9 import sys
10 import codecs
11 import hashlib
12 import subprocess as sp
13 from . import constants
14 from .GLError import GLError
15 from .GLConfig import GLConfig
16 from .GLFileSystem import GLFileSystem
19 #===============================================================================
20 # Define module information
21 #===============================================================================
22 __author__ = constants.__author__
23 __license__ = constants.__license__
24 __copyright__ = constants.__copyright__
27 #===============================================================================
28 # Define global constants
29 #===============================================================================
30 PYTHON3 = constants.PYTHON3
31 NoneType = type(None)
32 APP = constants.APP
33 DIRS = constants.DIRS
34 ENCS = constants.ENCS
35 UTILS = constants.UTILS
36 MODES = constants.MODES
37 TESTS = constants.TESTS
38 compiler = constants.compiler
39 joinpath = constants.joinpath
40 cleaner = constants.cleaner
41 string = constants.string
42 isabs = os.path.isabs
43 isdir = os.path.isdir
44 isfile = os.path.isfile
45 normpath = os.path.normpath
46 relpath = os.path.relpath
47 filter_filelist = constants.filter_filelist
50 #===============================================================================
51 # Define GLMakefileTable class
52 #===============================================================================
53 class GLMakefileTable(object):
54 '''This class is used to edit Makefile and store edits as table.
55 When user creates Makefile, he may need to use this class.'''
57 def __init__(self, config):
58 '''GLMakefileTable.__init__(config) -> GLMakefileTable
60 Create GLMakefileTable instance.'''
61 if type(config) is not GLConfig:
62 raise(TypeError('config must be a GLConfig, not %s' %
63 type(config).__name__))
64 self.config = config
65 self.table = list()
67 def __getitem__(self, y):
68 '''x.__getitem__(y) = x[y]'''
69 if type(y) is not int:
70 raise(TypeError('indices must be integers, not %s' %
71 type(y).__name__))
72 result = self.table[y]
73 return(dict(result))
75 def editor(self, dir, var, val):
76 '''GLMakefileTable.editor(dir, var, val)
78 This method is used to remember that ${dir}Makefile.am needs to be edited
79 to that ${var} mentions ${val}.'''
80 if type(dir) is bytes or type(dir) is string:
81 if type(dir) is bytes:
82 dir = dir.decode(ENCS['default'])
83 else: # if dir has not bytes or string type
84 raise(TypeError(
85 'dir must be a string, not %s' % (type(dir).__name__)))
86 if type(var) is bytes or type(var) is string:
87 if type(var) is bytes:
88 var = var.decode(ENCS['default'])
89 else: # if var has not bytes or string type
90 raise(TypeError(
91 'var must be a string, not %s' % (type(var).__name__)))
92 if type(val) is bytes or type(val) is string:
93 if type(val) is bytes:
94 val = val.decode(ENCS['default'])
95 else: # if val has not bytes or string type
96 raise(TypeError(
97 'val must be a string, not %s' % (type(val).__name__)))
98 dictionary = {'dir': dir, 'var': var, 'val': val}
99 self.table += [dictionary]
101 def parent(self):
102 '''GLMakefileTable.parent()
104 Add a special row to Makefile.am table with the first parent directory
105 which contains or will contain Makefile.am file.
106 GLConfig: sourcebase, m4base, testsbase, testflags, makefile.'''
107 m4base = self.config['m4base']
108 sourcebase = self.config['sourcebase']
109 testsbase = self.config['testsbase']
110 makefile = self.config['makefile']
111 inctests = self.config.checkTestFlag(TESTS['tests'])
112 dir1 = string('%s%s' % (m4base, os.path.sep))
113 mfd = string('Makefile.am')
114 if not makefile:
115 mfx = string('Makefile.am')
116 else: # if makefile
117 mfx = makefile
118 dir2 = string()
119 while dir1 and \
120 (joinpath(self.config['destdir'], dir1, mfd) or
121 joinpath(dir1, mfd) == joinpath(sourcebase, mfx) or
122 (inctests and joinpath(dir1, mfd) == joinpath(testsbase, mfx))):
123 dir2 = joinpath(os.path.basename(dir1), dir2)
124 dir1 = os.path.dirname(dir1)
125 self.editor(dir1, 'EXTRA_DIST', joinpath(dir2, 'gnulib-cache.m4'))
127 def count(self):
128 '''GLMakefileTable.count() -> int
130 Count number of edits which were applied.'''
131 return(len(self.table))