summaryrefslogtreecommitdiffstats
path: root/mgagnome
blob: d5f6c183aaf4711ad9f4a3bb9d9d0e864d3eb67e (plain)
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
#!/usr/bin/python -u

# A lot of the code comes from ftpadmin, see
#   http://git.gnome.org/browse/sysadmin-bin/tree/ftpadmin
# Written by Olav Vitters

# basic modules:
import os
import os.path
import sys
import re
import subprocess

# command line parsing, error handling:
import argparse
import errno

# overwriting files by moving them (safer):
import tempfile
import shutil

# version comparison:
import rpm

# opening tarballs:
import tarfile
import gzip
import bz2
import lzma # pyliblzma

# getting links from HTML document:
from sgmllib import SGMLParser
import urllib2
import urlparse

# for checking hashes
import hashlib

# for parsing ftp-release-list emails
import email
from email.mime.text import MIMEText

# to be able to sleep for a while
import time

# version freeze
import datetime

# packages --sort
import itertools

# check-latest
import requests

SLEEP_INITIAL=180
SLEEP_REPEAT=30
SLEEP_TIMES=30

re_majmin = re.compile(r'^([0-9]+\.[0-9]+).*')
re_version = re.compile(r'([-.]|\d+|[^-.\d]+)')

def version_cmp(a, b):
    """Compares two versions

    Returns
      -1 if a < b
      0  if a == b
      1  if a > b
    """

    return rpm.labelCompare(('1', a, '1'), ('1', b, '1'))

def get_latest_version(versions, max_version=None):
    """Gets the latest version number

    if max_version is specified, gets the latest version number before
    max_version"""
    latest = None
    for version in versions:
        if ( latest is None or version_cmp(version, latest) > 0 ) \
           and ( max_version is None or version_cmp(version, max_version) < 0 ):
            latest = version
    return latest

def get_safe_max_version(version):
    if not re_majmin.match(version):
        return None

    majmin_nr = map(long, re_majmin.sub(r'\1', version).split('.'))

    if majmin_nr[1] % 2 == 0:
        return "%d.%d" % (majmin_nr[0], majmin_nr[1] + 1)
    else:
        return "%d.%d" % (majmin_nr[0], majmin_nr[1] + 2)

def judge_version_increase(version_old, version_new):
        """Judge quality of version increase:

        Returns a tuple containing judgement and message

        Judgement:
        Less than 0: Error
        0 to 4: Better not
        5+: Ok"""
        versions = (version_old, version_new)

        # First do a basic version comparison to ensure version_new is actually newer
        compare = version_cmp(version_new, version_old)

        if compare == 0:
            # 1.0.0 -> 1.0.1
            return (-2, "Already at version %s!" % (version_old))

        if compare != 1:
            # 1.0.1 -> 1.0.0
            return (-3, "Version %s is older than current version %s!" % (version_new, version_old))

        # Version is newer, but we don't want to see if it follows the GNOME versioning scheme
        majmins = [re_majmin.sub(r'\1', ver) for ver in versions if re_majmin.match(ver) is not None]

        if len(majmins) == 1:
            return (-1, "Version number scheme changes: %s" % (", ".join(versions)))

        if len(majmins) == 0:
            return (0, "Unsupported version numbers: %s" % (", ".join(versions)))

        # Follows GNOME versioning scheme
        # Meaning: x.y.z
        #          x = major
        #          y = minor  : even if stable
        #          z = micro

        # Major+minor the same? Then go ahead and upgrade!
        if majmins[0] == majmins[1]:
            # Majmin of both versions are the same, looks good!
            # 1.1.x -> 1.1.x or 1.0.x -> 1.0.x
            return (10, None)

        # More detailed analysis needed, so figure out the numbers
        majmin_nrs = [map(long, ver.split('.')) for ver in majmins]

        # Check/ensure major version number is the same
        if majmin_nrs[0][0] != majmin_nrs[1][0]:
            # 1.0.x -> 2.0.x
            return (1, "Major version number increase")

        # Minor indicates stable/unstable
        devstate = (majmin_nrs[0][1] % 2 == 0, majmin_nrs[1][1] % 2 == 0)

        # Upgrading to unstable is weird
        if not devstate[1]:
            if devstate[0]:
                # 1.2.x -> 1.3.x
                return (1, "Stable to unstable increase")

            # 1.3.x -> 1.5.x
            return (4, "Unstable to unstable version increase")

        # Unstable => stable is always ok
        if not devstate[0]:
            # 1.1.x -> 1.2.x
            return (5, "Unstable to stable")

        # Can only be increase of minors from one stable to the next
        # 1.0.x -> 1.2.x
        return (6, "Stable version increase")

def line_input (file):
    for line in file:
        if line[-1] == '\n':
            yield line[:-1]
        else:
            yield line

def call_editor(filename):
    """Return a sequence of possible editor binaries for the current platform"""

    editors = []

    for varname in 'VISUAL', 'EDITOR':
        if varname in os.environ:
            editors.append(os.environ[varname])

    editors.extend(('/usr/bin/editor', 'vi', 'pico', 'nano', 'joe'))

    for editor in editors:
        try:
            ret = subprocess.call([editor, filename])
        except OSError, e:
            if e.errno == 2:
                continue
            raise

        if ret == 127:
            continue

        return True

class urllister(SGMLParser):
    def reset(self):
        SGMLParser.reset(self)
        self.urls = []

    def start_a(self, attrs):
        href = [v for k, v in attrs if k=='href']
        if href:
            self.urls.extend(href)

class XzTarFile(tarfile.TarFile):

    OPEN_METH = tarfile.TarFile.OPEN_METH.copy()
    OPEN_METH["xz"] = "xzopen"

    @classmethod
    def xzopen(cls, name, mode="r", fileobj=None, **kwargs):
        """Open gzip compressed tar archive name for reading or writing.
           Appending is not allowed.
        """
        if len(mode) > 1 or mode not in "rw":
            raise ValueError("mode must be 'r' or 'w'")

        if fileobj is not None:
            fileobj = _LMZAProxy(fileobj, mode)
        else:
            fileobj = lzma.LZMAFile(name, mode)

        try:
            # lzma doesn't immediately return an error
            # try and read a bit of data to determine if it is a valid xz file
            fileobj.read(_LZMAProxy.blocksize)
            fileobj.seek(0)
            t = cls.taropen(name, mode, fileobj, **kwargs)
        except IOError:
            raise tarfile.ReadError("not a xz file")
        except lzma.error:
            raise tarfile.ReadError("not a xz file")
        t._extfileobj = False
        return t

if not hasattr(tarfile.TarFile, 'xzopen'):
    tarfile.open = XzTarFile.open

def is_valid_hash(path, algo, hexdigest):
    if algo not in hashlib.algorithms:
        raise ValueError("Unknown hash algorithm: %s" % algo)

    local_hash = getattr(hashlib, algo)()

    with open(path, 'rb') as fp:
        data = fp.read(32768)
        while data:
            local_hash.update(data)
            data = fp.read(32768)

    return local_hash.hexdigest() == hexdigest

class SpecFile(object):
    re_update_version = re.compile(r'^(?P<pre>Version:\s*)(?P<version>.+)(?P<post>\s*)$', re.MULTILINE + re.IGNORECASE)
    re_update_release = re.compile(r'^(?P<pre>Release:\s*)(?P<release>%mkrel \d+)(?P<post>\s*)$', re.MULTILINE + re.IGNORECASE)

    def __init__(self, path):
        self.path = path
        self.cwd = os.path.dirname(path)

    @property
    def version(self):
        return subprocess.check_output(["rpm", "--specfile", self.path, "--queryformat", "%{VERSION}\n"]).splitlines()[0]
    @property
    def sources(self):
        ts = rpm.ts()
        spec = ts.parseSpec(self.path)
        srclist = spec.sources if isinstance(spec.sources, (list, tuple)) \
                        else spec.sources()
        return dict((os.path.basename(name), name) for name, no, flags in srclist)

    def update(self, version, force=False):
        """Update specfile (increase version)"""
        cur_version = self.version

        (judgement, msg) = judge_version_increase(cur_version, version)

        if judgement < 0:
            print >>sys.stderr, "ERROR: %s!" % (msg)
            return False

        if judgement < 5:
            print "WARNING: %s!" % (msg)
            if not force: return False

        # XXX - os.path.join is hackish
        svn_diff_output = subprocess.check_output(["svn", "diff", os.path.join(self.path, '..')])
        if svn_diff_output != '':
            print svn_diff_output
            print >>sys.stderr, "ERROR: Package has uncommitted changes!"
            if not force:
                return False

            # Forcing package submission: revert changes
            try:
                print >>sys.stderr, "WARNING: Force used; reverting svn changes"
                subprocess.check_call(["svn", "revert", "-R", os.path.join(self.path, '..')])
            except subprocess.CalledProcessError:
                return False

        with open(self.path, "rw") as f:
            data = f.read()

            if data.count("%subrel") != 0:
                print >>sys.stderr, "ERROR: %subrel found; don't know what to do!"
                return False

            if data.count("%mkrel") != 1:
                print >>sys.stderr, "ERROR: Multiple %mkrel found; don't know what to do!"
                return False

            data, nr = self.re_update_version.subn(r'\g<pre>%s\g<post>' % version, data, 1)
            if nr != 1:
                print >>sys.stderr, "ERROR: Could not increase version!"
                return False

            data, nr = self.re_update_release.subn(r'\g<pre>%mkrel 1\g<post>', data, 1)
            if nr != 1:
                print >>sys.stderr, "ERROR: Could not reset release!"
                return False

            # Overwrite file with new version number
            write_file(self.path, data)


        # Verify that RPM also agrees that version number has changed
        if self.version != version:
            print "ERROR: Increased version to %s, but RPM doesn't agree!?!" % version
            return False


        # Try to download the new tarball various times and wait between attempts
        tries = 0
        while tries < SLEEP_TIMES:
            tries += 1
            if tries > 1: time.sleep(SLEEP_REPEAT * 2 ** (tries // 10))
            try:
                # Download new tarball
                subprocess.check_call(['mgarepo', 'sync', '-d'], cwd=self.cwd)
                # success, so exit loop
                break
            except subprocess.CalledProcessError, e:
                # mgarepo sync returns 1 if the tarball cannot be downloaded
                if e.returncode != 1:
                    subprocess.check_call(["svn", "revert", "-R", os.path.join(self.path, '..')])
                    return False
        else:
            # failed to download tarball
            subprocess.check_call(["svn", "revert", "-R", os.path.join(self.path, '..')])
            return False


        try:
            # Check patches still apply
            subprocess.check_call(['bm', '-p', '--nodeps'], cwd=self.cwd)
        except subprocess.CalledProcessError:
            logfile = os.path.join(os.path.dirname(self.path), 'log.%s' % os.path.splitext(os.path.basename(self.path))[0])
            if os.path.exists(logfile):
                subprocess.call(['tail', '-n', '15', logfile])
            return False

        return True

class Patch(object):
    """Do things with patches"""

    re_dep3 = re.compile(r'^(?:#\s*)?(?P<header>[-A-Za-z0-9]+?):\s*(?P<data>.*)$')
    re_dep3_cont = re.compile(r'^#?\s+(?P<data>.*)$')

    def __init__(self, path, show_path=False):
        """Path: path to patch (might not exist)"""
        self.path = path
        self.show_path = show_path

    def __str__(self):
        return self.path if self.show_path else os.path.basename(self.path)

    def add_dep3(self):
        """Add DEP-3 headers to a patch file"""
        if self.dep3['valid']:
            return False

        new_headers = (
            ('Author', self.svn_author),
            ('Subject', ''),
            ('Applied-Upstream', ''),
            ('Forwarded', ''),
            ('Bug', ''),
        )

        with tempfile.NamedTemporaryFile(dir=os.path.dirname(self.path), delete=False) as fdst:
            with open(self.path, "r") as fsrc:
                # Start with any existing DEP3 headers
                for i in range(self.dep3['last_nr']):
                    fdst.write(fsrc.read())

                # After that add the DEP3 headers
                add_line = False
                for header, data in new_headers:
                    if header in self.dep3['headers']:
                        continue

                    # XXX - wrap this at 80 chars
                    add_line = True
                    print >>fdst, "%s: %s" % (header, "" if data is None else data)

                if add_line: print >>fdst, ""
                # Now copy any other data and the patch
                shutil.copyfileobj(fsrc, fdst)

            fdst.flush()
            os.rename(fdst.name, self.path)

        call_editor(self.path)

    #Author: fwang
    #Subject: Build fix: Fix glib header inclusion
    #Applied-Upstream: commit:30602
    #Forwarded: yes
    #Bug: http://bugzilla.abisource.com/show_bug.cgi?id=13247

    def _read_dep3(self):
        """Read DEP-3 headers from an existing patch file

        This will also parse git headers"""
        dep3 = {}
        headers = {}

        last_header = None
        last_nr = 0
        nr = 0
        try:
            with open(self.path, "r") as f:
                for line in line_input(f):
                    nr += 1
                    # stop trying to parse when real patch begins
                    if line == '---':
                        break

                    r = self.re_dep3.match(line)
                    if r:
                        info = r.groupdict()

                        # Avoid matching URLS
                        if info['data'].startswith('//') and info['header'].lower () == info['header']:
                            continue

                        headers[info['header']] = info['data']
                        last_header = info['header']
                        last_nr = nr
                        continue

                    r = self.re_dep3_cont.match(line)
                    if r:
                        info = r.groupdict()
                        if last_header:
                            headers[last_header] = " ".join((headers[last_header], info['data']))
                            last_nr = nr
                        continue

                    last_header = None
        except IOError:
            pass

        dep3['valid'] = \
            (('Description' in headers and headers['Description'].strip() != '')
                or ('Subject' in headers and headers['Subject'].strip() != '')) \
            and (('Origin' in headers and headers['Origin'].strip() != '') \
                or ('Author' in headers and headers['Author'].strip() != '') \
                or ('From' in headers and headers['From'].strip() != ''))
        dep3['last_nr'] = last_nr
        dep3['headers'] = headers

        self._dep3 = dep3

    @property
    def dep3(self):
        if not hasattr(self, '_dep3'):
            self._read_dep3()

        return self._dep3

    @property
    def svn_author(self):
        if not hasattr(self, '_svn_author'):
            try:
                contents = subprocess.check_output(['svn', 'log', '-q', "--", self.path], close_fds=True).strip("\n").splitlines()

                for line in contents:
                    if ' | ' not in line:
                        continue

                    fields = line.split(' | ')
                    if len(fields) >= 3:
                        self._svn_author = fields[1]
            except subprocess.CalledProcessError:
                pass

        if not hasattr(self, '_svn_author'):
            return None

        return self._svn_author


class Upstream(object):

    URL="http://download.gnome.org/sources/"
    limit = None
    _cache_versions = {}

    def __init__(self):
        urlopen = urllib2.build_opener()

        good_dir = re.compile('^[-A-Za-z0-9_+.]+/$')

        # Get the files
        usock = urlopen.open(self.URL)
        parser = urllister()
        parser.feed(usock.read())
        usock.close()
        parser.close()
        files = parser.urls

        tarballs = set([filename.replace('/', '') for filename in files if good_dir.search(filename)])
        if self.limit is not None:
            tarballs.intersection_update(self.limit)

        self.names = tarballs

    @classmethod
    def versions(cls, module):
        # XXX - ugly
        if module not in cls._cache_versions:
            versions = None

            url = '%s%s/cache.json' % (cls.URL, module)
            r = requests.get(url)
            j = r.json
            if j is not None and len(j) > 2 and module in j[2]:
                versions = j[2][module]

            cls._cache_versions[module] = versions

        return cls._cache_versions[module]

class Downstream(object):
    re_file = re.compile(r'^(?P<module>.*?)[_-](?:(?P<oldversion>([0-9]+[\.])*[0-9]+)-)?(?P<version>([0-9]+[\.\-])*[0-9]+)\.(?P<format>(?:tar\.|diff\.)?[a-z][a-z0-9]*)$')

    MEDIA="Core Release Source"
    PKGROOT='~/pkgs'
    DISTRO=None

    def __init__(self):
        contents = subprocess.check_output(['urpmf', '--qf', '%name|%version|%files', '.', "--media", self.MEDIA], close_fds=True).strip("\n").splitlines()

        FILES = {}
        TARBALLS = {}

        for line in  contents:
            try:
                srpm, version, filename = line.split("|")
            except ValueError:
                print >>sys.stderr, line
                continue

            if '.tar' in filename:
                r = self.re_file.match(filename)
                if r:
                    fileinfo = r.groupdict()
                    module = fileinfo['module']

                    if module not in TARBALLS:
                        TARBALLS[module] = {}

                    if srpm in TARBALLS[module]:
                        # srpm seen before, check if version is newer
                        if version_cmp(TARBALLS[module][srpm], version) == 1:
                            TARBALLS[module][srpm] = version
                    else:
                        TARBALLS[module][srpm] = version

            if srpm not in FILES:
                FILES[srpm] = set()
            FILES[srpm].add(filename)

        self.tarballs = TARBALLS
        self.files = FILES

    @classmethod
    def co(cls, package, cwd=None):
        if cwd is None:
            cwd = os.path.expanduser(cls.PKGROOT)

        cmd = ['mgarepo', 'co']
        if cls.DISTRO:
            cmd.extend(('-d', cls.DISTRO))
        cmd.append(package)
        return subprocess.check_call(cmd, cwd=cwd)

    def get_downstream_from_upstream(self, upstream, version):
        if upstream not in self.tarballs:
            raise ValueError("No packages for upstream name: %s" % upstream)

        if len(self.tarballs[upstream]) == 1:
            return self.tarballs[upstream].keys()

        # Directories packages are located in
        root = os.path.expanduser(self.PKGROOT)

        packages = {}
        for package in self.tarballs[upstream].keys():
            cwd = os.path.join(root, package)

            # Checkout package to ensure the checkout reflects the latest changes
            try:
                self.co(package, cwd=root)
            except subprocess.CalledProcessError:
                raise ValueError("Multiple packages found and cannot checkout %s" % package)

            # Determine version from spec file
            try:
                packages[package] = SpecFile(os.path.join(cwd, "SPECS", "%s.spec" % package)).version
            except subprocess.CalledProcessError:
                raise ValueError("Multiple packages found and cannot determine version of %s" % package)

        # Return all packages reflecting the current version
        matches = [package for package in packages if packages[package] == version]
        if len(matches):
            return matches

        # Return all packages reflecting the version before the current version
        latest_version = get_latest_version(packages.values(), max_version=version)
        matches = [package for package in packages if packages[package] == latest_version]
        if len(matches):
            return matches

        # Give up
        raise ValueError("Multiple packages found and cannot determine package for version %s" % version)

def write_file(path, data):
    with tempfile.NamedTemporaryFile(dir=os.path.dirname(path), delete=False) as fdst:
        fdst.write(data)
        fdst.flush()
        os.rename(fdst.name, path)

def cmd_co(options, parser):
    for package, module, package_version, spec_version, downstream_files in sorted(join_streams()):
        print "%s => %s" % (module, package)
        try:
            Downstream.co(package)
        except subprocess.CalledProcessError:
            pass

def join_streams(show_version=False, only_diff_version=False):
    root = os.path.expanduser(Downstream.PKGROOT)

    upstream = Upstream().names
    downstream = Downstream()

    matches = upstream & set(downstream.tarballs.keys())
    for module in matches:
        for package in downstream.tarballs[module].keys():
            package_version = downstream.tarballs[module][package]
            spec_version = None
            if show_version or only_diff_version:
                cwd = os.path.join(root, package)
                try:
                    spec_version = SpecFile(os.path.join(cwd, "SPECS", "%s.spec" % package)).version
                except subprocess.CalledProcessError:
                    spec_version = 'N/A'

            if only_diff_version and package_version == spec_version:
                continue

            yield (package, module, package_version, spec_version, downstream.files[package])

def cmd_group_owner(options, parser):
    groups = set(options.group)

    output = [pkg.split("\t") for pkg in subprocess.check_output(["urpmf", "-F|", "--qf", "%group\t%name\t%sourcerpm\t%version\t%release",  "."]).splitlines()]
    if not output: return

    # Filter by groups
    output = [pkg for pkg in output if pkg[0] in groups]
    if not output: return

    packages = {}
    for group, name, sourcerpm, version, release in output:
        if group not in packages:
            packages[group] = {}

        source = sourcerpm if sourcerpm else name
        end = ".src.rpm"
        if source.endswith(end): source = source[:len(source) - len(end)]
        end = "-%s-%s" %(version, release)
        if source.endswith(end): source = source[:len(source) - len(end)]

        if source not in packages[group]: packages[group][source] = set()

        packages[group][source].add(name)


    maints = dict([line.rpartition(" ")[::2] for line in subprocess.check_output(["mgarepo", "maintdb", "get"]).splitlines()])

    def get_output(source, maints, packages):
        for source in packages.keys():
             maint = maints.get(source, "?")

             yield "\t".join((maint, source, ",".join(sorted(packages[source]))))

    first = True
    for group in packages.keys():
        if first:
            first = False
        else:
            print ""
            print ""
        print group
        print ""

        for line in sorted(get_output(source, maints, packages[group])):
            print line

def cmd_ls(options, parser):
    streams = join_streams(show_version=options.show_version, only_diff_version=options.diff)
    if options.sort:
        SORT=dict(zip(options.sort.read().splitlines(), itertools.count()))

        streams = sorted(streams, key=lambda a: (SORT.get(a[1], 9999), a[0]))
    else:
        streams = sorted(streams)

    for package, module, package_version, spec_version, downstream_files in streams:
        sys.stdout.write(package)
        if options.upstream: sys.stdout.write("\t%s" % module)
        if options.show_version: sys.stdout.write("\t%s\t%s" % (spec_version, package_version))
        print

def cmd_check_latest(options, parser):
    streams = join_streams(show_version=True)

    for package, module, package_version, spec_version, downstream_files in streams:
        upgrade=set()
        sys.stdout.write(package)
        sys.stdout.write("\t%s\t%s" % (spec_version, package_version))

        safe_max_version = get_safe_max_version(spec_version)

        versions = Upstream.versions(module)
        if versions:
            latest_version = get_latest_version(versions)
            safe_version = get_latest_version(versions, safe_max_version)

            cmp_latest = version_cmp(latest_version, spec_version)
            if cmp_latest < 0:
                latest_version = 'N/A'
                upgrade.add('l')
            elif cmp_latest > 0:
                upgrade.add('L')

            cmp_safe = version_cmp(safe_version, spec_version)
            if cmp_safe < 0:
                safe_version = 'N/A'
                upgrade.add('s')
            elif cmp_safe > 0:
                upgrade.add('S')

            sys.stdout.write("\t%s" % latest_version)
            sys.stdout.write("\t%s" % safe_version)
            sys.stdout.write("\t%s" % "".join(sorted(upgrade)))

        print

def cmd_patches(options, parser):
    root = os.path.expanduser(Downstream.PKGROOT)

    for package, module, package_version, spec_version, downstream_files in sorted(join_streams()):
        for filename in downstream_files:
            if '.patch' in filename or '.diff' in filename:

                p = Patch(os.path.join(root, package, "SOURCES", filename), show_path=options.path)
                valid = ""
                forwarded = ""
                if p.dep3['headers']:
                    forwarded = p.dep3['headers'].get('Forwarded', "no")
                    if p.dep3['valid']:
                        valid="VALID"
                print "\t".join((module, package, str(p), forwarded, valid))

def cmd_dep3(options, parser):
    p = Patch(options.patch)
    p.add_dep3()

def cmd_package_new_version(options, parser):
    # Determine the package name
    if options.upstream:
        try:
            package = Downstream().get_downstream_from_upstream(options.package, options.version)[0]
        except ValueError, e:
            print >>sys.stderr, "ERROR: %s" % e
            sys.exit(1)
    else:
        package = options.package

    # Directories packages are located in
    root = os.path.expanduser(Downstream.PKGROOT)
    cwd = os.path.join(root, package)

    # Checkout package to ensure the checkout reflects the latest changes
    try:
        Downstream.co(package, cwd=root)
    except subprocess.CalledProcessError:
        sys.exit(1)

    # SpecFile class handles the actual version+release change
    s = SpecFile(os.path.join(cwd, "SPECS", "%s.spec" % package))
    print "%s => %s" % (s.version, options.version)
    if not s.update(options.version, force=options.force):
        sys.exit(1)

    # Check hash, if given
    if options.hexdigest is not None:
        sources = [name for name, origname in s.sources.iteritems() if '://' in origname]
        if not len(sources):
            print >>sys.stderr, "ERROR: Cannot determine source file (for hash check)!"
            sys.stderr(1)

        # If there are multiple sources, try to see if there is a preferred name
        # --> needed for metacity hash check (multiple tarball sources)
        if len(sources) > 1:
            preferred_name = '%s-%s.tar.xz' % (package, options.version)
            if preferred_name in sources:
                sources = [preferred_name]

        for filename in sources:
            path = os.path.join(cwd, "SOURCES", filename)
            if not is_valid_hash(path, options.algo, options.hexdigest):
                print >>sys.stderr, "ERROR: Hash file failed check for %s!" % path
                print >>sys.stderr, "ERROR: Reverting changes!"
                subprocess.call(['svn', 'revert', '-R', cwd], cwd=cwd)
                sys.exit(1)

    try:
        # If we made it this far, checkin the changes
        subprocess.check_call(['mgarepo', 'ci', '-m', 'new version %s' % options.version], cwd=cwd)

        # Submit is optional
        if options.submit:
            cmd = ['mgarepo', 'submit']
            if Downstream.DISTRO:
                cmd.extend(('--define', 'section=core/updates_testing', '-t', Downstream.DISTRO))
            subprocess.check_call(cmd, cwd=cwd)
    except subprocess.CalledProcessError:
        sys.exit(1)

def cmd_parse_ftp_release_list(options, parser):
    def _send_reply_mail(contents, orig_msg, to, packages=[], error=False):
        """Send an reply email"""
        contents.seek(0)
        msg = MIMEText(contents.read(), _charset='utf-8')

        if error:
            # XXX - ugly
            contents.seek(0)
            lastline = contents.read().rstrip().splitlines()[-1]
            # Remove things like "ERROR: " and so on from the last line
            lastline = re.sub(r'^(?:[^ :]+:\s+)+', '', lastline)
            # Remove things like "   - " (youri output from mgarepo submit)
            lastline = re.sub(r'^\s+-\s+', '', lastline)
            subjecterror = " (ERROR: %s)" % lastline if lastline else " (ERROR)"
        else:
            subjecterror = ""

        if packages:
            subject = "%s %s%s" % (", ".join(packages), orig_msg['X-Module-Version'], subjecterror)
        else:
            subject = "Re: %s%s" % (orig_msg['Subject'], subjecterror)

        msg['Subject'] = subject
        msg['To'] = to
        msg["In-Reply-To"] = orig_msg["Message-ID"]
        msg["References"] = orig_msg["Message-ID"]

        # Call sendmail program directly so it doesn't matter if the service is running
        cmd = ['/usr/sbin/sendmail', '-oi', '--']
        cmd.extend([to])
        p = subprocess.Popen(cmd, stdin=subprocess.PIPE)
        p.stdin.write(msg.as_string())
        p.stdin.flush()
        p.stdin.close()
        p.wait()


    msg = email.email.message_from_file(sys.stdin)

    if options.mail:
        stdout = tempfile.TemporaryFile()
        stderr = stdout
    else:
        stdout = sys.stdout
        stderr = sys.stderr

    try:
        module = msg['X-Module-Name']
        version = msg['X-Module-Version']
        hexdigest = msg['X-Module-SHA256-tar.xz']
    except KeyError, e:
        print >>stderr, "ERROR: %s" % e
        if options.mail: _send_reply_mail(stdout, msg, options.mail, error=True)
        sys.exit(1)

    try:
        packages = Downstream().get_downstream_from_upstream(module, version)
    except ValueError, e:
        print >>stderr, "ERROR: %s" % e
        if options.mail: _send_reply_mail(stdout, msg, options.mail, error=True)
        sys.exit(1)

    if options.wait:
        # maildrop aborts and will try to deliver after 5min
        # fork to avoid this
        if os.fork() != 0: sys.exit(0)
        # wait SLEEP_INITIAL after the message was sent
        secs = SLEEP_INITIAL
        t = email.utils.parsedate_tz(msg['Date'])
        if t is not None:
            msg_time = email.utils.mktime_tz(t)
            secs = SLEEP_INITIAL - (time.time() - msg_time)

        if secs > 0: time.sleep(secs)

    error = False
    for package in packages:
        cmd = ['mga-gnome', 'increase', '--hash', hexdigest]
        if options.submit:
            cmd.append('--submit')
        if options.force:
            cmd.append('--force')
        cmd.extend((package, version))
        if subprocess.call(cmd, stdout=stdout, stderr=stderr):
            error = True

    if options.mail: _send_reply_mail(stdout, msg, options.mail, packages=packages, error=error)

def main():
    description = """Mageia GNOME commands."""
    epilog="""Report bugs to Olav Vitters"""
    parser = argparse.ArgumentParser(description=description,epilog=epilog)
    parser.add_argument("-l", "--limit", type=argparse.FileType('r', 0),
                        dest="limit_upstream", metavar="FILE",
                        help="File containing upstream names")
    parser.add_argument("-d", "--distro", action="store", dest="distro",
                                       help="Distribution release")

    # SUBPARSERS
    subparsers = parser.add_subparsers(title='subcommands')
    #   install
    subparser = subparsers.add_parser('co', help='checkout all GNOME modules')
    subparser.set_defaults(
        func=cmd_co
    )

    subparser = subparsers.add_parser('packages', help='list all GNOME packages')
    subparser.add_argument("-m", "--m", action="store_true", dest="upstream",
                                       help="Show upstream module")
    subparser.add_argument(      "--version", action="store_true", dest="show_version",
                                       help="Show version numbers")
    subparser.add_argument(      "--diff", action="store_true", dest="diff",
                                       help="Only show packages with different version")
    subparser.add_argument(      "--sort", type=argparse.FileType('r', 0),
                        dest="sort", metavar="FILE",
                        help="Sort packages according to order in given FILE")

    subparser.set_defaults(
        func=cmd_ls, upstream=False, show_version=False, diff=False
    )

    subparser = subparsers.add_parser('group-owner', help='list packages by group')
    subparser.add_argument('group', metavar="GROUP", nargs='+')

    subparser.set_defaults(
            func=cmd_group_owner
    )

    subparser = subparsers.add_parser('check-latest', help='check for latest version of packages')
    subparser.set_defaults(
        func=cmd_check_latest
    )

    subparser = subparsers.add_parser('patches', help='list all GNOME patches')
    subparser.add_argument("-p", "--path", action="store_true", dest="path",
                                       help="Show full path to patch")
    subparser.set_defaults(
        func=cmd_patches, path=False
    )

    subparser = subparsers.add_parser('dep3', help='Add dep3 headers')
    subparser.add_argument("patch", help="Patch")
    subparser.set_defaults(
        func=cmd_dep3, path=False
    )

    subparser = subparsers.add_parser('increase', help='Increase version number')
    subparser.add_argument("package", help="Package name")
    subparser.add_argument("version", help="Version number")
    subparser.add_argument("-f", "--force", action="store_true", dest="force",
                                       help="Override warnings, just do it")
    subparser.add_argument("-u", "--upstream", action="store_true", dest="upstream",
                                       help="Package name reflects the upstream name")
    subparser.add_argument("-s", "--submit", action="store_true", dest="submit",
                                       help="Commit changes and submit")
    subparser.add_argument(      "--no-submit", action="store_false", dest="submit",
                                       help="Do not commit changes and submit")
    subparser.add_argument("-a", "--algorithm", choices=hashlib.algorithms, dest="algo",
                                       help="Hash algorithm")
    subparser.add_argument("--hash", dest="hexdigest",
                                       help="Hexdigest of the hash")
    subparser.set_defaults(
        func=cmd_package_new_version, submit=argparse.SUPPRESS, upstream=False, hexdigest=None, algo="sha256",
        force=False
    )

    subparser = subparsers.add_parser('gnome-release-email', help='Submit packages based on GNOME ftp-release-list email')
    subparser.add_argument("-m", "--mail", help="Email address to send the progress to")
    subparser.add_argument("-w", "--wait", action="store_true",
                                 help="Wait before trying to retrieve the new version")
    subparser.add_argument("-s", "--submit", action="store_true", dest="submit",
                                       help="Commit changes and submit")
    subparser.add_argument("-f", "--force", action="store_true",
                                 help="Force submission")
    subparser.set_defaults(
        func=cmd_parse_ftp_release_list, force=False, wait=False
    )

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(2)

    options = parser.parse_args()
    if options.limit_upstream:
        Upstream.limit = set(options.limit_upstream.read().strip("\n").splitlines())

    if not hasattr(options, 'submit'):
        options.submit = not options.distro

    if options.distro:
        Downstream.PKGROOT = os.path.join('~/pkgs', options.distro)
        Downstream.MEDIA = "Core Release {0} Source,Core {0} Updates Source,Core {0} Updates Testing Source".format(options.distro)
        Downstream.DISTRO = options.distro

    try:
        options.func(options, parser)
    except KeyboardInterrupt:
        print('Interrupted')
        sys.exit(1)
    except EOFError:
        print('EOF')
        sys.exit(1)
    except IOError, e:
        if e.errno != errno.EPIPE:
            raise
        sys.exit(0)

if __name__ == "__main__":
    os.environ['PYTHONUNBUFFERED'] = '1'
    main()