-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpkgconfig.py
More file actions
141 lines (103 loc) · 3.43 KB
/
pkgconfig.py
File metadata and controls
141 lines (103 loc) · 3.43 KB
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
132
133
134
135
136
137
138
139
140
141
from __future__ import print_function
import sys
try:
from subprocess import check_output
def execute(lt):
return check_output(lt)
except Exception:
from subprocess import Popen
from subprocess import PIPE
def execute(lt):
p = Popen(lt, stdout=PIPE)
result = ""
for x in p.stdout:
result += x
return result
def strip_string_list(inlist):
"""
strip_string_list(inlist):
Strip all strings in a list of strings from all leading and
trailing blanks.
input arguments:
inlist ............ input list of strings
return:
new list with all strings stripped.
"""
lt = []
for value in inlist:
lt.append(value.strip())
return lt
def remove_empty_strings(inlist):
"""
remove_empty_strings(inlist):
Remove all empty strings from the list of strings.
input arguments:
inlist ............. inpust list of strings
return:
list without empty strings
"""
cnt = inlist.count('')
outlist = list(inlist)
for i in range(cnt):
outlist.remove('')
return outlist
def split_result(result, key):
result = result.strip()
result = result.split(key)
result = remove_empty_strings(result)
return result
class package(object):
command = 'pkg-config'
def __init__(self, pkgname):
self.name = pkgname
def _decode(self, data):
if sys.version_info.major >= 3:
return data.decode('utf-8')
else:
return data
def _get_library_dirs(self):
result = self._decode(
execute([self.command, '--libs-only-L', self.name]))
result = split_result(result, '-L')
return strip_string_list(result)
def _get_include_dirs(self):
result = self._decode(
execute([self.command, '--cflags-only-I', self.name]))
result = split_result(result, '-I')
return strip_string_list(result)
def _get_libraries(self):
result = self._decode(
execute([self.command, '--libs-only-l', self.name]))
result = split_result(result, '-l')
return strip_string_list(result)
def _get_compiler_flags(self):
# first we obtain all compiler flags
total_result = self._decode(
execute([self.command, '--cflags', self.name]))
total_result = total_result.strip()
total_result = total_result.split(" ")
total_result = remove_empty_strings(total_result)
# now we have to obtain all the include files
includes = self._decode(
execute([self.command, '--cflags-only-I', self.name]))
includes = includes.strip()
includes = includes.split(" ")
includes = remove_empty_strings(includes)
for header in includes:
total_result.remove(header)
return total_result
library_dirs = property(_get_library_dirs)
libraries = property(_get_libraries)
compiler_flags = property(_get_compiler_flags)
include_dirs = property(_get_include_dirs)
# testing routine
if __name__ == "__main__":
if len(sys.argv) < 2:
print("You have to pass a package name to as a command line argument!")
sys.exit()
name = sys.argv[1]
p = package(name)
print("library directories: ", p.library_dirs)
print("libraries : ", p.libraries)
print("compiler flags : ", p.compiler_flags)
print("include directories: ", p.include_dirs)