Address review comments.
[arvados-dev.git] / jenkins / packaging-move-dev-to-attic.py
1 #!/usr/bin/env python3
2
3 # Copyright (C) The Arvados Authors. All rights reserved.
4 #
5 # SPDX-License-Identifier: AGPL-3.0
6
7 import argparse
8 import datetime
9 import os
10 import subprocess
11 import pprint
12 import re
13
14 from datetime import date
15
16 class DebugExecutor:
17   def __init__(self, package_list):
18     self.package_list = package_list
19
20   def do_it(self):
21     for a in self.package_list:
22       print (a[2])
23
24 class MoveExecutor:
25   def __init__(self, distro, package_list):
26     self.package_list = package_list
27     self.distro = distro
28
29   def move_it(self):
30     for a in self.package_list:
31       if a[2]:
32         source = a[0]
33         destination = source.replace('dev','attic')
34         os.makedirs(os.path.dirname(destination), exist_ok=True)
35         print ("Moving " + a[0] + " to " + destination)
36         f = os.path.basename(os.path.splitext(a[0])[0])
37         output = subprocess.getoutput("aptly repo move " + source + " " + destination)
38         print(output)
39
40   def update_it(self):
41     distroBase = re.sub('-.*$', '', self.distro)
42     output = subprocess.getoutput("aptly publish update " + distroBase + "-dev filesystem:" + distroBase + ":")
43     print(output)
44     output = subprocess.getoutput("aptly publish update " + distroBase + "-attic filesystem:" + distroBase + ":")
45     print(output)
46
47 class CollectPackageName:
48   def __init__(self, cache_dir, distro, min_packages,  cutoff_date):
49     self.cache_dir = cache_dir
50     self.distro = distro
51     self.min_packages = min_packages
52     self.cutoff_date_unixepoch = int(cutoff_date.strftime('%s'))
53
54   def collect_packages(self):
55     distroBase = re.sub('-.*$', '', self.distro)
56     directory=os.path.join(self.cache_dir,distroBase,'pool/main')
57
58     ## rtn will have 4 element tuple: package_name, the path, the creation time for sorting, and if it's a candidate for deletion
59     rtn = []
60
61     # Get the list of packages in the repo
62     output = subprocess.getoutput("aptly repo search " + self.distro)
63     for f in output.splitlines():
64       pkg = f.split('_')[0]
65       # This is nasty and slow, but aptly doesn't seem to have a way to provide
66       # the on-disk path for a package in its repository. We also can't query
67       # for the list of packages that fit the cutoff date constraint with a
68       # 'package-query' parameter: the 'Date' field would be appropriate for
69       # that, but it's not populated for our packages because we don't ship a
70       # changelog with them (that's where the 'Date' field comes from, as per
71       # the Debian policy manual, cf.
72       # https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-date).
73       the_file = subprocess.getoutput("find " + directory + " -name " + f + ".deb")
74       if the_file == "":
75           print("WARNING: skipping package, could not find file for package " + f + " under directory " + directory)
76           continue
77       rtn.append ( (pkg, the_file,
78                     os.path.getmtime(the_file),
79                     os.path.getmtime(the_file) < self.cutoff_date_unixepoch) )
80     return self.collect_candidates_excluding_N_last(rtn)
81
82   def collect_candidates_excluding_N_last(self, tuples_with_packages):
83     return_value = []
84
85     ## separate all file into packages. (use the first element in the tuple for this)
86     dictionary_per_package  = {}
87     for x in tuples_with_packages:
88       dictionary_per_package.setdefault(x[0], []).append(x[1:])
89
90     for pkg_name, metadata in dictionary_per_package.items():
91       candidates_local_copy = metadata[:]
92
93       ## order them by date
94       candidates_local_copy.sort(key=lambda tup: tup[1])
95
96       return_value.extend(candidates_local_copy[:-self.min_packages])
97
98     return return_value
99
100 def distro(astring):
101     if re.fullmatch(r'.*-dev', astring) == None:
102         raise ValueError
103     return astring
104
105 today = date.today()
106 parser = argparse.ArgumentParser(description='List the packages to delete.')
107 parser.add_argument('distro',
108                     type=distro,
109                     help='distro to process, must be a dev repository, e.g. buster-dev')
110 parser.add_argument('--repo_dir',
111                     default='/var/www/aptly_public/',
112                     help='parent directory of the aptly repositories (default:  %(default)s)')
113 parser.add_argument('--min_packages', type=int,
114                     default=5,
115                     help='minimum amount of packages to leave in the repo (default:  %(default)s)')
116 parser.add_argument('--cutoff_date', type=lambda s: datetime.datetime.strptime(s, '%Y-%m-%d'),
117                     default=today.strftime("%Y-%m-%d"),
118                     help='date to cut-off in format YYYY-MM-DD (default:  %(default)s)')
119
120 args = parser.parse_args()
121
122
123 p = CollectPackageName(args.repo_dir, args.distro, args.min_packages,  args.cutoff_date)
124
125 executor = MoveExecutor(args.distro, p.collect_packages())
126
127 executor.move_it()
128 executor.update_it()
129