-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_upload.rb
executable file
·97 lines (75 loc) · 2.75 KB
/
github_upload.rb
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
#!/usr/bin/env ruby1.9.1
require 'rubygems'
gem 'github_api', '>= 0.4.10'
require 'github_api'
require 'yaml'
require 'erubis'
config = YAML.load_file("github_upload.conf")
localdir = config["localdir"]
repoowner = config["repo-owner"]
repo = config["repo-name"]
@github = Github.new :login => config["login"], :password => config["password"]
# Upload & delete files on Github
puts "*** Building file lists from remote repository and local directory"
remotelisting = @github.repos.downloads repoowner, repo
localfiles = Dir.entries(localdir).delete_if { |f| f[0] == '.' }
remotefiles = Hash.new
remotelisting.each { |f| remotefiles[f[:name]] = f[:id] }
to_delete = remotefiles.keys - localfiles
to_upload = localfiles - remotefiles.keys
to_delete.each do |file|
puts "*** Deleting from github: " + file
@github.repos.delete_download repoowner, repo, remotefiles[file]
end
to_upload.each do |filename|
puts "*** Uploading to github: " + filename
localfilename = [localdir, filename].join('/')
filesize = File.new(localfilename).size.to_i
resource = @github.repos.create_download repoowner, repo,
"name" => filename,
"size" => filesize
begin
@github.repos.upload resource, localfilename
rescue Exception => e
puts "*** Failed to upload: " + filename
puts e
@github.repos.delete_download repoowner, repo, resource.id
end
end
# Update the index.html
# Build a list of available versions based on filenames
versions = localfiles.map { |f| f.split(/\.tar/)[0] }
versions.uniq!
downloadbaseurl = "https://github.com/downloads/%s/%s/" % [ repoowner, repo ]
file_listing = Array.new
versions.each do |version|
entry = Hash[
:version => version,
:mtime => File.mtime("%s/%s.tar.gz" % [localdir, version]),
:gzlink => "%s%s.tar.gz" % [downloadbaseurl, version],
:gzsig => "%s%s.tar.gz.asc" % [downloadbaseurl, version],
:gzmd5 => "%s%s.tar.gz.md5" % [downloadbaseurl, version],
]
if localfiles.include?("%s.tar.bz2" % [version])
entry[:bz2link] = "%s%s.tar.bz2" % [downloadbaseurl, version]
entry[:bz2sig] = "%s%s.tar.bz2.asc" % [downloadbaseurl, version]
entry[:bz2md5] = "%s%s.tar.bz2.md5" % [downloadbaseurl, version]
end
file_listing.push entry
end
file_listing.sort! {|x,y| y[:mtime] <=> x[:mtime]}
puts "*** Generating index.html"
erb = Erubis::Eruby.new(File.read('index.html.erb'))
File.open('index.html', 'w') { |f|
f.write(erb.result(:downloads => file_listing))
}
changed = `git status --porcelain index.html`
if changed.strip.length > 0
puts "*** Pushing index.html to github"
cmd = "git commit -q index.html -m \"index.html auto-update: %s\"" % [ Time.now() ]
system(cmd)
cmd = "git push -q https://%s:%[email protected]/%s/%s.git" % [ config["login"], config["password"], repoowner, repo ]
system(cmd)
else
puts "*** index.html unchanged"
end