This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fserver.drush.inc
868 lines (787 loc) · 27.8 KB
/
fserver.drush.inc
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
<?php
// $Id$
/**
@TODO:
(10:38:30 AM) Adrian Rossouw: if you need drush to be able to modify the files dir
(10:38:47 AM) Adrian Rossouw: you need to add : umask(0002) to your settings.php
(10:38:53 AM) Adrian Rossouw: drupal 6.x has a crazy bug in it
(10:39:10 AM) Adrian Rossouw: that all uploaded files are 600 , owned by the web server user
(10:39:15 AM) Adrian Rossouw: so your drush script can't modify it
(10:39:20 AM) Adrian Rossouw: or change the perms, or anythin
*/
/**
* Implementation of hook_drush_command().
*
* @See drush_parse_command() for a list of recognized keys.
*
* @return
* An associative array describing your command(s).
*/
function fserver_drush_command() {
$items = array();
$items['fserver-status'] = array(
'description' => 'Determine the packaging status of a project.',
'arguments' => array(
'project' => 'Machine name of project to display packaging status for, optional, default all projects.',
),
'examples' => array(
'drush fserver-status' => 'Display packaging status for all projects.',
'drush fserver-status fserver' => 'Display packaging status for project fserver.',
),
);
$items['fserver-package'] = array(
'description' => 'Update packaging for a project.',
'options' => array(
'user' => "User or uid for release node uid. @see also 'drush topic core-global-options'",
),
'arguments' => array(
'project' => 'Machine name of project to create a release for, optional, default all projects.',
),
'examples' => array(
'drush fserver-package fserver' => 'Create release node for project fserver.',
'drush fserver-package --user=bot.marvin' => 'Create release node for all projects and use uid of supplied option as uid for release nodes.',
),
);
$items['fserver-distro'] = array(
'description' => 'Update packaging for a distribution.',
'options' => array(
'version' => 'Version for release, mandatory.',
'user' => "User or uid for release node uid. @see also 'drush topic core-global-options'",
),
'arguments' => array(
'distro' => 'Machine name of distro to create a release for.',
),
'examples' => array(
'drush fserver-distro --version=6.x-1.0 fserver6_profile' => 'Create release node for distro fserver6_profile with version 6.x-1.0.',
),
);
return $items;
}
/**
* Implementation of hook_drush_help().
*/
function fserver_drush_help($section) {
switch ($section) {
case 'drush:fserver-status':
return dt("Determine the packaging status of a project.");
case 'drush:fserver-package':
return dt("Update packaging for a project.");
case 'drush:fserver-distro':
return dt("Package a new Drupal distribution from a distribution node.");
}
}
/**
* Package new releases for a specified project or all.
*/
function drush_fserver_package() {
if (!drush_get_option('uri') && !drush_get_option('l')) {
drush_die('You must use the --uri option to specify the location of this site.');
}
$projects = _fserver_drush_get_projects();
// Filter the project list by the argument.
$args = func_get_args();
$arg = array_shift($args);
if (!empty($arg)) {
if (isset($projects[$arg])) {
$projects = array($arg => $projects[$arg]);
}
else {
drush_die('The project '. $arg .' could not be found.');
}
}
$rows = array();
foreach ($projects as $project) {
$new_tags = $project->get_new_tags();
if (!empty($new_tags)) {
foreach ($new_tags as $tag_id => $tag) {
$release = $project->create_release($tag_id);
$rows[] = array($project->node->title, $tag_id, $release->field_fserver_file[0]['filename']);
}
}
}
if (!empty($rows)) {
array_unshift($rows, array(dt('Project'), dt('Release'), dt('Filename')));
drush_print_table($rows, TRUE);
}
else {
drush_print(dt('All projects are up to date. No packages were created.'));
}
}
/**
* Display project packaging status.
*/
function drush_fserver_status() {
$projects = _fserver_drush_get_projects();
// Filter the project list by the argument.
$args = func_get_args();
$arg = array_shift($args);
if (!empty($arg)) {
if (isset($projects[$arg])) {
$projects = array($arg => $projects[$arg]);
}
else {
drush_die('The project '. $arg .' could not be found.');
}
}
$rows = array(array(dt('Method'), dt('Project'), dt('New tags')));
foreach ($projects as $project) {
$new_tags = $project->get_new_tags();
$rows[] = array(
$project->method,
$project->node->title,
!empty($new_tags) ? implode(', ',array_keys($new_tags)) : dt("Up to date"),
);
}
drush_print_table($rows, TRUE);
}
/**
* Retrieve all fserver projects from the DB.
*/
function _fserver_drush_get_projects() {
static $projects;
if (!isset($projects)) {
$projects = array();
$result = db_query("SELECT nid, title FROM {node} WHERE type = 'fserver_project' AND status = 1");
while ($row = db_fetch_object($result)) {
$node = node_load($row->nid);
$project = new FserverProject($node, $node->field_fserver_name[0]['value'], $node->field_fserver_method[0]['value'], $node->field_fserver_repository[0]['value']);
$projects[$project->name] = $project;
}
}
return $projects;
}
/**
* Project class. Contains helpful methods for detecting tags, packaging, etc.
*/
class FserverProject {
var $node, $name, $method, $repository;
/**
* We will store the backend object here.
*/
var $backend;
/**
* Constructor.
*/
function __construct($node, $name, $method, $repository) {
$this->node = $node;
$this->name = $name;
$this->method = $method;
$this->repository = $repository;
// We try to create a backend for us to user later:
$method_class = 'FserverProject_' . $this->method;
if (class_exists($method_class)) {
$this->backend = new $method_class($this);
}
}
/**
* Initialize a working copy for this project.
*/
function init_wc() {
if (isset($this->backend)) {
$this->backend->init_wc();
}
}
/**
* Get new tags that have no corresponding release nodes.
*/
function get_new_tags() {
$new_tags = array();
$releases = $this->get_releases();
foreach ($this->get_tags() as $tag_id => $tag) {
if (!isset($releases[$tag_id])) {
$new_tags[$tag_id] = $tag;
}
}
return $new_tags;
}
/**
* Get release nodes for this project.
*/
function get_releases() {
if (!isset($this->releases)) {
$this->releases = array();
$result = db_query("SELECT nid FROM {node} WHERE type = 'fserver_release' AND status = 1");
while ($row = db_fetch_object($result)) {
$node = node_load($row->nid);
if ($node && isset($node->field_fserver_project[0]['nid']) && $node->field_fserver_project[0]['nid'] == $this->node->nid) {
$this->releases[fserver_generate_version($node, TRUE)] = $node;
}
}
}
return $this->releases;
}
function get_tag($tag_id) {
if (isset($this->backend)) {
return $this->backend->get_tag($tag_id);
}
}
/**
* Get tags for this project.
*/
function get_tags() {
if (isset($this->backend)) {
return $this->backend->get_tags();
}
return array();
}
/**
* Create a release node for the given tag.
*/
function create_release($tag_id) {
$tag = $this->get_tag($tag_id);
if ($tag && $file = $this->create_package($tag_id)) {
$node = new stdClass();
$node->type = 'fserver_release';
$node->status = 1;
$node->created = !empty($tag['timestamp']) ? $tag['timestamp'] : time();
// Use uid of supplied option as release node uid
if (drush_get_option(array('u', 'user'))) {
global $user;
$node->uid = $user->uid;
}
else {
$node->uid = $this->node->uid;
}
$node->title = "{$this->name} {$tag_id}";
$node->body = !empty($tag['message']) ? $tag['message'] : '';
$node->field_fserver_file = array(0 => (array) $file);
$node->field_fserver_project = array(0 => array('nid' => $this->node->nid));
$node->field_fserver_api = array(0 => array('value' => $tag['core']));
$node->field_fserver_versionmajor = array(0 => array('value' => $tag['major']));
$node->field_fserver_versionpatch = array(0 => array('value' => $tag['patch']));
$node->field_fserver_versionextra = array(0 => array('value' => $tag['extra']));
// Handle the release extra info
$extratype = FSERVER_EXTRATYPE_RELEASE;
$extranum = NULL;
if (!empty($tag['extra'])) {
$extratypes = fserver_cck_options('extratype');
$extratypes = array_flip($extratypes);
$extratype = array_key_exists($tag['extratype'], $extratypes) ? $extratypes[$tag['extratype']] : FSERVER_EXTRATYPE_OTHER;
$extranum = $tag['extranum'];
}
$node->field_fserver_versionextratype = array(0 => array('value' => $extratype));
$node->field_fserver_versionextranum = array(0 => array('value' => $extranum));
// @TODO
$node->field_fserver_recommended = array(0 => array('value' => 1));
$node->field_fserver_security = array(0 => array('value' => 0));
node_save($node);
}
return $node;
}
/**
* Create an archive package for the given tag.
*/
function create_package($tag_id) {
if (isset($this->backend)) {
return $this->backend->create_package($tag_id);
}
return FALSE;
}
/**
* Write packaging information for a release.
*/
function write_package_info($tag, $exclude_mask = array('.', '..', 'CVS')) {
if (isset($this->backend)) {
$fserver_url = url("fserver", array('purl' => array('disabled' => TRUE), 'absolute' => TRUE));
$packaging = array();
$packaging[] = "; Information added by fserver";
$packaging[] = "core = \"{$tag['core']}\"";
$packaging[] = "datestamp = \"{$tag['timestamp']}\"";
$packaging[] = "project = \"{$this->name}\"";
$packaging[] = "project status url = \"{$fserver_url}\"";
$packaging[] = "version = \"{$tag['version']}\"";
$packaging = "\n" . implode("\n", $packaging);
$files = file_scan_directory($this->backend->path, '.info$', $exclude_mask);
foreach ($files as $path => $file) {
// @TODO: Parse the info file or not?
$info_file = file_get_contents($file->filename);
$info_file .= $packaging;
file_put_contents($file->filename, $info_file);
}
}
}
}
class FserverProject_git {
/**
* We store the parent FserverProject object here.
*/
var $FserverProject;
/**
* Constructor.
*/
function __construct(&$FserverProject) {
$this->FserverProject = $FserverProject;
}
/**
* Destructor.
*/
function __destruct() {
if (isset($this->path)) {
drush_op('drush_shell_exec', "rm -rf {$this->path}");
}
}
/**
* Initialize a working copy for this project.
*/
function init_wc() {
if (!isset($this->path)) {
$this->path = file_directory_temp() . '/fserver_git_'. $this->FserverProject->name .'_'. time();
drush_op('drush_shell_exec', "git clone {$this->FserverProject->repository} {$this->path}");
}
}
/**
* Get tags for this project.
*/
function get_tags() {
if (!isset($this->tags)) {
$this->tags = array();
_drush_shell_exec_output_set(' '); // Clear output cache.
drush_op('drush_shell_exec', "git ls-remote --tags {$this->FserverProject->repository} | awk '{print $2;}'");
$lines = drush_shell_exec_output();
while ($line = array_shift($lines)) {
$vc = array_pop(explode('/', $line));
$parsed = fserver_parse_tag_name($vc);
if (!empty($parsed)) {
$this->tags[$parsed['version']] = $vc;
}
}
}
return $this->tags;
}
function get_tag($tag_id) {
$tags = $this->get_tags();
if (isset($tags[$tag_id])) {
$vc = $tags[$tag_id];
$tag = fserver_parse_tag_name($vc);
$this->init_wc();
// Retrieve tag info
_drush_shell_exec_output_set(' '); // Clear output cache.
drush_op('drush_shell_exec', "git --git-dir={$this->path}/.git log -1 --pretty=format:%ct {$vc}");
$info = drush_shell_exec_output();
$tag['timestamp'] = $info[0];
drush_op('drush_shell_exec', "git --git-dir={$this->path}/.git tag -l {$vc} -n 1000");
$info = drush_shell_exec_output();
// Remove the tag from the first line of the message:
$info[0] = str_replace($vc, '', $info[0]);
$tag['message'] = implode("\n", $info);
return $tag;
}
}
/**
* Create an archive package for the given tag.
*/
function create_package($tag_id) {
$this->init_wc();
$tag = $this->get_tag($tag_id);
if ($tag) {
$base = "{$this->FserverProject->name}-{$tag_id}";
$file_path = file_directory_path() .'/fserver';
// Create the directory if it doesn't exist.
if (file_check_directory($file_path, TRUE)) {
$vc = $tag['original'];
drush_op('drush_shell_exec', "git --git-dir={$this->path}/.git checkout {$vc}");
$this->FserverProject->write_package_info($tag, array('.git'));
drush_op('drush_shell_exec', "tar -Pcf - --exclude='.git' --transform='s,{$this->path},{$this->FserverProject->name},' {$this->path} | gzip > {$file_path}/{$base}.tgz");
// @TODO
// We need to properly chown and permission these directories.
// Is it possible for drush to know the web server user?
drush_op('chmod', $file_path, 0775);
drush_op('chgrp', $file_path, fserver_posix_groupname());
drush_op('chmod', "{$file_path}/{$base}.tgz", 0775);
drush_op('chgrp', "{$file_path}/{$base}.tgz", fserver_posix_groupname());
$file = new stdClass();
// Use uid of supplied option as release file uid
if (drush_get_option(array('u', 'user'))) {
global $user;
$file->uid = $user->uid;
}
else {
$file->uid = $this->node->uid;
}
$file->filename = "{$base}.tgz";
$file->filepath = "{$file_path}/{$base}.tgz";
$file->filemime = file_get_mimetype($file->filename);
$file->filesize = filesize($file->filepath);
$file->status = FILE_STATUS_PERMANENT;
$file->timestamp = time();
drupal_write_record('files', $file);
return $file;
}
}
return FALSE;
}
}
class FserverProject_svn {
/**
* We store the parent FserverProject object here.
*/
var $FserverProject;
/**
* Constructor.
*/
function __construct(&$FserverProject) {
$this->FserverProject = $FserverProject;
}
/**
* Destructor.
*/
function __destruct() {
if (isset($this->path)) {
drush_op('drush_shell_exec', "rm -rf {$this->path}");
}
}
/**
* Initialize a working copy for this project.
*/
function init_wc() {
if (!isset($this->path)) {
$this->path = file_directory_temp() . '/fserver_svn_'. $this->FserverProject->name .'_'. time();
//drush_op('drush_shell_exec', "git clone {$this->FserverProject->repository} {$this->path}");
}
}
/**
* Get tags for this project.
*/
function get_tags() {
if (!isset($this->tags)) {
$this->tags = array();
_drush_shell_exec_output_set(' '); // Clear output cache.
drush_op('drush_shell_exec', "svn ls {$this->FserverProject->repository}/tags | awk '{print $1;}'");
$lines = drush_shell_exec_output();
while ($line = array_shift($lines)) {
$vc = array_shift(explode('/', $line));
$parsed = fserver_parse_tag_name($vc);
if (!empty($parsed)) {
$this->tags[$parsed['version']] = $vc;
}
}
}
return $this->tags;
}
function get_tag($tag_id) {
$tags = $this->get_tags();
if (isset($tags[$tag_id])) {
$vc = $tags[$tag_id];
$tag = fserver_parse_tag_name($vc);
$this->init_wc();
// Retrieve tag info
_drush_shell_exec_output_set(' '); // Clear output cache.
drush_op('drush_shell_exec', "svn info --non-interactive --xml {$this->FserverProject->repository}/tags/{$vc}");
$info = drush_shell_exec_output();
if (!empty($info)) {
$info = implode("\n", $info);
// $info is now an xml string:
$info_xml = simplexml_load_string($info);
foreach ($info_xml->xpath('//commit/date') as $date_element) {
$date = strtotime((string)$date_element);
}
}
$tag['timestamp'] = is_numeric($date) ? $date : time();
return $tag;
}
}
/**
* Create an archive package for the given tag.
*/
function create_package($tag_id) {
$this->init_wc();
$tag = $this->get_tag($tag_id);
if ($tag) {
$base = "{$this->FserverProject->name}-{$tag_id}";
$file_path = file_directory_path() .'/fserver';
// Create the directory if it doesn't exist.
if (file_check_directory($file_path, TRUE)) {
$vc = $tag['original'];
if (is_dir($this->path)) {
// I'd quite like to do a switch here, but couldn't get it to work reliably.
drush_op('drush_shell_exec', "rm -rf {$this->path}");
}
drush_op('drush_shell_exec', "svn checkout --non-interactive {$this->FserverProject->repository}/tags/{$vc} {$this->path}");
$this->FserverProject->write_package_info($tag, array('.svn'));
drush_op('drush_shell_exec', "tar -Pcf - --exclude='.svn' --transform='s,{$this->path},{$this->FserverProject->name},' {$this->path} | gzip > {$file_path}/{$base}.tgz");
// @TODO
// We need to properly chown and permission these directories.
// Is it possible for drush to know the web server user?
drush_op('chmod', $file_path, 0775);
drush_op('chgrp', $file_path, fserver_posix_groupname());
drush_op('chmod', "{$file_path}/{$base}.tgz", 0775);
drush_op('chgrp', "{$file_path}/{$base}.tgz", fserver_posix_groupname());
$file = new stdClass();
$file->uid = $this->node->uid;
$file->filename = "{$base}.tgz";
$file->filepath = "{$file_path}/{$base}.tgz";
$file->filemime = file_get_mimetype($file->filename);
$file->filesize = filesize($file->filepath);
$file->status = FILE_STATUS_PERMANENT;
$file->timestamp = time();
drupal_write_record('files', $file);
return $file;
}
}
return FALSE;
}
}
/**
* Discover the web server group. Taken from provision.
*/
function fserver_posix_groupname() {
$info = posix_getgrgid(posix_getgid());
$common_groups = array(
'httpd',
'www-data',
'apache',
'nogroup',
'nobody',
$info['name']);
foreach ($common_groups as $group) {
$groupname = '';
if (is_numeric($group)) {
$info = posix_getgrgid($group);
$groupname = $info['name'];
}
else {
$info = posix_getgrnam($group);
$groupname = $info['name'];
}
if ($groupname) {
return $groupname;
break;
}
}
return NULL;
}
function drush_fserver_distro() {
if (!drush_get_option('version')) {
drush_die('No version supplied, please use --version to specify a version for your release.');
}
$distros = _fserver_drush_get_distros();
// Filter the project list by the argument.
$args = func_get_args();
$arg = array_shift($args);
$version = drush_get_option('version');
if (!empty($arg)) {
if (isset($distros[$arg])) {
$distros = array($arg => $distros[$arg]);
}
else {
drush_die('The distribution '. $arg .' could not be found.');
}
}
$rows = array();
foreach ($distros as $distro) {
$release = $distro->create_release($version);
$rows[] = array($distro->node->title, $release->field_fserver_file[0]['filename']);
}
if (!empty($rows)) {
array_unshift($rows, array(dt('Project'), dt('Filename')));
drush_print_table($rows, TRUE);
}
else {
drush_print(dt('No distributions to update.'));
}
}
function _fserver_drush_get_distros() {
static $distros;
if (!isset($distros)) {
$projects = array();
$result = db_query("SELECT nid, title FROM {node} WHERE type = 'fserver_distro' AND status = 1");
while ($row = db_fetch_object($result)) {
$node = node_load($row->nid);
$distro = new FserverDistro($node, $node->field_fserver_name[0]['value']);
$distros[$distro->name] = $distro;
}
}
return $distros;
}
/**
* Project class. Contains helpful methods for detecting tags, packaging, etc.
*/
class FserverDistro {
var $node, $name;
/**
* Constructor.
*/
function __construct($node, $name, $version = NULL) {
$this->node = $node;
$this->name = $name;
//$this->version = $version;
}
/**
* Destructor.
*/
function __destruct() {
if (isset($this->path)) {
drush_op('drush_shell_exec', "rm -rf {$this->path}");
}
}
/**
* Initialize a working copy for this project.
*/
function init_wc() {
if (!isset($this->raw_version)) {
drush_die('Version was not parsed properly. Please supply versions in the drupal-6--1-0-alpha1 format.');
}
if (!isset($this->path)) {
$this->path = file_directory_temp() . '/fserver_'. $this->name .'_'. time();
if (file_check_directory($this->path, TRUE)) {
if(!file_save_data($this->node->field_fserver_drushmake[0]['value'], $this->path .'/'. $this->name .'-'. $this->raw_version .'.make', FILE_EXISTS_REPLACE)) {
return FALSE;
}
// @TODO
// We need to properly chown and permission these directories.
// Is it possible for drush to know the web server user?
drush_op('chmod', $this->path, 0775);
drush_op('chgrp', $this->path, fserver_posix_groupname());
drush_op('chmod', "{$this->path}/{$this->name}-{$this->raw_version}.make", 0775);
drush_op('chgrp', "{$this->path}/{$this->name}-{$this->raw_version}.make", fserver_posix_groupname());
}
}
return TRUE;
}
/**
* Get release nodes for this project.
*/
function get_releases() {
if (!isset($this->releases)) {
$this->releases = array();
$result = db_query("SELECT nid FROM {node} WHERE type = 'fserver_release' AND status = 1");
while ($row = db_fetch_object($result)) {
$node = node_load($row->nid);
if ($node && isset($node->field_fserver_project[0]['nid']) && $node->field_fserver_project[0]['nid'] == $this->node->nid) {
$this->releases[fserver_generate_version($node, TRUE)] = $node;
}
}
}
return $this->releases;
}
function parse_version($version) {
if (isset($version)) {
$this->version = array();
$parsed = fserver_parse_tag_name($version);
if (!empty($parsed)) {
$extra = $parsed['extra'] ? $parsed['extra'] : 0;
$view = views_get_view('fserver_release_lookup');
$view->set_display('default');
$args = array();
$args[] = $this->node->nid;
$args[] = $parsed['core'];
$args[] = $extra;
$view->set_arguments($args);
$preview = $view->preview($form_state['display_id'], $args);
$query = db_prefix_tables($view->build_info['query']);
_db_query_callback($view->build_info['query_args'], TRUE);
$query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
// Next line required because views passes 0 as "IS NULL" to the query, and we actually want 0, so it's been hacked in manually
$query .= " AND (node_data_field_fserver_project.field_fserver_versionmajor_value = %d) AND (node_data_field_fserver_api.field_fserver_versionpatch_value = %d)";
$items = db_query($query, $parsed['major'], $parsed['patch']);
while ($result = db_fetch_object($items)) {
$results[] = $result->nid;
}
if (!$results[0]) {
$this->version = $parsed;
$this->raw_version = $parsed['version'];
}
else {
return FALSE;
}
}
else {
return FALSE;
}
}
return $this->version;
}
/**
* Create a release node for the given tag.
*/
function create_release($version = NULL) {
$version = $this->parse_version($version);
if ($file = $this->create_package()) {
//if ($version) {
$node = new stdClass();
$node->type = 'fserver_release';
$node->status = 1;
$node->created = !empty($tag['timestamp']) ? $tag['timestamp'] : time();
// Use uid of supplied option as release node uid
if (drush_get_option(array('u', 'user'))) {
global $user;
$node->uid = $user->uid;
}
else {
$node->uid = $this->node->uid;
}
$node->title = "{$this->name} {$tag_id}";
$node->body = !empty($tag['message']) ? $tag['message'] : '';
$node->field_fserver_file = array(0 => (array) $file);
$node->field_fserver_project = array(0 => array('nid' => $this->node->nid));
$node->field_fserver_api = array(0 => array('value' => $version['core']));
$node->field_fserver_versionmajor = array(0 => array('value' => $version['major']));
$node->field_fserver_versionpatch = array(0 => array('value' => $version['patch']));
$node->field_fserver_versionextra = array(0 => array('value' => $version['extra']));
// Handle the release extra info
$extratype = FSERVER_EXTRATYPE_RELEASE;
$extranum = NULL;
if (!empty($version['extra'])) {
$extratypes = fserver_cck_options('extratype');
$extratypes = array_flip($extratypes);
$extratype = array_key_exists($tag['extratype'], $extratypes) ? $extratypes[$version['extratype']] : FSERVER_EXTRATYPE_OTHER;
$extranum = $version['extranum'];
}
$node->field_fserver_versionextratype = array(0 => array('value' => $extratype));
$node->field_fserver_versionextranum = array(0 => array('value' => $extranum));
// @TODO
$node->field_fserver_recommended = array(0 => array('value' => 1));
$node->field_fserver_security = array(0 => array('value' => 0));
node_save($node);
}
return $node;
}
/**
* Create an archive package for the given tag.
*/
function create_package() {
if (!$this->init_wc()) {
return FALSE;
}
$base = "{$this->name}-{$this->raw_version}";
$file_path = file_directory_path() .'/fserver';
// Create the directory if it doesn't exist.
if (file_check_directory($file_path, TRUE)) {
$drush_info = drush_read_drush_info();
if ($drush_info['drush_version'] >= 5 ) {
drush_print('>= v.5');
$makefile = $this->node->field_fserver_drushmake[0]['value'];
drush_invoke_process('@self', 'make', array("$makefile", "{$file_path}/{$base}"), array("--tar"));
}
else {
drush_backend_invoke('make', array("{$this->path}/{$base}.make", "{$file_path}/{$base}", "--tar"));
}
drush_op('drush_shell_exec', "mv {$file_path}/{$base}.tar.gz {$file_path}/{$base}.tgz");
// @TODO
// We need to properly chown and permission these directories.
// Is it possible for drush to know the web server user?
drush_op('chmod', $file_path, 0775);
drush_op('chgrp', $file_path, fserver_posix_groupname());
drush_op('chmod', "{$file_path}/{$base}.tgz", 0775);
drush_op('chgrp', "{$file_path}/{$base}.tgz", fserver_posix_groupname());
$file = new stdClass();
// Use uid of supplied option as file uid
if (drush_get_option(array('u', 'user'))) {
global $user;
$file->uid = $user->uid;
}
else {
$file->uid = $this->node->uid;
}
$file->filename = "{$base}.tgz";
$file->filepath = "{$file_path}/{$base}.tgz";
$file->filemime = file_get_mimetype($file->filename);
$file->filesize = filesize($file->filepath);
$file->status = FILE_STATUS_PERMANENT;
$file->timestamp = time();
drupal_write_record('files', $file);
return $file;
}
return FALSE;
}
}