-
Notifications
You must be signed in to change notification settings - Fork 2
/
ecosadmin.tcl
1649 lines (1397 loc) · 51.4 KB
/
ecosadmin.tcl
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env tclsh
# {{{ Banner
#===============================================================================
#
# ecosadmin.tcl
#
# A package install/uninstall tool.
#
#===============================================================================
## ####ECOSGPLCOPYRIGHTBEGIN####
## -------------------------------------------
## This file is part of eCos, the Embedded Configurable Operating System.
## Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2009 Free Software Foundation, Inc.
##
## eCos is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free
## Software Foundation; either version 2 or (at your option) any later
## version.
##
## eCos is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
## for more details.
##
## You should have received a copy of the GNU General Public License
## along with eCos; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
##
## As a special exception, if other files instantiate templates or use
## macros or inline functions from this file, or you compile this file
## and link it with other works to produce a work based on this file,
## this file does not by itself cause the resulting work to be covered by
## the GNU General Public License. However the source code for this file
## must still be made available in accordance with section (3) of the GNU
## General Public License v2.
##
## This exception does not invalidate any other reasons why a work based
## on this file might be covered by the GNU General Public License.
## -------------------------------------------
## ####ECOSGPLCOPYRIGHTEND####
#===============================================================================
######DESCRIPTIONBEGIN####
#
# Author(s): jld
# Contributors: bartv
# Date: 1999-06-18
# Purpose: To install and uninstall packages from an eCos component
# repository
# Description:
# Usage:
#
#####DESCRIPTIONEND####
#===============================================================================
#
# }}}
# {{{ Version check
# ----------------------------------------------------------------------------
# ecosadmin.tcl requires at least version 8.0 of Tcl, since it makes use of
# namespaces. It is possible that some users still have older versions.
if { [info tclversion] < 8.0 } {
puts "This script requires Tcl 8.0 or later. You are running Tcl [info patchlevel]."
return
}
# }}}
# {{{ Namespace definition
# ----------------------------------------------------------------------------
# Namespaces. All code and variables in this script are kept in the namespace
# "ecosadmin". This is not really necessary for stand-alone operation, but if it
# ever becomes desirable to embed this script in a larger application then
# using a namespace is a lot easier.
#
# As a fringe benefit, all global variables can be declared inside this
# namespace and initialised.
#
namespace eval ecosadmin {
# Is this program running under Windows ?
variable windows_host [expr {$tcl_platform(platform) == "windows"}]
variable null_device ""
if { $windows_host != 0 } {
set ecosadmin::null_device "nul"
} else {
set ecosadmin::null_device "/dev/null"
}
# Where is the component repository ? The following input sources
# are available:
# 1) the environment variable ECOS_REPOSITORY.
# 2) $argv0 should correspond to the location of the ecosadmin.tcl
# script.
#
variable component_repository ""
if { [info exists ::env(ECOS_REPOSITORY)] } {
# override the calculation of the repository location using the
# (undocumented) ECOS_REPOSITORY environment variable
set component_repository $::env(ECOS_REPOSITORY)
} else {
set component_repository [pwd]
if { [file dirname $argv0] != "." } {
set component_repository [file join $component_repository [file dirname $argv0]]
}
}
# Details of the command line arguments, if any.
variable list_packages_arg 0; # list
variable clean_database_arg 0; # clean
variable check_database_arg 0; # check
variable accept_license_arg 0; # --accept_license
variable extract_license_arg 0; # --extract_license
variable add_package ""; # add FILE
variable remove_package ""; # remove PACKAGE
variable keep_targets_arg 1; # --keep_targets, --remove_targets
variable merge_repository ""; # merge REPOSITORY
variable version_arg ""; # --version VER
# Details of all known packages, targets and templates
# read from the ecos.db file
variable known_packages ""
variable known_targets ""
variable known_templates ""
array set package_data {};
array set target_data {};
array set template_data {};
# List of packages merged from another repository
variable merge_packages ""
# What routines should be invoked for outputting fatal errors and
# for warning messages ?
variable fatal_error_handler ecosadmin::cli_fatal_error
variable warning_handler ecosadmin::cli_warning
variable report_handler ecosadmin::cli_report
# Keep or remove the CVS directories?
variable keep_cvs 0
}
# }}}
# {{{ Infrastructure
# ----------------------------------------------------------------------------
# Minimal infrastructure support.
#
# There must be some way of reporting fatal errors, of outputting warnings,
# and of generating report messages. The implementation of these things
# obviously depends on whether or not TK is present. In addition, if this
# script is being run inside a larger application then that larger
# application must be able to install its own versions of the routines.
#
# Once it is possible to report fatal errors, an assertion facility becomes
# feasible.
#
# These routines output fatal errors, warnings or miscellaneous messages.
# Their implementations depend on the mode in which this script is operating.
#
proc ecosadmin::fatal_error { msg } {
$ecosadmin::fatal_error_handler "$msg"
}
proc ecosadmin::warning { msg } {
$ecosadmin::warning_handler "$msg"
}
proc ecosadmin::report { msg } {
$ecosadmin::report_handler "$msg"
}
#
# Command line versions.
# NOTE: some formatting so that there are linebreaks at ~72 columns would be
# a good idea.
#
proc ecosadmin::cli_fatal_error_handler { msg } {
error "$msg"
}
proc ecosadmin::cli_warning_handler { msg } {
puts "ecosadmin warning: $msg"
}
proc ecosadmin::cli_report_handler { msg } {
puts "$msg"
}
#
# Determine the default destination for warnings and for fatal errors.
# After the first call to this function it is possible to use assertions.
#
proc ecosadmin::initialise_error_handling { } {
set ecosadmin::fatal_error_handler ecosadmin::cli_fatal_error_handler
set ecosadmin::warning_handler ecosadmin::cli_warning_handler
set ecosadmin::report_handler ecosadmin::cli_report_handler
}
#
# These routines can be used by containing programs to provide their
# own error handling.
#
proc ecosadmin::set_fatal_error_handler { fn } {
ASSERT { $fn != "" }
set ecosadmin::fatal_error_handler $fn
}
proc ecosadmin::set_warning_handler { fn } {
ASSERT { $fn != "" }
set ecosadmin::warning_handler $fn
}
proc ecosadmin::set_report_handler { fn } {
ASSERT { $fn != "" }
set ecosadmin::report_handler $fn
}
#
# A very simple assertion facility. It takes a single argument, an expression
# that should be evaluated in the calling function's scope, and on failure it
# should generate a fatal error.
#
proc ecosadmin::ASSERT { condition } {
set result [uplevel 1 [list expr $condition]]
if { $result == 0 } {
fatal_error "assertion predicate \"$condition\"\nin \"[info level -1]\""
}
}
# }}}
# {{{ Utilities
# ----------------------------------------------------------------------------
# cdl_compare_version. This is a partial implementation of the full
# cdl_compare_version facility defined in the product specification. Its
# purpose is to order the various versions of a given package with
# the most recent version first. As a special case, "current" is
# always considered the most recent.
#
# There are similarities between cdl_compare_version and with Tcl's
# package vcompare, but cdl_compare_version is more general.
#
proc ecosadmin::cdl_compare_version { arg1 arg2 } {
if { $arg1 == $arg2 } {
return 0
}
if { $arg1 == "current"} {
return -1
}
if { $arg2 == "current" } {
return 1
}
set index1 0
set index2 0
set ch1 ""
set ch2 ""
set num1 ""
set num2 ""
while { 1 } {
set ch1 [string index $arg1 $index1]
set ch2 [string index $arg2 $index2]
set num1 ""
set num2 ""
if { ($ch1 == "") && ($ch2 == "") } {
# Both strings have terminated at the same time. There may have
# been some spurious leading zeroes in numbers.
return 0
} elseif { $ch1 == "" } {
# The first string has ended first. If ch2 is a separator then
# arg2 is a derived version, e.g. v0.3.p1 and hence newer. Otherwise ch2
# is an experimental version v0.3beta and hence older.
if { [string match \[-._\] $ch2] } {
return 1
} else {
return -1
}
} elseif { $ch2 == "" } {
# Equivalent to the above.
if { [string match \[-._\] $ch1] } {
return -1
} else {
return 1
}
}
# There is still data to be processed.
# Check for both strings containing numbers at the current index.
if { ( [string match \[0-9\] $ch1] ) && ( [string match \[0-9\] $ch2] ) } {
# Extract the entire numbers from the version string.
while { [string match \[0-9\] $ch1] } {
set num1 "$num1$ch1"
incr index1
set ch1 [string index $arg1 $index1]
}
while { [string match \[0-9\] $ch2] } {
set num2 "$num2$ch2"
incr index2
set ch2 [string index $arg2 $index2]
}
if { $num1 < $num2 } {
return 1
} elseif { $num1 > $num2 } {
return -1
}
continue
}
# This is not numerical data. If the two characters are the same then
# move on.
if { $ch1 == $ch2 } {
incr index1
incr index2
continue
}
# Next check if both strings are at a separator. All separators can be
# used interchangeably.
if { ( [string match \[-._\] $ch1] ) && ( [string match \[-._\] $ch2] ) } {
incr index1
incr index2
continue
}
# There are differences in the characters and they are not interchangeable.
# Just return a standard string comparison.
return [string compare $ch1 $ch2]
}
}
# }}}
# {{{ Argument parsing
# ----------------------------------------------------------------------------
# The argv0 argument should be the name of this script. It can be used
# to get at the component repository location. If this script has been
# run incorrectly then currently it will fail: in future it may be
# desirable to check an environment variable instead.
#
# The argv argument is a string containing the rest of the arguments.
# If any of the arguments contain spaces then this argument will be
# surrounded by braces. If any of the arguments contain braces then
# things will break.
#
proc ecosadmin::parse_arguments { argv0 argv } {
if { $argv != "" } {
# There are arguments. If any of the arguments contained
# spaces then these arguments will have been surrounded
# by braces, which is a nuisance. So start by turning the
# arguments into a numerically indexed array.
set argc 0
array set args { }
foreach arg $argv {
set args([incr argc]) $arg
}
# Now examine each argument with regular expressions. It is
# useful to have some variables filled in by the regexp
# matching.
set dummy ""
set match1 ""
set match2 ""
for { set i 1 } { $i <= $argc } { incr i } {
# Check for --list and the other simple ones.
if { [regexp -- {^-?-?list$} $args($i)] == 1 } {
set ecosadmin::list_packages_arg 1
continue
}
# check for the clean command
if { [regexp -- {^-?-?clean$} $args($i)] == 1 } {
set ecosadmin::clean_database_arg 1
continue
}
# check for the check command
if { [regexp -- {^-?-?check$} $args($i)] == 1 } {
set ecosadmin::check_database_arg 1
continue
}
# check for --version
if { [regexp -- {^-?-version=?(.*)$} $args($i) dummy match1] == 1 } {
if { $match1 != "" } {
set ecosadmin::version_arg $match1
} else {
if { $i == $argc } {
fatal_error "missing argument after --version"
} else {
set ecosadmin::version_arg $args([incr i])
}
}
continue
}
# check for --accept_license
if { [regexp -- {^-?-accept_license$} $args($i)] == 1 } {
set ecosadmin::accept_license_arg 1
continue
}
# check for --extract_license
if { [regexp -- {^-?-extract_license$} $args($i)] == 1 } {
set ecosadmin::extract_license_arg 1
continue
}
# check for --keep_targets
if { [regexp -- {^-?-keep_targets$} $args($i)] == 1 } {
set ecosadmin::keep_targets_arg 1
continue
}
# check for --remove_targets
if { [regexp -- {^-?-remove_targets$} $args($i)] == 1 } {
set ecosadmin::keep_targets_arg 0
continue
}
# check for the add command
if { [regexp -- {^-?-?add=?(.*)$} $args($i) dummy match1] == 1 } {
if { $match1 != "" } {
set ecosadmin::add_package $match1
} else {
if { $i == $argc } {
fatal_error "missing argument after add"
} else {
set ecosadmin::add_package $args([incr i])
}
}
continue
}
# check for the merge command
if { [regexp -- {^-?-?merge=?(.*)$} $args($i) dummy match1] == 1 } {
if { $match1 != "" } {
set ecosadmin::merge_repository $match1
} else {
if { $i == $argc } {
fatal_error "missing argument after merge"
} else {
set ecosadmin::merge_repository $args([incr i])
}
}
continue
}
# check for the remove command
if { [regexp -- {^-?-?remove=?(.*)$} $args($i) dummy match1] == 1 } {
if { $match1 != "" } {
set ecosadmin::remove_package $match1
} else {
if { $i == $argc } {
fatal_error "missing argument after remove"
} else {
set ecosadmin::remove_package $args([incr i])
}
}
continue
}
# Check for --srcdir
if { [regexp -- {^-?-srcdir=?([ \.\\/:_a-zA-Z0-9-]*)$} $args($i) dummy match1] == 1 } {
if { $match1 == "" } {
if { $i == $argc } {
puts "ecosrelease: missing argument after --srcdir"
exit 1
} else {
set match1 $args([incr i])
}
}
set ecosadmin::component_repository $match1
continue
}
# An unrecognised argument.
fatal_error "invalid argument $args($i)"
}
}
# Convert user-specified UNIX-style Cygwin pathnames to Windows Tcl-style as necessary
set ecosadmin::component_repository [get_pathname_for_tcl $ecosadmin::component_repository]
set ecosadmin::add_package [get_pathname_for_tcl $ecosadmin::add_package]
set ecosadmin::merge_repository [get_pathname_for_tcl $ecosadmin::merge_repository]
}
#
# Display help information if the user has typed --help, -H, --H, or -help.
# The help text uses two hyphens for consistency with configure.
# Arguably this should change.
proc ecosadmin::argument_help { } {
puts "Usage: ecosadmin \[ command \]"
puts " commands are:"
puts " list : list packages"
puts " check : check database"
puts " add FILE : add packages"
puts " remove PACKAGE \[ --version VER \] \[ --remove_targets \] : remove a package"
}
# }}}
# {{{ Packages file
proc ecosadmin::read_data { silentflag } {
ASSERT { $ecosadmin::component_repository != "" }
set ecosadmin::known_packages ""
set ecosadmin::known_targets ""
set ecosadmin::known_templates ""
# A safe interpreter is used to process the packages file.
# This is somewhat overcautious, but it is also harmless.
# The following two commands are made accessible to the slave
# interpreter and are responsible for updating the actual data.
proc add_known_package { name } {
lappend ::ecosadmin::known_packages $name
}
proc add_known_target { name } {
lappend ::ecosadmin::known_targets $name
}
proc add_known_template { name } {
lappend ::ecosadmin::known_templates $name
}
proc set_package_data { name value } {
set ::ecosadmin::package_data($name) $value
}
proc set_target_data { name value } {
set ::ecosadmin::target_data($name) $value
}
proc set_template_data { name value } {
set ::ecosadmin::template_data($name) $value
}
# Create the parser, add the aliased commands, and then define
# the routines that do the real work.
set parser [interp create -safe]
$parser alias add_known_package ecosadmin::add_known_package
$parser alias add_known_target ecosadmin::add_known_target
$parser alias add_known_template ecosadmin::add_known_template
$parser alias set_package_data ecosadmin::set_package_data
$parser alias set_target_data ecosadmin::set_target_data
$parser alias set_template_data ecosadmin::set_template_data
$parser eval {
set current_package ""
set current_target ""
set current_template ""
proc package { name body } {
add_known_package $name
set_package_data "$name,alias" ""
set_package_data "$name,versions" ""
set_package_data "$name,dir" ""
set_package_data "$name,hardware" 0
set ::current_package $name
eval $body
set ::current_package ""
}
proc target { name body } {
add_known_target $name
set_target_data "$name,packages" ""
set ::current_target $name
eval $body
set ::current_target ""
}
#if 0
# templates are no longer specified in the package database
proc template { name body } {
add_known_template $name
set_template_data "$name,packages" ""
set ::current_template $name
eval $body
set ::current_template ""
}
#endif
proc packages { str } {
if { $::current_template != "" } {
set_template_data "$::current_template,packages" $str
} elseif { $::current_target != "" } {
set_target_data "$::current_target,packages" $str
} else {
ASSERT 0
}
}
proc directory { dir } {
set_package_data "$::current_package,dir" $dir
}
proc alias { str } {
if { $::current_package != "" } {
set_package_data "$::current_package,alias" $str
}
}
proc hardware { } {
set_package_data "$::current_package,hardware" 1
}
proc description { str } { }
proc disable { str } { }
proc enable { str } { }
proc script { str } { }
proc set_value { str1 str2 } { }
}
# The parser is ready to evaluate the script. To avoid having to give the
# safe interpreter file I/O capabilities, the file is actually read in
# here and then evaluated.
set filename [file join $ecosadmin::component_repository "ecos.db"]
set status [ catch {
set fd [open $filename r]
set script [read $fd]
close $fd
$parser eval $script
} message ]
if { $status != 0 } {
ecosadmin::fatal_error "parsing $filename:\n$message"
}
# The interpreter and the aliased commands are no longer required.
rename set_package_data {}
rename set_target_data {}
rename set_template_data {}
rename add_known_package {}
interp delete $parser
# At this stage the packages file has been read in. It is a good idea to
# check that all of these packages are present and correct, and incidentally
# figure out which versions are present.
foreach pkg $ecosadmin::known_packages {
set pkgdir [file join $ecosadmin::component_repository $ecosadmin::package_data($pkg,dir)]
if { ![file exists $pkgdir] || ![file isdir $pkgdir] } {
if { "" == $silentflag } {
warning "package $pkg at $pkgdir missing"
}
} else {
# Each subdirectory should correspond to a release. A utility routine
# is available for this.
set ecosadmin::package_data($pkg,versions) [locate_subdirs $pkgdir]
if { $ecosadmin::package_data($pkg,versions) == "" } {
fatal_error "package $pkg has no version directories"
}
}
# Sort all the versions using a version-aware comparison version
set ecosadmin::package_data($pkg,versions) [
lsort -command ecosadmin::cdl_compare_version $ecosadmin::package_data($pkg,versions)
]
}
}
#
# Given a package name as supplied by the user, return the internal package name.
# This involves searching through the list of aliases.
#
proc ecosadmin::find_package { name } {
foreach pkg $ecosadmin::known_packages {
if { [string toupper $pkg] == [string toupper $name] } {
return $pkg
}
foreach alias $ecosadmin::package_data($pkg,alias) {
if { [string toupper $alias] == [string toupper $name] } {
return $pkg
}
}
}
return ""
}
# }}}
# {{{ Directory and file utilities
# ----------------------------------------------------------------------------
# Start with a number of utility routines to access all files in
# a directory, stripping out well-known files such as makefile.am.
# The routines take an optional pattern argument if only certain
# files are of interest.
#
# Note that symbolic links are returned as well as files.
#
proc ecosadmin::locate_files { dir { pattern "*"} } {
ASSERT { $dir != "" }
# Start by getting a list of all the files.
set filelist [glob -nocomplain -- [file join $dir $pattern]]
if { $pattern == "*" } {
# For "everything", include ".*" files, but excluding .
# and .. directories
lappend filelist [glob -nocomplain -- [file join $dir ".\[a-zA-Z0-9\]*"]]
}
# Eliminate the pathnames from all of these files
set filenames ""
foreach file $filelist {
if { [string range $file end end] != "~" } {
lappend filenames [file tail $file]
}
}
# Eliminate any subdirectories.
set subdirs ""
foreach name $filenames {
if { [file isdir [file join $dir $name]] } {
lappend subdirs $name
}
}
foreach subdir $subdirs {
set index [lsearch -exact $filenames $subdir]
set filenames [lreplace $filenames $index $index]
}
return $filenames
}
#
# This utility returns all sub-directories, as opposed to all files.
# A variant glob pattern is used here. This version is not recursive.
proc ecosadmin::locate_subdirs { dir { pattern "*" }} {
ASSERT { $dir != "" }
set dirlist [glob -nocomplain -- [file join $dir $pattern]]
# Eliminate the pathnames and the regular (non-directory) files
set dirnames ""
foreach dir $dirlist {
if { [file isdirectory $dir] } {
lappend dirnames [file tail $dir]
}
}
# Get rid of the CVS directory, if any
if { $ecosadmin::keep_cvs == 0 } {
set index [lsearch -exact $dirnames "CVS"]
if { $index != -1 } {
set dirnames [lreplace $dirnames $index $index]
}
}
# That should be it.
return $dirnames
}
#
# A variant which is recursive. This one does not support a pattern.
#
proc ecosadmin::locate_all_subdirs { dir } {
ASSERT { $dir != "" }
set result ""
foreach subdir [locate_subdirs $dir] {
lappend result $subdir
foreach x [locate_all_subdirs [file join $dir $subdir]] {
lappend result [file join $subdir $x]
}
}
return $result
}
#
# This routine returns a list of all the files in a given directory and in
# all subdirectories, preserving the subdirectory name.
#
proc ecosadmin::locate_all_files { dir { pattern "*" } } {
ASSERT { $dir != "" }
set files [locate_files $dir $pattern]
set subdirs [locate_subdirs $dir]
foreach subdir $subdirs {
set subfiles [locate_all_files [file join $dir $subdir] $pattern]
foreach file $subfiles {
lappend files [file join $subdir $file]
}
}
return $files
}
#
# Sometimes a directory may be empty, or contain just a CVS subdirectory,
# in which case there is no point in copying it across.
#
proc ecosadmin::is_empty_directory { dir } {
ASSERT { $dir != "" }
set contents [glob -nocomplain -- [file join $dir "*"]]
if { [llength $contents] == 0 } {
return 1
}
if { ([llength $contents] == 1) && [string match {*CVS} $contents] } {
return 1
}
return 0
}
#
# ----------------------------------------------------------------------------
# Take a cygwin32 filename such as //d/tmp/pkgobj and turn it into something
# acceptable to Tcl, i.e. d:/tmp/pkgobj. There are a few other complications...
proc ecosadmin::get_pathname_for_tcl { name } {
if { ( $ecosadmin::windows_host ) && ( $name != "" ) } {
# If there is no logical drive letter specified
if { [ string match "?:*" $name ] == 0 } {
# Invoke cygpath to resolve the POSIX-style path
if { [ catch { exec cygpath -w $name } result ] != 0 } {
fatal_error "processing filepath $name:\n$result"
}
} else {
set result $name
}
# Convert backslashes to forward slashes
regsub -all -- {\\} $result "/" name
}
return $name
}
# ----------------------------------------------------------------------------
# Make sure that a newly created or copied file is writable. This operation
# is platform-specific. Under Unix at most the current user is given
# permission, since there does not seem to be any easy way to get hold
# of the real umask.
proc ecosadmin::make_writable { name } {
ASSERT { $name != "" }
ASSERT { [file isfile $name] }
if { [file writable $name] == 0 } {
if { $ecosadmin::windows_host != 0 } {
file attributes $name -readonly 0
} else {
set mask [file attributes $name -permissions]
set mask [expr $mask | 0200]
file attributes $name -permissions $mask
}
}
}
# }}}
# {{{ main()
#-----------------------------------------------------------------------
# Procedure target_requires_missing_package determines whether a
# target entry is dependent on missing packages. It is called when
# filtering templates out of the database
proc ecosadmin::target_requires_missing_package { target } {
foreach package $ecosadmin::target_data($target,packages) {
if { [ lsearch $ecosadmin::known_packages $package ] == -1 } {
return 1
}
}
return 0
}
#-----------------------------------------------------------------------
# Procedure template_requires_missing_package determines whether a
# template entry is dependent on missing packages. It is called when
# filtering templates out of the database
proc ecosadmin::template_requires_missing_package { template } {
foreach package $ecosadmin::template_data($template,packages) {
if { [ lsearch $ecosadmin::known_packages $package ] == -1 } {
return 1
}
}
return 0
}
#-----------------------------------------------------------------------
# Procedure target_requires_any_package determines whether a target entry
# is dependent on specified packages. It is called when removing packages
# to determine whether a target should also be removed
proc ecosadmin::target_requires_any_package { target packages } {
foreach package $packages {
if { [ lsearch $ecosadmin::target_data($target,packages) $package ] != -1 } {
return 1
}
}
return 0
}
#-----------------------------------------------------------------------
# Procedure template_requires_any_package determines whether a template entry
# is dependent on specified packages. It is called when removing packages
# to determine whether a template should also be removed
proc ecosadmin::template_requires_any_package { template packages } {
foreach package $packages {
if { [ lsearch $ecosadmin::template_data($template,packages) $package ] != -1 } {
return 1
}
}
return 0
}
#-----------------------------------------------------------------------
# Procedure merge_new_packages adds any entries in the specified data
# file to the eCos repository database iff they are not already present
proc ecosadmin::merge_new_packages { datafile } {
# array of targets which require merging of package list
array set ::ecosadmin::merge_target_packages {}
variable ::ecosadmin::merge_targets ""
variable ::ecosadmin::target_packages ""
# open the eCos database file for appending
set ecosfile [ file join $ecosadmin::component_repository "ecos.db" ]
variable outfile [ open $ecosfile a+ ]
# initialize the list of merged packages
set ecosadmin::merge_packages ""
# this procedure is called when the interpreter encounters a
# package command in the datafile
proc merge { command name body } {
ecosadmin::report "processing $command $name"
# append the new package/target/template only if it is not already known
if { ( ( $command == "package" ) && ( [ lsearch -exact $ecosadmin::known_packages $name ] == -1 ) ) ||
( ( $command == "target" ) && ( [ lsearch -exact $ecosadmin::known_targets $name ] == -1 ) ) ||
( ( $command == "template" ) && ( [ lsearch -exact $ecosadmin::known_templates $name ] == -1 ) ) } {
puts $ecosadmin::outfile "$command $name {$body}\n"
} elseif { ($command == "target") } {
# target is already defined so store any new package names for a possible merge
set merge_required 0
foreach pkg $ecosadmin::target_packages {
# for each package name in the target record
if { [ lsearch $ecosadmin::target_data($name,packages) $pkg ] == -1 } {
# package name is not in the existing target record
lappend ecosadmin::merge_target_packages($name) $pkg
set merge_required 1
}
}
if { $merge_required == 1 } {
# at least one package name is not in the existing target record
lappend ecosadmin::merge_targets $name
}
}
# add new packages to the list of merged packages
if { ( "package" == $command ) } {
lappend ecosadmin::merge_packages $name
}
}
proc set_target_packages { packages } {
set ecosadmin::target_packages $packages
}
# Create the parser, add the aliased commands, and then define
# the routines that do the real work.
set parser [ interp create -safe ]
$parser alias merge ecosadmin::merge
$parser alias set_target_packages ecosadmin::set_target_packages