Debian Codegraph @

artemis_parts/zip.pl

raw
1 # -*- cperl -*-
2 ## This file is part of Artemis, copyright (c) 2002-2008 Bruce Ravel
3 ##
4
5
6
7 ###===================================================================
8 ### project file (zip-based) subsystem
9 ###===================================================================
10
11
12 sub initialize_project {
13 my $do_search = $_[0];
14 ## need to make name unique in case of multiple instances of Artemis.
15 ## the project_folder global variable should suffice to disambiguate
16 ## the multiple instances.
17 my $instance = 0;
18 my $project_dir = File::Spec->catfile($stash_dir, "artemis.project.$instance", "");
19 unless (same_directory($project_folder, $project_dir)) {
20 while (-d $project_dir) {
21 ++$instance;
22 $project_dir = File::Spec->catfile($stash_dir, "artemis.project.$instance", "");
23 };
24 if (($instance > 3) and ($do_search)) {
25 my $dialog =
26 $top -> Dialog(-bitmap => 'questhead',
27 -text => "There seem to be a large number of abandoned project folders. Project folders can be abandoned when Artemis exits abnormally.\n\nArtemis can clean these up at this time.\n\nYou should NOT clean these up if you have other active instances of Artemis running.",
28 -title => 'Artemis: Question...',
29 -buttons => ['Clean up',
30 "Don't clean up"],
31 -default_button => 'Cancel',
32 -font => $config{fonts}{med},
33 -popover => 'cursor');
34 &posted_Dialog;
35 $top -> deiconify;
36 $top -> raise;
37 my $response = $dialog->Show;
38 if ($response eq 'Clean up') {
39 Echo("Cleaning up adndoned project folders");
40 opendir S, $stash_dir;
41 map { rmtree(File::Spec->catfile($stash_dir, $_)) } (grep(/artemis\.project/, readdir S));
42 closedir S;
43 $project_dir = File::Spec->catfile($stash_dir, "artemis.project.0", "");
44 };
45 Echo("");
46 };
47 mkpath $project_dir unless (-d $project_dir);
48 $project_folder = $project_dir;
49 };
50 ## one trailing slash of the correct variety...
51 if ($is_windows) {
52 ($project_folder .= "\\") unless ($project_folder =~ /\\$/);
53 } else {
54 ($project_folder .= "/") unless ($project_folder =~ /\/$/);
55 };
56 $props{'Project location'} = $project_dir;
57 ## touch the marker file
58 unless (-f File::Spec->catfile($project_dir, "HORAE")) {
59 open A, ">".File::Spec->catfile($project_dir, "HORAE");
60 #print A " \n";
61 close A;
62 };
63 ## copy the readme file
64 my $readme_file = $paths{data0} -> find('artemis', 'readme');
65 copy($readme_file, File::Spec->catfile($project_dir, "README")) if (-e $readme_file);
66 if ($is_windows) {
67 require Win32::File;
68 Win32::File::SetAttributes(File::Spec->catfile($project_dir, "README"), 0);
69 };
70 ## make the descriptions directory
71 my $project_descr_dir = File::Spec->catfile($project_folder, "descriptions");
72 mkpath $project_descr_dir unless (-d $project_descr_dir);
73 ## make the data directory
74 my $project_data_dir = File::Spec->catfile($project_folder, "chi_data");
75 mkpath $project_data_dir unless (-d $project_data_dir);
76 ## make the log files directory
77 my $project_log_dir = File::Spec->catfile($project_folder, "log_files");
78 mkpath $project_log_dir unless (-d $project_log_dir);
79 ## make the tmp directory
80 my $project_tmp_dir = File::Spec->catfile($project_folder, "tmp");
81 mkpath $project_tmp_dir unless (-d $project_tmp_dir);
82 ## make the fits directory
83 my $project_fit_dir = File::Spec->catfile($project_folder, "fits");
84 mkpath $project_fit_dir unless (-d $project_fit_dir);
85 ## determine the autosave filename
86 $autosave_filename = File::Spec->catfile($stash_dir, "artemis.autosave");
87 };
88
89 sub clean_old_trap_files {
90 opendir S, $stash_dir;
91 my @list = grep {/ARTEMIS/} readdir S;
92 closedir S;
93 map {unlink File::Spec->catfile($stash_dir, $_)} @list;
94 };
95
96
97 ## make a project feff folder
98 sub initialize_feff {
99 my $id = $_[0];
100 my $project_feff_dir = File::Spec->catfile($project_folder, $id);
101 mkpath $project_feff_dir unless (-d $project_feff_dir);
102 return $project_feff_dir;
103 };
104
105
106 sub unpack_zip {
107 Echo("Opening project zipfile $_[0] ...");
108 ## check to see if it seems as though a project has started
109 ## system "ls -R /home/bruce/.horae/stash/";
110 ## opendir P, $project_folder;
111 ## my @dirs = grep {$_ !~ /^\./ and -d $_} (readdir P);
112 ## closedir P;
113 ## my $i = 0;
114 ## find( sub{ ++$i if -f; print $_, $/ }, @dirs );
115 ## print $i, $/;
116 if (($paths{data0}->get('file') and (-e $paths{data0}->get('file'))) or
117 (&all_feff)) {
118 my $dialog =
119 $top -> Dialog(-bitmap => 'questhead',
120 -text => "You have already started a project. Do you want to discard the current project and replace it with this one? Or do you want to cancel the import of this project? (Merging projects is not currently supported.)",
121 -title => 'Artemis: Question...',
122 -buttons => ['Replace', 'Cancel'],
123 -default_button => 'Cancel',
124 -font => $config{fonts}{med},
125 -popover => 'cursor');
126 &posted_Dialog;
127 Echo("Aborting project file"), return 1 if ($dialog->Show() eq 'Cancel');
128 Echo("");
129 &delete_project;
130 };
131
132 my ($zip, $name);
133 #if (ref $_[0] =~ /Archive/) {
134 # $zip = $_[0];
135 #} else {
136 $zip = Archive::Zip->new();
137 Echo('Error reading project file $_[0]'), return 1 unless ($zip->read($_[0]) == AZ_OK);
138 $name = &push_mru($_[0], 1, "project") || $_[0];
139 #};
140 $zip->extractTree("", $project_folder);
141 undef $zip;
142 Echo("Opening project zipfile $name ... done!");
143 return 0;
144 };
145
146
147 sub zip_project {
148 my $proj = $_[0];
149 my $zip = Archive::Zip->new();
150 $zip->addTree( $project_folder, "" );
151 die 'error writing zip-style project' unless $zip->writeToFileNamed( $proj ) == AZ_OK;
152 undef $zip;
153 };
154
155 sub convert_project_to_zip {
156
157 my $stash = $_[0];
158 Echo("Converting old-style project file to the zip format ... ");
159
160 &initialize_project(0);
161 ## copy the old-style project file to the description file in the
162 ## stash directory
163 copy($stash, File::Spec->catfile($project_folder, "descriptions", 'artemis'));
164
165
166 open PROJ, $stash or die "could not open $stash as a project file\n";
167
168 $top -> Busy();
169 my $cpt = new Safe;
170 #use vars qw($old_path @args @strings @journal @plot_features);
171 my $from_version = 0;
172 while (<PROJ>) {
173 next unless (/^\@args/);
174 @ {$cpt->varglob('args')} = $cpt->reval($_);
175 ## this is a little fast 'n' loose, but the args array is stored in
176 ## a form that can be read directly into a hash, so ...
177 my %args = @ {$cpt->varglob('args')};
178 ## copy the data files into the data folder
179 if ($args{type} eq 'data') {
180 unless ($args{file} =~ /^\s*$/) {
181 unless (-f $args{file}) {
182 my $dialog =
183 $top -> Dialog(-bitmap => 'questhead',
184 -text => "The file $args{file} does not seem to exist. Artemis will now prompt you for the real location of this file.",
185 -title => 'Artemis: Problem finding a data file ...',
186 -buttons => ['OK', 'Cancel'],
187 -default_button => 'OK',
188 -font => $config{fonts}{med},
189 -popover => 'cursor');
190 &posted_Dialog;
191 my $response = $dialog->Show();
192 if ($response eq 'Cancel') {
193 rmtree($project_folder);
194 $project_folder = "";
195 $top -> Unbusy();
196 Echo("Project aborted.");
197 return 0;
198 };
199 Echo("");
200 my $path = $current_data_dir || cwd;
201 my $types = [['chi(k) data', '*.chi'],
202 ['All files', '*'],];
203 my $file ||= $top -> getOpenFile(-filetypes=>$types,
204 ##(not $is_windows) ?
205 ## (-sortcmd=>sub{$Tk::FBox::a cmp $Tk::FBox::b}) : () ,
206 -initialdir=>$path,
207 -title => "Artemis: Open a data file");
208 unless ($file) {
209 rmtree($project_folder);
210 $project_folder = "";
211 $top -> Unbusy();
212 Echo("Project aborted.");
213 return 0;
214 };
215 push_mru($file);
216 $args{file} = $file;
217 };
218 copy($args{file}, File::Spec->catfile($project_folder, "chi_data"));
219 };
220 ## copy the feff calculations into feff folders
221 } elsif ($args{type} eq 'feff') {
222 my $project_feff_dir = File::Spec->catfile($project_folder, $args{id});
223 mkpath $project_feff_dir unless (-d $project_feff_dir);
224 unless ((-d $args{path}) and (-f $args{'feff.inp'})) {
225 my $dialog =
226 $top -> Dialog(-bitmap => 'questhead',
227 -text => "The Feff calculation which used $args{'feff.inp'} does not seem to exist. Artemis will now prompt you for the correct directory.",
228 -title => 'Artemis: Problem finding a data file ...',
229 -buttons => ['OK', 'Cancel'],
230 -default_button => 'OK',
231 -font => $config{fonts}{med},
232 -popover => 'cursor');
233 &posted_Dialog;
234 my $response = $dialog->Show();
235 if ($response eq 'Cancel') {
236 rmtree($project_folder);
237 $project_folder = "";
238 $top -> Unbusy();
239 Echo("Project aborted.");
240 return 0;
241 };
242 Echo("");
243 my $dir = q{};
244 if ($Tk::VERSION < 804) {
245 $top -> Dialog(-bitmap => 'error',
246 -text => "Selecting folders requires perl/Tk 804. You are using perl/Tk $Tk::VERSION. Drat!",
247 -title => 'Artemis: Unable to change folders',
248 -buttons => ['OK'],
249 -font => $config{fonts}{med},
250 -default_button => "OK", )
251 -> Show();
252 #$dir = $top -> DirSelect(-width=>40, -dir=>$current_data_dir,
253 # -title=> "Artemis: Select a directory",
254 # -text => "Select the correct path to your FEFF calculation",
255 # ) -> Show;
256 } else {
257 $dir = $top -> chooseDirectory(-initialdir => $current_data_dir,
258 -title => "Artemis: Select a directory",
259 #-mustexist => 1,
260 );
261 };
262 unless ($dir) {
263 rmtree($project_folder);
264 $project_folder = "";
265 $top -> Unbusy();
266 Echo("Project aborted.");
267 return 0;
268 };
269 $current_data_dir = $dir;
270 $args{path} = $dir;
271 };
272 opendir FFF, $args{path};
273 my @list = grep { -f File::Spec->catfile($args{path},$_) } readdir FFF;
274 closedir FFF;
275 map { copy(File::Spec->catfile($args{path},$_), $project_feff_dir) } @list;
276 };
277 };
278 close PROJ;
279
280 ## backup the old-style project file for safe keeping
281 rename($stash, $stash.".oldstyle");
282 ## save the project as a zip file
283 my $zip = Archive::Zip->new();
284 $zip->addTree( $project_folder, "" );
285 die 'write error' unless $zip->writeToFileNamed( $stash ) == AZ_OK;
286 undef $zip;
287 ## clean up the stash folder. this is perhaps a bit silly since it
288 ## will soon be opened up again, but doing it this was will not
289 ## require special code.
290 rmtree($project_folder);
291 $project_folder = "";
292 &initialize_project(0);
293 $top -> Unbusy();
294 Echo("Converting old-style project file to the zip format ... done!");
295 return 1;
296
297 };
298
299
300
301
302
303 ## END OF THE PROJECT FILE (ZIP-BASED) SUBSYSTEM
304