Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Add sage --package download and fix-checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
vbraun committed Jan 30, 2016
1 parent 6852d4c commit 8a6c664
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 17 deletions.
51 changes: 50 additions & 1 deletion build/sage_bootstrap/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from sage_bootstrap.env import SAGE_DISTFILES
from sage_bootstrap.package import Package
from sage_bootstrap.tarball import Tarball
from sage_bootstrap.updater import ChecksumUpdater, PackageUpdater



class Application(object):
Expand Down Expand Up @@ -107,9 +109,56 @@ def update(self, package_name, new_version, url=None):
$ sage --package update pari 2015 --url=http://localhost/pari/tarball.tgz
"""
log.debug('Updating %s to %s', package_name, new_version)
from sage_bootstrap.updater import PackageUpdater
update = PackageUpdater(package_name, new_version)
if url is not None:
log.debug('Downloading %s', url)
update.download_upstream(url)
update.fix_checksum()

def download(self, package_name):
"""
Download a package
$ sage --package download pari
Using cached file /home/vbraun/Code/sage.git/upstream/pari-2.8-2044-g89b0f1e.tar.gz
/home/vbraun/Code/sage.git/upstream/pari-2.8-2044-g89b0f1e.tar.gz
"""
log.debug('Downloading %s', package_name)
package = Package(package_name)
package.tarball.download()
print(package.tarball.upstream_fqn)

def fix_all_checksums(self):
"""
Fix the checksum of a package
$ sage --package fix-checksum
"""
for pkg in Package.all():
if not os.path.exists(pkg.tarball.upstream_fqn):
log.debug('Ignoring {0} because tarball is not cached'.format(pkg.tarball_filename))
continue
if pkg.tarball.checksum_verifies():
log.debug('Checksum of {0} unchanged'.format(pkg.tarball_filename))
continue
update = ChecksumUpdater(pkg.name)
print('Updating checksum of {0}'.format(pkg.tarball_filename))
update.fix_checksum()

def fix_checksum(self, package_name):
"""
Fix the checksum of a package
$ sage --package fix-checksum pari
Updating checksum of pari-2.8-2044-g89b0f1e.tar.gz
"""
log.debug('Correcting the checksum of %s', package_name)
update = ChecksumUpdater(package_name)
pkg = update.package
if pkg.tarball.checksum_verifies():
print('Checksum of {0} unchanged'.format(pkg.tarball_filename))
else:
print('Updating checksum of {0}'.format(pkg.tarball_filename))
update.fix_checksum()


44 changes: 44 additions & 0 deletions build/sage_bootstrap/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,29 @@
"""


epilog_download = \
"""
Download the tarball for a package and print the filename to stdout
EXAMPLE:
$ sage --package download pari
Using cached file /home/vbraun/Code/sage.git/upstream/pari-2.8-2044-g89b0f1e.tar.gz
/home/vbraun/Code/sage.git/upstream/pari-2.8-2044-g89b0f1e.tar.gz
"""


epilog_fix_checksum = \
"""
Fix the checksum of a package
EXAMPLE:
$ sage --package fix-checksum pari
Updating checksum of pari-2.8-2044-g89b0f1e.tar.gz
"""


def make_parser():
"""
The main commandline argument parser
Expand Down Expand Up @@ -167,6 +190,20 @@ def make_parser():
parser_update.add_argument(
'--url', type=str, default=None, help='Download URL')

parser_download = subparsers.add_parser(
'download', epilog=epilog_download,
formatter_class=argparse.RawDescriptionHelpFormatter,
help='Download tarball')
parser_download.add_argument(
'package_name', type=str, help='Package name')

parser_fix_checksum = subparsers.add_parser(
'fix-checksum', epilog=epilog_fix_checksum,
formatter_class=argparse.RawDescriptionHelpFormatter,
help='Fix the checksum of package. Use "*" for all packages.')
parser_fix_checksum.add_argument(
'package_name', nargs='?', default=None, type=str, help='Package name')

return parser


Expand Down Expand Up @@ -194,6 +231,13 @@ def run():
app.apropos(args.incorrect_name)
elif args.subcommand == 'update':
app.update(args.package_name, args.new_version, url=args.url)
elif args.subcommand == 'download':
app.download(args.package_name)
elif args.subcommand == 'fix-checksum':
if args.package_name is None:
app.fix_all_checksums()
else:
app.fix_checksum(args.package_name)
else:
raise RuntimeError('unknown subcommand: {0}'.format(args))

Expand Down
37 changes: 22 additions & 15 deletions build/sage_bootstrap/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,17 @@



class PackageUpdater(object):
class ChecksumUpdater(object):

def __init__(self, package_name, new_version):
def __init__(self, package_name):
self.__package = None
self.package_name = package_name
self._update_version(new_version)

def _update_version(self, new_version):
old = Package(self.package_name)
package_version_txt = os.path.join(old.path, 'package-version.txt')
with open(package_version_txt, 'w') as f:
f.write(new_version.strip() + '\n')


@property
def package(self):
if self.__package is None:
self.__package = Package(self.package_name)
return self.__package

def download_upstream(self, download_url):
tarball = self.package.tarball
print('Downloading tarball to {0}'.format(tarball.upstream_fqn))
Download(download_url, tarball.upstream_fqn).run()

def fix_checksum(self):
checksums_ini = os.path.join(self.package.path, 'checksums.ini')
Expand All @@ -64,3 +52,22 @@ def checksums_ini(self):
'' # newline at end
]
return '\n'.join(result)


class PackageUpdater(object):

def __init__(self, package_name, new_version):
super(PackageUpdater, self).__init__(package_name)
self._update_version(new_version)

def _update_version(self, new_version):
old = Package(self.package_name)
package_version_txt = os.path.join(old.path, 'package-version.txt')
with open(package_version_txt, 'w') as f:
f.write(new_version.strip() + '\n')

def download_upstream(self, download_url):
tarball = self.package.tarball
print('Downloading tarball to {0}'.format(tarball.upstream_fqn))
Download(download_url, tarball.upstream_fqn).run()

25 changes: 24 additions & 1 deletion build/test/test_package_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from sage_bootstrap.download.mirror_list import MIRRORLIST_FILENAME
from sage_bootstrap.util import is_url
from sage_bootstrap.package import Package
from test.capture import CapturedLog


EXECUTABLE = os.path.join(
Expand Down Expand Up @@ -73,7 +74,7 @@ def test_name(self):

def test_tarball(self):
pkg = Package('configure')
rc, stdout, stderr = self.run_command(EXECUTABLE, 'tarball', 'configure')
rc, stdout, stderr = self.run_command(EXECUTABLE, 'tarball', pkg.name)
# returns successfully
self.assertEqual(rc, 0)
# Prints to stdout
Expand All @@ -89,3 +90,25 @@ def test_apropos(self):
self.assertTrue(stdout.startswith('Did you mean:'))
# Prints nothing to stderr
self.assertEqual(stderr, '')

def test_download(self):
pkg = Package('configure')
with CapturedLog() as log:
pkg.tarball.download()
rc, stdout, stderr = self.run_command(EXECUTABLE, 'download', pkg.name)
# returns successfully
self.assertEqual(rc, 0)
# Prints filename to stdout
self.assertEqual(stdout.rstrip(), pkg.tarball.upstream_fqn)
# Prints info to stderr
self.assertTrue(stderr.startswith('Using cached file'))

def test_fix_checksum(self):
pkg = Package('configure')
rc, stdout, stderr = self.run_command(EXECUTABLE, 'fix-checksum', 'configure')
# returns successfully
self.assertEqual(rc, 0)
# Prints to stdout
self.assertEqual(stdout.rstrip(), 'Checksum of {0} unchanged'.format(pkg.tarball_filename))
# Prints nothing to stderr
self.assertEqual(stderr, '')

0 comments on commit 8a6c664

Please sign in to comment.