Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Bauersfeld committed Apr 12, 2022
1 parent 56bb536 commit f384143
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
33 changes: 28 additions & 5 deletions customize/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,33 @@
import os
from os.path import dirname, isfile, abspath, join, basename
import sys
import ghlib


def upload(args):
git = util.Git('.')
gh = ghlib.GitHub('https://api.github.com', os.environ['GITHUB_TOKEN'])
repo = gh.getRepo(args.repo_id)
repo.upload_latest(git.branch(), git.revision(git.branch()), args.dist)


def inject(args):
args.script = abspath(args.script)
args.dist = abspath(args.dist)
args.output = abspath(args.output)
scriptdir = dirname(args.script)
gitdir = scriptdir

if not isfile(args.script):
util.error('Given script "%s" does not exist!' % (args.script))

os.chdir(scriptdir)
util.info('Working directory is "%s"!' % (scriptdir))

git = util.make_git(gitdir)
git = util.Git(scriptdir)
inputdist = util.extract_dist(args.dist)
customization_hash = util.hashstr(
util.sha1sumd(inputdist) +
util.git_revision(git, util.git_branch(git))
git.revision(git.branch())
)
if customization_hash != util.get_customization_hash(args.output):
util.info('Customization hashes of input and output differ. Recreating output...')
Expand All @@ -42,8 +49,6 @@ def inject(args):
util.info('Creating output archive "%s"...' % (args.output))
util.tar_czf(inputdist, args.output)

#gh = GitHub('https://api.github.com', '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')
#repo = gh.getRepo('zbazztian/customized-dist')

def main():
parser = argparse.ArgumentParser(prog="customize")
Expand Down Expand Up @@ -72,6 +77,24 @@ def main():
)
inject_parser.set_defaults(func=inject)

# upload
upload_parser = subparsers.add_parser(
'upload',
help='Upload a customized distribution as a release, using the GitHub REST API',
description='Upload a customized CodeQL distribution as a release, using the GitHub REST API',
)
upload_parser.add_argument(
'--dist',
required=True,
help='A .tar.gz file containing a CodeQL distribution',
)
upload_parser.add_argument(
'--repo-id',
required=True,
help='The repository id in the format of "orgoruser/reponame"',
)
upload_parser.set_defaults(func=upload)

def print_usage(args):
print(parser.format_usage())

Expand Down
20 changes: 10 additions & 10 deletions customize/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ def hashstr(s):
return sha1.hexdigest()


def make_git(checkout_dir):
def git(*args):
class Git:
def __init__(self, checkout_dir):
self.checkout_dir = checkout_dir

def __call__(self, *args):
command = ['git'] + list(args)
try:
return subprocess.run(
command,
capture_output=True,
check=True,
cwd=checkout_dir,
cwd=self.checkout_dir,
).stdout.decode().strip()
except subprocess.CalledProcessError as cpe:
print('Command "%s" failed with exit code %d' % (' '.join(command), cpe.returncode))
Expand All @@ -59,15 +62,12 @@ def git(*args):
print(cpe.stderr.decode(), flush=True)
raise

return git


def git_branch(git):
return git('branch', '--show-current')
def branch(self):
return self('branch', '--show-current')


def git_revision(git, branch):
return git('rev-parse', branch)
def revision(self, branch):
return self('rev-parse', branch)


def is_dist(directory):
Expand Down

0 comments on commit f384143

Please sign in to comment.