-
Notifications
You must be signed in to change notification settings - Fork 18
/
updaterepo.py
executable file
·141 lines (115 loc) · 5.33 KB
/
updaterepo.py
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
#!/usr/bin/python2
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Team Kodi
# http://kodi.tv
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
import os
import shutil
import git
import sys
import logging
import packager
import packager.textures
from distutils.version import LooseVersion
from io import BytesIO
try:
from ConfigParser import ConfigParser
except ImportError:
from configparser import ConfigParser
from collections import namedtuple
Target = namedtuple('Target', ['name', 'branches', 'min_versions'])
config = ConfigParser()
logger = logging.getLogger("updaterepo")
def read_targets():
"""
Reads config file from the remove git configuration repo and returns the targets to generate
addon repository for.
"""
repo = git.Repo(config.get('configuration_repo', 'location'))
remote_name = config.get('configuration_repo', 'remote_name')
ref = remote_name + '/' + config.get('configuration_repo', 'branch')
filename = config.get('configuration_repo', 'filename')
if config.getboolean('debug', 'fetch_remotes'):
repo.remotes[remote_name].fetch()
# python 2 workaround
content = repo.refs[ref].commit.tree[filename].data_stream.read()
target_config = ConfigParser({'branches': None, 'minversions': None})
target_config.readfp(BytesIO(content))
for target in target_config.sections():
if '/' in target:
continue
branches = [b.strip(' \n\r') for b in target_config.get(target, 'branches').split(',')]
min_versions = {}
if target_config.get(target, 'minversions'):
for version_string in target_config.get(target, 'minversions').split(','):
id_part, version_part = version_string.strip(' \n\r').split(':', 1)
id_part = id_part.strip(' \n\r')
version_part = version_part.strip(' \n\r')
min_versions[id_part] = LooseVersion(version_part)
yield Target(target, branches, min_versions)
def update_all_targets():
remote_name = config.get('source_repo', 'remote_name')
outdir = config.get('general', 'destination')
version_to_keep = config.getint('general', 'version_to_keep')
source_locations = config.get('source_repo', 'locations').replace('\n', '').split(',')
binary_locations = config.get('binary_repo', 'locations').replace('\n', '').split(',')
if not outdir:
logger.fatal("No destination specified.")
return
if config.getboolean('debug', 'fetch_remotes'):
for path in source_locations:
git.Repo(path).remotes[remote_name].fetch()
current_targets = list(read_targets())
# Delete targets that have been removed since last update
previous_targets = [name for name in os.listdir(outdir) if not name.startswith('.')]
removed_targets = set(previous_targets) - set([t.name for t in current_targets])
for target in removed_targets:
logger.debug("Deleting unknown target %s", target)
shutil.rmtree(os.path.join(outdir, target))
# Now update the active ones
for target in current_targets:
refs = [remote_name + '/' + branch for branch in target.branches]
dest = os.path.join(outdir, target.name)
logger.info("============================ %s ============================", target.name)
logger.info("Branches: %s", target.branches)
logger.info("Min. versions: %s", target.min_versions)
logger.info("Destination: %s", dest)
try:
os.makedirs(dest)
except OSError:
pass
added, removed = packager.update_changed_artifacts(source_locations, refs, [binary_location + '/' + branch for binary_location in binary_locations for branch in target.branches], target.min_versions, dest)
logger.info("Results: %d artifacts added, %d artifacts removed", added, removed)
logger.info("Purging old artifact version... To keep: %d", version_to_keep)
packager.delete_old_artifacts(dest, version_to_keep)
def main():
filename = sys.argv[1] if len(sys.argv) > 1 else os.path.join(os.path.dirname(__file__), 'config.cfg')
if not config.read(filename):
print("Fatal: Could not read config file '%s'" % filename)
sys.exit(1)
try:
texturepacker_override = config.get('general', 'texturepacker')
except:
texturepacker_override = ""
if texturepacker_override:
packager.textures.texturepacker_binary = texturepacker_override
if not packager.textures.check_texturepacker():
print("Fatal: Could not invoke TexturePacker with command '%s'" % packager.textures.texturepacker_binary)
sys.exit(1)
logging.basicConfig(level=config.getint('debug', 'level'), format='%(levelname)s [%(name)s] %(message)s')
update_all_targets()
if __name__ == '__main__':
main()