This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
/
utils.py
131 lines (101 loc) · 3.62 KB
/
utils.py
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
# Copyright (C) 2013 anonymous <brainpower@gulli.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os, re, subprocess
import pyalpm
from pycman import config, transaction
def run_command(*args):
out = b""
#err = b""
proc = subprocess.Popen(args, stdout=subprocess.PIPE) #, stderr=subprocess.PIPE)
while(proc.returncode == None):
out += proc.communicate()[0]
#err += proc.communicate()[1]
return out #,err
def get_devices():
lst = []
out = run_command("blkid").decode("utf-8")
lines = out.split("\n")
regex = re.compile(r"^(.*):.*TYPE=\"(.*?)\".*")
for line in lines:
x = regex.match(line)
if x:
lst.append( (x.group(1), x.group(2)))
return lst
def get_filesystem_types():
paths = os.environ["PATH"].split(":")
stdpaths = ["/bin", "/usr/bin", "/sbin", "/usr/sbin"]
types = []
for stdpath in stdpaths:
if not stdpath in paths:
paths.append(stdpath)
for path in paths:
for line in run_command("find", path , "-name", "mkfs.*").decode().split("\n"):
types.append(line.split(".")[-1])
types.sort()
return list(filter(None, types))
class Pacman(object):
def __init__(self):
self.handle = config.init_with_config("/etc/pacman.conf")
for db in self.handle.get_syncdbs(): # do a -Sy on start
t = transaction.init_from_options(self.handle, None)
db.update(False) # dont force update
t.release()
self.update_members()
def update_members(self):
self.syncdbs = { p.name:p for p in self.handle.get_syncdbs() }
self.repos = [ p.name for p in self.handle.get_syncdbs() ]
self.groupcaches = [ (db,self.syncdbs[db].grpcache) for db in self.syncdbs ]
self.groups = { gc[0]:[ g[0] for g in gc[1]] for gc in self.groupcaches }
self.group_pkgs = { gc[0]:{ g[0]:g[1] for g in gc[1]} for gc in self.groupcaches }
def get_repos(self):
return self.repos
def get_groups(self):
return self.groups
def get_package_list(self, repo, group=None):
if not group:
return [ (False, p.name, p.version, p.desc) for p in self.syncdbs[repo].pkgcache]
else:
return [ (False, p.name, p.version, p.desc) for p in self.group_pkgs[repo][group]]
#~ def get_repo_of_group(self, group):
#~ for repo in self.groups:
#~ if group in self.groups[repo]:
#~ return repo
def get_repos_of_group(self, group):
return [repo for repo in self.groups if group in self.groups[repo]]
def get_groups_of_repo(self, repo):
return self.groups[repo];
def get_repos_of_pkg(self, pkg):
lst = []
for repo in self.repos:
if pkg in [p.name for p in self.syncdbs[repo].pkgcache]:
lst.append(repo);
return lst
def get_groups_of_pkg(self, pkg):
for repo in self.get_repos():
for p in self.syncdbs[repo].pkgcache:
if p.name == pkg:
return p.groups;
def main():
#~ devs = get_devices()
#~ for dev,typ in devs:
#~ print(dev,typ)
#print(get_filesystem_types())
pacman = Pacman()
#~ print(pacman.get_repos())
#~ print(pacman.get_package_list("core"))
#print(pacman.get_groups())
#print(pacman.get_package_list("core", "base"))
print(pacman.get_repo_of_group("unity"))
if __name__ == "__main__":
main()