Commit ef54fd6b authored by Timo Koch's avatar Timo Koch
Browse files

Restructure folders and make util a package

parent 711a745d
Pipeline #6321 waiting for manual action with stages
......@@ -17,18 +17,18 @@ import itertools
from pathlib import Path
from functools import partial
from util import versionTable
from makeinstallscript import makeInstallScript, supportedLanguages
from makeinstallscript import getScriptExtension
from makeinstallscript import filterDependencies, addDependencyVersions
from makeinstallscript import addDependencyPatches, makeScriptWriter
from util.common import versionTable
from util.make_installscript import makeInstallScript, supportedLanguages
from util.make_installscript import getScriptExtension
from util.make_installscript import filterDependencies, addDependencyVersions
from util.make_installscript import addDependencyPatches, makeScriptWriter
try:
path = os.path.split(os.path.abspath(__file__))[0]
sys.path.append(os.path.join(path, '../bin/util'))
from getmoduleinfo import getDependencies
from common import callFromPath, runCommand, userQuery, queryYesNo
from common import includedCppProjectHeaders, findMatchingFiles
from util.getmoduleinfo import getDependencies
from util.common import callFromPath, runCommand, userQuery, queryYesNo
from util.common import includedCppProjectHeaders, findMatchingFiles
except Exception:
sys.exit('Could not import common modul or getModuleInfo')
......
import os
import sys
try:
path = os.path.split(os.path.abspath(__file__))[0]
sys.path.append(os.path.join(path, '../util'))
from common import callFromPath, runCommand, makeTable
except Exception:
sys.exit('Could not import common module')
def isGitRepository(pathToRepo='.'):
try:
run = callFromPath(pathToRepo)(runCommand)
run('git status')
return True
except Exception:
return False
def getRemote(pathToRepo='.'):
run = callFromPath(pathToRepo)(runCommand)
return run('git ls-remote --get-url').strip('\n')
def fetchRepo(remote, pathToRepo='.'):
run = callFromPath(pathToRepo)(runCommand)
run('git fetch {}'.format(remote))
def hasUntrackedFiles(pathToRepo='.'):
run = callFromPath(pathToRepo)(runCommand)
return run('git ls-files --others --exclude-standard') != ''
def isPersistentBranch(branchName):
if branchName == 'origin/master':
return True
if branchName.startswith('origin/releases/'):
return True
return False
# get the most recent commit that also exists on remote master/release branch
# may be used to find a commit we can use as basis for a pub module
def mostRecentCommonCommitWithRemote(modFolderPath,
branchFilter=isPersistentBranch):
run = callFromPath(modFolderPath)(runCommand)
def findBranches(sha):
candidates = run('git branch -r --contains {}'.format(sha)).split('\n')
candidates = [branch.strip().split(' ->')[0] for branch in candidates]
return list(filter(branchFilter, candidates))
revList = run('git rev-list HEAD').split('\n')
for rev in revList:
branches = findBranches(rev)
if branches:
return branches[0], rev
raise RuntimeError('Could not find suitable ancestor commit'
' on a branch that matches the given filter')
# function to extract persistent, remotely available git versions for all
def getPersistentVersions(modFolderPaths, ignoreUntracked=False):
result = {}
for modFolderPath in modFolderPaths:
if not isGitRepository(modFolderPath):
raise Exception('Folder is not a git repository')
if hasUntrackedFiles(modFolderPath) and not ignoreUntracked:
raise Exception(
"Found untracked files in '{}'. "
"Please commit, stash, or remove them. Alternatively, if you "
"are sure they are not needed set ignoreUntracked=True"
.format(modFolderPath)
)
result[modFolderPath] = {}
result[modFolderPath]['remote'] = getRemote(modFolderPath)
# update remote to make sure we find all upstream commits
fetchRepo(result[modFolderPath]['remote'], modFolderPath)
branch, rev = mostRecentCommonCommitWithRemote(modFolderPath)
run = callFromPath(modFolderPath)(runCommand)
result[modFolderPath]['revision'] = rev
result[modFolderPath]['date'] = run(
'git log -n 1 --format=%ai {}'.format(rev)
).strip('\n')
result[modFolderPath]['author'] = run(
'git log -n 1 --format=%an {}'.format(rev)
).strip('\n')
# this may return HEAD if we are on some detached HEAD tree
result[modFolderPath]['branch'] = branch
return result
def getPatches(persistentVersions):
result = {}
for path, gitInfo in persistentVersions.items():
run = callFromPath(path)(runCommand)
unCommPatch = run('git diff')
unpubPatch = run(
'git format-patch --stdout {}'.format(gitInfo['revision'])
)
result[path] = {}
result[path]['unpublished'] = unpubPatch if unpubPatch else None
result[path]['uncommitted'] = unCommPatch if unCommPatch else None
return result
def versionTable(versions,
config={
'name': 'module name',
'branch': 'branch name',
'revision': 'commit sha',
'date': 'commit date'
},
padding=2):
return makeTable(versions, config)
def printVersionTable(versions):
print(versionTable(versions=versions))
......@@ -203,3 +203,127 @@ def findMatchingFiles(path, pattern):
if fnmatch.fnmatch(file, pattern):
result.append(os.path.join(relativeRootPath, file))
return result
def isGitRepository(pathToRepo='.'):
try:
run = callFromPath(pathToRepo)(runCommand)
run('git status')
return True
except Exception:
return False
def getRemote(pathToRepo='.'):
run = callFromPath(pathToRepo)(runCommand)
return run('git ls-remote --get-url').strip('\n')
def fetchRepo(remote, pathToRepo='.'):
run = callFromPath(pathToRepo)(runCommand)
run('git fetch {}'.format(remote))
def hasUntrackedFiles(pathToRepo='.'):
run = callFromPath(pathToRepo)(runCommand)
return run('git ls-files --others --exclude-standard') != ''
def isPersistentBranch(branchName):
if branchName == 'origin/master':
return True
if branchName.startswith('origin/releases/'):
return True
return False
# get the most recent commit that also exists on remote master/release branch
# may be used to find a commit we can use as basis for a pub module
def mostRecentCommonCommitWithRemote(modFolderPath,
branchFilter=isPersistentBranch):
run = callFromPath(modFolderPath)(runCommand)
def findBranches(sha):
candidates = run('git branch -r --contains {}'.format(sha)).split('\n')
candidates = [branch.strip().split(' ->')[0] for branch in candidates]
return list(filter(branchFilter, candidates))
revList = run('git rev-list HEAD').split('\n')
for rev in revList:
branches = findBranches(rev)
if branches:
return branches[0], rev
raise RuntimeError('Could not find suitable ancestor commit'
' on a branch that matches the given filter')
# function to extract persistent, remotely available git versions for all
def getPersistentVersions(modFolderPaths, ignoreUntracked=False):
result = {}
for modFolderPath in modFolderPaths:
if not isGitRepository(modFolderPath):
raise Exception('Folder is not a git repository')
if hasUntrackedFiles(modFolderPath) and not ignoreUntracked:
raise Exception(
"Found untracked files in '{}'. "
"Please commit, stash, or remove them. Alternatively, if you "
"are sure they are not needed set ignoreUntracked=True"
.format(modFolderPath)
)
result[modFolderPath] = {}
result[modFolderPath]['remote'] = getRemote(modFolderPath)
# update remote to make sure we find all upstream commits
fetchRepo(result[modFolderPath]['remote'], modFolderPath)
branch, rev = mostRecentCommonCommitWithRemote(modFolderPath)
run = callFromPath(modFolderPath)(runCommand)
result[modFolderPath]['revision'] = rev
result[modFolderPath]['date'] = run(
'git log -n 1 --format=%ai {}'.format(rev)
).strip('\n')
result[modFolderPath]['author'] = run(
'git log -n 1 --format=%an {}'.format(rev)
).strip('\n')
# this may return HEAD if we are on some detached HEAD tree
result[modFolderPath]['branch'] = branch
return result
def getPatches(persistentVersions):
result = {}
for path, gitInfo in persistentVersions.items():
run = callFromPath(path)(runCommand)
unCommPatch = run('git diff')
unpubPatch = run(
'git format-patch --stdout {}'.format(gitInfo['revision'])
)
result[path] = {}
result[path]['unpublished'] = unpubPatch if unpubPatch else None
result[path]['uncommitted'] = unCommPatch if unCommPatch else None
return result
def versionTable(versions,
config={
'name': 'module name',
'branch': 'branch name',
'revision': 'commit sha',
'date': 'commit date'
},
padding=2):
return makeTable(versions, config)
def printVersionTable(versions):
print(versionTable(versions=versions))
......@@ -5,8 +5,8 @@ import string
import shutil
import argparse
import subprocess
from getmoduleinfo import getModuleFile
from getmoduleinfo import extractModuleInfos
from .moduleinfo import getModuleFile
from .moduleinfo import extractModuleInfos
# require python 3
if sys.version_info[0] < 3:
......
......@@ -4,14 +4,7 @@ import os
import sys
import textwrap
from abc import ABC, abstractmethod
try:
path = os.path.split(os.path.abspath(__file__))[0]
sys.path.append(os.path.join(path, '../bin/util'))
from common import addPrefixToLines, escapeCharacters
except Exception:
sys.exit("Could not import required modules")
from .common import addPrefixToLines, escapeCharacters
def getRawString(text):
......
......@@ -11,17 +11,10 @@ import argparse
import subprocess
import textwrap
from util import getPersistentVersions, versionTable, getPatches
from installscriptwriter import InstallScriptWriterBash
from installscriptwriter import InstallScriptWriterPython
try:
path = os.path.split(os.path.abspath(__file__))[0]
sys.path.append(os.path.join(path, '../bin/util'))
from getmoduleinfo import getModuleInfo, getDependencies
except Exception:
sys.exit('Could not import getModuleInfo')
from .common import getPersistentVersions, versionTable, getPatches
from .moduleinfo import getModuleInfo, getDependencies
from .installscript_writer import InstallScriptWriterBash
from .installscript_writer import InstallScriptWriterPython
if sys.version_info[0] < 3:
sys.exit("\nError': Python3 required")
......
import os
from common import runCommand
from common import callFromPath
from .common import runCommand
from .common import callFromPath
def extractModuleInfos(moduleFile, keys):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment