]> git.ipfire.org Git - people/jschlag/pbs.git/blob - manager/repositories.py
Drop dependency on textile
[people/jschlag/pbs.git] / manager / repositories.py
1 #!/usr/bin/python
2
3 import logging
4 import os
5 import pakfire
6
7 import base
8
9 from backend.constants import *
10
11 class RepositoriesUpdateEvent(base.Event):
12 priority = 6
13
14 @property
15 def interval(self):
16 return self.pakfire.settings.get_int("repository_update_interval", 600)
17
18 def run(self):
19 for distro in self.pakfire.distros.get_all():
20 for repo in distro.repositories:
21 # Skip repostories that do not need an update at all.
22 if not repo.needs_update():
23 logging.info("Repository %s - %s is already up to date." % (distro.name, repo.name))
24 continue
25
26 e = RepositoryUpdateEvent(self.pakfire, repo.id)
27 self.scheduler.add_event(e)
28
29
30 class RepositoryUpdateEvent(base.Event):
31 # This is an important task, but it may take a while to process.
32 priority = 5
33
34 def run(self, repo_id):
35 # Run this in a new process.
36 self.run_subprocess_background(self.update_repo, repo_id)
37
38 @staticmethod
39 def update_repo(_pakfire, repo_id):
40 repo = _pakfire.repos.get_by_id(repo_id)
41 assert repo
42
43 logging.info("Going to update repository %s..." % repo.name)
44
45 # Update the timestamp when we started at last.
46 repo.updated()
47
48 # Find out for which arches this repository is created.
49 arches = repo.arches
50
51 # Add the source repository.
52 arches.append(_pakfire.arches.get_by_name("src"))
53
54 for arch in arches:
55 changed = False
56
57 # Get all package paths that are to be included in this repository.
58 paths = repo.get_paths(arch)
59
60 repo_path = os.path.join(
61 REPOS_DIR,
62 repo.distro.identifier,
63 repo.identifier,
64 arch.name
65 )
66
67 if not os.path.exists(repo_path):
68 os.makedirs(repo_path)
69
70 source_files = []
71 remove_files = []
72
73 for filename in os.listdir(repo_path):
74 path = os.path.join(repo_path, filename)
75
76 if not os.path.isfile(path):
77 continue
78
79 remove_files.append(path)
80
81 for path in paths:
82 filename = os.path.basename(path)
83
84 source_file = os.path.join(PACKAGES_DIR, path)
85 target_file = os.path.join(repo_path, filename)
86
87 # Do not add duplicate files twice.
88 if source_file in source_files:
89 continue
90
91 source_files.append(source_file)
92
93 try:
94 remove_files.remove(target_file)
95 except ValueError:
96 changed = True
97
98 if remove_files:
99 changed = True
100
101 # If nothing in the repository data has changed, there
102 # is nothing to do.
103 if changed:
104 logging.info("The repository has updates...")
105 else:
106 logging.info("Nothing to update.")
107 continue
108
109 # Find the key to sign the package.
110 key_id = None
111 if repo.key:
112 key_id = repo.key.fingerprint
113
114 # Create package index.
115 p = pakfire.PakfireServer(arch=arch.name)
116
117 p.repo_create(repo_path, source_files,
118 name="%s - %s.%s" % (repo.distro.name, repo.name, arch.name),
119 key_id=key_id)
120
121 # Remove files afterwards.
122 for file in remove_files:
123 file = os.path.join(repo_path, file)
124
125 try:
126 os.remove(file)
127 except OSError:
128 logging.warning("Could not remove %s." % file)