streamlined python code

This commit is contained in:
Trilarion
2018-06-28 14:50:35 +02:00
parent f7fd84817e
commit 20a44a6694
14 changed files with 398 additions and 158 deletions

View File

@ -1,90 +1,10 @@
"""
Downloads source releases from Sourceforge and puts them into a git repository
Downloads source releases from Sourceforge and puts them into a git repository
"""
import os
import shutil
import urllib.request
import json
import time
import zipfile
import subprocess
import datetime
import distutils.dir_util
import sys
def determine_version(name):
# to lower case
name = name.lower()
# cut leading terms
terms = ['phaos-', 'phaos', 'pv']
for t in terms:
if name.startswith(t):
name = name[len(t):]
# cut trailing '.zip'
t = '.zip'
if name.endswith(t):
name = name[:-len(t)]
return name
def determine_last_modified_date(folder):
latest_last_modified = 0
for dirpath, dirnames, filenames in os.walk(folder):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
lastmodified = os.path.getmtime(filepath)
if lastmodified > latest_last_modified:
latest_last_modified = lastmodified
return latest_last_modified
def unzip_keep_last_modified(archive, destination):
"""
Assuming that destination is a directory and already existing.
"""
with zipfile.ZipFile(archive, 'r') as zip:
# zip.extractall(destination)
for zip_entry in zip.infolist():
name, date_time = zip_entry.filename, zip_entry.date_time
date_time = time.mktime(date_time + (0, 0, -1))
zip.extract(zip_entry, destination)
os.utime(os.path.join(destination, name), (date_time, date_time))
def strip_wrapping(folder):
names = os.listdir(folder)
while len(names) == 1:
folder = os.path.join(folder, names[0])
names = os.listdir(folder)
return folder
def copy_tree(source, destination):
# this gave an FileNotFoundError: [Errno 2] No such file or directory: '' on Windows
# distutils.dir_util.copy_tree(archive_path, git_path)
for dirpath, dirnames, filenames in os.walk(source):
# first create all the directory on destination
directories_to_be_created = [os.path.join(destination, os.path.relpath(os.path.join(dirpath, x), source)) for x in dirnames]
for directory in directories_to_be_created:
os.makedirs(directory, exist_ok=True)
# second copy all the files
filepaths_source = [os.path.join(dirpath, x) for x in filenames]
filepaths_destination = [os.path.join(destination, os.path.relpath(x, source)) for x in filepaths_source]
for src, dst in zip(filepaths_source, filepaths_destination):
shutil.copyfile(src, dst)
def subprocess_run(cmd):
"""
"""
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode:
print("error {} in call {}".format(result.returncode, cmd))
print(result.stderr.decode('ascii'))
sys.exit(-1)
else:
print(' output: {}'.format(result.stdout.decode('ascii')))
from utils.utils import *
if __name__ == '__main__':
@ -113,7 +33,7 @@ if __name__ == '__main__':
raise RuntimeError("files with duplicate archives, cannot deal with that")
# determine version from file name
versions = [determine_version(x) for x in archives]
versions = [determine_archive_version_generic(x, leading_terms=['phaos-', 'phaos', 'pv'], trailing_terms=['zip']) for x in archives]
# for version in versions:
# print(version)
@ -146,10 +66,10 @@ if __name__ == '__main__':
unzip_keep_last_modified(archive, unzipped_archive)
# go up in unzipped archives until the very first non-empty folder
unzipped_archives = [strip_wrapping(x) for x in unzipped_archives]
unzipped_archives = [strip_wrapped_folders(x) for x in unzipped_archives]
# determine date
dates = [determine_last_modified_date(x) for x in unzipped_archives]
dates = [determine_latest_last_modified_date(x) for x in unzipped_archives]
dates_strings = [datetime.datetime.fromtimestamp(x).strftime('%Y-%m-%d') for x in dates]
# if len(dates_strings) != len(set(dates_strings)):
# raise RuntimeError("Some on the same day, cannot cope with that")
@ -168,6 +88,8 @@ if __name__ == '__main__':
os.mkdir(git_path)
os.chdir(git_path)
subprocess_run(['git', 'init'])
subprocess_run(['git', 'config', 'user.name', 'Trilarion'])
subprocess_run(['git', 'config', 'user.email', 'Trilarion@users.noreply.gitlab.com'])
# now process revision by revision
print('process revisions')