File: create_missing_dirs.py

package info (click to toggle)
gnat-gps 18-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,716 kB
  • sloc: ada: 362,679; python: 31,031; xml: 9,597; makefile: 1,030; ansic: 917; sh: 264; java: 17
file content (70 lines) | stat: -rw-r--r-- 2,736 bytes parent folder | download
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
"""
This script will create missing directories for a project tree when loading
it into GPS. It is different from the gnatmake switch -p, which will also
create the directories but only when the project is compiled. Since the project
is first loaded in GPS, the latter would return errors when creating the
project if some directories are missing.

A new preference is created that allows you to disable the creation
of directories, and the default is to create missing directories.
"""

#############################################################################
# No user customization below this line
#############################################################################

import GPS
import os

attempted = []  # List of directories that this plugin has attempted to create


def on_project_changed(self):
    global attempted
    CreatedDirs = False
    created = []
    try:
        must_create = GPS.Preference("Auto-Create-Dirs").get()
    except Exception:
        must_create = True

    if must_create:
        prjs = GPS.Project.root().dependencies(True)
        prjs.append(GPS.Project.root())
        for i in prjs:
            dirs = [i.get_attribute_as_string("Exec_Dir"),
                    i.get_attribute_as_string("Library_Dir"),
                    i.get_attribute_as_string("Object_Dir"),
                    i.get_attribute_as_string("Library_Src_Dir"),
                    i.get_attribute_as_string("Library_ALI_Dir"),
                    i.get_attribute_as_string("Artifacts_Dir", "IDE"),
                    i.get_attribute_as_string("Documentation_Dir",
                                              "Documentation")]
            for j in dirs:
                if i and i not in [".", "", " "]:
                    dir = os.path.join(
                        os.path.dirname(i.file().path), j).strip()
                    # Only attempt to create each directory once
                    if dir not in attempted:
                        if not os.path.exists(dir):
                            os.makedirs(dir)
                            attempted.append(dir)
                            created.append(dir)
                            CreatedDirs = True
        if CreatedDirs:
            GPS.Console("Messages").write("Created missing dirs\n")
            GPS.Console("Messages").write(created.join("\n"))
            GPS.Console("Messages").write("\n")
            GPS.Project.recompute()


GPS.parse_xml("""
<preference name="Auto-Create-Dirs"
   page="Plugins/create_missing_dirs"
   label="Auto Create Missing Dirs"
   tip="Automatically creates missing directories when a project is loaded."
   default="True"
   type="boolean"/>
""")

GPS.Hook("project_view_changed").add(on_project_changed)