File: debcopy

package info (click to toggle)
debpartial 0%2B20020130
  • links: PTS
  • area: main
  • in suites: woody
  • size: 92 kB
  • ctags: 53
  • sloc: ruby: 860; sh: 136; makefile: 36
file content (183 lines) | stat: -rwxr-xr-x 4,134 bytes parent folder | download
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/ruby
#
# debcopy - Debian Packages/Sources partial copy tool
#
# Usage: debcopy [-l] <source> <dest>
#
#  where <source> is a top directory of a debian archive,
#  and <dest> is a top directory of a new debian partial archive.
#
#  debcopy searches all Packages.gz and Sources.gz under <dest>/dists
#  and copies all files listed in the Packages.gz and Sources.gz
#  files into <dest> from <source>. -l creates symbolic links
#  instead of copying files.
#
# Copyright (C) 2002  Masato Taruishi <taru@debian.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License with
#  the Debian GNU/Linux distribution in file /usr/share/common-licenses/GPL;
#  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
#  Suite 330, Boston, MA  02111-1307  USA
#
require 'getoptlong'
require 'zlib'
require 'ftools'

$link = false

def usage
  $stderr.puts "Usage: #{__FILE__} [-l] <source> <dest>"
   exit 1
end

def each (file, &block)
  fin = GzipReader.open(file)
  fin.each do |line|
    yield line
  end
  fin.close
end

def each_file (file, &block)
  each (file) do |line|
    if /Filename: (.*)/ =~ line
      yield $1
    end
  end
end

def each_sourcefile (file, &block)
  dir = nil
  each (file) do |line|
    case line
    when /^Directory: (.*)$/
      dir = $1
    when /^ \S+ \d+ (\S+)$/
      yield dir + "/" + $1
    end
  end
end

def calc_relpath (source, dest)

  pwd = Dir::pwd

  Dir::chdir source
  source = Dir::pwd
  Dir::chdir pwd
  Dir::chdir dest
  dest = Dir::pwd
  Dir::chdir pwd

  src_ary = source.split("/")
  src_ary.shift
  dest_ary = dest.split("/")
  dest_ary.shift

  return dest if src_ary[0] != dest_ary[0]

  src_ary.clone.each_index do |i|
    break if src_ary[0] != dest_ary[0]
    src_ary.shift
    dest_ary.shift
  end

  src_ary.size.times do |i|
    dest_ary.unshift("..")
  end

  dest_ary.join("/")

end

def do_copy(path)
  if $link
    pwd=calc_relpath(File.dirname($dest_dir + "/" + path), $source_dir)
    File.symlink(pwd + "/" + path, $dest_dir + "/" + path)
  else
    File.copy($source_dir + "/" + path, $dest_dir + "/" + path)
  end
end

def copy(path)

  s=$source_dir + "/" + path
  d=$dest_dir + "/" + path

  if FileTest.exist?(d)
    $stats["ignore"] += 1
    return
  end
  if FileTest.exist?(s)
    File.mkpath(File.dirname(d))
    do_copy(path)
    $stats["copy"] += 1
  else
    $stats["notfound"] += 1
    $stderr.puts s + " not found."
  end
end

opts = GetoptLong.new(["--symlink", "-l", GetoptLong::NO_ARGUMENT],
		      ["--help", "-h", GetoptLong::NO_ARGUMENT])

opts.each do |opt,arg|
  case opt
  when "--symlink"
    $link = true
  when "--help"
    usage
  end
end

usage if ARGV.size != 2

$source_dir = ARGV.shift
$dest_dir = ARGV.shift

if $link
  $source_dir = Dir::pwd + "/" + $source_dir unless $source_dir =~ /\A\//
  $dest_dir = Dir::pwd + "/" + $dest_dir unless $dest_dir =~ /\A\//
end

$stats = {}
$stats["ignore"] = 0
$stats["copy"] = 0
$stats["notfound"] = 0

open("|find #{$dest_dir}/dists -name Packages.gz") do |o|
  o.each_line do |file|
    file.chomp!
    print "Processing #{file}... "
    $stdout.flush
    each_file(file) do |path|
      copy(path)
    end
    puts "done"
  end
end
open("|find #{$dest_dir}/dists -name Sources.gz") do |o|
  o.each_line do |file|
    file.chomp!
    print "Processing #{file}... "
    $stdout.flush
    each_sourcefile (file.chomp) do |path|
      copy(path)
    end
    puts "done"
  end
end

puts "Number of Copied Files: " + $stats["copy"].to_s
puts "Number of Ignored Files: " + $stats["ignore"].to_s
puts "Number of Non-existence File: " + $stats["notfound"].to_s