-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.rb
More file actions
173 lines (149 loc) · 4.66 KB
/
app.rb
File metadata and controls
173 lines (149 loc) · 4.66 KB
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
$LOAD_PATH << __dir__
require 'sinatra'
# require 'sinatra/json'
require 'slim'
require 'securerandom'
# require 'lib/list' # Commented out while remote SPARQL server is unavailable
require 'logger'
$logger = Logger.new(STDOUT)
$logger.level = Logger::DEBUG
configure {
set :server, :puma
set :show_exceptions, true
set :environment, :development
set :logging, :true
}
class CommandError < StandardError
end
def system_log(cmd)
$logger.debug("Invoking: #{cmd}\n")
result = `#{cmd} 2>&1`
status = $?.exitstatus
result2 = result.force_encoding('utf-8')
$logger.debug(result2)
if status!=0
raise CommandError, "Failed to run command: "+result2
end
end
# Run tests on startup
system_log("cp -vau /bhxiv-gen-pdf/example /tmp/")
system_log("cd /bhxiv-gen-pdf && ruby ./bin/gen-pdf --debug /tmp/example/logic")
class BHXIV < Sinatra::Base
helpers do
def create_workdir(id)
workdir = "/tmp/#{id}"
FileUtils.mkdir(workdir)
workdir
end
def stage_zipfile(id, zipfile)
workdir = create_workdir(id)
filepath = zipfile[:tempfile].path
system_log("unzip #{filepath} -d #{workdir}")
end
def stage_gitrepo(id, git_url)
workdir = create_workdir(id)
system_log("git clone --depth 1 -c core.askPass=echo #{git_url} #{workdir}/#{File.basename(git_url)}")
end
def create_outdir(id)
outdir_path = File.dirname(__FILE__) + "/public/papers/#{id}"
FileUtils.mkdir_p(outdir_path)
outdir_path
end
def gen_pdf(id, git_url = nil)
# Find paper.md
glob = "/tmp/#{id}/**/paper.md"
$logger.debug(glob)
files = Dir.glob(glob)
if files.size < 1
raise CommandError, "Can not find a paper.md in directory structure!"
end
paper_dir = File.dirname(files.first)
# Prepare output dir
outdir = create_outdir(id)
pdf_path = "#{outdir}/paper.pdf"
# Generate
system_log("gen-pdf #{paper_dir} #{pdf_path}")
# Return pdf_path "/papers/#{id}/paper.pdf"
"/papers/#{id}/paper.pdf"
end
end
configure do
set :root, File.dirname(__FILE__)
set :protection, :except => [:json_csrf]
# Commented out while remote SPARQL server is unavailable
# set :events, BHXIVUtils::PaperList.biohackathon_events()
# set :all_papers, BHXIVUtils::PaperList.all_papers(settings.events)
# set :all_papers_expanded, BHXIVUtils::PaperList.expand_authors(settings.all_papers)
# set :count_events, settings.events.length
# set :count_papers, settings.all_papers.map { |k,v| v }.flatten.length
# set :count_authors, BHXIVUtils::PaperList.count_authors()
# Temporary fallback values
set :events, {}
set :all_papers, {}
set :all_papers_expanded, {}
set :count_events, 0
set :count_papers, 0
set :count_authors, 0
end
error CommandError do
# 'Sorry there was a nasty error - ' + env['sinatra.error'].message
@error_msg = env['sinatra.error'].message
slim :error
end
error do
'Server error: ' + env['sinatra.error'].message
end
get '/' do
# Commented out while remote SPARQL server is unavailable
# @biohackathons = settings.events
# @papers = settings.all_papers
# @count_events = settings.count_events
# @count_papers = settings.count_papers
# @count_authors = settings.count_authors
# Temporary fallback values
@biohackathons = {}
@papers = {}
@count_events = 0
@count_papers = 0
@count_authors = 0
slim :index
end
post '/gen-pdf' do
# Get form parameters
$logger.debug(params)
git_url = params[:repository]
zipfile = params[:zipfile]
pdf_path = if zipfile || git_url
id = SecureRandom.uuid
if zipfile
stage_zipfile(id, zipfile)
gen_pdf(id)
elsif git_url
stage_gitrepo(id, git_url)
gen_pdf(id, git_url)
end
end
if pdf_path
content_type 'application/pdf' # not sure this works before a redirect, but it does not hurt
redirect pdf_path
else
status 500
end
end
# Commented out while remote SPARQL server is unavailable
# get '/list.json' do
# content_type :json
# h = BHXIVUtils::PaperList.to_h(settings.events, settings.all_papers_expanded)
# JSON(h)
# end
# get '/list' do
# @biohackathons = settings.events
# @papers = settings.all_papers
# # expand authors (we could have done this more lazily)
# @papers = settings.all_papers_expanded
# @count_events = settings.count_events
# @count_papers = settings.count_papers
# @count_authors = settings.count_authors
# slim :list
# end
end