3b346a582e279299eeaa757774a7cafa55ecc26c
[ebuildfind.git] / commands / parse.py
blob3b346a582e279299eeaa757774a7cafa55ecc26c
1 from __future__ import with_statement
3 import os, re
5 from django.conf import settings
7 os.environ['DJANGO_SETTINGS_MODULE'] = 'ebuilds.settings'
9 from layman.debug import Message
11 from ebuilds.ebuildfind.models import Ebuild, Overlay
13 from whoosh_manager import WhooshEbuildManager
14 from manage_layman import LaymanManager
16 OVERLAYS_BASE = settings.ROOT_PATH + "ebuilds/" + "ebuildfind/commands/var/overlays/"
18 class EbuildParser:
19 def __init__(self, overlay, category, app, file_path):
20 path, filename = os.path.split(os.path.realpath(file_path))
22 name_version, ext = os.path.splitext(filename)
23 n = len(app)
25 version = name_version[n+1:]
27 self.name = app
28 self.version = version
29 self.category = category
30 self.overlay = overlay
32 with open(file_path) as file:
33 f = file.read()
34 regex1 = re.compile(r'([a-z_]+\s*\(\))\s*{.*}', re.DOTALL)
35 f = re.sub(regex1, r'', f)
36 regex2 = re.compile(r'(for.*done)', re.DOTALL)
37 f = re.sub(regex2, r'', f)
38 data = dict(re.findall("(DESCRIPTION|HOMEPAGE|KEYWORDS|LICENSE|IUSE)=\"(.+)\"", f))
40 def notnull(d, key):
41 try:
42 return d[key]
43 except:
44 return ""
46 self.description = notnull(data, "DESCRIPTION")
47 self.description = self.description.replace("\n"," ")
48 self.homepage = notnull(data, "HOMEPAGE")
49 self.keywords = notnull(data, "KEYWORDS")
50 self.license = notnull(data, "LICENSE")
51 self.iuse = notnull(data, "IUSE")
53 def __repr__(self):
54 output = "%s/%s [%s] @ %s" % (self.category, self.name, self.version, self.overlay)
55 output += " " + self.description
56 output += " " + self.homepage
57 output += " " + self.keywords
58 output += " " + self.license
59 output += " " + self.iuse
60 return output
62 def exclude_directory(path, dir):
63 exclude_dir = ["eclass", "profiles", "README-TODO", "Documentation", "sets", "index"]
64 return os.path.isdir(path) and dir not in exclude_dir and not dir.startswith(".")
66 def ParseEbuilds():
67 i = 0
68 Ebuild.objects.all().delete()
69 overlays = os.listdir(OVERLAYS_BASE)
71 whoosh = WhooshEbuildManager(True)
73 for overlay in overlays:
74 path_overlay = os.path.join(OVERLAYS_BASE, overlay)
76 if exclude_directory(path_overlay, overlay):
77 overlay_name = overlay
78 overlay = Overlay.objects.get(name=overlay)
80 categories = os.listdir(path_overlay)
82 for category in categories:
83 path_overlay_category = os.path.join(path_overlay, category)
85 if exclude_directory(path_overlay_category, category):
86 apps = os.listdir(path_overlay_category)
87 for app in apps:
88 path_overlay_category_app = os.path.join(path_overlay_category, app)
89 if exclude_directory(path_overlay_category_app, app):
90 ebuilds = os.listdir(path_overlay_category_app)
92 for ebuild in ebuilds:
93 if ebuild.endswith(".ebuild"):
94 if exclude_directory(path_overlay_category_app, ebuild):
96 path_ebuild = os.path.join(path_overlay_category_app, ebuild)
98 e = EbuildParser(overlay_name, category, app, path_ebuild)
101 ebuild = Ebuild()
102 ebuild.name = unicode(e.name)
103 ebuild.category = unicode(e.category)
104 ebuild.version = unicode(e.version)
105 ebuild.description = unicode(e.description, errors="replace")
106 ebuild.keywords = unicode(e.keywords)
107 ebuild.license = unicode(e.license)
108 ebuild.iuse = unicode(e.iuse)
109 ebuild.homepage = unicode(e.homepage)
110 ebuild.overlay = overlay
111 ebuild.save()
113 whoosh.Update(ebuild)
115 def ParseOverlays():
116 h = LaymanManager()
117 overlays = h.List()
119 for name, overlay in overlays.items() :
120 """ check if new overlay is ready """
121 o = Overlay.objects.all().filter(name=name)
123 if not o:
124 o = Overlay()
125 o.name = name
126 o.description = overlay["description"]
127 o.link = overlay["link"]
128 o.save()
130 def main():
131 ParseOverlays()
132 ParseEbuilds()
134 if __name__ == "__main__":
135 main()