Collect hash during creating an archive
All checks were successful
Build / audit (push) Successful in 13m45s
Build / test (push) Successful in 13m48s

This commit is contained in:
Eugen Wissner 2024-03-17 11:00:13 +01:00
parent cd28e6fb90
commit 3d81917627
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
3 changed files with 29 additions and 18 deletions

View File

@ -2,6 +2,7 @@
v. 2.0. If a copy of the MPL was not distributed with this file, You can v. 2.0. If a copy of the MPL was not distributed with this file, You can
obtain one at https://mozilla.org/MPL/2.0/. -} obtain one at https://mozilla.org/MPL/2.0/. -}
-- | Contains routines for downloading, cloning and uploading sources.
module SlackBuilder.Download module SlackBuilder.Download
( cloneAndUpload ( cloneAndUpload
, extractRemote , extractRemote
@ -10,6 +11,7 @@ module SlackBuilder.Download
, hostedSources , hostedSources
, remoteFileExists , remoteFileExists
, responseBodySource , responseBodySource
, sinkFileAndHash
, sinkHash , sinkHash
, updateSlackBuildVersion , updateSlackBuildVersion
, uploadCommand , uploadCommand
@ -66,7 +68,7 @@ import Conduit
, (.|) , (.|)
, ZipSink(..) , ZipSink(..)
, await , await
, sourceFile , sourceFile, MonadResource
) )
import Data.Conduit.Tar (untar, FileInfo(..)) import Data.Conduit.Tar (untar, FileInfo(..))
import Crypto.Hash (Digest, MD5, hashInit, hashFinalize, hashUpdate) import Crypto.Hash (Digest, MD5, hashInit, hashFinalize, hashUpdate)
@ -244,9 +246,14 @@ download uri packagePath = runReq defaultHttpConfig go
target = packagePath </> fromMaybe downloadFileName attachmentName target = packagePath </> fromMaybe downloadFileName attachmentName
digest <- runConduitRes digest <- runConduitRes
$ responseBodySource response $ responseBodySource response
.| getZipSink (ZipSink (sinkFile target) *> ZipSink sinkHash) .| sinkFileAndHash target
pure (targetFileName, digest) pure (targetFileName, digest)
-- | Writes a file to the destination path and accumulates its MD5 checksum.
sinkFileAndHash :: MonadResource m => FilePath -> ConduitT ByteString Void m (Digest MD5)
sinkFileAndHash target = getZipSink
$ ZipSink (sinkFile target) *> ZipSink sinkHash
-- | Downloads a compressed tar archive and extracts its contents on the fly to -- | Downloads a compressed tar archive and extracts its contents on the fly to
-- a directory. -- a directory.
-- --

View File

@ -2,6 +2,7 @@
v. 2.0. If a copy of the MPL was not distributed with this file, You can v. 2.0. If a copy of the MPL was not distributed with this file, You can
obtain one at https://mozilla.org/MPL/2.0/. -} obtain one at https://mozilla.org/MPL/2.0/. -}
-- | Contains data describing packages and how they are should be updated,
module SlackBuilder.Package module SlackBuilder.Package
( DownloadPlaceholder(..) ( DownloadPlaceholder(..)
, Download(..) , Download(..)

View File

@ -49,7 +49,6 @@ import System.Process
( CmdSpec(..) ( CmdSpec(..)
, CreateProcess(..) , CreateProcess(..)
, StdStream(..) , StdStream(..)
, callProcess
, withCreateProcess , withCreateProcess
, waitForProcess , waitForProcess
) )
@ -61,11 +60,13 @@ import System.Console.ANSI
, ConsoleLayer(..) , ConsoleLayer(..)
) )
import System.Directory (listDirectory, doesDirectoryExist, withCurrentDirectory, removeDirectoryRecursive) import System.Directory (listDirectory, doesDirectoryExist, withCurrentDirectory, removeDirectoryRecursive)
import Control.Monad (filterM) import Control.Monad (filterM, void)
import Data.List (isPrefixOf, isSuffixOf, partition) import Data.List (isPrefixOf, isSuffixOf, partition)
import Conduit (runConduitRes, (.|), sourceFile) import Conduit (runConduitRes, (.|), yield)
import Data.Functor ((<&>)) import Data.Functor ((<&>))
import Data.Bifunctor (Bifunctor(..)) import Data.Bifunctor (Bifunctor(..))
import Data.Conduit.Tar (tarFilePath)
import qualified Data.Conduit.Lzma as Lzma
autoUpdatable :: [PackageDescription] autoUpdatable :: [PackageDescription]
autoUpdatable = autoUpdatable =
@ -403,31 +404,33 @@ reuploadWithTemplate downloadTemplate commands packagePath version = do
$ NonEmpty.last $ snd $ fromJust $ URI.uriPath uri' $ NonEmpty.last $ snd $ fromJust $ URI.uriPath uri'
packagePathRelativeToCurrent = repository' </> Text.unpack packagePath packagePathRelativeToCurrent = repository' </> Text.unpack packagePath
(checksum, relativeTarball') <- case commands of (relativeTarball', checksum) <- case commands of
[] -> do [] -> do
(downloadedFileName, checksum) <- download uri' packagePathRelativeToCurrent (downloadedFileName, checksum) <- download uri' packagePathRelativeToCurrent
pure (checksum, packagePathRelativeToCurrent </> downloadedFileName) pure (packagePathRelativeToCurrent </> downloadedFileName, checksum)
_ -> do _ -> do
changedArchiveRootName <- extractRemote uri' packagePathRelativeToCurrent changedArchiveRootName <- extractRemote uri' packagePathRelativeToCurrent
let relativeTarball = packagePathRelativeToCurrent let relativeTarball = packagePathRelativeToCurrent
</> fromMaybe downloadFileName changedArchiveRootName </> fromMaybe downloadFileName changedArchiveRootName
prepareSource relativeTarball prepareSource relativeTarball
checksum <- liftIO $ runConduitRes $ sourceFile relativeTarball .| sinkHash
pure (checksum, relativeTarball)
download' <- handleReupload relativeTarball' downloadFileName download' <- handleReupload relativeTarball' downloadFileName
pure $ Package.Download download' checksum pure $ Package.Download download' checksum
where where
name' = Text.pack $ takeBaseName $ Text.unpack packagePath name' = Text.pack $ takeBaseName $ Text.unpack packagePath
prepareSource tarballPath = do prepareSource tarballPath =
let packedDirectory = dropExtension $ dropExtension tarballPath liftIO (traverse (defaultCreateProcess tarballPath) commands)
in liftIO (traverse (defaultCreateProcess packedDirectory) commands) >> liftIO (tarCompress tarballPath)
>> liftIO <* liftIO (removeDirectoryRecursive tarballPath)
( withCurrentDirectory (takeDirectory tarballPath) tarCompress tarballPath =
$ callProcess "tar" ["Jcvf", takeFileName tarballPath, takeFileName packedDirectory] let archiveBaseFilename = takeFileName tarballPath
) appendTarExtension = (<.> "tar.xz")
>> liftIO (removeDirectoryRecursive packedDirectory) in fmap (appendTarExtension tarballPath,)
$ withCurrentDirectory (takeDirectory tarballPath)
$ runConduitRes $ yield archiveBaseFilename
.| void tarFilePath
.| Lzma.compress Nothing
.| sinkFileAndHash (appendTarExtension archiveBaseFilename)
handleReupload relativeTarball downloadFileName = do handleReupload relativeTarball downloadFileName = do
downloadURL' <- SlackBuilderT $ asks downloadURL downloadURL' <- SlackBuilderT $ asks downloadURL