summaryrefslogtreecommitdiffstats
path: root/tools/scripts/cipd_package.py
blob: 8ab448c86606486614ac637547af8765fee1bf24 (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
# Copyright (C) 2023 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

import glob
import os
import re
import subprocess
import sys
import version_resolver as resolver

androidx_package_name = 'chromium/third_party/androidx'

def subprocessCall(args):
    print(args)
    return subprocess.call(args)

def subprocessCheckOutput(args):
    print(args)
    return subprocess.check_output(args).decode()

class PackageDEPSParser(resolver.DEPSParser):
    def __init__(self):
        super().__init__()

    def createEntitiesFromScope(self, scope):
        entities = []
        for dep in scope:
            if (type(scope[dep]) == dict and 'packages' in scope[dep] \
            and 'dep_type' in scope[dep] and scope[dep]['dep_type'] == 'cipd'):
                subdir = self.subdir(dep)
                if subdir is None:
                    continue
                entity = CIPDEntity(subdir, sp=self.topmost_supermodule_path_prefix)
                path = entity.pathRelativeToTopMostSupermodule()
                for pkg in scope[dep]['packages']:
                   p = Package(pkg['package'], pkg['version'], path)
                   entity.packages.append(p)
                entities.append(entity)
        return entities

    def parse(self, deps_content, module_whitelist = []):
        exec(deps_content, self.global_scope, self.local_scope)
        entities = []
        entities.extend(self.createEntitiesFromScope(self.local_scope['deps']))
        return entities

class Package:
    def __init__(self, name, version, path):
        self.name = name
        self.version = version
        self.path = path
        self.fileName = name.replace('/','.') + '.pkg'

    def fetchAndDeploy(self):
        if os.path.isdir(self.path):
            currentDir = os.getcwd()
            os.chdir(self.path)
            subprocessCall(['cipd', 'pkg-fetch', self.name ,'-out' , self.fileName, '-version', self.version ])
            subprocessCall(['cipd', 'pkg-deploy', self.fileName , '-root', '.' ])
            os.chdir(currentDir)
        else:
            print('-- missing directory' + self.path + ' skipping')

    def listFiles(self):
        if os.path.isdir(self.path):
            currentDir = os.getcwd()
            os.chdir(self.path)
            files = []
            if os.path.isfile(self.fileName):
                files = subprocessCheckOutput(['cipd', 'pkg-inspect', self.fileName]).splitlines()
                files = map( lambda x: x.replace( ' F ', self.path + '/'), files)
            else:
                print('-- missing package file ' + self.path + ' skipping')
            os.chdir(currentDir)
            return files
        else:
            print('-- missing directory' + self.path + ' skipping')
        return []

class CIPDEntity:
    def __init__(self, path='', packages=[], os=[], sp=''):
        self.path = path
        self.packages = []
        self.topmost_supermodule_path_prefix = sp

    def pathRelativeToTopMostSupermodule(self):
        return os.path.normpath(os.path.join(self.topmost_supermodule_path_prefix, self.path))

    def findPackage(self, package):
        pkg = [p for p in self.packages if p.name == package]
        if len(pkg) > 1:
            raise Exception(package + " is ambiguous package name for" + self.path)
        return pkg[0] if pkg else None

    def readEntities(self):
        cipd_packages = []
        cipd_packages = resolver.read(PackageDEPSParser)
        print('DEPS file provides the following packages:')
        for cipd_package in cipd_packages:
            print(cipd_package.pathRelativeToTopMostSupermodule() + ':')
            for package in cipd_package.packages:
                print(' * {:<80}'.format(package.name) + package.version)
        return cipd_packages