1 | //----------------------------------------------------------- |
---|
2 | // Copyright Christian Arnault LAL-Orsay CNRS |
---|
3 | // arnault@lal.in2p3.fr |
---|
4 | // See the complete license in cmt_license.txt "http://www.cecill.info". |
---|
5 | //----------------------------------------------------------- |
---|
6 | |
---|
7 | #include "cmt_system.h" |
---|
8 | #include "cmt.h" |
---|
9 | #include "cmt_install_area.h" |
---|
10 | #include "cmt_syntax.h" |
---|
11 | #include "cmt_use.h" |
---|
12 | #include "cmt_symbol.h" |
---|
13 | #include "cmt_cmtpath_pattern.h" |
---|
14 | #include "cmt_project.h" |
---|
15 | #include "cmt_log.h" |
---|
16 | |
---|
17 | CmtInstallAreaMgr& CmtInstallAreaMgr::instance () |
---|
18 | { |
---|
19 | static CmtInstallAreaMgr me; |
---|
20 | |
---|
21 | return (me); |
---|
22 | } |
---|
23 | |
---|
24 | void CmtInstallAreaMgr::setup_current_installarea () |
---|
25 | { |
---|
26 | m_installarea = build_current_installarea (); |
---|
27 | |
---|
28 | if (m_installarea == "") return; |
---|
29 | |
---|
30 | cmt_string macro_name; |
---|
31 | |
---|
32 | Project* project = Project::find_by_cmtpath (m_installarea); |
---|
33 | |
---|
34 | if (project != 0) |
---|
35 | { |
---|
36 | macro_name = project->get_name (); |
---|
37 | } |
---|
38 | else |
---|
39 | { |
---|
40 | macro_name = "cmt"; |
---|
41 | } |
---|
42 | |
---|
43 | macro_name += "_installarea_prefix"; |
---|
44 | |
---|
45 | Symbol* macro = Symbol::find (macro_name); |
---|
46 | |
---|
47 | cmt_string installarea_prefix; |
---|
48 | |
---|
49 | if (macro != 0) |
---|
50 | { |
---|
51 | installarea_prefix = macro->build_macro_value (); |
---|
52 | } |
---|
53 | |
---|
54 | if (installarea_prefix != "") |
---|
55 | { |
---|
56 | m_installarea += CmtSystem::file_separator (); |
---|
57 | m_installarea += installarea_prefix; |
---|
58 | } |
---|
59 | |
---|
60 | Use& current_use = Use::current (); |
---|
61 | cmt_string buffer; |
---|
62 | |
---|
63 | if (!Symbol::is_selected ("CMTINSTALLAREA")) |
---|
64 | { |
---|
65 | buffer = "macro CMTINSTALLAREA \""; |
---|
66 | buffer += m_installarea; |
---|
67 | buffer += "\""; |
---|
68 | |
---|
69 | SyntaxParser::parse_requirements_line (buffer, ¤t_use); |
---|
70 | buffer = ""; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | void CmtInstallAreaMgr::setup () |
---|
75 | { |
---|
76 | m_installarea = build_current_installarea (); |
---|
77 | |
---|
78 | setup_current_installarea (); |
---|
79 | } |
---|
80 | |
---|
81 | void CmtInstallAreaMgr::config () const |
---|
82 | { |
---|
83 | cmt_string installarea = build_current_installarea (); |
---|
84 | |
---|
85 | CmtPathPattern::apply_all (); |
---|
86 | |
---|
87 | if (installarea == "") return; |
---|
88 | |
---|
89 | CmtMessage::warning ("Doing cleanup in the installation area " + installarea); |
---|
90 | /* |
---|
91 | if (!Cmt::get_quiet ()) |
---|
92 | { |
---|
93 | cerr << "#CMT> Warning: Doing cleanup in the installation area " << installarea << endl; |
---|
94 | } |
---|
95 | */ |
---|
96 | |
---|
97 | Symbol* macro = Symbol::find ("cmt_installarea_paths"); |
---|
98 | if (macro == 0) return; |
---|
99 | cmt_string installarea_paths = macro->build_macro_value (); |
---|
100 | |
---|
101 | CmtSystem::cmt_string_vector areapaths; |
---|
102 | CmtSystem::split (installarea_paths, " \t", areapaths); |
---|
103 | |
---|
104 | // Try a cleanup only in this selected install area |
---|
105 | |
---|
106 | for (int i = 0; i < areapaths.size (); i++) |
---|
107 | { |
---|
108 | const cmt_string& p = areapaths[i]; |
---|
109 | |
---|
110 | cmt_string path = installarea; |
---|
111 | path += CmtSystem::file_separator (); |
---|
112 | path += p; |
---|
113 | |
---|
114 | Symbol::expand (path); |
---|
115 | |
---|
116 | path.replace_all ("/", CmtSystem::file_separator ()); |
---|
117 | path.replace_all ("\\", CmtSystem::file_separator ()); |
---|
118 | |
---|
119 | CmtSystem::cmt_string_vector refs; |
---|
120 | cmt_regexp expression (".*[.]cmtref$"); |
---|
121 | |
---|
122 | // Look for all cmtref files in this PATH pattern |
---|
123 | |
---|
124 | CmtSystem::scan_dir (path, expression, refs); |
---|
125 | |
---|
126 | for (int j = 0; j < refs.size (); j++) |
---|
127 | { |
---|
128 | const cmt_string& ref_file = refs[j]; |
---|
129 | |
---|
130 | // We get the absolute location of the installed file |
---|
131 | |
---|
132 | cmt_string ref; |
---|
133 | ref.read (ref_file); |
---|
134 | int pos; |
---|
135 | |
---|
136 | ref.replace_all ("\"", ""); |
---|
137 | |
---|
138 | if (Cmt::get_debug ()) |
---|
139 | { |
---|
140 | cout << "CmtInstallAreaMgr::config> " << ref_file << " " << ref << endl; |
---|
141 | } |
---|
142 | |
---|
143 | pos = ref.find ("\r\n"); |
---|
144 | if (pos != cmt_string::npos) ref.erase (pos); |
---|
145 | pos = ref.find ('\n'); |
---|
146 | if (pos != cmt_string::npos) ref.erase (pos); |
---|
147 | pos = ref.find ('\r'); |
---|
148 | if (pos != cmt_string::npos) ref.erase (pos); |
---|
149 | pos = ref.find (' '); |
---|
150 | if (pos != cmt_string::npos) ref.erase (pos); |
---|
151 | |
---|
152 | // If the referenced file cannot be reached we remove the |
---|
153 | // corresponding installation |
---|
154 | // (this happens if the referenced file has be removed, or |
---|
155 | // moved away) |
---|
156 | |
---|
157 | if (!CmtSystem::test_file (ref)) |
---|
158 | { |
---|
159 | cmt_string ref_name; |
---|
160 | |
---|
161 | // Get the name of the referenced file |
---|
162 | CmtSystem::basename (ref, ref_name); |
---|
163 | |
---|
164 | // Get the installation directory |
---|
165 | CmtSystem::dirname (ref_file, ref); |
---|
166 | |
---|
167 | ref += CmtSystem::file_separator (); |
---|
168 | ref += ref_name; |
---|
169 | |
---|
170 | // Remove both the installed file |
---|
171 | // and the reference file |
---|
172 | |
---|
173 | CmtMessage::info ("Removing obsolete installed file [" + ref + "]" |
---|
174 | " (and " + ref_file + ")"); |
---|
175 | /* |
---|
176 | if (!Cmt::get_quiet ()) |
---|
177 | { |
---|
178 | cerr << "# Removing obsolete installed file [" << ref << "]"; |
---|
179 | cerr << " (and " << ref_file << ")" << endl; |
---|
180 | } |
---|
181 | */ |
---|
182 | |
---|
183 | CmtSystem::remove_file (ref); |
---|
184 | CmtSystem::remove_file (ref_file); |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | const cmt_string& CmtInstallAreaMgr::get_installarea () const |
---|
191 | { |
---|
192 | return (m_installarea); |
---|
193 | } |
---|
194 | |
---|
195 | cmt_string CmtInstallAreaMgr::build_current_installarea () const |
---|
196 | { |
---|
197 | cmt_string installarea; |
---|
198 | |
---|
199 | const Use& use = Use::current (); |
---|
200 | cmt_string current_path (use.real_path); |
---|
201 | if (!use.located ()) |
---|
202 | { |
---|
203 | CmtMessage::verbose ("build_current_installarea: Current use not located"); |
---|
204 | current_path = CmtSystem::pwd (); |
---|
205 | } |
---|
206 | //const cmt_string pwd = CmtSystem::pwd (); |
---|
207 | |
---|
208 | installarea = Project::find_in_cmt_paths (current_path); |
---|
209 | //installarea = Project::find_in_cmt_paths (pwd); |
---|
210 | |
---|
211 | return (installarea); |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | |
---|