Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ed60ffa918fbed9a9aa6296c19ce998fe7244a4e (plain) (blame)
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
# ###############################################################################
# Copyright (c) 2013 CEA LIST.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
# Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
#
# ###############################################################################

"""
This script cleans up a repository of Eclipse features and plugins
in order to improve their Tycho compatibility
"""

import os  # File handling
import os.path  # File path handling
import shutil  # File handling
import sys  # System
import subprocess  # Process handling
import xml.dom.minidom  # Minimal XML

import console  # Console pretty printing
import eclipse  # Eclipse API
import xmlutils  # XML utilities


def print_usage():
    """
    Print how to use this script
    :return: None
    """
    print("Usage:")
    print("  python cleaner.py [-h | --help] [--color] <targets>")
    print("  <targets> is the list of files and directories to clean")
    print("Options:")
    print("    -h, --help: print this screen")
    print("    --color: activate console color")


def cleanup_bundle(bundle):
    """
    Cleanup the specified Eclipse bundle
    :param bundle: An Eclipse bundle
    :return: None
    """
    __cleanup_classpath(bundle)
    __cleanup_build_properties(bundle)


def __cleanup_classpath(bundle):
    """
    Cleanup the classpath of the specified Eclipse bundle
    :param bundle: An Eclipse bundle
    :return: None
    """
    subs = os.listdir(bundle.location)
    if not ".classpath" in subs:
        return
    doc = xml.dom.minidom.parse(os.path.join(bundle.location, ".classpath"))
    dirty = False
    for entry in doc.getElementsByTagName("classpathentry"):
        data = entry.getAttribute("excluding")
        folder = entry.getAttribute("path")
        if data is not None and len(data) > 0:
            files = data.split("|")
            for file in files:
                if file is not None and len(file) > 0:
                    dirty = True
                    full = os.path.join(bundle.location, os.path.join(folder, os.path.join(file)))
                    print("Found " + full)
                    if full.endswith("/"):
                        subprocess.call(["git", "rm", "-r", full])
                    else:
                        subprocess.call(["git", "rm", full])
            entry.parentNode.removeChild(entry)
    if dirty:
        xmlutils.output(doc, os.path.join(bundle.location, ".classpath"))
        console.log("Bundle " + bundle.name + " => Fixed .classpath to remove excluded sources")


def __cleanup_build_properties(bundle):
    """
    Cleanup the build properties of the specified Eclipse bunle
    :param bundle: An Eclipse bundle
    :return: None
    """
    subs = os.listdir(bundle.location)
    if not "build.properties" in subs:
        return
    properties = open(os.path.join(bundle.location, "build.properties"), "r")
    found = False
    for line in properties:
        if line.find("src.includes") != -1:
            found = True
            break
    properties.close()
    if not found:
        if not "about.html" in subs:
            shutil.copy("about.html", os.path.join(bundle.location, "about.html"))
        properties = open(os.path.join(bundle.location, "build.properties"), "a")
        properties.write("src.includes = about.html\n")
        properties.close()
        console.log("Bundle " + bundle.name + " => Fixed build.properties to add src.includes")


def cleanup(directories):
    """
    Cleanup the Eclipse bundles in the specified directories
    :param directories: A collection of directories
    :return: None
    """
    # Build the repo
    repo = eclipse.Repository()
    for directory in directories:
        repo.load(directory)
    # Do the cleanup
    for name in repo.plugins:
        cleanup_bundle(repo.plugins[name])
    for name in repo.features:
        cleanup_bundle(repo.features[name])


# Main script
if __name__ == "__main__":
    # Checks the arguments
    nb = len(sys.argv)
    if nb <= 1:
        print_usage()
        sys.exit(1)

    targets = []
    # Parse the arguments
    for arg in sys.argv[1:]:
        if arg == "-h" or arg == "--help":
            print_usage()
            sys.exit(0)
        elif arg == console.CLI_COLOR:
            console.USE_COLOR = True
        else:
            targets.append(arg)
    # Execute
    cleanup(targets)
    sys.exit(0)

Back to the top