~epidermis/epidermis/devel

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright © David D Lowe 2008-2011
# This program is free software under the terms of GPLv2. For more
# information, see the file named COPYING distributed with this project.

"""Runs validate(), update_downloads() and update_preview()
validate():Checks every pigment in repoDir to make sure it's state is not STATE_EMPTY or STATE_INCOMPLETE or STATE_INFO,
that all pigments have their data in a data/ folder,
and that all skins have their dependencies in the repo
Exitcode is the number of errors found
update_downloads():Writes downloads.xml
update_preview(): writes preview_summary.xml, preview_summary.tar.gz and fills previewDir
build_pigments(): makes .pigment files"""
# Does not check packs

from __future__ import absolute_import

repoDir = "/var/www/ghns/"
repoUrl = ""
previewDir = "preview"
ignoreSymlinkWarnings = False

# TODO: prepare repo for upload
# musn't link to localhost anymore, for example

import sys
import getopt
import os
from xml.dom import minidom
import xml
import subprocess
import pdb



def main():
    _prepare_ep_paths()
    
    stopat = 99
    if len(sys.argv) > 1:
        if sys.argv[1] == "--validate":
            stopat = 1
        elif sys.argv[1] == "--update-downloads":
            stopat = 2
        elif sys.argv[1] == "--update-preview":
            stopat = 3
        elif sys.argv[1] == "--build-pigments":
            stopat = 99
        else:
            print >> sys.stderr, "unrecognised argument: " + sys.argv[1]
            print >> sys.stderr, "reconginsed argumnts are: --validate , --update-downloads , --update-preview, --build-pigments"
    
    print "validating pigments..."
    errors, pigments = validate()
        
    if errors > 0:
        print >> sys.stderr, "found %s error(s)" % errors

        sys.exit(errors)
    else:
        print "no errors found, preliminary check passed"

    if stopat > 1:
        update_downloads(pigments)
        
    if stopat > 2:
        update_preview(pigments)
        
    if stopat > 3:
        build_pigments(pigments)

def validate():
    """returns (errors, pigments)"""
    from epidermis import const
    from epidermis import managepigments
    from epidermis.pigments import pigment
    
    errors = 0
    
    readPigments = []
    for pigmentType in const.PIGMENT_TYPES[1:] + ["skin"]:
        plural = managepigments.get_pigment_type(pigmentType).directoryName
        for dir in os.listdir(os.path.join(repoDir, plural)):
            if os.path.isdir(os.path.join(repoDir,plural,dir)):
                curr = os.path.join(repoDir, plural, dir)
                if os.path.exists(os.path.join(curr,"pigment.xml")):
                    pigm = managepigments.create_pigment(pigmentType)
                    try:
                        if pigmentType == "skin":
                            pigm.read_with_pigments(os.path.join(curr, "pigment.xml"), readPigments)
                        else:
                            pigm.read(os.path.join(curr, "pigment.xml"))
                    except Exception, ee:
                        print "error: " + os.path.join(curr, "pigment.xml") + " cannot be read, following exception:"
                        print "::", str(ee)
                        errors += 1
                    else:
                        newErrors = 0
                        if not os.path.abspath(os.path.join(repoDir, plural, pigm.codeName)) == curr:
                            print "error: " + os.path.join(curr, "pigment.xml") + "'s directory does not match its codeName: " + pigm.codeName
                            newErrors += 1
                        elif not os.path.isdir(os.path.join(curr, "data")):
                            print "error: Cannot find directory " + os.path.join(curr, "data")
                            newErrors += 1
                        else:
                            for attach in pigm.attachments:
                                justFilename = attach[(-1*len(attach.split("/")[-1])):]
                                if not os.path.exists(os.path.join(curr, "data", justFilename)):
                                    print "error: " + os.path.join(curr, "pigment.xml") + " cannot be read, cannot find the following attachment in data:"
                                    print "::", "\"" + justFilename + "\""
                                    newErrors += 1
                                elif not os.access(os.path.join(curr, "data", justFilename), os.R_OK):
                                    print "error: " + os.path.join(curr, "pigment.xml") + " cannot be read, permission error for following attachment:"
                                    print "::", "\"" + justFilename + "\""
                                    newErrors += 1
                            for parent, dirs, files in os.walk(curr):
                                if not os.access(os.path.realpath(parent), os.R_OK + os.X_OK):
                                    if not os.path.isdir(os.path.realpath(parent)):
                                        if not ignoreSymlinkWarnings:
                                            print "warning: " + parent + " is a symlink that points to a directory that does not exist"
                                    elif os.stat(parent).st_uid != 0:
                                        print "error: " + parent + " is not owned by root"
                                        newErrors +=1
                                        continue
                                    else:
                                        print "error: " + parent + " cannot be read, permissions problem"
                                        newErrors += 1
                                        continue
                                for file in files:
                                    if not os.access(os.path.realpath(os.path.join(parent, file)), os.R_OK):
                                        if not os.path.exists(os.path.realpath(os.path.join(parent, file))):
                                            if not ignoreSymlinkWarnings:
                                                print "warning: " + os.path.join(parent, file) + " is a symlink that points to a file that does not exist"
                                        elif os.stat(os.path.join(parent, file)).st_uid != 0:
                                            print "error: " + os.path.join(parent, file) + " is not owned by root"
                                            newErrors += 1
                                        else:
                                            print "error: " + os.path.join(parent, file) + " cannot be read, permissions problem"
                                            newErrors += 1

                            
                        if newErrors == 0:
                            if pigm.get_state() in (pigment.STATE_EMPTY, pigment.STATE_INCOMPLETE):
                                print "error: " + os.path.join(curr, "pigment.xml") + " is " + pigm.debug_get_state_str()
                                errors += 1
                            elif not pigm.is_attachment_correct():
                                print "error: " + os.path.join(curr, "pigment.xml") + " has an invalid attachment"
                                errors += 1
                        else:
                            errors += newErrors
                        if pigm.copyright.lower() == "unknown":
                            print "warning: " + os.path.join(curr, "pigment.xml") + " has unknown copyright"
                        readPigments.append(pigm)
                else:
                    print "error: missing " + os.path.join(curr,"pigment.xml")
                    errors += 1
                    
    

    return (errors, readPigments)


def update_downloads(pigments):
    """pigments: list of Pigment objects"""
    from epidermis import const
    from epidermis import managepigments
    from epidermis.pigments import pigment
    from xml.dom import minidom
    
    docDownloads = minidom.Document()
    rootDownloads = docDownloads.createElement("ghnsdownload")
    rootDownloads.setAttribute("epidermisVersion","0.4")
    docDownloads.appendChild(rootDownloads)
    
    stuffPreview = docDownloads.createElement("stuff")
    stuffPreview.setAttribute("category", "epidermis/repo_preview_summary")
    nn = docDownloads.createElement("name")
    nn.appendChild(docDownloads.createTextNode("Summary"))
    pp = docDownloads.createElement("payload")
    pp.appendChild(docDownloads.createTextNode(repoUrl + "preview_summary.tar.gz"))
    for ii in (nn, pp):
        stuffPreview.appendChild(ii)
    rootDownloads.appendChild(stuffPreview) 
    
    for pigm in pigments:
        stuff = docDownloads.createElement("stuff")
        stuff.setAttribute("category", "epidermis/pigment/" + pigm.type.codeName)
        for lang in pigm.humanName.keys():
            name = docDownloads.createElement("name")
            name.setAttribute("lang", lang)
            name.appendChild(docDownloads.createTextNode(pigm.get_human_name(lang)))
            stuff.appendChild(name)
        author = docDownloads.createElement("author")
        author.appendChild(docDownloads.createTextNode(pigm.author))
        licence = docDownloads.createElement("licence")
        licence.appendChild(docDownloads.createTextNode(pigm.copyright))
        version = docDownloads.createElement("version")
        version.appendChild(docDownloads.createTextNode("1.0"))
        release = docDownloads.createElement("release")
        release.appendChild(docDownloads.createTextNode("1"))
        releasedate = docDownloads.createElement("releasedate")
        payload = docDownloads.createElement("payload")
        payload.appendChild(docDownloads.createTextNode(\
            os.path.join(repoDir, pigm.type.directoryName, pigm.codeName, pigm.codeName + "_" + pigm.type.codeName + ".pigment")))
        for ii in (author, licence, version, release, releasedate, payload):
            stuff.appendChild(ii)
        rootDownloads.appendChild(stuff)
    
    fileDownloads = open(os.path.join(repoDir, "downloads.xml"), "w")
    docDownloads.writexml(fileDownloads)
    fileDownloads.close()
    
    subprocess.call(["xml_pp","-i",os.path.join(repoDir, "downloads.xml")])
    subprocess.call(["chmod", "644", os.path.join(repoDir, "downloads.xml")])

    print "Wrote %s" % os.path.join(repoDir, "downloads.xml")
    
    
    
def update_preview(pigments):
    """pigments: a list of Pigment objects"""
    from epidermis import const
    from epidermis import managepigments
    from epidermis.pigments import pigment
    from xml.dom import minidom
    
    docSummary = minidom.Document()
    rootSummary = docSummary.createElement("previewSummary")
    docSummary.appendChild(rootSummary)
    
    ptElement = {}
    for pt in const.PIGMENT_TYPES:
        ptElement[pt] = docSummary.createElement(managepigments.get_pigment_type(pt).directoryName)
        rootSummary.appendChild(ptElement[pt])
    
    for pigm in pigments:
        item = docSummary.createElement("item")
        item.setAttribute("id", pigm.type.abbreviation + "-" + pigm.codeName)
        for lang in pigm.humanName.keys():
            name = docSummary.createElement("name")
            name.setAttribute("lang", lang)
            name.appendChild(docSummary.createTextNode(pigm.get_human_name(lang)))
            item.appendChild(name)
        pigmentEl = docSummary.createElement("codename")
        pigmentEl.appendChild(docSummary.createTextNode(pigm.codeName))
        copyright = docSummary.createElement("copyright")
        copyright.appendChild(docSummary.createTextNode(pigm.copyright))
        for it in (pigmentEl, copyright):
            item.appendChild(it)
        for lang in pigm.description.keys():
            description = docSummary.createElement("description")
            description.setAttribute("lang", lang)
            description.appendChild(docSummary.createTextNode(pigm.get_description(lang)))
            item.appendChild(description)
        author = docSummary.createElement("author")
        author.appendChild(docSummary.createTextNode(pigm.author))
        item.appendChild(author)
        if pigm.type.codeName == "skin":
            for pt2 in pigm.linkedPigments.keys():
                if pt2 in const.PIGMENT_TYPES[1:]:
                    link = docSummary.createElement(pt2 + "Dependancy")
                    link.appendChild(docSummary.createTextNode(pigm.linkedPigments[pt2].codeName))
                    item.appendChild(link)
        preview = docSummary.createElement("preview")
        preview.appendChild(docSummary.createTextNode(previewDir + "/" + pigm.type.directoryName + \
            "/" + pigm.codeName + "/" + pigm.previewFilename))
        item.appendChild(preview)
        ptElement[pigm.type.codeName].appendChild(item)
    
    fileSummary = open(os.path.join(repoDir, "preview_summary.xml"), "w")
    docSummary.writexml(fileSummary)
    fileSummary.close()
    
    import subprocess
    subprocess.call(["xml_pp", "-i", os.path.join(repoDir, "preview_summary.xml")])
    subprocess.call(["chmod", "644", os.path.join(repoDir, "preview_summary.xml")])

    print "Wrote %s" % os.path.join(repoDir, "preview_summary.xml")    
    
    subprocess.call(["rm", "-f", "-r", os.path.join(repoDir, previewDir)])
    subprocess.call(["mkdir", "-p", os.path.join(repoDir, previewDir)])
    for pt in const.PIGMENT_TYPES:
        subprocess.call(["mkdir", "-p", os.path.join(repoDir, previewDir, managepigments.get_pigment_type(pt).directoryName)])
    for pigm in pigments:
        if len(pigm.previewFilename) > 0:
            subprocess.call(["mkdir", "-p", os.path.join(repoDir, previewDir, pigm.type.directoryName, pigm.codeName)])
            subprocess.call(["cp", os.path.join(repoDir, pigm.type.directoryName, pigm.codeName, pigm.previewFilename), \
                os.path.join(repoDir, previewDir, pigm.type.directoryName, pigm.codeName)])
    
    subprocess.call(["tar", "-cf", "preview_summary.tar", "preview_summary.xml", previewDir], cwd=repoDir)
    subprocess.call(["rm", "-f", "preview_summary.tar.gz"], cwd=repoDir)
    subprocess.call(["gzip", "preview_summary.tar"], cwd=repoDir)
    
    print "Finished %s/ and preview_summary.tar.gz" % previewDir

def build_pigments(pigments):
    """Builds .pigment files from the pigment.xml, preview and data in the repo
    pigments: list of Pigment objects in the repo
    Updates pigment.xml to be pretty printed as well"""
    
    from epidermis import const
    from epidermis import managepigments
    
    
    for pigm in pigments:
        curr = os.path.join(repoDir, pigm.type.directoryName, pigm.codeName)
        sys.stdout.write(os.path.join(curr, pigm.codeName + "_" + pigm.type.codeName + ".pigment") + " ... ")
        sys.stdout.flush()
        
        try:
            subprocess.call(["mkdir", "-p", "/tmp/epidermis/build_pigment/"])
            subprocess.call("rm -r -f /tmp/epidermis/build_pigment/*", shell=True)
            subprocess.call(["xml_pp", "-i", os.path.join(curr, "pigment.xml")])
            subprocess.call(["chmod", "644", os.path.join(curr, "pigment.xml")])
            if len(pigm.previewFilename) > 0:
                subprocess.call(["cp", "-r", "data/", "pigment.xml", pigm.previewFilename, "/tmp/epidermis/build_pigment"], cwd=curr)
            else:
                subprocess.call(["cp", "-r", "data/", "pigment.xml", "/tmp/epidermis/build_pigment"], cwd=curr)
            subprocess.call(["mkdir", "/tmp/epidermis/build_pigment/information"])
            subprocess.call(["mv", "pigment.xml", "information/"], cwd="/tmp/epidermis/build_pigment")
            if len(pigm.previewFilename) > 0:
                subprocess.call(["mv", pigm.previewFilename, "information/"], cwd="/tmp/epidermis/build_pigment")
            subprocess.call(["tar", "-cf", "build.tar", "data/", "information/"], cwd="/tmp/epidermis/build_pigment")
            subprocess.call(["gzip", "build.tar"], cwd="/tmp/epidermis/build_pigment")
            subprocess.call(["mv", "-f", os.path.join("/tmp/epidermis/build_pigment", "build.tar.gz"), \
                os.path.join(curr, pigm.codeName + "_" + pigm.type.codeName + ".pigment")])
        except Exception, ee:
            print ""
            raise(ee)
        else:
            print "built"
    

def _prepare_ep_paths():
    ep_path = determine_path()[:(-1*len(determine_path().split("/")[-1]))]
    sys.path.insert(1, ep_path)


def determine_path ():
    """Borrowed from wxglade.py"""
    try:
        root = __file__
        if os.path.islink (root):
            root = os.path.realpath (root)
        return os.path.dirname (os.path.abspath (root))
    except Exception, ee:
        print >> sys.stderr, "I'm sorry, but something is wrong."
        print >> sys.stderr, "There is no __file__ variable. Please contact the author."
        sys.exit (1)
        raise(ee)

if __name__ == "__main__":
    main()