Refactor DistCatcher.
This commit is contained in:
parent
f2c3f252a9
commit
a3978aea48
1 changed files with 45 additions and 38 deletions
|
@ -83,52 +83,59 @@ def run_dist(session, buildsystems, resolver, fixers, quiet=False):
|
||||||
|
|
||||||
|
|
||||||
class DistCatcher(object):
|
class DistCatcher(object):
|
||||||
def __init__(self, directory):
|
def __init__(self, directories):
|
||||||
self.export_directory = directory
|
self.directories = directories
|
||||||
self.files = []
|
self.files = []
|
||||||
self.existing_files = None
|
self.existing_files = None
|
||||||
self.start_time = time.time()
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def default(cls, directory):
|
||||||
|
return cls([
|
||||||
|
os.path.join(directory, 'dist'),
|
||||||
|
directory,
|
||||||
|
os.path.join(directory, '..')])
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self.existing_files = os.listdir(self.export_directory)
|
self.existing_files = {}
|
||||||
|
for directory in self.directories:
|
||||||
|
try:
|
||||||
|
self.existing_files[directory] = {
|
||||||
|
entry.name: entry for entry in os.scandir(directory)}
|
||||||
|
except FileNotFoundError:
|
||||||
|
self.existing_files[directory] = {}
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def find_files(self):
|
def find_files(self):
|
||||||
new_files = os.listdir(self.export_directory)
|
for directory in self.directories:
|
||||||
diff_files = set(new_files) - set(self.existing_files)
|
old_files = self.existing_files[directory]
|
||||||
diff = set([n for n in diff_files if is_dist_file(n)])
|
possible_new = []
|
||||||
if len(diff) == 1:
|
possible_updated = []
|
||||||
fn = diff.pop()
|
for entry in os.scandir(directory):
|
||||||
logging.info("Found tarball %s in package directory.", fn)
|
if not entry.is_file() or not is_dist_file(entry.name):
|
||||||
self.files.append(os.path.join(self.export_directory, fn))
|
continue
|
||||||
return fn
|
old_entry = old_files.get(entry.name)
|
||||||
if "dist" in diff_files:
|
if not old_entry:
|
||||||
for entry in os.scandir(os.path.join(self.export_directory, "dist")):
|
possible_new.append(entry)
|
||||||
if is_dist_file(entry.name):
|
continue
|
||||||
logging.info("Found tarball %s in dist directory.", entry.name)
|
if entry.stat().st_mtime < self.start_time:
|
||||||
self.files.append(entry.path)
|
possible_updated.append(entry)
|
||||||
return entry.name
|
continue
|
||||||
logging.info("No tarballs found in dist directory.")
|
if len(possible_new) == 1:
|
||||||
|
fn = possible_new[0]
|
||||||
|
logging.info("Found new tarball %s in %s.", fn, directory)
|
||||||
|
self.files.append(os.path.join(directory, fn))
|
||||||
|
return entry.name
|
||||||
|
elif len(possible_new) > 1:
|
||||||
|
logging.warning(
|
||||||
|
"Found multiple tarballs %r in %s.", possible_new, directory)
|
||||||
|
return
|
||||||
|
|
||||||
parent_directory = os.path.dirname(self.export_directory)
|
if len(possible_updated) == 1:
|
||||||
diff = (set(os.listdir(parent_directory)) -
|
fn = possible_updated[0]
|
||||||
set([os.path.basename(self.export_directory)]))
|
logging.info("Found updated tarball %s in %s.", fn, directory)
|
||||||
if len(diff) == 1:
|
self.files.append(os.path.join(directory, fn))
|
||||||
fn = diff.pop()
|
return entry.name
|
||||||
if is_dist_file(fn):
|
|
||||||
logging.info("Found tarball %s in parent directory.", fn)
|
|
||||||
self.files.append(os.path.join(parent_directory, fn))
|
|
||||||
return fn
|
|
||||||
logging.warning(
|
|
||||||
"Found file %s in parent directory, "
|
|
||||||
"but not in supported dist format", fn)
|
|
||||||
|
|
||||||
if "dist" in new_files:
|
|
||||||
for entry in os.scandir(os.path.join(self.export_directory, "dist")):
|
|
||||||
if is_dist_file(entry.name) and entry.stat().st_mtime > self.start_time:
|
|
||||||
logging.info("Found tarball %s in dist directory.", entry.name)
|
|
||||||
self.files.append(entry.path)
|
|
||||||
return entry.name
|
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
self.find_files()
|
self.find_files()
|
||||||
|
@ -175,7 +182,7 @@ def create_dist(
|
||||||
fixers.extend([
|
fixers.extend([
|
||||||
GitIdentityFixer(session), SecretGpgKeyFixer(session)])
|
GitIdentityFixer(session), SecretGpgKeyFixer(session)])
|
||||||
|
|
||||||
with DistCatcher(export_directory) as dc:
|
with DistCatcher.default(export_directory) as dc:
|
||||||
session.chdir(reldir)
|
session.chdir(reldir)
|
||||||
run_dist(session, buildsystems, resolver, fixers)
|
run_dist(session, buildsystems, resolver, fixers)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue