| 1 |
#!/usr/bin/env python3.5 |
| 2 |
|
| 3 |
from subprocess import PIPE, STDOUT, check_output, Popen |
| 4 |
from os.path import join |
| 5 |
import tempfile |
| 6 |
import os |
| 7 |
import sqlite3 |
| 8 |
import time |
| 9 |
import re |
| 10 |
import sys |
| 11 |
from datetime import datetime |
| 12 |
from gzip import GzipFile |
| 13 |
|
| 14 |
TMPDIR = tempfile.TemporaryDirectory() |
| 15 |
|
| 16 |
version_rex = re.compile(rb'^pristine-tar \((.+?)\).+') |
| 17 |
def pristine_tar_version(): |
| 18 |
# we want to have fresh info, so we read this every time |
| 19 |
with GzipFile('/usr/share/doc/pristine-tar/changelog.gz') as f: |
| 20 |
line = f.readline() |
| 21 |
m = version_rex.match(line) |
| 22 |
assert m |
| 23 |
return m.group(1) |
| 24 |
|
| 25 |
class DbLog: |
| 26 |
|
| 27 |
def save(self, path, tarball, result, t): |
| 28 |
log("{}: {} => {} ({:.2f} s)", path, tarball, result, t) |
| 29 |
|
| 30 |
def skip(self, path, tarball): |
| 31 |
return False |
| 32 |
|
| 33 |
CREATE = """ |
| 34 |
CREATE TABLE IF NOT EXISTS executions |
| 35 |
(t REAL, path TEXT, tarball TEXT, success BOOLEAN, version TEXT, duration REAL); |
| 36 |
""" |
| 37 |
|
| 38 |
|
| 39 |
class Db: |
| 40 |
|
| 41 |
def __init__(self, path, skip_newer_than): |
| 42 |
self.path = path |
| 43 |
self.db = sqlite3.connect(path) |
| 44 |
self.db.execute(CREATE) |
| 45 |
self.max_age = skip_newer_than |
| 46 |
|
| 47 |
def skip(self, path, tarball): |
| 48 |
res = self.db.execute('''select MAX(t) from executions where path = ? and tarball = ?''', |
| 49 |
(path, tarball)) |
| 50 |
best = res.fetchone()[0] |
| 51 |
if best is None: |
| 52 |
# not present |
| 53 |
return False |
| 54 |
now = datetime.utcnow().timestamp() |
| 55 |
return (best + self.max_age >= now) |
| 56 |
|
| 57 |
def save(self, path, tarball, result, t): |
| 58 |
now = datetime.utcnow().timestamp() |
| 59 |
version = pristine_tar_version() |
| 60 |
log('Saving {} => {} with {}', path, tarball, 'success' if result else 'failure') |
| 61 |
self.db.execute(''' |
| 62 |
insert into executions (t, path, tarball, success, version, duration) |
| 63 |
values (?, ?, ?, ?, ?, ?)''', (now, path, tarball, result, version, t)) |
| 64 |
self.db.commit() |
| 65 |
|
| 66 |
def get_pristines(path): |
| 67 |
files = check_output('pristine-tar list'.split(), cwd = path) |
| 68 |
files = files.strip().split(b'\n') |
| 69 |
return [ f for f in files if len(f.strip()) > 0 ] |
| 70 |
|
| 71 |
VERBOSE = False |
| 72 |
def set_verbose(value): |
| 73 |
global VERBOSE |
| 74 |
VERBOSE = value |
| 75 |
|
| 76 |
def log(msg, *vals): |
| 77 |
if VERBOSE: |
| 78 |
print(msg.format(*vals)) |
| 79 |
|
| 80 |
def try_checkout(path, name): |
| 81 |
try: |
| 82 |
tmp_name = join(bytes(TMPDIR.name, 'utf8'), name) |
| 83 |
cmd = ['pristine-tar', 'checkout', tmp_name] |
| 84 |
log('Running: {}', cmd) |
| 85 |
t = time.monotonic() |
| 86 |
p = Popen(cmd, cwd = path, stdout = PIPE, stderr = STDOUT, |
| 87 |
close_fds = True) |
| 88 |
output, _ = p.communicate() |
| 89 |
duration = time.monotonic() - t |
| 90 |
finally: |
| 91 |
check_output(['rm', '-f', tmp_name], cwd = path) |
| 92 |
return output, p.returncode == 0, duration |
| 93 |
|
| 94 |
def try_all_checkouts(path, db): |
| 95 |
pristines = get_pristines(path) |
| 96 |
for tarball in pristines: |
| 97 |
if db.skip(path, tarball): |
| 98 |
log('Skipping {} => {}', path, tarball) |
| 99 |
continue |
| 100 |
log('Checking {} => {}', path, tarball) |
| 101 |
out, result, t = try_checkout(path, tarball) |
| 102 |
db.save(path, tarball, result, t) |
| 103 |
|
| 104 |
def is_pristine_path(path): |
| 105 |
git = join(path, b'.git') |
| 106 |
if os.path.isdir(git): |
| 107 |
return True |
| 108 |
else: |
| 109 |
return False |
| 110 |
|
| 111 |
def try_all_tree(path, db): |
| 112 |
path = os.path.abspath(path) |
| 113 |
for cd, fs, ds in os.walk(bytes(path)): |
| 114 |
if is_pristine_path(cd): |
| 115 |
try_all_checkouts(cd, db) |
| 116 |
|
| 117 |
# path = b'/home/toma/projects/debian/pkgs/fasm' |
| 118 |
def main(): |
| 119 |
import argparse |
| 120 |
day = 60 * 3600 * 24 |
| 121 |
parser = argparse.ArgumentParser(description = 'Crawl pristine-all executions.') |
| 122 |
parser.add_argument('--db', metavar='PATH', type=str, default = 'pristine.sql', help = 'store results here') |
| 123 |
parser.add_argument('--verbose', action='store_true', help = 'be verbose') |
| 124 |
subparsers = parser.add_subparsers(help='operations', dest = 'action') |
| 125 |
parser_crawl = subparsers.add_parser('crawl', help='crawl path') |
| 126 |
parser_crawl.add_argument('--age', metavar='N', type=int, default = 365, help = 'reindex older than N days') |
| 127 |
parser_crawl.add_argument('path', metavar='PATH', type=str, help = 'path tree to process') |
| 128 |
args = parser.parse_args() |
| 129 |
set_verbose(args.verbose) |
| 130 |
if args.action == 'crawl': |
| 131 |
db = Db(args.db, args.age * day) |
| 132 |
try_all_tree(bytes(args.path, 'utf8'), db) |
| 133 |
|
| 134 |
main() |