aboutsummaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/fetch/__init__.py
blob: b452751c7b24ca7651e30c40ede39ae76474ed67 (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
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
"""
BitBake 'Fetch' implementations

Classes for obtaining upstream sources for the
BitBake build tools.
"""

# Copyright (C) 2003, 2004  Chris Larson
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Based on functions from the base bb module, Copyright 2003 Holger Schurig

from __future__ import absolute_import
from __future__ import print_function
import os, re
import logging
import bb
from   bb import data
from   bb import persist_data
from   bb import utils

__version__ = "1"

logger = logging.getLogger("BitBake.Fetch")

class MalformedUrl(Exception):
    """Exception raised when encountering an invalid url"""

class FetchError(Exception):
    """Exception raised when a download fails"""

class NoMethodError(Exception):
    """Exception raised when there is no method to obtain a supplied url or set of urls"""

class MissingParameterError(Exception):
    """Exception raised when a fetch method is missing a critical parameter in the url"""

class ParameterError(Exception):
    """Exception raised when a url cannot be proccessed due to invalid parameters."""

class MD5SumError(Exception):
    """Exception raised when a MD5SUM of a file does not match the expected one"""

class InvalidSRCREV(Exception):
    """Exception raised when an invalid SRCREV is encountered"""

def decodeurl(url):
    """Decodes an URL into the tokens (scheme, network location, path,
    user, password, parameters).
    """

    m = re.compile('(?P<type>[^:]*)://((?P<user>.+)@)?(?P<location>[^;]+)(;(?P<parm>.*))?').match(url)
    if not m:
        raise MalformedUrl(url)

    type = m.group('type')
    location = m.group('location')
    if not location:
        raise MalformedUrl(url)
    user = m.group('user')
    parm = m.group('parm')

    locidx = location.find('/')
    if locidx != -1 and type.lower() != 'file':
        host = location[:locidx]
        path = location[locidx:]
    else:
        host = ""
        path = location
    if user:
        m = re.compile('(?P<user>[^:]+)(:?(?P<pswd>.*))').match(user)
        if m:
            user = m.group('user')
            pswd = m.group('pswd')
    else:
        user = ''
        pswd = ''

    p = {}
    if parm:
        for s in parm.split(';'):
            s1, s2 = s.split('=')
            p[s1] = s2

    return (type, host, path, user, pswd, p)

def encodeurl(decoded):
    """Encodes a URL from tokens (scheme, network location, path,
    user, password, parameters).
    """

    (type, host, path, user, pswd, p) = decoded

    if not type or not path:
        raise MissingParameterError("Type or path url components missing when encoding %s" % decoded)
    url = '%s://' % type
    if user:
        url += "%s" % user
        if pswd:
            url += ":%s" % pswd
        url += "@"
    if host:
        url += "%s" % host
    url += "%s" % path
    if p:
        for parm in p:
            url += ";%s=%s" % (parm, p[parm])

    return url

def uri_replace(uri, uri_find, uri_replace, d):
    if not uri or not uri_find or not uri_replace:
        logger.debug(1, "uri_replace: passed an undefined value, not replacing")
    uri_decoded = list(decodeurl(uri))
    uri_find_decoded = list(decodeurl(uri_find))
    uri_replace_decoded = list(decodeurl(uri_replace))
    result_decoded = ['', '', '', '', '', {}]
    for i in uri_find_decoded:
        loc = uri_find_decoded.index(i)
        result_decoded[loc] = uri_decoded[loc]
        if isinstance(i, basestring):
            if (re.match(i, uri_decoded[loc])):
                result_decoded[loc] = re.sub(i, uri_replace_decoded[loc], uri_decoded[loc])
                if uri_find_decoded.index(i) == 2:
                    if d:
                        localfn = bb.fetch.localpath(uri, d)
                        if localfn:
                            result_decoded[loc] = os.path.join(os.path.dirname(result_decoded[loc]), os.path.basename(bb.fetch.localpath(uri, d)))
            else:
                return uri
    return encodeurl(result_decoded)

methods = []
urldata_cache = {}
saved_headrevs = {}

def fetcher_init(d):
    """
    Called to initialize the fetchers once the configuration data is known.
    Calls before this must not hit the cache.
    """
    pd = persist_data.persist(d)
    # When to drop SCM head revisions controlled by user policy
    srcrev_policy = bb.data.getVar('BB_SRCREV_POLICY', d, 1) or "clear"
    if srcrev_policy == "cache":
        logger.debug(1, "Keeping SRCREV cache due to cache policy of: %s", srcrev_policy)
    elif srcrev_policy == "clear":
        logger.debug(1, "Clearing SRCREV cache due to cache policy of: %s", srcrev_policy)
        try:
            bb.fetch.saved_headrevs = pd['BB_URI_HEADREVS'].items()
        except:
            pass
        del pd['BB_URI_HEADREVS']
    else:
        raise FetchError("Invalid SRCREV cache policy of: %s" % srcrev_policy)

    for m in methods:
        if hasattr(m, "init"):
            m.init(d)

def fetcher_compare_revisions(d):
    """
    Compare the revisions in the persistant cache with current values and
    return true/false on whether they've changed.
    """

    pd = persist_data.persist(d)
    data = pd['BB_URI_HEADREVS'].items()
    data2 = bb.fetch.saved_headrevs

    changed = False
    for key in data:
        if key not in data2 or data2[key] != data[key]:
            logger.debug(1, "%s changed", key)
            changed = True
            return True
        else:
            logger.debug(2, "%s did not change", key)
    return False

# Function call order is usually:
#   1. init
#   2. go
#   3. localpaths
# localpath can be called at any time

def init(urls, d, setup = True):
    urldata = {}

    fn = bb.data.getVar('FILE', d, 1)
    if fn in urldata_cache:
        urldata = urldata_cache[fn]

    for url in urls:
        if url not in urldata:
            urldata[url] = FetchData(url, d)

    if setup:
        for url in urldata:
            if not urldata[url].setup:
                urldata[url].setup_localpath(d)

    urldata_cache[fn] = urldata
    return urldata

def mirror_from_string(data):
    return [ i.split() for i in (data or "").replace('\\n','\n').split('\n') if i ]

def verify_checksum(u, ud, d):
    """
    verify the MD5 and SHA256 checksum for downloaded src

    return value:
        - True: checksum matched
        - False: checksum unmatched

    if checksum is missing in recipes file, "BB_STRICT_CHECKSUM" decide the return value.
    if BB_STRICT_CHECKSUM = "1" then return false as unmatched, otherwise return true as
    matched
    """

    if not ud.type in ["http", "https", "ftp", "ftps"]:
        return

    md5data = bb.utils.md5_file(ud.localpath)
    sha256data = bb.utils.sha256_file(ud.localpath)

    if (ud.md5_expected == None or ud.sha256_expected == None):
        logger.warn('Missing SRC_URI checksum for %s, consider adding to the recipe:\n'
                    'SRC_URI[%s] = "%s"\nSRC_URI[%s] = "%s"',
                    ud.localpath, ud.md5_name, md5data,
                    ud.sha256_name, sha256data)
        if bb.data.getVar("BB_STRICT_CHECKSUM", d, True) == "1":
            raise FetchError("No checksum specified for %s." % u)
        return

    if (ud.md5_expected != md5data or ud.sha256_expected != sha256data):
        logger.error('The checksums for "%s" did not match.\n'
                     '  MD5: expected "%s", got "%s"\n'
                     '  SHA256: expected "%s", got "%s"\n',
                     ud.localpath, ud.md5_expected, md5data,
                     ud.sha256_expected, sha256data)
        raise FetchError("%s checksum mismatch." % u)

def go(d, urls = None):
    """
    Fetch all urls
    init must have previously been called
    """
    if not urls:
        urls = d.getVar("SRC_URI", 1).split()
    urldata = init(urls, d, True)

    for u in urls:
        ud = urldata[u]
        m = ud.method
        localpath = ""

        if not ud.localfile:
            continue

        lf = bb.utils.lockfile(ud.lockfile)

        if m.try_premirror(u, ud, d):
            # First try fetching uri, u, from PREMIRRORS
            mirrors = mirror_from_string(bb.data.getVar('PREMIRRORS', d, True))
            localpath = try_mirrors(d, u, mirrors, False, m.forcefetch(u, ud, d))
        elif os.path.exists(ud.localfile):
            localpath = ud.localfile

        # Need to re-test forcefetch() which will return true if our copy is too old
        if m.forcefetch(u, ud, d) or not localpath:
            # Next try fetching from the original uri, u
            try:
                m.go(u, ud, d)
                localpath = ud.localpath
            except FetchError:
                # Remove any incomplete file
                bb.utils.remove(ud.localpath)
                # Finally, try fetching uri, u, from MIRRORS
                mirrors = mirror_from_string(bb.data.getVar('MIRRORS', d, True))
                localpath = try_mirrors (d, u, mirrors)
                if not localpath or not os.path.exists(localpath):
                    raise FetchError("Unable to fetch URL %s from any source." % u)

        ud.localpath = localpath

        if os.path.exists(ud.md5):
            # Touch the md5 file to show active use of the download
            try:
                os.utime(ud.md5, None)
            except:
                # Errors aren't fatal here
                pass
        else:
            # Only check the checksums if we've not seen this item before
            verify_checksum(u, ud, d)
            Fetch.write_md5sum(u, ud, d)

        bb.utils.unlockfile(lf)

def checkstatus(d, urls = None):
    """
    Check all urls exist upstream
    init must have previously been called
    """
    urldata = init([], d, True)

    if not urls:
        urls = urldata

    for u in urls:
        ud = urldata[u]
        m = ud.method
        logger.debug(1, "Testing URL %s", u)
        # First try checking uri, u, from PREMIRRORS
        mirrors = mirror_from_string(bb.data.getVar('PREMIRRORS', d, True))
        ret = try_mirrors(d, u, mirrors, True)
        if not ret:
            # Next try checking from the original uri, u
            try:
                ret = m.checkstatus(u, ud, d)
            except:
                # Finally, try checking uri, u, from MIRRORS
                mirrors = mirror_from_string(bb.data.getVar('MIRRORS', d, True))
                ret = try_mirrors (d, u, mirrors, True)

        if not ret:
            raise FetchError("URL %s doesn't work" % u)

def localpaths(d):
    """
    Return a list of the local filenames, assuming successful fetch
    """
    local = []
    urldata = init([], d, True)

    for u in urldata:
        ud = urldata[u]
        local.append(ud.localpath)

    return local

srcrev_internal_call = False

def get_autorev(d):
    return get_srcrev(d)

def get_srcrev(d):
    """
    Return the version string for the current package
    (usually to be used as PV)
    Most packages usually only have one SCM so we just pass on the call.
    In the multi SCM case, we build a value based on SRCREV_FORMAT which must
    have been set.
    """

    #
    # Ugly code alert. localpath in the fetchers will try to evaluate SRCREV which
    # could translate into a call to here. If it does, we need to catch this
    # and provide some way so it knows get_srcrev is active instead of being
    # some number etc. hence the srcrev_internal_call tracking and the magic
    # "SRCREVINACTION" return value.
    #
    # Neater solutions welcome!
    #
    if bb.fetch.srcrev_internal_call:
        return "SRCREVINACTION"

    scms = []

    # Only call setup_localpath on URIs which supports_srcrev()
    urldata = init(bb.data.getVar('SRC_URI', d, 1).split(), d, False)
    for u in urldata:
        ud = urldata[u]
        if ud.method.supports_srcrev():
            if not ud.setup:
                ud.setup_localpath(d)
            scms.append(u)

    if len(scms) == 0:
        logger.error("SRCREV was used yet no valid SCM was found in SRC_URI")
        raise ParameterError

    if bb.data.getVar('BB_SRCREV_POLICY', d, True) != "cache":
        bb.data.setVar('__BB_DONT_CACHE', '1', d)

    if len(scms) == 1:
        return urldata[scms[0]].method.sortable_revision(scms[0], urldata[scms[0]], d)

    #
    # Mutiple SCMs are in SRC_URI so we resort to SRCREV_FORMAT
    #
    format = bb.data.getVar('SRCREV_FORMAT', d, 1)
    if not format:
        logger.error("The SRCREV_FORMAT variable must be set when multiple SCMs are used.")
        raise ParameterError

    for scm in scms:
        if 'name' in urldata[scm].parm:
            name = urldata[scm].parm["name"]
            rev = urldata[scm].method.sortable_revision(scm, urldata[scm], d)
            format = format.replace(name, rev)

    return format

def localpath(url, d, cache = True):
    """
    Called from the parser with cache=False since the cache isn't ready
    at this point. Also called from classed in OE e.g. patch.bbclass
    """
    ud = init([url], d)
    if ud[url].method:
        return ud[url].localpath
    return url

def runfetchcmd(cmd, d, quiet = False):
    """
    Run cmd returning the command output
    Raise an error if interrupted or cmd fails
    Optionally echo command output to stdout
    """

    # Need to export PATH as binary could be in metadata paths
    # rather than host provided
    # Also include some other variables.
    # FIXME: Should really include all export varaiables?
    exportvars = ['PATH', 'GIT_PROXY_COMMAND', 'GIT_PROXY_HOST',
                  'GIT_PROXY_PORT', 'GIT_CONFIG', 'http_proxy', 'ftp_proxy',
                  'https_proxy', 'no_proxy', 'ALL_PROXY', 'all_proxy',
                  'SSH_AUTH_SOCK', 'SSH_AGENT_PID', 'HOME']

    for var in exportvars:
        val = data.getVar(var, d, True)
        if val:
            cmd = 'export ' + var + '=\"%s\"; %s' % (val, cmd)

    logger.debug(1, "Running %s", cmd)

    # redirect stderr to stdout
    stdout_handle = os.popen(cmd + " 2>&1", "r")
    output = ""

    while True:
        line = stdout_handle.readline()
        if not line:
            break
        if not quiet:
            print(line, end=' ')
        output += line

    status = stdout_handle.close() or 0
    signal = status >> 8
    exitstatus = status & 0xff

    if signal:
        raise FetchError("Fetch command %s failed with signal %s, output:\n%s" % (cmd, signal, output))
    elif status != 0:
        raise FetchError("Fetch command %s failed with exit code %s, output:\n%s" % (cmd, status, output))

    return output

def try_mirrors(d, uri, mirrors, check = False, force = False):
    """
    Try to use a mirrored version of the sources.
    This method will be automatically called before the fetchers go.

    d Is a bb.data instance
    uri is the original uri we're trying to download
    mirrors is the list of mirrors we're going to try
    """
    fpath = os.path.join(data.getVar("DL_DIR", d, 1), os.path.basename(uri))
    if not check and os.access(fpath, os.R_OK) and not force:
        logger.debug(1, "%s already exists, skipping checkout.", fpath)
        return fpath

    ld = d.createCopy()
    for (find, replace) in mirrors:
        newuri = uri_replace(uri, find, replace, ld)
        if newuri != uri:
            try:
                ud = FetchData(newuri, ld)
            except bb.fetch.NoMethodError:
                logger.debug(1, "No method for %s", uri)
                continue

            ud.setup_localpath(ld)

            try:
                if check:
                    found = ud.method.checkstatus(newuri, ud, ld)
                    if found:
                        return found
                else:
                    ud.method.go(newuri, ud, ld)
                    return ud.localpath
            except (bb.fetch.MissingParameterError,
                    bb.fetch.FetchError,
                    bb.fetch.MD5SumError):
                import sys
                (type, value, traceback) = sys.exc_info()
                logger.debug(2, "Mirror fetch failure: %s", value)
                bb.utils.remove(ud.localpath)
                continue
    return None


class FetchData(object):
    """
    A class which represents the fetcher state for a given URI.
    """
    def __init__(self, url, d):
        self.localfile = ""
        (self.type, self.host, self.path, self.user, self.pswd, self.parm) = decodeurl(data.expand(url, d))
        self.date = Fetch.getSRCDate(self, d)
        self.url = url
        if not self.user and "user" in self.parm:
            self.user = self.parm["user"]
        if not self.pswd and "pswd" in self.parm:
            self.pswd = self.parm["pswd"]
        self.setup = False

        if "name" in self.parm:
            self.md5_name = "%s.md5sum" % self.parm["name"]
            self.sha256_name = "%s.sha256sum" % self.parm["name"]
        else:
            self.md5_name = "md5sum"
            self.sha256_name = "sha256sum"
        self.md5_expected = bb.data.getVarFlag("SRC_URI", self.md5_name, d)
        self.sha256_expected = bb.data.getVarFlag("SRC_URI", self.sha256_name, d)

        for m in methods:
            if m.supports(url, self, d):
                self.method = m
                return
        raise NoMethodError("Missing implementation for url %s" % url)

    def setup_localpath(self, d):
        self.setup = True
        if "localpath" in self.parm:
            # if user sets localpath for file, use it instead.
            self.localpath = self.parm["localpath"]
            self.basename = os.path.basename(self.localpath)
        else:
            premirrors = bb.data.getVar('PREMIRRORS', d, True)
            local = ""
            if premirrors and self.url:
                aurl = self.url.split(";")[0]
                mirrors = mirror_from_string(premirrors)
                for (find, replace) in mirrors:
                    if replace.startswith("file://"):
                        path = aurl.split("://")[1]
                        path = path.split(";")[0]
                        local = replace.split("://")[1] + os.path.basename(path)
                        if local == aurl or not os.path.exists(local) or os.path.isdir(local):
                            local = ""
                self.localpath = local
            if not local:
                try:
                    bb.fetch.srcrev_internal_call = True
                    self.localpath = self.method.localpath(self.url, self, d)
                finally:
                    bb.fetch.srcrev_internal_call = False
                # We have to clear data's internal caches since the cached value of SRCREV is now wrong.
                # Horrible...
                bb.data.delVar("ISHOULDNEVEREXIST", d)

        if self.localpath is not None:
            # Note: These files should always be in DL_DIR whereas localpath may not be.
            basepath = bb.data.expand("${DL_DIR}/%s" % os.path.basename(self.localpath), d)
            self.md5 = basepath + '.md5'
            self.lockfile = basepath + '.lock'


class Fetch(object):
    """Base class for 'fetch'ing data"""

    def __init__(self, urls = []):
        self.urls = []

    def supports(self, url, urldata, d):
        """
        Check to see if this fetch class supports a given url.
        """
        return 0

    def localpath(self, url, urldata, d):
        """
        Return the local filename of a given url assuming a successful fetch.
        Can also setup variables in urldata for use in go (saving code duplication
        and duplicate code execution)
        """
        return url
    def _strip_leading_slashes(self, relpath):
        """
        Remove leading slash as os.path.join can't cope
        """
        while os.path.isabs(relpath):
            relpath = relpath[1:]
        return relpath

    def setUrls(self, urls):
        self.__urls = urls

    def getUrls(self):
        return self.__urls

    urls = property(getUrls, setUrls, None, "Urls property")

    def forcefetch(self, url, urldata, d):
        """
        Force a fetch, even if localpath exists?
        """
        return False

    def supports_srcrev(self):
        """
        The fetcher supports auto source revisions (SRCREV)
        """
        return False

    def go(self, url, urldata, d):
        """
        Fetch urls
        Assumes localpath was called first
        """
        raise NoMethodError("Missing implementation for url")

    def try_premirror(self, url, urldata, d):
        """
        Should premirrors be used?
        """
        if urldata.method.forcefetch(url, urldata, d):
            return True
        elif os.path.exists(urldata.md5) and os.path.exists(urldata.localfile):
            return False
        else:
            return True

    def checkstatus(self, url, urldata, d):
        """
        Check the status of a URL
        Assumes localpath was called first
        """
        logger.info("URL %s could not be checked for status since no method exists.", url)
        return True

    def getSRCDate(urldata, d):
        """
        Return the SRC Date for the component

        d the bb.data module
        """
        if "srcdate" in urldata.parm:
            return urldata.parm['srcdate']

        pn = data.getVar("PN", d, 1)

        if pn:
            return data.getVar("SRCDATE_%s" % pn, d, 1) or data.getVar("CVSDATE_%s" % pn, d, 1) or data.getVar("SRCDATE", d, 1) or data.getVar("CVSDATE", d, 1) or data.getVar("DATE", d, 1)

        return data.getVar("SRCDATE", d, 1) or data.getVar("CVSDATE", d, 1) or data.getVar("DATE", d, 1)
    getSRCDate = staticmethod(getSRCDate)

    def srcrev_internal_helper(ud, d):
        """
        Return:
            a) a source revision if specified
            b) True if auto srcrev is in action
            c) False otherwise
        """

        if 'rev' in ud.parm:
            return ud.parm['rev']

        if 'tag' in ud.parm:
            return ud.parm['tag']

        rev = None
        if 'name' in ud.parm:
            pn = data.getVar("PN", d, 1)
            rev = data.getVar("SRCREV_%s_pn-%s" % (ud.parm['name'], pn), d, 1)
            if not rev:
                rev = data.getVar("SRCREV_pn-%s_%s" % (pn, ud.parm['name']), d, 1)
            if not rev:
                rev = data.getVar("SRCREV_%s" % (ud.parm['name']), d, 1)
        if not rev:
            rev = data.getVar("SRCREV", d, 1)
        if rev == "INVALID":
            raise InvalidSRCREV("Please set SRCREV to a valid value")
        if not rev:
            return False
        if rev is "SRCREVINACTION":
            return True
        return rev

    srcrev_internal_helper = staticmethod(srcrev_internal_helper)

    def localcount_internal_helper(ud, d):
        """
        Return:
            a) a locked localcount if specified
            b) None otherwise
        """

        localcount = None
        if 'name' in ud.parm:
            pn = data.getVar("PN", d, 1)
            localcount = data.getVar("LOCALCOUNT_" + ud.parm['name'], d, 1)
        if not localcount:
            localcount = data.getVar("LOCALCOUNT", d, 1)
        return localcount

    localcount_internal_helper = staticmethod(localcount_internal_helper)

    def verify_md5sum(ud, got_sum):
        """
        Verify the md5sum we wanted with the one we got
        """
        wanted_sum = ud.parm.get('md5sum')
        if not wanted_sum:
            return True

        return wanted_sum == got_sum
    verify_md5sum = staticmethod(verify_md5sum)

    def write_md5sum(url, ud, d):
        md5data = bb.utils.md5_file(ud.localpath)
        # verify the md5sum
        if not Fetch.verify_md5sum(ud, md5data):
            raise MD5SumError(url)

        md5out = file(ud.md5, 'w')
        md5out.write(md5data)
        md5out.close()
    write_md5sum = staticmethod(write_md5sum)

    def latest_revision(self, url, ud, d):
        """
        Look in the cache for the latest revision, if not present ask the SCM.
        """
        if not hasattr(self, "_latest_revision"):
            raise ParameterError

        pd = persist_data.persist(d)
        revs = pd['BB_URI_HEADREVS']
        key = self.generate_revision_key(url, ud, d)
        rev = revs[key]
        if rev != None:
            return str(rev)

        revs[key] = rev = self._latest_revision(url, ud, d)
        return rev

    def sortable_revision(self, url, ud, d):
        """

        """
        if hasattr(self, "_sortable_revision"):
            return self._sortable_revision(url, ud, d)

        pd = persist_data.persist(d)
        localcounts = pd['BB_URI_LOCALCOUNT']
        key = self.generate_revision_key(url, ud, d)

        latest_rev = self._build_revision(url, ud, d)
        last_rev = localcounts[key + '_rev']
        uselocalcount = bb.data.getVar("BB_LOCALCOUNT_OVERRIDE", d, True) or False
        count = None
        if uselocalcount:
            count = Fetch.localcount_internal_helper(ud, d)
        if count is None:
            count = localcounts[key + '_count']

        if last_rev == latest_rev:
            return str(count + "+" + latest_rev)

        buildindex_provided = hasattr(self, "_sortable_buildindex")
        if buildindex_provided:
            count = self._sortable_buildindex(url, ud, d, latest_rev)

        if count is None:
            count = "0"
        elif uselocalcount or buildindex_provided:
            count = str(count)
        else:
            count = str(int(count) + 1)

        localcounts[key + '_rev'] = latest_rev
        localcounts[key + '_count'] = count

        return str(count + "+" + latest_rev)

    def generate_revision_key(self, url, ud, d):
        key = self._revision_key(url, ud, d)
        return "%s-%s" % (key, bb.data.getVar("PN", d, True) or "")

from . import cvs
from . import git
from . import local
from . import svn
from . import wget
from . import svk
from . import ssh
from . import perforce
from . import bzr
from . import hg
from . import osc
from . import repo

methods.append(local.Local())
methods.append(wget.Wget())
methods.append(svn.Svn())
methods.append(git.Git())
methods.append(cvs.Cvs())
methods.append(svk.Svk())
methods.append(ssh.SSH())
methods.append(perforce.Perforce())
methods.append(bzr.Bzr())
methods.append(hg.Hg())
methods.append(osc.Osc())
methods.append(repo.Repo())