Root/schhist/sanitize-profile

1#!/usr/bin/perl
2#
3# sanitize-profile - Remove items from a KiCad profile that may cause an upset
4#
5# Written 2010, 2012 by Werner Almesberger
6# Copyright 2010, 2012 Werner Almesberger
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13
14
15@LIBS = ("/usr/share/kicad/library", "/usr/local/share/kicad/library");
16
17
18sub rewrite
19{
20    local ($s) = @_;
21
22    return $s if $section ne "eeschema/libraries";
23    return $s unless /^LibName(\d+)=(.*)\s*$/;
24    my $lib = $2;
25    delete $add{$lib};
26    if ($1 == $in_lib) {
27    $in_lib++;
28    } else {
29    print STDERR "LibName$1 when expecting LibName$in_lib. Renumbering.\n";
30    $in_lib = $1+1;
31    }
32    $out_lib++;
33    my $var = "LibName$out_lib";
34    if ($lib =~ /\//) {
35    return "$var=$lib\n" if -r "$lib.lib";
36    }
37    for (".", @libdir, @LIBS) {
38    return "$var=$lib\n" if -r "$_/$lib.lib";
39    }
40    print STDERR "cannot find $lib\n";
41    $out_lib--;
42    return undef;
43}
44
45
46sub complement
47{
48    for (sort keys %add) {
49    print STDERR "adding $_\n";
50    $out_lib++;
51    push(@f, "LibName$out_lib=$_\n");
52    delete $add{$_};
53    }
54}
55
56
57sub usage
58{
59    print STDERR "usage: $0 file.pro [outfile]\n";
60    exit(1);
61}
62
63
64for (split(",", $ENV{"SCHHIST_ADDLIBS"})) {
65    $add{$_} = 1 if $_ ne "";
66}
67
68($file, $out) = @ARGV;
69&usage if $#ARGV > 1;
70
71($dir = $file) =~ s|[^/]*$||;
72$dir = "." if $dir eq "";
73
74open(FILE, $file) || die "$file: $!";
75while (<FILE>) {
76    if (/^\[(\S+)\]/) {
77    if ($1 eq "eeschema/libraries") {
78        $in_lib = 1;
79        $out_lib = 0;
80    } else {
81        &complement if $section eq "eeschema/libraries";
82    }
83    $section = $1;
84    }
85    if ($section eq "eeschema") {
86    @libdir = split(/;/, $1) if /^LibDir=(.*)\s*$/;
87    }
88    $s = &rewrite($_);
89    push(@f, $s) if defined $s;
90}
91close FILE;
92&complement;
93
94if (!defined $out) {
95    rename($file, "$file.bak");
96    $out= $file;
97}
98
99open(FILE, ">$out") || die "$out: $!";
100print FILE join("", @f) || die "$out: $!";
101close FILE || die "$out: $!";
102

Archive Download this file

Branches:
master



interactive