This repository has been archived by the owner on Apr 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
style.pl
executable file
·96 lines (87 loc) · 2.81 KB
/
style.pl
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
#!/usr/bin/env perl
#
# Copyright (C) 2012 - GNS3 development team
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED ``AS IS'' AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# Fixes coding style
# http://redmine.gns3.net/projects/gns3-devel/wiki/CodingStyle
# This tool is not part of the GNS3 distribution
# find . \! -path "*build*" \! -path "*Ui*" -name "*.py" -exec ./style.pl {} \;
use strict;
use warnings;
my $style_utf8 = '# -*- coding: utf-8 -*-' . "\n";
my $style_tabs = '# vim: expandtab ts=4 sw=4 sts=4:' . "\n";
my $style_modeline = '# vim: expandtab ts=4 sw=4 sts=4 fileencoding=utf-8:' . "\n";
my @toremove = ($style_utf8, $style_tabs);
my @header = ($style_modeline);
if (scalar(@ARGV) < 1)
{
print STDERR "usage: $0 <file.py> [...]\n";
exit 1;
}
sub strip_data
{
my $data = shift;
foreach (@toremove)
{
if ("$_" eq "$data")
{
return "";
}
}
return $data;
}
foreach (@ARGV)
{
my $filename = $_ . ".style";
rename $_, $filename or die "rename $_, $filename: $!.\n";
open IFILE, $filename or die "open $filename: $!.\n";
open OFILE, ">", $_ or die "open $_: $!.\n";
my @data = <IFILE>;
close IFILE;
my @clean_data = map { s/\r\n/\n/g; $_; } @data;
@clean_data = map { strip_data($_) } @clean_data;
my $idx = 0;
if (scalar(@clean_data) > 1 && $data[0] =~ m/^\#\!/)
{
$idx = 1;
}
foreach (@clean_data)
{
$_ =~ s/\s+\n$/\n/;
my $line = $_;
my $i = 0;
foreach (@header)
{
if ("$_" eq "$line")
{
splice @header, $i, 1;
}
else
{
$i = $i + 1;
}
}
}
foreach (@header)
{
splice @clean_data, $idx, 0, $_;
}
foreach (@clean_data)
{
print OFILE $_;
}
unlink $filename;
close OFILE;
}