xref: /aoo41x/main/solenv/bin/cwsattach.pl (revision 7e90fac2)
1cdf0e10cSrcweir:
2cdf0e10cSrcweireval 'exec perl -wS $0 ${1+"$@"}'
3cdf0e10cSrcweir    if 0;
4*7e90fac2SAndrew Rist#**************************************************************
5*7e90fac2SAndrew Rist#
6*7e90fac2SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
7*7e90fac2SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
8*7e90fac2SAndrew Rist#  distributed with this work for additional information
9*7e90fac2SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
10*7e90fac2SAndrew Rist#  to you under the Apache License, Version 2.0 (the
11*7e90fac2SAndrew Rist#  "License"); you may not use this file except in compliance
12*7e90fac2SAndrew Rist#  with the License.  You may obtain a copy of the License at
13*7e90fac2SAndrew Rist#
14*7e90fac2SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
15*7e90fac2SAndrew Rist#
16*7e90fac2SAndrew Rist#  Unless required by applicable law or agreed to in writing,
17*7e90fac2SAndrew Rist#  software distributed under the License is distributed on an
18*7e90fac2SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19*7e90fac2SAndrew Rist#  KIND, either express or implied.  See the License for the
20*7e90fac2SAndrew Rist#  specific language governing permissions and limitations
21*7e90fac2SAndrew Rist#  under the License.
22*7e90fac2SAndrew Rist#
23*7e90fac2SAndrew Rist#**************************************************************
24*7e90fac2SAndrew Rist
25*7e90fac2SAndrew Rist
26cdf0e10cSrcweir#
27cdf0e10cSrcweir# cwsattach.pl - attach files to CWS
28cdf0e10cSrcweir#
29cdf0e10cSrcweir
30cdf0e10cSrcweiruse strict;
31cdf0e10cSrcweiruse Getopt::Long;
32cdf0e10cSrcweiruse Cwd;
33cdf0e10cSrcweir
34cdf0e10cSrcweir#### module lookup
35cdf0e10cSrcweirmy @lib_dirs;
36cdf0e10cSrcweirBEGIN {
37cdf0e10cSrcweir    if ( !defined($ENV{SOLARENV}) ) {
38cdf0e10cSrcweir        die "No environment found (environment variable SOLARENV is undefined)";
39cdf0e10cSrcweir    }
40cdf0e10cSrcweir    push(@lib_dirs, "$ENV{SOLARENV}/bin/modules");
41cdf0e10cSrcweir    push(@lib_dirs, "$ENV{COMMON_ENV_TOOLS}/modules") if defined($ENV{COMMON_ENV_TOOLS});
42cdf0e10cSrcweir}
43cdf0e10cSrcweiruse lib (@lib_dirs);
44cdf0e10cSrcweir
45cdf0e10cSrcweiruse Cws;
46cdf0e10cSrcweir
47cdf0e10cSrcweir#### script id #####
48cdf0e10cSrcweir
49cdf0e10cSrcweir( my $script_name = $0 ) =~ s/^.*\b(\w+)\.pl$/$1/;
50cdf0e10cSrcweir
51cdf0e10cSrcweirmy $script_rev;
52cdf0e10cSrcweirmy $id_str = ' $Revision: 1.3 $ ';
53cdf0e10cSrcweir$id_str =~ /Revision:\s+(\S+)\s+\$/
54cdf0e10cSrcweir  ? ($script_rev = $1) : ($script_rev = "-");
55cdf0e10cSrcweir
56cdf0e10cSrcweirprint STDERR "$script_name -- version: $script_rev\n";
57cdf0e10cSrcweir
58cdf0e10cSrcweir#### global #####
59cdf0e10cSrcweir
60cdf0e10cSrcweirmy $is_debug = 1;       # enable debug
61cdf0e10cSrcweirmy $opt_master = '';    # option: master workspace
62cdf0e10cSrcweirmy $opt_child  = '';    # option: child workspace
63cdf0e10cSrcweirmy $opt_mime_type = '';  # option: mime type
64cdf0e10cSrcweir
65cdf0e10cSrcweir
66cdf0e10cSrcweir#### main #####
67cdf0e10cSrcweir
68cdf0e10cSrcweirmy $arg_file = parse_options();
69cdf0e10cSrcweirattach_cws($arg_file);
70cdf0e10cSrcweirexit(0);
71cdf0e10cSrcweir
72cdf0e10cSrcweir#### subroutines ####
73cdf0e10cSrcweir
74cdf0e10cSrcweirsub attach_cws
75cdf0e10cSrcweir{
76cdf0e10cSrcweir    my $filename = shift;
77cdf0e10cSrcweir    # get master and child workspace
78cdf0e10cSrcweir    my $masterws = $opt_master ? uc($opt_master) : $ENV{WORK_STAMP};
79cdf0e10cSrcweir    my $childws  = $opt_child  ? $opt_child  : $ENV{CWS_WORK_STAMP};
80cdf0e10cSrcweir
81cdf0e10cSrcweir    if ( !defined($masterws) ) {
82cdf0e10cSrcweir        print_error("Can't determine master workspace environment.\n"
83cdf0e10cSrcweir                    . "Please initialize environment with setsolar ...", 1);
84cdf0e10cSrcweir    }
85cdf0e10cSrcweir
86cdf0e10cSrcweir    if ( !defined($childws) ) {
87cdf0e10cSrcweir        print_error("Can't determine child workspace environment.\n"
88cdf0e10cSrcweir                    . "Please initialize environment with setsolar ...", 1);
89cdf0e10cSrcweir    }
90cdf0e10cSrcweir
91cdf0e10cSrcweir    my $cws = Cws->new();
92cdf0e10cSrcweir    $cws->child($childws);
93cdf0e10cSrcweir    $cws->master($masterws);
94cdf0e10cSrcweir
95cdf0e10cSrcweir    my $mime_type  = $opt_mime_type  ? $opt_mime_type  : find_mime_type($filename);
96cdf0e10cSrcweir
97cdf0e10cSrcweir    no strict;
98cdf0e10cSrcweir
99cdf0e10cSrcweir    if ( is_valid_cws($cws) ) {
100cdf0e10cSrcweir        #print "CWS is valid filename=" . $filename . " mime_type=" . $mime_type . "\n";
101cdf0e10cSrcweir        open(DATA,"<$filename") || die "can't open filename";
102cdf0e10cSrcweir        $data="";
103cdf0e10cSrcweir	while(<DATA>) {
104cdf0e10cSrcweir		$data.=$_;
105cdf0e10cSrcweir	}
106cdf0e10cSrcweir        my $result=$cws->save_attachment($filename,$mime_type,$data);
107cdf0e10cSrcweir    } else {
108cdf0e10cSrcweir        print STDERR "cws is not valid";
109cdf0e10cSrcweir    }
110cdf0e10cSrcweir    exit(0)
111cdf0e10cSrcweir}
112cdf0e10cSrcweir
113cdf0e10cSrcweir
114cdf0e10cSrcweirsub find_mime_type
115cdf0e10cSrcweir{
116cdf0e10cSrcweir    my $filename = shift;
117cdf0e10cSrcweir    $filename=~/(.*)\.(.*$)/;
118cdf0e10cSrcweir    my $ext=$2;
119cdf0e10cSrcweir    my $fmime='';
120cdf0e10cSrcweir
121cdf0e10cSrcweir    if ( defined($ext) ) {
122cdf0e10cSrcweir        open(MIME,"< $ENV{SOLARENV}/inc/mime.types")|| die "can not open mimetype file";
123cdf0e10cSrcweir        while (<MIME>) {
124cdf0e10cSrcweir            my @a=split();
125cdf0e10cSrcweir            my $iscomment=0;
126cdf0e10cSrcweir            if ( /(\s*\#).*/ ) {
127cdf0e10cSrcweir                $iscomment=1;
128cdf0e10cSrcweir            } else {
129cdf0e10cSrcweir                $iscomment=0;
130cdf0e10cSrcweir            }
131cdf0e10cSrcweir            if ( $iscomment eq 0 && $#a >= 1 && $fmime eq '' ) {
132cdf0e10cSrcweir                my $i=1;
133cdf0e10cSrcweir                for ($i=1; $i<=$#a; $i++) {
134cdf0e10cSrcweir                    if ( $a[$i] eq $ext ) {
135cdf0e10cSrcweir                        $fmime=$a[0];
136cdf0e10cSrcweir                    }
137cdf0e10cSrcweir                }
138cdf0e10cSrcweir            }
139cdf0e10cSrcweir        }
140cdf0e10cSrcweir
141cdf0e10cSrcweir    }
142cdf0e10cSrcweir    if ( $fmime eq '' ) {
143cdf0e10cSrcweir        $fmime="application/octet-stream";
144cdf0e10cSrcweir    }
145cdf0e10cSrcweir    return $fmime;
146cdf0e10cSrcweir}
147cdf0e10cSrcweir
148cdf0e10cSrcweir
149cdf0e10cSrcweirsub is_valid_cws
150cdf0e10cSrcweir{
151cdf0e10cSrcweir    my $cws = shift;
152cdf0e10cSrcweir
153cdf0e10cSrcweir    my $masterws = $cws->master();
154cdf0e10cSrcweir    my $childws  = $cws->child();
155cdf0e10cSrcweir    # check if we got a valid child workspace
156cdf0e10cSrcweir    my $id = $cws->eis_id();
157cdf0e10cSrcweir    if ( !$id ) {
158cdf0e10cSrcweir        print_error("Child workspace '$childws' for master workspace '$masterws' not found in EIS database.", 2);
159cdf0e10cSrcweir    }
160cdf0e10cSrcweir    print_message("Master workspace '$masterws', child workspace '$childws':");
161cdf0e10cSrcweir    return 1;
162cdf0e10cSrcweir}
163cdf0e10cSrcweir
164cdf0e10cSrcweirsub parse_options
165cdf0e10cSrcweir{
166cdf0e10cSrcweir    # parse options and do some sanity checks
167cdf0e10cSrcweir    my $help = 0;
168cdf0e10cSrcweir    my $success = GetOptions('h' => \$help, 'm=s' => \$opt_master, 'c=s'=> \$opt_child, 't=s'=> \$opt_mime_type);
169cdf0e10cSrcweir    if ( $help || !$success || $#ARGV < 0 ) {
170cdf0e10cSrcweir        usage();
171cdf0e10cSrcweir        exit(1);
172cdf0e10cSrcweir    }
173cdf0e10cSrcweir
174cdf0e10cSrcweir    return $ARGV[0];
175cdf0e10cSrcweir}
176cdf0e10cSrcweir
177cdf0e10cSrcweirsub print_message
178cdf0e10cSrcweir{
179cdf0e10cSrcweir    my $message     = shift;
180cdf0e10cSrcweir
181cdf0e10cSrcweir    print STDERR "$script_name: ";
182cdf0e10cSrcweir    print STDERR "$message\n";
183cdf0e10cSrcweir    return;
184cdf0e10cSrcweir}
185cdf0e10cSrcweir
186cdf0e10cSrcweirsub print_error
187cdf0e10cSrcweir{
188cdf0e10cSrcweir    my $message     = shift;
189cdf0e10cSrcweir    my $error_code  = shift;
190cdf0e10cSrcweir
191cdf0e10cSrcweir    print STDERR "$script_name: ";
192cdf0e10cSrcweir    print STDERR "ERROR: $message\n";
193cdf0e10cSrcweir
194cdf0e10cSrcweir    if ( $error_code ) {
195cdf0e10cSrcweir        print STDERR "\nFAILURE: $script_name aborted.\n";
196cdf0e10cSrcweir        exit($error_code);
197cdf0e10cSrcweir    }
198cdf0e10cSrcweir    return;
199cdf0e10cSrcweir}
200cdf0e10cSrcweir
201cdf0e10cSrcweirsub usage
202cdf0e10cSrcweir{
203cdf0e10cSrcweir    print STDERR "Usage: cwsattach [-h] [-m master] [-c child] [-t mimetype] filename\n";
204cdf0e10cSrcweir    print STDERR "\n";
205cdf0e10cSrcweir    print STDERR "Attach files to CWS in EIS database\n";
206cdf0e10cSrcweir    print STDERR "\n";
207cdf0e10cSrcweir    print STDERR "Options:\n";
208cdf0e10cSrcweir    print STDERR "\t-h\t\thelp\n";
209cdf0e10cSrcweir    print STDERR "\t-m master\toverride MWS specified in environment\n";
210cdf0e10cSrcweir    print STDERR "\t-c child\toverride CWS specified in environment\n";
211cdf0e10cSrcweir    print STDERR "\t-t mimetype\texplicitly set mime type\n";
212cdf0e10cSrcweir    print STDERR "Examples:\n";
213cdf0e10cSrcweir    print STDERR "\tcwsattach barfoo.html\n";
214cdf0e10cSrcweir    print STDERR "\tcwsattach -t text bar.cxx\n";
215cdf0e10cSrcweir    print STDERR "\tcwsattach -t text/rtf foo.rtf\n";
216cdf0e10cSrcweir}
217