af710487 |
1 | ############################################################################### |
3abe9331 |
2 | # Copyright (c) 2000-2015 Ericsson Telecom AB |
af710487 |
3 | # All rights reserved. This program and the accompanying materials |
4 | # are made available under the terms of the Eclipse Public License v1.0 |
5 | # which accompanies this distribution, and is available at |
6 | # http://www.eclipse.org/legal/epl-v10.html |
7 | ############################################################################### |
8 | #!/usr/bin/perl |
9 | ############################################################################### |
10 | # This script is intended to archive the project hierarchy from a TPD file structure |
11 | ############################################################################### |
12 | use 5.010; |
13 | use strict; |
14 | use warnings; |
15 | use Cwd; |
16 | use File::Copy; |
17 | |
18 | my $ttcn3_dir = $ENV{'TTCN3_DIR'}; |
19 | my $bindir = $ttcn3_dir . "/bin"; |
20 | my $home = cwd(); |
21 | my @list = readFile(); |
22 | my $tpd = getTPDFileName(\@list); |
23 | chomp ($tpd); |
24 | if (-l $tpd) #get the path if it is a symlink |
25 | { |
26 | $tpd = `readlink $tpd`; |
27 | chomp ($tpd); |
28 | } |
29 | my $root = getPathToRootDir(\@list); # get the workspace directory of the OS |
30 | chomp ($root); |
31 | chdir ($root) or die "cannot change: $!\n"; |
32 | my $cutstring = cwd; # this is string generated from the the absolut path to the workspace |
33 | my $archiveDir = getArchiveDir(\@list); #directory to place the archive |
34 | $archiveDir = $home . "/" . $archiveDir; |
35 | chomp ($archiveDir); |
36 | my $createDir = "mkdir -p " . $archiveDir; |
37 | my $res = system($createDir); |
38 | my $backupFileName = createBackupFileName(); |
39 | my $backupfile = $archiveDir . "/" . $backupFileName; |
40 | if ($res != 0) { die (" creating directory " . $archiveDir ." failed\n"); } |
41 | my $archive = $bindir . "/" . "ttcn3_makefilegen" ." -V -P " . $cutstring . " -t " . $tpd . " | xargs tar cfz ". $archiveDir . "/" . $backupFileName . " 2> /dev/null"; |
42 | system($archive); #running it |
43 | if (-e $backupfile) { print ("archiving succeeded\n"); } |
44 | else { print ("archiving failed\n"); } |
45 | chdir ($home) or die "cannot change: $!\n"; |
46 | ############################################################ |
47 | sub readFile |
48 | { |
49 | my $makefile = "Makefile"; |
50 | open ( FILE, "<", $makefile ) or die ( "failed to open file: $home\/$makefile\n" ); |
51 | my @lines = <FILE>; |
52 | close FILE; |
53 | return @lines; |
54 | } |
55 | ############################################################ |
56 | sub getPathToRootDir #get the relative path to OS workspace |
57 | { |
58 | my @list = @{$_[0]}; |
59 | my $search = qr/^ROOT_DIR =/s; |
60 | my $offset = 0; |
61 | my $line; |
62 | for my $i ( 0 .. $#list ) |
63 | { |
64 | if ( $list[$i] =~ $search ) |
65 | { |
66 | $line = $list[$i]; |
67 | my $dot = '.'; |
68 | $offset = index($list[$i], $dot); |
69 | last; |
70 | } |
71 | } |
72 | if ($offset == 0) { die ( "no ROOT_DIR variable was found in the Makefile\n" ); } |
73 | my $path = substr $line, $offset; |
74 | return $path; |
75 | } |
76 | ############################################################ |
77 | sub getTPDFileName # TPD filename what the Makefile is created from |
78 | { |
79 | my @list = @{$_[0]}; |
80 | my $search = qr/^TPD =/s; |
81 | my $offset = 0; |
82 | my $line; |
83 | for my $i ( 0 .. $#list ) |
84 | { |
85 | if ( $list[$i] =~ $search ) |
86 | { |
87 | $line = $list[$i]; |
88 | my $assign = '='; |
89 | $offset = index($list[$i], $assign); |
90 | last; |
91 | } |
92 | } |
93 | if ($offset == 0) { die ( "no TPD variable was found in the Makefile\n" ); } |
94 | my $file = substr $line, $offset + 1; |
95 | $file =~ s/^\s+|\s+$//; # remove heading and traling whitespaces |
96 | return $file; |
97 | } |
98 | ############################################################ |
99 | sub getArchiveDir # the name of the archive directory |
100 | { |
101 | my @list = @{$_[0]}; |
102 | my $search = qr/^ARCHIVE_DIR =/s; |
103 | my $offset = 0; |
104 | my $line; |
105 | for my $i ( 0 .. $#list ) |
106 | { |
107 | if ( $list[$i] =~ $search ) |
108 | { |
109 | $line = $list[$i]; |
110 | my $assign = '='; |
111 | $offset = index($list[$i], $assign); |
112 | last; |
113 | } |
114 | } |
115 | if ($offset == 0) { die ( "no ARCHIVE_DIR variable was found in the Makefile\n" ); } |
116 | my $dir = substr $line, $offset + 1; |
117 | $dir =~ s/^\s+|\s+$//; # remove heading and trailing whitespaces |
118 | return $dir; |
119 | } |
120 | ############################################################ |
121 | sub getExecutableName # the name of the target executable |
122 | { |
123 | my @list = @{$_[0]}; |
124 | my $search = qr/^EXECUTABLE =/s; |
125 | my $offset = 0; |
126 | my $line; |
127 | for my $i ( 0 .. $#list ) |
128 | { |
129 | if ( $list[$i] =~ $search ) |
130 | { |
131 | $line = $list[$i]; |
132 | my $assign = '='; |
133 | $offset = index($list[$i], $assign); |
134 | last; |
135 | } |
136 | } |
137 | if ($offset == 0) { die ( "no EXCUTABLE variable was found in the Makefile\n" ); } |
138 | my $exec = substr $line, $offset + 1; |
139 | $exec =~ s/^\s+|\s+$//; # remove heading and trailing whitespaces |
140 | return $exec; |
141 | } |
142 | ############################################################ |
143 | sub createBackupFileName |
144 | { |
145 | my $backupFile = getExecutableName(\@list); |
146 | my $dot = '.'; |
147 | my $result = index($backupFile, $dot); |
148 | if ($result > -1) |
149 | { |
150 | $backupFile = substr $backupFile, 0, $result; |
151 | } |
152 | chomp ($backupFile); |
153 | my $date = `date '+%y%m%d-%H%M'`; |
154 | chomp ($date); |
155 | my $baseName = $backupFile . "-" . $date . ".tgz"; |
156 | chomp ($baseName); |
157 | return $baseName; |
158 | } |
159 | ############################################################ |