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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#! python
# SPDX-License-Identifier: LGPL-2.1-or-later

# (c) 2006 Werner Mayer LGPL
#
# FreeCAD RevInfo script to get the revision information from Subversion, Bazaar, and Git.
#
# Under Linux the Subversion tool SubWCRev shipped with TortoiseSVN isn't
# available which is provided by this script.
# 2012/02/01: The script was extended to support git
# 2011/02/05: The script was extended to support also Bazaar

import os, sys, re, datetime, time, getopt
from urllib.parse import urlparse
import xml.sax
import xml.sax.handler
import xml.sax.xmlreader

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO


# SAX handler to parse the subversion output
class SvnHandler(xml.sax.handler.ContentHandler):
    def __init__(self):
        super().__init__()
        self.inUrl = 0
        self.inDate = 0
        self.mapping = {}

    def startElement(self, name, attributes):
        if name == "entry":
            self.buffer = ""
            self.mapping["Rev"] = attributes["revision"]
        elif name == "url":
            self.inUrl = 1
        elif name == "date":
            self.inDate = 1

    def characters(self, data):
        if self.inUrl:
            self.buffer += data
        elif self.inDate:
            self.buffer += data

    def endElement(self, name):
        if name == "url":
            self.inUrl = 0
            self.mapping["Url"] = self.buffer
            self.buffer = ""
        elif name == "date":
            self.inDate = 0
            self.mapping["Date"] = self.buffer
            self.buffer = ""


class VersionControl:
    def __init__(self):
        self.rev = ""
        self.date = ""
        self.url = ""

    def extractInfo(self, srcdir, bindir):
        return False

    def printInfo(self):
        print("")

    def writeVersion(self, lines):
        content = []
        for line in lines:
            line = line.replace("$WCREV$", self.rev)
            line = line.replace("$WCDATE$", self.date)
            line = line.replace("$WCURL$", self.url)
            content.append(line)
        return content


class UnknownControl(VersionControl):
    def extractInfo(self, srcdir, bindir):
        # Do not overwrite existing file with almost useless information
        if os.path.exists(bindir + "/src/Build/Version.h.out"):
            return False
        self.rev = "Unknown"
        self.date = "Unknown"
        self.url = "Unknown"
        return True

    def printInfo(self):
        print("Unknown version control")


class DebianChangelog(VersionControl):
    def extractInfo(self, srcdir, bindir):
        # Do not overwrite existing file with almost useless information
        if os.path.exists(bindir + "/src/Build/Version.h.out"):
            return False
        try:
            f = open(srcdir + "/debian/changelog")
        except Exception:
            return False
        c = f.readline()
        f.close()
        r = re.search("bzr(\\d+)", c)
        if r is not None:
            self.rev = r.groups()[0] + " (Launchpad)"

        t = time.localtime()
        self.date = ("%d/%02d/%02d %02d:%02d:%02d") % (
            t.tm_year,
            t.tm_mon,
            t.tm_mday,
            t.tm_hour,
            t.tm_min,
            t.tm_sec,
        )
        self.url = "https://code.launchpad.net/~vcs-imports/freecad/trunk"
        return True

    def printInfo(self):
        print("debian/changelog")


class BazaarControl(VersionControl):
    def extractInfo(self, srcdir, bindir):
        info = os.popen("bzr log -l 1 %s" % (srcdir)).read()
        if len(info) == 0:
            return False
        lines = info.split("\n")
        for i in lines:
            r = re.match("^revno: (\\d+)$", i)
            if r is not None:
                self.rev = r.groups()[0]
                continue
            r = re.match("^timestamp: (\\w+ \\d+-\\d+-\\d+ \\d+:\\d+:\\d+)", i)
            if r is not None:
                self.date = r.groups()[0]
                continue
        return True

    def printInfo(self):
        print("bazaar")


class DebianGitHub(VersionControl):
    # https://gist.github.com/0penBrain/7be59a48aba778c955d992aa69e524c5
    # https://gist.github.com/yershalom/a7c08f9441d1aadb13777bce4c7cdc3b
    # https://github.community/t5/GitHub-API-Development-and/How-to-get-all-branches-which-contain-a-commit-from-SHA-using/td-p/25006
    def extractInfo(self, srcdir, bindir):
        try:
            f = open(srcdir + "/debian/git-build-recipe.manifest")
        except Exception:
            return False

        # Read the first two lines
        recipe = f.readline()
        commit = f.readline()
        f.close()

        base_url = "https://api.github.com"
        owner = "FreeCAD"
        repo = "FreeCAD"
        sha = commit[commit.rfind(":") + 1 : -1]
        self.hash = sha

        try:
            import requests

            request_url = "{}/repos/{}/{}/commits?per_page=1&sha={}".format(
                base_url, owner, repo, sha
            )
            commit_req = requests.get(request_url)
            if not commit_req.ok:
                return False

            commit_date = commit_req.headers.get("last-modified")

        except Exception:
            # if connection fails then use the date of the file git-build-recipe.manifest
            commit_date = recipe[recipe.rfind("~") + 1 : -1]

        try:
            # Try to convert into the same format as GitControl
            t = time.strptime(commit_date, "%a, %d %b %Y %H:%M:%S GMT")
            commit_date = ("%d/%02d/%02d %02d:%02d:%02d") % (
                t.tm_year,
                t.tm_mon,
                t.tm_mday,
                t.tm_hour,
                t.tm_min,
                t.tm_sec,
            )
        except Exception:
            t = time.strptime(commit_date, "%Y%m%d%H%M")
            commit_date = ("%d/%02d/%02d %02d:%02d:%02d") % (
                t.tm_year,
                t.tm_mon,
                t.tm_mday,
                t.tm_hour,
                t.tm_min,
                t.tm_sec,
            )

        self.date = commit_date
        self.branch = "unknown"

        try:
            # Try to determine the branch of the sha
            # There is no function of the rest API of GH but with the url below we get HTML code
            branch_url = "https://github.com/{}/{}/branch_commits/{}".format(owner, repo, sha)
            branch_req = requests.get(branch_url)
            if branch_req.ok:
                html = branch_req.text
                pattern = '<li class="branch"><a href='
                start = html.find(pattern) + len(pattern)
                end = html.find("\n", start)
                link = html[start:end]
                start = link.find(">") + 1
                end = link.find("<", start)
                self.branch = link[start:end]

            link = commit_req.headers.get("link")
            beg = link.rfind("&page=") + 6
            end = link.rfind(">")
            self.rev = link[beg:end] + " (GitHub)"
        except Exception:
            pass

        self.url = "git://github.com/{}/{}.git {}".format(owner, repo, self.branch)
        return True

    def writeVersion(self, lines):
        content = VersionControl.writeVersion(self, lines)
        content.append("// Git relevant stuff\n")
        content.append('#define FCRepositoryHash   "%s"\n' % (self.hash))
        content.append('#define FCRepositoryBranch "%s"\n' % (self.branch))
        return content

    def printInfo(self):
        print("Debian/GitHub")


class GitControl(VersionControl):
    # http://www.hermanradtke.com/blog/canonical-version-numbers-with-git/
    # http://blog.marcingil.com/2011/11/creating-build-numbers-using-git-commits/
    # http://gitref.org/remotes/#fetch
    # http://cworth.org/hgbook-git/tour/
    # http://git.or.cz/course/svn.html
    # git help log
    def getremotes(self):
        """return a mapping of remotes and their fetch urls"""
        rr = os.popen("git remote -v")
        rrstr = rr.read().strip()
        if rr.close() is None:  # exit code == 0
            self.remotes = dict(
                l[:-8].split("\t") for l in rrstr.splitlines() if l.endswith(" (fetch)")
            )
            self.branchlst = (
                os.popen("git show -s --pretty=%d HEAD").read().strip(" ()\n").split(", ")
            )  # used for possible remotes

    def geturl(self):
        urls = []
        for ref in self.branchlst:
            if "/" in ref:
                remote, branch = ref.split("/", 1)
                if remote in self.remotes:
                    url = self.remotes[remote]
                    # rewrite github to public url
                    match = re.match(r"git@github\.com:(\S+?)/(\S+\.git)", url) or re.match(
                        r"https://github\.com/(\S+)/(\S+\.git)", url
                    )
                    if match is not None:
                        url = "git://github.com/%s/%s" % match.groups()
                    match = re.match(r"ssh://\S+?@(\S+)", url)
                    if match is not None:
                        url = "git://%s" % match.group(1)
                    parsed_url = urlparse(url)
                    entryscore = (
                        url == "git://github.com/FreeCAD/FreeCAD.git",
                        parsed_url.netloc == "github.com",
                        branch == self.branch,
                        branch == "main",
                        "@" not in url,
                    )
                    # used for sorting the list
                    if branch == self.branch:  # add branch name
                        url = "%s %s" % (url, branch)
                    urls.append((entryscore, url))
        if len(urls) > 0:
            self.url = sorted(urls)[-1][1]
        else:
            self.url = "Unknown"

    def revisionNumber(self, srcdir, origin=None):
        """sets the revision number

        for main and release branches all commits are counted

        for other branches. The version number is split in to two parts:

        The first number reflects the number of commits in common with the

        blessed main repository. The second part (separated by " +") reflects

        the number of commits that are different from the main repository"""
        referencecommit = "7d8e53aaab17961d85c5009de34f69f2af084e8b"
        referencerevision = 14555

        result = None
        null_device = "nul" if os.name == "nt" else "/dev/null"
        countallfh = os.popen(f"git rev-list --count {referencecommit}..HEAD 2>{null_device}")
        countallstr = countallfh.read().strip()
        if countallfh.close() is not None:  # reference commit not present, use the date
            date_object = datetime.datetime.strptime(self.date, "%Y/%m/%d %H:%M:%S")
            formatted_date = date_object.strftime("%Y%m%d")
            self.rev = f"{formatted_date} (Git shallow)"
            return
        else:
            countall = int(countallstr)

        if (
            origin is not None
            and self.branch.lower() != "main"
            and "release" not in self.branch.lower()
        ):
            mbfh = os.popen("git merge-base %s/main HEAD" % origin)
            mergebase = mbfh.read().strip()
            if mbfh.close() is None:  # exit code == 0
                try:
                    countmergebase = int(
                        os.popen("git rev-list --count %s..%s" % (referencecommit, mergebase))
                        .read()
                        .strip()
                    )
                    if countall > countmergebase:
                        result = "%04d +%d (Git)" % (
                            countmergebase + referencerevision,
                            countall - countmergebase,
                        )
                except ValueError:
                    pass
        self.rev = result or ("%04d (Git)" % (countall + referencerevision))

    def namebranchbyparents(self):
        """name multiple branches in case that the last commit was a merge

        a merge is identified by having two or more parents

        if the describe does not return a ref name (the hash is added)

        if one parent is the main and the second one has no ref name, one branch was

        merged."""
        parents = os.popen("git log -n1 --pretty=%P").read().strip().split(" ")
        if len(parents) >= 2:  # merge commit
            parentrefs = []
            names = []
            hasnames = 0
            for p in parents:
                refs = os.popen("git show -s --pretty=%%d %s" % p).read().strip(" ()\n").split(", ")
                if refs[0] != "":  # has a ref name
                    parentrefs.append(refs)
                    names.append(refs[-1])
                    hasnames += 1
                else:
                    parentrefs.append(p)
                    names.append(p[:7])
            if hasnames >= 2:  # merging main into dev is not enough
                self.branch = ",".join(names)

    def extractInfo(self, srcdir, bindir):
        self.hash = os.popen("git log -1 --pretty=format:%H").read().strip()
        if self.hash == "":
            return False  # not a git repo
        # date/time
        import time

        info = os.popen("git log -1 --date=raw --pretty=format:%cd").read()
        # commit time is more meaningful than author time
        # use UTC
        self.date = time.strftime(
            "%Y/%m/%d %H:%M:%S", time.gmtime(float(info.strip().split(" ", 1)[0]))
        )
        for self.branch in os.popen("git branch --no-color").read().split("\n"):
            if re.match(r"\*", self.branch) is not None:
                break
        self.branch = self.branch[2:]
        self.getremotes()  # setup self.remotes and branchlst

        self.geturl()
        origin = None  # remote for the blessed main
        for fetchurl in (
            "git@github.com:FreeCAD/FreeCAD.git",
            "https://github.com/FreeCAD/FreeCAD.git",
        ):
            for key, url in self.remotes.items():
                if fetchurl in url:
                    origin = key
                    break
            if origin is not None:
                break

        self.revisionNumber(srcdir, origin)
        if self.branch.lower() != "main" and "release" not in self.branch.lower():
            self.namebranchbyparents()
        if self.branch == "(no branch)":  # check for remote branches
            if len(self.branchlst) >= 2:
                self.branch = self.branchlst[1]
            else:  # guess
                self.branch = "(%s)" % os.popen("git describe --all --dirty").read().strip()
        # if the branch name contained any slashes but was not a remote
        # there might be no result by now. Hence we assume origin
        if self.url == "Unknown":
            for i in info:
                r = re.match("origin\\W+(\\S+)", i)
                if r is not None:
                    self.url = r.groups()[0]
                    break
        return True

    def printInfo(self):
        print("git")

    def writeVersion(self, lines):
        content = VersionControl.writeVersion(self, lines)
        content.append("// Git relevant stuff\n")
        content.append('#define FCRepositoryHash   "%s"\n' % (self.hash))
        content.append('#define FCRepositoryBranch "%s"\n' % (self.branch))
        return content


class MercurialControl(VersionControl):
    def extractInfo(self, srcdir, bindir):
        return False

    def printInfo(self):
        print("mercurial")


class Subversion(VersionControl):
    def extractInfo(self, srcdir, bindir):
        parser = xml.sax.make_parser()
        handler = SvnHandler()
        parser.setContentHandler(handler)

        # Create an XML stream with the required information and read in with a SAX parser
        Ver = os.popen("svnversion %s -n" % (srcdir)).read()
        Info = os.popen("svn info %s --xml" % (srcdir)).read()
        try:
            inpsrc = xml.sax.InputSource()
            strio = StringIO.StringIO(Info)
            inpsrc.setByteStream(strio)
            parser.parse(inpsrc)
        except Exception:
            return False

        # Information of the Subversion stuff
        self.url = handler.mapping["Url"]
        self.rev = handler.mapping["Rev"]
        self.date = handler.mapping["Date"]
        self.date = self.date[:19]
        # Same format as SubWCRev does
        self.date = self.date.replace("T", " ")
        self.date = self.date.replace("-", "/")

        # Date is given as GMT. Now we must convert to local date.
        m = time.strptime(self.date, "%Y/%m/%d %H:%M:%S")
        # Copy the tuple and set tm_isdst to 0 because it's GMT
        l = (
            m.tm_year,
            m.tm_mon,
            m.tm_mday,
            m.tm_hour,
            m.tm_min,
            m.tm_sec,
            m.tm_wday,
            m.tm_yday,
            0,
        )
        # Take timezone into account
        t = time.mktime(l) - time.timezone
        self.date = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(t))

        # Get the current local date
        self.time = time.strftime("%Y/%m/%d %H:%M:%S")

        self.mods = "Src not modified"
        self.mixed = "Src not mixed"
        self.range = self.rev

        # if version string ends with an 'M'
        r = re.search("M$", Ver)
        if r is not None:
            self.mods = "Src modified"

        # if version string contains a range
        r = re.match("^\\d+\\:\\d+", Ver)
        if r is not None:
            self.mixed = "Src mixed"
            self.range = Ver[: r.end()]
        return True

    def printInfo(self):
        print("subversion")


def main():
    # if(len(sys.argv) != 2):
    #    sys.stderr.write("Usage:  SubWCRev \"`svn info .. --xml`\"\n")

    srcdir = "."
    bindir = "."
    try:
        opts, args = getopt.getopt(sys.argv[1:], "sb:", ["srcdir=", "bindir="])
    except getopt.GetoptError:
        pass

    for o, a in opts:
        if o in ("-s", "--srcdir"):
            srcdir = a
        if o in ("-b", "--bindir"):
            bindir = a

    vcs = [
        GitControl(),
        DebianGitHub(),
        BazaarControl(),
        Subversion(),
        MercurialControl(),
        DebianChangelog(),
        UnknownControl(),
    ]
    for i in vcs:
        if i.extractInfo(srcdir, bindir):
            # Open the template file and the version file
            inp = open("%s/src/Build/Version.h.in" % (bindir))
            lines = inp.readlines()
            inp.close()
            lines = i.writeVersion(lines)
            out = open("%s/src/Build/Version.h.out" % (bindir), "w")
            out.writelines(lines)
            out.write("\n")
            out.close()
            i.printInfo()
            sys.stdout.write("%s/src/Build/Version.h.out written\n" % (bindir))
            break


if __name__ == "__main__":
    main()