summaryrefslogtreecommitdiff
path: root/librewolf/debian/import-tar.py
blob: d188624b1461c0364225550b59a55c34bece12b6 (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
#!/usr/bin/env python3

import glob
import tarfile
import sys
from optparse import OptionParser
import os

class GitImportTar(object):
    def __init__(self, filename, head):
        self.mark = 1
        self.git = sys.stdout
        self.files = {}
        self.name = filename
        self.mtime = 0
        self.head = head

    def addfile(self, info, prefix = '', file = None):
        if info.isdir():
            return
        self.git.write("blob\n" +
                       "mark :%d\n" % self.mark)
        mode = info.mode
        if info.issym():
            self.git.write("data %d\n" % len(info.linkname) +
                           info.linkname)
            mode = 0o120000
        elif file:
            self.git.write("data %d\n" % (info.size))
            self.git.flush()
            self.git.buffer.write(file.read(info.size))

        self.git.write("\n")
        if not prefix in self.files:
            self.files[prefix] = {}
        self.files[prefix][info.name] = (self.mark, mode)
        self.mark += 1
        if info.mtime > self.mtime:
            self.mtime = info.mtime

    def close(self):
        self.git.write("commit refs/heads/%s\n" % (self.head) +
                       "author T Ar Creator' <tar@example.com> %d +0000\n" % (self.mtime) +
                       "committer T Ar Creator' <tar@example.com> %d +0000\n" % (self.mtime) +
                       "data <<EOM\n" +
                       "Imported from %s\n" % (self.name) +
                       "EOM\n\n" +
                       "from refs/heads/%s^0\n" % (self.head) +
                       "deleteall\n")
        for prefix, fileset in self.files.items():
            basedir = os.path.commonprefix(list(fileset.keys()))
            for path, info in fileset.items():
                (mark, mode) = info
                if mode != 0o120000:
                    mode = 0o755 if (mode & 0o111) else 0o644
                path = path[len(basedir):].lstrip('/')
                if prefix != '':
                    path = prefix + '/' + path
                self.git.write("M %o :%d %s\n" % (mode, mark, path))

def main():
    parser = OptionParser()
    parser.add_option("-H", "--head", dest="head",
        help="import on the given head", metavar="NAME")
    (options, args) = parser.parse_args()

    if not options.head:
        options.head = "upstream"

    (name, ext) = os.path.splitext(args[0])
    if ext[0:2] != '.t':
        (name, ext) = os.path.splitext(name)

    git_import = GitImportTar(os.path.basename(args[0]), options.head)
    for file in [args[0]] + glob.glob(name + "-*" + ext + "*"):
        (_, this_ext) = os.path.splitext(file)
        if this_ext == ext:
            prefix = file[len(name)+1:-len(this_ext)]
        else:
            prefix = file[len(name)+1:-len(this_ext) - len(ext)]
        tar = tarfile.open(file, "r:*")

        while True:
            info = tar.next()
            if not info:
                break
            if info.isfile():
                file = tar.extractfile(info)
                git_import.addfile(info, prefix, file)
            else:
                git_import.addfile(info, prefix)

        tar.close()
    git_import.close()

if __name__ == '__main__':
    main()
bgstack15