~ymyasoedov/maggy/trunk

« back to all changes in this revision

Viewing changes to maggy-server

  • Committer: Yuri Myasoedov
  • Date: 2015-07-19 18:02:00 UTC
  • Revision ID: ymyasoedov@yandex.ru-20150719180200-zaqwamp3czbqqx61
Clean the code

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
use Cwd;
7
7
use File::Copy;
8
8
use File::Copy::Recursive qw(dirmove);
9
 
use File::Path qw(make_path);
 
9
use File::Path qw(make_path remove_tree);
10
10
use File::Temp qw(tempdir);
11
11
use File::chdir;
12
12
 
 
13
use Maggy::Common qw(cmd);
 
14
 
13
15
# Documentation browser under "/perldoc"
14
16
plugin 'PODRenderer';
15
17
 
26
28
    }
27
29
}
28
30
 
 
31
my $data;
 
32
read_packages();
 
33
 
 
34
use Data::Dumper;
 
35
print Dumper($data);
 
36
 
 
37
sub read_packages {
 
38
    my $dir = $config->{storage}{git};
 
39
    opendir(my $dh, $dir) or die sprintf("Can't open dir %s\n", $dir);
 
40
    while (readdir $dh) {
 
41
        next if $_ eq '.' or $_ eq '..';
 
42
        if (-d "$dir/$_") {
 
43
            $data->{package}{$_} = 1;
 
44
        }
 
45
    }
 
46
    closedir($dh);
 
47
}
 
48
 
29
49
#-----------------------------------------------------------------------
30
50
 
31
51
get '/' => sub {
37
57
    my $c = shift;
38
58
    my $upload = $c->req->upload('file');
39
59
    my $filename = $upload->filename;
40
 
    
41
60
    my $tmpdir = tempdir('maggy-XXXXXX', TMPDIR => 1);
42
 
    print "Creating $tmpdir\n";
43
 
    my $r = $upload->move_to("$tmpdir/$filename");
 
61
    $upload->move_to("$tmpdir/$filename");
 
62
 
 
63
    # Unpack source RPM to cpio archive
 
64
    my $rpm2cpio = $config->{utils}{rpm2cpio};
 
65
    cmd("$rpm2cpio $tmpdir/$filename > $tmpdir/archive.cpio");
 
66
    
44
67
    my $package_name;
45
 
    system("$config->{utils}{rpm2cpio} $tmpdir/$filename > $tmpdir/archive.cpio");
46
68
    {
 
69
        # Unpack files from cpio archive
47
70
        local $CWD = $tmpdir;
48
 
        system("cpio -idmv < archive.cpio");
 
71
        cmd("cpio -idmv < archive.cpio");
 
72
        
 
73
        # Remove source RPM and cpio
49
74
        for ($filename, "archive.cpio") {
50
75
            print "Removing $_\n";
51
76
            unlink $_ or die "Error: can't unlink file $_\n";
52
77
        }
53
 
        # Move blobs to storage
 
78
        
 
79
        # Find sources and spec
54
80
        my $dir = getcwd;
55
 
        opendir(my $dh, $dir) or die sprintf("Can't open dir %s\n", $dir);
56
 
        my (@blobs, $spec);
 
81
        my (@sources, $spec);
 
82
        opendir(my $dh, $dir) or 
 
83
            die sprintf("Error: can't open dir %s\n", $dir);
57
84
        while (readdir $dh) {
58
85
            next if $_ eq '.' || $_ eq '..';
59
86
            if (/\.spec$/) {
61
88
                $spec = $_;
62
89
            }
63
90
            elsif (-B "$dir/$_") {
64
 
                push @blobs, $_;
 
91
                push @sources, $_;
65
92
            }
66
93
        }
67
94
        closedir($dh);
 
95
        
68
96
        die "Error: can't find spec file\n" unless $spec;
69
97
        $package_name = substr $spec, 0, -5;
 
98
        if (exists $data->{package}{$package_name}) {
 
99
            return $c->render(text => 'Package already exists');
 
100
        }
70
101
        mkdir "$config->{storage}{sources}/$package_name" or die "Can't create dir\n";
71
 
        foreach (@blobs) {
72
 
            move("$dir/$_", "$config->{storage}{sources}/$package_name/$_") or die "Can't move file\n";
 
102
 
 
103
        # Move sources to storage
 
104
        foreach my $source (@sources) {
 
105
            my $result = cmd("sha256sum $source");
 
106
            cmd("echo '$result->{output}' >> $dir/sources");
 
107
            move("$dir/$source", "$config->{storage}{sources}/$package_name/$source") or die "Can't move file\n";
73
108
        }
74
 
        system("git init . && git add * && git commit -m \"Initial commit\"");
 
109
        cmd("git init . && git add * && git commit -m \"Initial commit\"");
75
110
    }
76
111
    mkdir "$config->{storage}{git}/$package_name" or die "Can't create dir\n";
77
112
    
79
114
    dirmove("$tmpdir/.git", "$config->{storage}{git}/$package_name");
80
115
    {
81
116
        local $CWD = "$config->{storage}{git}/$package_name";
82
 
        system('git config --bool core.bare true');
 
117
        cmd('git config --bool core.bare true');
83
118
    }
84
 
    
 
119
 
 
120
    # Remove temporary directory
 
121
    remove_tree($tmpdir);
 
122
 
85
123
    $c->render(text => 'Package has been imported');
86
124
};
87
125