Debian Codegraph @

extra/haxelib_src/src/haxelib/server/Repo.hx

raw
1 /*
2 * Copyright (C)2005-2016 Haxe Foundation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22 package haxelib.server;
23
24 import haxe.io.*;
25 import neko.Web;
26 import sys.io.*;
27 import sys.*;
28
29 import haxelib.Data;
30 import haxelib.SemVer;
31 import haxelib.server.Paths.*;
32 import haxelib.server.SiteDb;
33 import haxelib.server.FileStorage;
34
35 class Repo implements SiteApi {
36
37 static function run() {
38 FileSystem.createDirectory(TMP_DIR);
39
40 var ctx = new haxe.remoting.Context();
41 ctx.addObject("api", new Repo());
42
43 if( haxe.remoting.HttpConnection.handleRequest(ctx) )
44 return;
45 else
46 throw "Invalid remoting call";
47 }
48
49 public function new() {}
50
51 public function search( word : String ) : List<{ id : Int, name : String }> {
52 return Project.containing(word);
53 }
54
55 public function infos( project : String ) : ProjectInfos {
56 var p = Project.manager.select($name == project);
57 if( p == null )
58 throw "No such Project : "+project;
59 var vl = Version.manager.search($project == p.id);
60
61 var sumDownloads = function(version:Version, total:Int) return total += version.downloads;
62 var totalDownloads = Lambda.fold(vl, sumDownloads, 0);
63
64 return {
65 name : p.name,
66 curversion : if( p.versionObj == null ) null else p.versionObj.toSemver(),
67 desc : p.description,
68 versions:
69 [for ( v in vl ) {
70 name : v.toSemver(),
71 comments : v.comments,
72 downloads : v.downloads,
73 date : v.date
74 }],
75 owner : p.ownerObj.name,
76 contributors : [for (d in Developer.manager.search( $project==p.id)) {name: d.userObj.name, fullname: d.userObj.fullname}],
77 website : p.website,
78 license : p.license,
79 downloads : totalDownloads,
80 tags : Tag.manager.search($project == p.id).map(function(t) return t.tag),
81 };
82 }
83
84 public function getLatestVersion( project : String ) : SemVer {
85 var p = Project.manager.select($name == project);
86 if( p == null )
87 throw "No such Project : "+project;
88
89 var vl = Version.manager.unsafeObjects('SELECT * FROM Version WHERE project = ${p.id} ORDER BY major DESC, minor DESC, patch DESC, ifnull(preview, 100) DESC, previewNum DESC LIMIT 1', false);
90 return vl.first().toSemver();
91 }
92
93 public function user( name : String ) : UserInfos {
94 var u = User.manager.search($name == name).first();
95 if( u == null )
96 throw "No such user : "+name;
97 var pl = Project.manager.search($owner == u.id);
98 var projects = new Array();
99 for( p in pl )
100 projects.push(p.name);
101 return {
102 name : u.name,
103 fullname : u.fullname,
104 email : u.email,
105 projects : projects,
106 };
107 }
108
109 public function register( name : String, pass : String, mail : String, fullname : String ) : Void {
110 if( name.length < 3 )
111 throw "User name must be at least 3 characters";
112 if( !Data.alphanum.match(name) )
113 throw "Invalid user name, please use alphanumeric characters";
114 if( User.manager.count($name == name) > 0 )
115 throw 'User name "$name" is already taken';
116 var u = new User();
117 u.name = name;
118 u.pass = pass;
119 u.email = mail;
120 u.fullname = fullname;
121 u.insert();
122 }
123
124 public function isNewUser( name : String ) : Bool {
125 return User.manager.select($name == name) == null;
126 }
127
128 public function checkDeveloper( prj : String, user : String ) : Void {
129 var p = Project.manager.search({ name : prj }).first();
130 if( p == null )
131 return;
132 for( d in Developer.manager.search({ project : p.id }) )
133 if( d.userObj.name == user )
134 return;
135 throw "User '"+user+"' is not a developer of project '"+prj+"'";
136 }
137
138 public function checkPassword( user : String, pass : String ) : Bool {
139 var u = User.manager.search({ name : user }).first();
140 return u != null && u.pass == pass;
141 }
142
143 public function getSubmitId() : String {
144 return Std.string(Std.random(100000000));
145 }
146
147 public function processSubmit( id : String, user : String, pass : String ) : String {
148 var tmpFile = Path.join([TMP_DIR_NAME, Std.parseInt(id)+".tmp"]);
149 return FileStorage.instance.readFile(
150 tmpFile,
151 function(path):String {
152 var file = try sys.io.File.read(path,true) catch( e : Dynamic ) throw "Invalid file id #"+id;
153 var zip = try haxe.zip.Reader.readZip(file) catch( e : Dynamic ) { file.close(); neko.Lib.rethrow(e); };
154 file.close();
155
156 var infos = Data.readInfos(zip,true);
157 var u = User.manager.search({ name : user }).first();
158 if( u == null || u.pass != pass )
159 throw "Invalid username or password";
160
161 var devs = infos.contributors.map(function(user) {
162 var u = User.manager.search({ name : user }).first();
163 if( u == null )
164 throw "Unknown user '"+user+"'";
165 return u;
166 });
167
168 var tags = Lambda.array(infos.tags);
169 tags.sort(Reflect.compare);
170
171 var p = Project.manager.search({ name : infos.name }).first();
172
173 // create project if needed
174 if( p == null ) {
175 if( devs[0].name != user ) {
176 throw 'A new project should be submitted by its owner, which is the first user in the contributors field (${devs[0].name})';
177 }
178
179 p = new Project();
180 p.name = infos.name;
181 p.description = infos.description;
182 p.website = infos.url;
183 p.license = infos.license;
184 p.ownerObj = u;
185 p.insert();
186 for( u in devs ) {
187 var d = new Developer();
188 d.userObj = u;
189 d.projectObj = p;
190 d.insert();
191 }
192 for( tag in tags ) {
193 var t = new Tag();
194 t.tag = tag;
195 t.projectObj = p;
196 t.insert();
197 }
198 }
199
200 // check submit rights
201 var pdevs = Developer.manager.search({ project : p.id });
202 var isdev = false;
203 for( d in pdevs )
204 if( d.userObj.id == u.id ) {
205 isdev = true;
206 break;
207 }
208 if( !isdev )
209 throw "You are not a developer of this project";
210
211 var otags = Tag.manager.search({ project : p.id });
212 var curtags = otags.map(function(t) return t.tag).join(":");
213
214 var ownerChanged = devs[0].id != p.ownerObj.id;
215 var devsChanged = (pdevs.length != devs.length);
216 if (!devsChanged) { // same length, check whether elements are the same
217 for (d in pdevs) {
218 var exists = Lambda.exists(devs, function(u) return u.id == d.userObj.id);
219 if (!exists) {
220 devsChanged = true;
221 break;
222 }
223 }
224 }
225
226 // update public infos
227 if( ownerChanged || devsChanged || infos.description != p.description || p.website != infos.url || p.license != infos.license || tags.join(":") != curtags ) {
228 if( u.id != p.ownerObj.id )
229 throw "Only project owner can modify project infos";
230 p.description = infos.description;
231 p.website = infos.url;
232 p.license = infos.license;
233 if( ownerChanged ) {
234 p.ownerObj = devs[0];
235 }
236 p.update();
237 if( devsChanged ) {
238 for( d in pdevs )
239 d.delete();
240 for( u in devs ) {
241 var d = new Developer();
242 d.userObj = u;
243 d.projectObj = p;
244 d.insert();
245 }
246 }
247 if( tags.join(":") != curtags ) {
248 for( t in otags )
249 t.delete();
250 for( tag in tags ) {
251 var t = new Tag();
252 t.tag = tag;
253 t.projectObj = p;
254 t.insert();
255 }
256 }
257 }
258
259 // look for current version
260 var current = null;
261 for( v in Version.manager.search({ project : p.id }) )
262 if( v.name == infos.version ) {
263 current = v;
264 break;
265 }
266
267 // update documentation
268 var doc = null;
269 var docXML = Data.readDoc(zip);
270 if( docXML != null ) {
271 try {
272 var p = new haxe.rtti.XmlParser();
273 p.process(Xml.parse(docXML).firstElement(),null);
274 p.sort();
275 var roots = new Array();
276 for( x in p.root )
277 switch( x ) {
278 case TPackage(name,_,_):
279 switch( name ) {
280 case "flash","sys","cs","java","haxe","js","neko","cpp","php","python": // don't include haXe core types
281 default: roots.push(x);
282 }
283 default:
284 // don't include haXe root types
285 }
286 var s = new haxe.Serializer();
287 s.useEnumIndex = true;
288 s.useCache = true;
289 s.serialize(roots);
290 doc = s.toString();
291 } catch ( e:Dynamic ) {
292 // If documentation can't be generated, ignore it.
293 }
294 }
295
296 // update file
297 var fileName = Data.fileName(p.name, infos.version);
298 var storage = FileStorage.instance;
299 storage.importFile(path, Path.join([Paths.REP_DIR_NAME, fileName]), true);
300 storage.deleteFile(tmpFile);
301
302 var semVer = SemVer.ofString(infos.version);
303
304 // update existing version
305 if( current != null ) {
306 current.documentation = doc;
307 current.comments = infos.releasenote;
308 current.update();
309 return "Version "+current.name+" (id#"+current.id+") updated";
310 }
311
312 // add new version
313 var v = new Version();
314 v.projectObj = p;
315 v.major = semVer.major;
316 v.minor = semVer.minor;
317 v.patch = semVer.patch;
318 v.preview = semVer.preview;
319 v.previewNum = semVer.previewNum;
320
321 v.comments = infos.releasenote;
322 v.downloads = 0;
323 v.date = Date.now().toString();
324 v.documentation = doc;
325 v.insert();
326
327 // p.versionObj is the one shown on the website
328 if (p.versionObj == null || p.versionObj.toSemver() < v.toSemver()) {
329 p.versionObj = v;
330 }
331
332 p.update();
333 return "Version " + v.toSemver() + " (id#" + v.id + ") added";
334 }
335 );
336 }
337
338 public function postInstall( project : String, version : String ) {
339 var p = Project.manager.select($name == project);
340 if( p == null )
341 throw "No such Project : " + project;
342
343 var version = SemVer.ofString(version);
344 // don't use macro select because of
345 // https://github.com/HaxeFoundation/haxe/issues/4931
346 // and https://github.com/HaxeFoundation/haxe/issues/4932
347 var v = Version.manager.dynamicSearch({
348 project: p.id,
349 major: version.major,
350 minor: version.minor,
351 patch: version.patch,
352 preview: if (version.preview == null) null else version.preview.getIndex(),
353 previewNum: version.previewNum
354 }).first();
355
356 if( v == null )
357 throw "No such Version : " + version;
358 v.downloads++;
359 v.update();
360 p.downloads++;
361 p.update();
362 }
363
364 static function main() {
365 var error = null;
366 SiteDb.init();
367 try {
368 run();
369 } catch( e : Dynamic ) {
370 error = { e : e };
371 }
372 SiteDb.cleanup();
373 if( error != null )
374 neko.Lib.rethrow(error.e);
375 }
376
377 }