File: dh-make-drupal

package info (click to toggle)
dh-make-drupal 1.3-1%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 196 kB
  • sloc: ruby: 1,592; makefile: 9
file content (1121 lines) | stat: -rwxr-xr-x 42,226 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
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
#!/usr/bin/ruby
# -*- coding: utf-8 -*-

# dh-make-drupal
#
# Creates Debian packages from Drupal projects (modules, themes, translations).

Version = '1.3'
Author = 'Gunnar Wolf <gwolf@gwolf.org>'
Copyright = <<COPYRIGHT
Copyright © 2009-2012 Gunnar Wolf
            Instituto de Investigaciones Económicas, UNAM

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 3 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
along with this program.  If not, see <http://www.gnu.org/licenses/>.
COPYRIGHT

Description = <<DESCR
The purpose of this program is to generate Debian packages for any Drupal
projects (that is, modules, themes or translations) - Please note note that
as of October 2010 the infrastructure for publishing translations has
changed, so its current support status through this program is suboptimal

Given that Drupal developers publish their work through the main Drupal
site (http://drupal.org), this program fetches the information for the
latest available versions (for the right Drupal release, and with the
specified stability level), and prepares a Debian package from it.

This Debian package can be locally installed using 'dpkg', or uploaded to your
Apt repository.

Keep in mind that this package only goes as far as it can, being an automated
tool. The generated packages will probably require verification/tweaking to
be of production quality.

This program has been inspired -and named in a similar fashion to- Debian's
pkg-perl group's dh-make-perl.
DESCR

require 'optparse'
require 'digest/md5'
require 'etc'
require 'fileutils'
require 'hpricot'
require 'open-uri'
require 'singleton'
require 'tmpdir'
require 'yaml'

module DrupalProject
  class SkipRequested < Exception; end
  # This class acts basically as an universally available hash, with
  # some syntactic sugar. Calling any method ending in '=' will assign
  # the specified value to that key, and calling any method not ending
  # in '=' will give back the value. For extra bonus points, logs
  # options changes if so requested.
  class Options
    @@opts = {}
    def self.method_missing(meth,*args)
      attr = meth.to_s
      if attr.gsub! /\=$/,''
        Logger.instance.debug("Setting configuration key «%s» to «%s»" %
                              [attr, args.to_s] )
        @@opts[attr] = args.length<2 ? args[0] : args
      end
      @@opts[attr]
    end
    def self.to_h
      @@opts
    end
  end

  # Finds the needed information for a given project's files
  # (corresponding to the specified Drupal release and minimum
  # stability level indicated), and allows for downloading it
  class Downloader
    class LackingMock < Exception; end
    attr_accessor :release, :project, :d_ver, :filename
    # Fetches the needed information from the Drupal website. The
    # project name is specified as the first parameter; the Drupal
    # version to fetch the project for is specified as the second
    # parameter (defaulting to 6); the third parameter is the minimum
    # status (as reported by the author) to download - It can be (in
    # decreasing order) :recommended, :supported or :developer,
    # defaulting to :recommended.
    def initialize(project, d_ver=7, status=:recommended)
      @project = project
      @d_ver = d_ver
      fetch_info(status) unless status == :mock
    end

    def self.mock(project, d_ver, tarball, proj_ver)
      raise LackingMock unless project and d_ver and tarball and proj_ver
      raise Errno::ENOENT unless File.exists? tarball

      down = self.new(project, d_ver, :mock)
      down.filename = tarball
      down.release = rel = Release.new

      rel.project = Project.new(project)
      rel.project.p_type = ProjType.new(Options.proj_type)
      rel.project.descr = 'Generated from a local file - Sorry, cannot ' +
        'even fetch a proper description!'
      rel.drupal_version = d_ver
      rel.version = proj_ver

      Logger.instance.debug 'Mocking download information for local file: ' +
        down.to_yaml

      down
    end

    # A #Downloader instantiation only gets the available
    # information. To actually download the file, call #download. The
    # file will be saved under the name specified by the #Release
    # object. Note the filename will follow the regular Debian scheme
    # (i.e. drupal6-og_1.1.orig.tar.gz), not the usual Drupal scheme
    # (og-6.x-1.1.tar.gz).
    def download
      if Options.report_only
        Logger.instance.info 'Reporting only, as requested in the command line'
        puts "Highest available version for %s (Drupal %s, status >= %s):" %
          [@project, @d_ver, @status]
        puts "Project page: %s" % release.project.url
        puts "Release: %s (%s)" % [release.version, release.sym_status]
        puts "Download URL: %s)" % release.url
        raise SkipRequested
      end
      @release.save_file
      @filename = release.dest_file
    end

    private
    def fetch_info(status)
      Logger.instance.debug(("Preparing package for '%s' for Drupal %s, "+
                             "status >= %s") % [project, d_ver, status])
      @release = DrupalProject::VersionsList.for(@project).
        choose(d_ver, status)
      Logger.instance.info "Found %s version %s (status: %s)" % 
        [@release.project, @release.version, @release.sym_status]
      Logger.instance.debug "Download URL: #{release.url}"
    end
  end

  class DebianPackager
    def initialize(down)
      if ! Options.debianize
        Logger.instance.debug('Skipping Debian package creation as ' +
                              'requested at command line')
        raise SkipRequested
      end

      Logger.instance.debug 'Starting Debian package creation'
      @d_ver = down.d_ver
      @release = down.release
      @project = @release.project
      @version = @release.version
      @author = @project.author
      @maint_name = ENV['DEBFULLNAME'] || Etc::getpwuid.gecos.gsub(/,+$/, '')
      @maint_mail = ENV['DEBEMAIL'] || ENV['EMAIL']
      @pkgname = PackageName.for(@project.name, @project.p_type, @d_ver)
      @tarball = down.filename
      @instdir = '%s-%s' % [@pkgname, @release.version]
      @builddir = '/usr/share/drupal%s' % @d_ver
      if @project.p_type.ck_tar_in_dir?
        @builddir = File.join(@builddir, @project.p_type.dir, @project.name )
      end

      ck_orig_tarball
      Logger.instance.info 'Debian package name: %s' % @pkgname
    end

    # Unpacks the downloaded tarball and creates the Debian package
    # structure in it
    def build_structure
      setup_directory
      setup_source_format
      setup_changelog
      setup_compat
      setup_control
      setup_copyright
      setup_dirs
      setup_install_files
      setup_watch
      setup_rules
    end

    # Builds the Debian package from the created directory structure
    def build_package(switches)
      if Options.skip_build
        Logger.instance.debug('Skipping Debian package build as requested at ' +
                              'command line')
        raise SkipRequested
      end
      if ! File.exists? '/usr/bin/dpkg-buildpackage'
        Logger.instance.error 'dpkg-buildpackage not found - Cannot build ' +
          'the generated package.'
        exit 1
      end

      cmdline = '/usr/bin/dpkg-buildpackage %s' % switches
      Logger.instance.info 'Starting Debian package build'
      Logger.instance.debug 'Invoking external command: %s' % cmdline

      system('cd %s && %s 2>&1' % [@instdir, cmdline])
    end

    private
    # Checks if the original tarball looks like a sane Drupal project
    # file, and populate @filelist with the relevant
    # information. Raises a RuntimeError if it does not look right.
    def ck_orig_tarball
      @filelist = []

      @filelist = IO.popen('tar tzf %s' % @tarball).readlines.map do |file|
        if @project.p_type.ck_tar_in_dir?
          # Project types which ship their whole contents inside a
          # directory with the same name as themselves (modules,
          # themes): Refuse to continue if there are files I don't know
          # how to handle (i.e. are not in the expected place)
          raise RuntimeError,('Downloaded file %s has an unexpected '+
                              'directory hierarchy (%s) - Aborting.') %
            [@tarball, file] unless file.gsub!(/^#{@project.name}\//, '')
        end
        file.gsub(/\n$/, '')
      end.reject {|file| file.empty?}
      Logger.instance.debug('Original tarball verified - %s files included' %
                            @filelist.size)
    end

    # Sets up the directory for starting the Debian packaging
    def setup_directory
      curdir = Dir.pwd
      FileUtils.rm_r(@instdir) if Options.force_overwrite and
        File.exists?(@instdir)
      Dir.mkdir(@instdir)
      Dir.mkdir(File.join(@instdir, 'debian'))

      Dir.mktmpdir do |tmpdir|
        system("cd #{tmpdir}; tar xzf #{File.join(curdir, @tarball)}")

        move_from = (@project.p_type.ck_tar_in_dir? ?
                     File.join(tmpdir, @project.name) : tmpdir)

        Dir.open(move_from).entries.reject { |e|
          ['.','..'].include? e
        }.each {|f|
          FileUtils.mv(File.join(move_from,f), @instdir)
        }
      end
    end

    # Sets up the source format declaration
    def setup_source_format
      Dir.mkdir(File.join(@instdir, 'debian/source'))
      put_in_file 'source/format', '3.0 (quilt)'
    end

    # Creates the debian/changelog file
    def setup_changelog
      timestamp = Time.now.strftime '%a, %d %b %Y %H:%M:%S %z'
      deb_ver = '%s-1' % @release.version
      distr = 'unstable'

      entry = ("%s (%s) %s; urgency=low\n\n" % [@pkgname, deb_ver, distr]) +
        "  * Initial release\n\n" +
        (" -- %s <%s>  %s" % [@maint_name, @maint_mail, timestamp] )

      put_in_file 'changelog', entry
    end

    # Creates the debian/compat file
    def setup_compat
      put_in_file 'compat', '8'
    end

    # Creates the debian/watch file
    def setup_watch
      res = ['version=3',
             ( 'https://drupal.org/project/%s .*/%s-%s.x-(\d[\d_.]+)\.tar\.gz' %
               [@project.name, @project.name, @d_ver] ) ].join("\n")
      put_in_file 'watch', res
    end

    # Creates the debian/copyright file
    def setup_copyright
      res = ['Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/',
             'Upstream-Name: %s' % @project.name,
             'Source: %s' % @project.url,
             'Warning:',
             ' -=-=-=- WARNING -=-=-=-',
             '  This file has been autogenerated by dh-make-drupal.',
             '  .',
             '  While this program does its best to achieve proper results,',
             '  copyright information is a very sensible topic which REQUIRES',
             '  HUMAN VALIDATION. Please make sure that this information is',
             '  correct.',
             ' -=-=-=-=-=-=-=-=-=-=-=-',
             '',
             'Files: *'
            ]

      if @project.author
        start_yr = @project.creation.year
        release_yr = @release.date.year
        years = (start_yr == release_yr) ? start_yr :
          '%s - %s' % [start_yr, release_yr]
        res << 'Copyright: %s %s (%s)' %
          [ years, @project.author.name, @project.author.info_url ]
        Logger.instance.debug "Author copyright information found: \n" +
          res.join("\n")
      else
        Logger.instance.debug 'Author copyright information not found'
        res << 'Copyright: Copyright information could not be found'
      end

      # Canonically, Drupal modules include LICENSE.txt. Even more,
      # canonically it is the GPLv2 - For further joy, it's usually
      # one of two exact same files! :-)
      if license = File.join(@instdir, find_license[0])
        data = File.read(license) || '' # Avoid an exception if file is missing
        if ["998ed0c116c0cebfcd9b2107b0d82973",
            "b234ee4d69f5fce4486a80fdaf4a4263"].include? Digest::MD5.hexdigest(data)
          res << 'License: GPL-2' << '' << 'License: GPL-2' <<
            ' This package is licensed under the GNU General Public ' <<
            ' License (GPL) version 2.' << ' .' <<
            ' On Debian GNU/Linux systems, the complete text of the GNU ' <<
            ' General Public License can be found in:' << ' .' <<
            ' /usr/share/common-licenses/GPL-2'
          Logger.instance.debug 'Upstream ships the canonical GPLv2'
        else
          res << 'License: Unknown' << '' << 'License: Unknown' <<
            'Cannot automatically determine the license. Please check by hand.' <<
            ' .' << ' Author-supplied data:' << ' .' << data
          Logger.instance.info('Cannot automatically determine the chosen ' +
                               'license - Please check by hand')
        end
      else
        Logger.instance.warn('No license file found in distribution, cannot ' +
                             'guess copyright information - Please check by ' +
                             'hand.')
        res << 'License: Unknown' << '' << 'License: Unknown' <<
          ' Copyright information could not be found in the sources — Please' <<
          ' check by hand'
      end

      put_in_file 'copyright', res.join("\n")
    end

    # Creates the debian/control file
    def setup_control
      depends = Dependencies.new(@release, @instdir).get
      # For the recommendations, we remove anything that would be
      # duplicated from the dependencies
      recommends = ( Options.skip_recommend ? [] :
                     ( Recommendations.new(@release, @instdir).get - 
                       depends ))
      long = long_descr

      control = ['Source: %s' % @pkgname,
                 'Section: web',
                 'Priority: extra',
                 'Maintainer: %s <%s>' % [@maint_name, @maint_mail],
                 'Build-Depends: debhelper (>= 8.0.0)',
                 'Standards-Version: 3.9.3',
                 'Homepage: %s' % @project.url,
                 '',
                 'Package: %s' % @pkgname,
                 'Architecture: all',
                 'Depends: %s' % depends.join(', ')]
      if !recommends.empty?
        control << 'Recommends: %s' % recommends.join(', ')

        long += ("\n\nThe 'recommended' packages were auto-generated " +
                 "by dh-make-drupal out of several naïve assumptions " +
                 "and might not be real packages. ").word_wrap.prefix

      end
      control << 'Description: %s' % short_descr
      control << long

      put_in_file 'control', control.join("\n")
    end

    # Creates the debian/rules file
    def setup_rules
      put_in_file 'rules', ['#!/usr/bin/make -f', '%:', "\tdh $@"].join("\n")
      FileUtils.chmod(0755, File.join(@instdir, 'debian', 'rules'))
    end

    # Creates the debian/dirs file
    def setup_dirs
      subdirs = subdirs_for(@instdir).reject {|d| d =~ /^#{@instdir}.debian/}

      dirs = [@builddir, subdirs.map {|d| File.join(@builddir,d)}].flatten
      put_in_file '%s.dirs' % @pkgname, dirs.join("\n")
    end

    def setup_install_files
      install = files_to_install.map {|f| '%s %s' % [f, @builddir]}
      docs = find_docs
      changelogs = find_changelog

      ['changelogs','docs','install'].each do |f|
        eval('put_in_file "%s.%s", %s.sort.join("\n") unless %s.empty? ' %
             [@pkgname,f,f,f])
      end
    end

    # Builds a short description for the package
    def short_descr
      '%s %s for Drupal %s' % [@project.name, @project.p_type.human.downcase,
                               @d_ver]
    end

    # Gets the long description for the package
    def long_descr
      ( "%s\n\nThis is an auto-generated description made by dh-make-drupal." %
        @project.descr ).word_wrap.prefix
    end

    # Returns the list of files and directories at the top level of
    # this project's hierarchy
    def files_at_root
      @filelist.map {|f| f.gsub /\/.*/, ''}.uniq
    end

    # Returns the changelog's filename, if one is found
    def find_changelog
      files_at_root.select {|f| f =~ /^(changelog|changes)/i}
    end

    # Returns the license's filename, if one is found
    def find_license
      files_at_root.select {|f| f =~ /license/i}
    end

    # Returns the project's documentation files, if found - All the
    # .txt files, excluding changelog and license
    def find_docs
      files_at_root.select {|f| f =~ /\.txt$/} - find_changelog - find_license
    end

    # The list of files to copy to the debianized package
    def files_to_install
      files_at_root - find_changelog - find_license - find_docs
    end

    # Creates the specified file inside the debian/ directory, with
    # the contents received as the second parameter
    def put_in_file(filename, data)
      File.open(File.join(@instdir, 'debian', filename), 'w') do |f|
        f.puts data
      end
    end

    def subdirs_for(dir)
      res=[dir]
      ignore = ['.', '..']
      Dir.open(dir).each do |subdir|
        full = File.join(dir,subdir)
        next if ignore.include?(subdir)
        next unless FileTest.directory?(full)
        res << subdirs_for(full)
      end
      res.flatten.uniq
    end
  end

  # Generates the package name for a given project name and type
  # (#ProjType), for the specified Drupal version.
  class PackageName
    def self.for(name, type, d_ver)
      ('drupal%s-%s-%s' % [d_ver, type.name_part, name]).gsub(/_/, '-')
    end
  end

  # Dependency-related information is (currently?) only expressed in
  # the {module}.info file inside the tarball and can thus only be
  # gathered once the module is unpacked. Information in any other
  # .info file (i.e. for submodules) will be expressed in Recommendations.
  #
  # All dependencies are expected to be on modules. If this
  # contradicts reality... I'll be glad to change it, I guess :)
  class Dependencies
    class Irrelevant < Exception; end
    # Core Drupal dependencies were gathered by:
    #
    # $ dpkg -L drupal5 | grep ^/usr/share/drupal./modules |cut -d / -f 6|sort|uniq|grep -v README
    # (and equivalent for drupal6, drupal7).
    CoreDrupalModules = {'5' => %w(aggregator block blog blogapi book color
        comment contact drupal filter forum help legacy locale menu node path
        ping poll profile search statistics system taxonomy throttle tracker
        upload user watchdog),
      '6' => %w(aggregator block blog blogapi book color comment contact dblog
        filter forum help locale menu node openid path php ping poll profile
        search statistics syslog system taxonomy throttle tracker translation
        trigger update upload user),
      '7' => %w(aggregator block blog book color comment contact contextual
        dashboard dblog field field_ui file filter forum help image locale menu
        node openid overlay path php poll profile rdf search shortcut
        simpletest statistics syslog system taxonomy toolbar tracker
        translation trigger update user)
    }

    # Builds the dependency lists from the information declared in the
    # project's .info file
    def initialize(release, basedir)
      @d_ver = release.drupal_version
      @depends = ['${misc:Depends}', 'drupal%s' % @d_ver]

      begin
        filename = File.join(basedir, '%s.info' % release.project.name)
        File.open(filename).lines.each { |info_fh| parse_depends(info_fh) }
      rescue Errno::EACCES, Errno::ENOENT
        Logger.instance.warn(('Expected .info file (%s) not found or not ' +
                              'readable. Cannot fetch dependency ' +
                              'information.') % filename)
      end
    end

    # Fetches the list of Debian package dependencies. It is handed
    # back as an array.
    def get
      @depends
    end

    private
    # Parse the dependencies declared in the .info file; skip those
    # which are part of the core Drupal installation
    def parse_depends(line)
      begin
        raise Irrelevant unless line and
          line =~ /dependencies\[.*\]\s*=\s*(.*)\n?/
        # Some modules add what we would regard to as garbage to the
        # dependencies - Unneeded quoting is the most bothering
        # example. So, clean up the dependency, leaving only
        # alphanumeric and hyphens. Oh, and underscores are converted
        # to hyphens while we are at it.
        dep = $1.gsub(/[^a-zA-Z0-9_-]/,'').gsub(/_/, '-')
        begin
          if CoreDrupalModules[@d_ver].include?(dep)
            Logger.instance.debug(('Declared dependency %s is part of ' +
                                   'Drupal %s core - Skipping') % 
                                  [dep, @d_ver])
            raise Irrelevant
          end
        rescue NoMethodError
          Logger.instance.warn(('No list of core modules available for ' +
                                'Drupal version %s - Cannot infer what ' +
                                'to exclude, including everything.') % @d_ver)
        end
        dep_pkg = PackageName.for(dep, ProjType.new('Modules'), @d_ver)
        Logger.instance.info('Adding dependency on %s' % dep_pkg)
        @depends << dep_pkg
      rescue Irrelevant
        # Just skip it, it's irrelevant!
      end
    end
  end

  # Recommended packages will be handled much as dependencies - For
  # modules which have several .info files for their submodules, that
  # information will be reported only as a recommendation.
  class Recommendations < Dependencies
    require 'find'
    def initialize(release, basedir)
      @d_ver = release.drupal_version
      @depends = []

      Find.find(basedir) do |filename|
        next unless filename =~ /\.info$/
        next if filename =~ /#{release.project.name}\.info/
        begin
          File.open(filename).lines.each { |info_fh| parse_depends(info_fh) }
        rescue Errno::EACCES, Errno::ENOENT
          Logger.instance.warn(('Expected .info file (%s) not found or not ' +
                                'readable. Cannot fetch full ' +
                                'recommendations information.') % filename)
        end
      end

      # Remove duplicate dependencies; some submodules will also
      # depend on the master package, remove that dependency as well
      @depends = @depends.sort.uniq - [PackageName.for(release.project.name,
                                                       release.project.p_type,
                                                       @d_ver) ]
    end
  end

  # Stores the basic settings on how to treat the different kind of
  # projects available through the Drupal website.
  #
  # Currently we are only dealing with modules, themes and
  # translations. The other available types (theme engines,
  # installation profiles and drupal project) are outside our scope -
  # although for some of them, this code could be trivially expanded.
  class ProjType
    Known = {'Modules' => {:human => 'Modules',
        :name_part => 'mod',
        :ck_tar_in_dir => true,
        :dir => 'modules'
      },
      'Themes' => {:human => 'Themes',
        :name_part => 'thm',
        :ck_tar_in_dir => true,
        :dir => 'themes'},
      'Translations' => {:human => 'Translations',
        :name_part => 'trans',
        :ck_tar_in_dir => false,
        :dir => ''}
    }

    # Takes the type string (as reported by each project in the
    # 'Breadcrumbs' of its project page). If the key is not defined,
    # raises a NameError exception.
    def initialize(key)
      raise NameError, "Unknown project type #{key}" unless Known.has_key?(key)
      @key = key
    end

    # Human-readable name
    def human; Known[@key][:human];end

    # Directory part to store projects in (inside the Drupal root)
    def dir; Known[@key][:dir]; end

    # The particle to add in the generated package name
    def name_part; Known[@key][:name_part];end

    # Whether to check the tarball structure for this particular project type
    def ck_tar_in_dir?; Known[@key][:ck_tar_in_dir]; end
  end

  class Author
    attr_accessor :name, :info_url
    def self.fetch_from(url)
      auth = self.new

      begin
        doc = Hpricot(open(url, 'User-Agent' => "dh-make-drupal %s" % [Version]))
      rescue OpenURI::HTTPError
        raise IOError, "Could not open author information site at #{url}: " + $!
      end
      auth.info_url = url
      auth.name = doc.search('dd.profile-profile_full_name').inner_text

      auth
    end
  end

  class Project
    class UnknownProjectType < RuntimeError;end
    attr_accessor :name, :url, :p_type, :descr, :author, :creation, :html
    def initialize(name)
      @name = name
    end

    def fetch_data
      @url = "https://drupal.org/project/#{@name}"
      Logger.instance.debug "Fetching project information from #{@url}"

      begin
        @html = Hpricot(open(@url, 'User-Agent' => "dh-make-drupal %s" % [Version]))
      rescue OpenURI::HTTPError
        raise IOError, "Could not open #{name} project website at #{@url}: " + $!
      end

      # Get the project description. Fetch only the first paragraph -
      # This is usually enough for the .deb, and it should be
      # hand-tuned if needed.
      @descr = @html.search('div.node-content')[0].search('p')[0].inner_text

      # When was the project created?
      @creation = Time.parse(@html.search('div.submitted').
                             search('em').inner_text)

      # Project author: We can only get the "first" author (the one
      # that uploaded the node to Drupal, AFAICT). Still, we do what
      # we can.
      relative_url = @html.search('div.submitted a')[0].get_attribute('href').gsub( /^\//, '')
      @author = Author.fetch_from('https://drupal.org/%s' % relative_url)

      # Which kind of project is this? We get the active tab in the
      # 'links' menu.
      #
      # Note that support for translations seems to be moving to a
      # different infrastructure. As it is today, we can still work
      # with translations (although they don't set an "active" link -
      # but they are easy to heuristically spot ;-) ). We will later
      # see if a stronger change is needed - It works fine as it is
      # right now.
      begin
        @p_type = ProjType.new( @html.search('ul.links li.active')[0].inner_text )
      rescue NoMethodError
        begin
          trans_url = "https://localize.drupal.org/translate/languages/#{@name}"
          if trans = open(trans_url)
            @p_type = ProjType.new('Translations')
            trans_warning = "Translations are probably outdated\n" +
              "Please compare module with #{trans_url} and\n" +
              "https://localize.drupal.org/translate/downloads"
            @descr += "\n .\n#{trans_warning}"
            Logger.instance.warn trans_warning
          end
        rescue OpenURI::HTTPError
          raise UnknownProjectType, 'Tried hard, cannot guess.'
        end
      end
      Logger.instance.debug 'Project type for %s: %s' % [@name,
                                                         @p_type.human]
    end
  end

  # Fetches the list of available versions for a given project, and
  # allows for filtering it to match the user's requested criteria
  class VersionsList < Array
    class UnknownStatus < Exception;end
    attr_accessor :project
    # Builds the list of available versions for the requested project
    def self.for(proj_name)
      list = VersionsList.new
      list.project = Project.new(proj_name)
      list.project.fetch_data
      st_map = {'ok' => :recommended, 'warning' => :supported,
        'error' => :developer}

      # We do web-scraping, although it is quite fragile, because the
      # RSS feed provided by drupal.org does not show the release's status

      # In the Drupal pages, releases are listed inside <div> elements
      # indicating (via their CSS classes) the status of the contained
      # releases, and a table with the details.
      #
      # We will often get more than one element in releases - "official
      # releases" and "development snapshots" are given as two
      # tables. We should look for the highest (i.e. stablest) release
      # we can get.
      list.project.html.search('div.download-table').
        each do |div|
        div.attributes['class'].split(/\s+/).
          select {|c| c =~ /download-table-(.+)/}
        status = st_map[$1]

        div.search('tr').each do |tr|
          rel = Release.from_tr(tr, status) or next
          rel.project = list.project
          list << rel
        end
      end

      list
    end

    # Returns all the versions available for the given Drupal version
    # for the specified project. The version should be the standard
    # family nomenclature used in Drupal (i.e. '4.7', '5', '6'). The
    # versions are converted to strings for comparison.
    def for_drupal_version(ver)
      self.clone.delete_if {|item| item.drupal_version != ver.to_s}
    end

    # Gives the highest available version for this project available
    # for the specified (first parameter) Drupal version, with the
    # minimum requested stability (second parameter).
    #
    # If no matching versions are found, a EOFError exception will be
    # raised.
    def choose(drupal_ver, min_status)
      Logger.instance.debug(("Going over %d available releases, " +
                             "searching for compatibility with Drupal %s, " +
                             "minimum development status %s (%d)") % 
                            [ self.size, drupal_ver, min_status, 
                              Release::Statuses[min_status] ])

      res = self.for_drupal_version(drupal_ver).with_min_status(min_status).
        sort_by {|item| item.version}
      if res.empty?
        raise EOFError, "No suitable version found for Drupal %s (level>=%s)" %
          [drupal_ver, min_status]
      end
      return res[-1] # Last element: Highest available suitable version
    end

    # Returns all the versions available for the given project which have
    # a stability status at least equal the specified status. The status
    # can be :developer (lowest), :supported or :recommended (highest).
    def with_min_status(min_status)
      statuses = Release::Statuses
      unless min = statuses[min_status.to_sym]
        Logger.instance.error "Unknown status specified. Valid statuses: " +
          statuses.keys.join(', ')
        return nil
      end

      self.clone.delete_if {|item| item.status < min}
    end

    private
    # This class should not be directly initialized from the outside -
    # call VersionsList.for(project) instead
    def initialize
    end
  end

  # Represents the information for any given release of a Drupal project
  class Release
    Statuses = {:developer => 0, :supported => 1, :recommended => 2}
    attr_accessor(:project, :drupal_version, :version, :status, :url, :date)

    # Returns the list of statuses, highest first
    def self.statuses
      Statuses.keys.sort_by {|k| 0 - Statuses[k]}
    end

    # Creates a DrupalProject::Release from a drupal.org table row
    # (yes, heavily dependent on their Web layout). Note that you will
    # still have to explicitly 'give' this Release its project and
    # type once it is created.
    def self.from_tr(tr, status)
      rel = self.new
      rel.status = Statuses[status]

      # We might receive non-interesting (i.e. empty or header)
      # rows. Check first of all if we have version and link strings,
      # and chicken out otherwise.
      columns = tr/'td'
      return nil unless columns[0] and columns[1]

      # We split the full version (first column) into Drupal and
      # project versions
      full_ver = (columns[0]/'a').text
      Logger.instance.debug "Found version %s (%s)" %
        [full_ver, rel.sym_status]
      unless full_ver =~ /^([\d\.]+).x-(.+)$/
        Logger.instance.info "cannot parse version #{full_ver} - Ignoring"
        return nil
      end
      rel.drupal_version = $1
      rel.version = mangle($2)

      begin
        rel.date = Time.parse(columns[2].inner_text)
        Logger.instance.debug "This release was uploaded on #{rel.date.to_s}"
      rescue => err
        Logger.instance.warn "Could not parse date «%s» - " +
          "Registering current date" % columns[2]
        rel.date = Time.now
      end

      rel.url = (columns[1]/'a')[0].attributes['href']

      rel
    end

    # Returns the filename to which this release should be saved to
    def dest_file
      '%s_%s.orig.tar.gz' % [ PackageName.for(project.name, project.p_type,
                                              drupal_version), version]
    end

    # Fetches the this project's released tar.gz, saves it with the
    # filename specified by #dest_file
    def save_file
      Logger.instance.debug "Retreiving remote file #{@url}"
      Logger.instance.debug "Attempting to save in #{dest_file}"
      # Ok, unlinking is not the same as overwriting, except for
      # practical purposes :)
      File.unlink(dest_file) if (File.exists?(dest_file) and
                                 Options.force_overwrite)
      begin
        if File.exists?(dest_file)
          raise Errno::EEXIST, "Destination filename for source tarball "+
            "(#{dest_file}) already exists. Cannot continue."
        end
        File.open(dest_file, 'w') {|f|
          f.write open(url, 'User-Agent' => "dh-make-drupal %s" % [Version]).read}
      rescue OpenURI::HTTPError
        Logger.instance.error "Requested URI #{url} could not be retreived: " + $!
      end
    end

    # Returns the symbolic status for this revision
    def sym_status
      Statuses.each {|k,v| return k if v == @status}
      nil
    end

    def self.mangle(version)
      return version unless Options.mangle_version
      version.gsub(/\.x[-_.]?(dev)/, '~~\1').gsub(/[-_.]?(alpha|beta|rc)/, '~\1')
    end
  end

  # Reports the progress of the requested operations to the user,
  # according to the minimum severity level specified. Handles four
  # severity levels: Error, Warning, Info and Debug - Respectively, 0,
  # 1, 2 and 3.
  class Logger
    include Singleton
    Levels = %w(E W I D)
    # Private method, not meant to be called directly (this is a
    # singleton object)
    def initialize(level=1)
      @@level = level
    end

    # Redefines the reporting level. If the specified level is below
    # or above the meaningful levels, it will be adjusted to the
    # (respectively) lowest or highest.
    def level=(level)
      l = level.to_i
      l = 0 if l < 0
      l = Levels.size if l > Levels.size
      @@level = l
    end

    # Reports as a message as an error (priority 0). This will always
    # be shown to the user.
    def error(msg); say(0,msg);end
    # Reports the message as a warning (priority 1)
    def warn(msg);  say(1,msg);end
    # Reports the message as informational (priority 2)
    def info(msg);  say(2,msg);end
    # Reports the message as debugging (priority 3)
    def debug(msg); say(3,msg);end

    private
    def say(level, msg)
      puts '%s %s' % [prefix(level), msg] if @@level >= level
    end

    def prefix(level)
      '%s:%s' % [Levels[level], ' '*level]
    end
  end
end

class String
  def word_wrap(maxlen=70)
    self.gsub(/\t/,"     ").gsub(/.{1,#{maxlen}}(?:\s|\Z)/) do 
      ($& + 5.chr).gsub(/\n\005/,"\n").gsub(/\005/,"\n")
    end
  end

  def prefix(with=' ')
    self.split(/\n/).map {|l| '%s%s' % [with, l.empty? ? '.' : l]}.join("\n")
  end
end


class Application
  def initialize
    statuses = DrupalProject::Release::statuses
    projtypes = DrupalProject::ProjType::Known.keys.sort

    # Set default options
    options = DrupalProject::Options
    options.d_ver = 7
    options.min_status = statuses[0]
    options.force_overwrite = false
    options.debug = 1
    options.report_only = false
    options.debianize = true
    options.skip_build = false
    options.skip_recommend = false
    options.switches = '-us -uc'
    options.tarball = false
    options.proj_version = nil
    options.proj_type = projtypes[0]
    options.mangle_version = true

    optparse = OptionParser.new do |opts|
      opts.banner = Description
      opts.version = Version

      opts.on('-v', '--version') do
        puts "#{$0} version #{Version}\n\n#{Copyright}\nWritten by #{Author}"

        exit 0
      end

      opts.on( '-h', '--help', 'Display this screen' ) do
        puts opts
        exit 0
      end

      opts.on('-d VERSION', '--drupal-version', 'Drupal version'
              ) { |ver| options.d_ver = ver }

      opts.on('-r', '--report-only',
              "Check only for project availability, don't download or " +
              "perform any other actions locally. Implies -D."
              ) { options.report_only = true }

      opts.on('-f', '--force', 'Proceed even if this will overwrite ' +
              'currently existing files'
              ) { options.force_overwrite = true }

      opts.on('--debug LEVEL', 'Debug level for generated messages ' +
              '(0=highest, 5=lowest)'
              ) {|level| options.debug = level}

      opts.on('-s', '--min-status STATUS', statuses,
              'Minimum status to consider for packaging. ' +
              'Accepted values: %s. Defaults to %s.' %
              [ statuses.map {|s| "'#{s}'"}.join(', '), statuses[0]]
              ) {|status| options.min_status = status}

      opts.on('-D', '--dont-debianize',
              'Do not attempt to debianize the project, only download the ' +
              'tarball'
              ) { options.debianize = false }

      opts.on('-b', '--no-build', 'Prepare the debianized directory, but ' +
              'omit the actual package build process. This option is ' +
              'incompatible with either -D and -r.'
              ) { options.skip_build = true }

      opts.on('-R', '--no-recommends', 'Omits the generation of the ' +
              'Recommends: line, which is built by scanning of ' +
              'submodule-provided .info files and may be misleading or ' +
              'introducing too much noise'
              ) { options.skip_recommend = true }

      opts.on('--build-switches SWITCHES', 'Switches to pass to  ' +
              'dpkg-buildpackage. Defaults to "-us -uc" (do not sign the ' +
              'generated package). In order not to give any switches, ' +
              'specify an empty quoted string (i.e. --build-switches=\'\').'
              ) { |switches| options.switches = switches }

      opts.on('-t', '--tarball FILE',
              'Use the specified tarball as the original ' +
              'project tar.gz, don\'t look for any other available versions ' +
              'and don\'t download from the Drupal website. This will ' +
              'require you also to provide a project version number with -V ' +
              'and the project type with -T'
              ) { |tar| options.tarball = tar }

      opts.on('-T', '--proj-type TYPE', projtypes,
              'Type of project we are packaging. This option is ' +
              'only meaningful when working on a local tarball (-t), and ' +
              'will be ignored otherwise. Accepted values: %s. Defaults ' +
              'to %s.' % [ projtypes, projtypes[0] ]
              ) { |type| options.proj_type = type }

      opts.on('-V', '--proj_version VERSION', 'Provide a project version ' +
           'number. This option is only meaningful when working on a local ' +
           'tarball (-t), and will be ignored otherwise'
              ) { |ver| options.proj_version = ver }

      opts.on('-m', '--mangle-version PATTERN', 'Debian versioning logic ' +
              'includes the «~» character meaning «anything below» the ' +
              'preceding version number. This is most useful when dealing ' +
              'with pre-release qualificators (in order, 1.x-dev, 1.0-alpha1, '+
              '1.0-beta, 1.0rc3). dh-make-drupal will try to recognize such ' +
              'patterns and mangle them so they sort correctly in Debian ' +
              '(and so that when a stable version is released it appears as ' +
              'higher - For the above mentioned version numbers, they would ' +
              'result in 1~~dev, 1.0~alpha1, 1.0~beta, 1.0~rc3). You can use ' +
              'this switch to tell dh-make-drupal to omit this mangling.'
              ) { options.mangle_version = false }
    end

    optparse.parse!
    options.project = ARGV[0]

    if options.project.nil?
      STDERR.puts "USAGE:"
      STDERR.puts "  #{$0} [options] project"
      STDERR.puts "  #{$0} --help for full invocation options"
      exit 1
    end
  end

  def run
    o = DrupalProject::Options
    log = DrupalProject::Logger.instance
    log.level = o.debug
    log.debug "Parsed options:\n#{o.to_h.to_yaml}"

    begin
      if o.tarball
        begin
          down = DrupalProject::Downloader.mock(o.project, o.d_ver, 
                                                o.tarball, o.proj_version)
        rescue DrupalProject::Downloader::LackingMock
          log.error 'Missing information: Project name, tarball and project ' +
            'version are required when working with a local tarball.'
          exit 1
        rescue Errno::ENOENT
          log.error 'Specified tarball (%s) does not exist, cannot continue' %
            o.tarball
          exit 1
        end
      else
        down = DrupalProject::Downloader.new(o.project, o.d_ver, o.min_status)
        down.download 
      end

      deb = DrupalProject::DebianPackager.new(down) 
      deb.build_structure
      deb.build_package(o.switches)
    rescue DrupalProject::SkipRequested
      # All fine, nothing to see, please move along
    end
  end
end

app = Application.new
app.run